Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Processes II<br />
int msgctl(int msqid, int crod, struct msqid_ds *buff);<br />
cmd of IPC_RMID to remove message queue from system<br />
Multiplexed Messages<br />
client 1<br />
pid=123<br />
client 21<br />
pid=456<br />
client 3<br />
pid=789<br />
type=1 123 1 456 1 789<br />
type=123/456/789 1<br />
server<br />
message<br />
queue<br />
Features:<br />
- read in any order<br />
- assign priorities<br />
- read any message<br />
Semaphores<br />
Semaphores are a synchronization primitive. We will use to synchronize access to shared memory segments.<br />
A semaphore is a integer resource counter. If we have one resource, a shared file, <strong>the</strong>n valid values are 0 & 1.<br />
process A<br />
process B<br />
kernel<br />
semaphore: 0 or 1<br />
Since our use of semaphores is to provide resource synchronization between different processes, <strong>the</strong> semaphore<br />
value must be stored in <strong>the</strong> kernel.<br />
To obtain a resource that is controlled by a semaphore, a process needs to test its current value, and if value> 0,<br />
decrement <strong>the</strong> value by 1.<br />
If value = 0, <strong>the</strong> process must wait until value> 0 (wait for some o<strong>the</strong>r process to release resource).<br />
To release resource, a process increments <strong>the</strong> value. System V implementation of semaphores is done in <strong>the</strong> kernel<br />
- guarantee a group of operations is atomic.<br />
#include <br />
#include /* defines ipc-perm structure */<br />
struct semid_ds {<br />
struct ipc-perm sem-perm; /* operation perm struct */<br />
struct sem *sem_base; /* ptr to 1st semaphore in set */<br />
ushort sem_nsems; /* # of semaphores in set */<br />
time_t sem_otime; /* time of last semop */<br />
time_t sem_ctime; /* time of last change */<br />
};<br />
struct sem {<br />
ushort semval; /* semaphore value, non -ve */<br />
short sempid; /* pid of last operation */<br />
ushort semncnt; /* # awaiting semval > cval */<br />
ushort semzcnt; /* # awaiting semval = 0 */<br />
};<br />
Kernel also maintains for each value in <strong>the</strong> set:<br />
- process ID of process that did last operation<br />
- number of processes waiting for value to increase<br />
- number of processes waiting for value to = zero<br />
138