next up previous contents
Next: Consumer Up: User Level IPC Previous: User Level IPC   Contents

Producer

The pseudocode for the producer is shown in figure 48. First the writer tests if there is enough free space in the ring buffer placed in shared memory between the two communicating processes. qLength denotes the capacity of the queue. size is a variable that is placed in shared memory to holds the amount of bytes that are currently stored in the shared queue. It is not necessary to synchronise the access to this shared variable here. Rather busy-waiting is performed, until there is enough free space. To copy the message into the queue, we use a variable called free. It determines how many bytes can be written in one shot. It is the number of free bytes measured from the current writing position to the end of the queue. The queue simulates a ring buffer, and the index pointing to the next free place is adjusted after the write operation. Now the shared variable that holds the number of bytes in the ring buffer can be updated. This access represents a critical section. Any kind of mutual exclusion algorithm can be used for this task, e.g. Pettersons or Dekkers algorithms[Sta92]. For the implementation that served as a source for the measurements in figure 50 we used Posix thread mutexes that have been placed in shared memory between the two communicating processes. They also implement a user-level mutual exclusion algorithm based on spinlocks[TRG$^$87]. If the time during which the lock is held is small, the process will not be banned from the CPU, so that no context switch will occur. We experienced that there is no significant difference in using a user-level algorithm or thread mutexes for this task. When using a System V semaphore for this task, we measure a performance drop from 90 000 calls/sec to about 20 000 calls/sec in the worst case scenario. Finally the producer has to test if the reader is blocking on an empty queue. To do this there is another shared variable named toRead that the consumer sets if it decides to block. If the consumer is waiting the producer will wake it up. It will then test again if there are already enough data to be read.
\begin{Figure}
% latex2html id marker 1267\begin{tabbing}
\hspace*{3cm}\= \hsp...
...alCondition();
\end{tabbing}\caption {
Pseudocode for the producer.}\end{Figure}


next up previous contents
Next: Consumer Up: User Level IPC Previous: User Level IPC   Contents
Johannes Gutleber
1999-10-29