Concepts of Programming Languages

(Sean Pound) #1

588 Chapter 13 Concurrency


The following are concise pseudocode descriptions of wait and release:

wait(aSemaphore)
if aSemaphore’s counter > 0 then
decrement aSemaphore’s counter
else
put the caller in aSemaphore’s queue
attempt to transfer control to some ready task
(if the task ready queue is empty, deadlock occurs)
end if

release(aSemaphore)
if aSemaphore’s queue is empty (no task is waiting) then
increment aSemaphore’s counter
else
put the calling task in the task-ready queue
transfer control to a task from aSemaphore’s queue
end

We can now present an example program that implements cooperation syn-
chronization for a shared buffer. In this case, the shared buffer stores integer
values and is a logically circular structure. It is designed for use by possibly
multiple producer and consumer tasks.
The following pseudocode shows the definition of the producer and con-
sumer tasks. Two semaphores are used to ensure against buffer underflow or
overflow, thus providing cooperation synchronization. Assume that the buffer
has length BUFLEN, and the routines that actually manipulate it already exist
as FETCH and DEPOSIT. Accesses to the counter of a semaphore are specified
by dot notation. For example, if fullspots is a semaphore, its counter is
referenced by fullspots.count.

semaphore fullspots, emptyspots;
fullspots.count = 0;
emptyspots.count = BUFLEN;
task producer;
loop
-- produce VALUE --
wait(emptyspots); { wait for a space }
DEPOSIT(VALUE);
release(fullspots); { increase filled spaces }
end loop;
end producer;

task consumer;
loop
wait(fullspots); { make sure it is not empty }
Free download pdf