|
Computer Science 432 |
Lecture 08: Process Synchronization, Semaphores, Synchronization Problems
Date: October 3, 2006
Due at the start of class, Thursday, October 5.
Turn in short answers to these questions. Please turn in a hard copy (typeset or handwritten are OK). We will discuss these questions during class, so no late submissions are accepted.
SG&G 6.3, 6.4
You need not turn in answers to the next two questions, but we will discuss them briefly in class.
SG&G 6.5, 6.6
There are no new readings for next time. At this point, you should be up to Section 6.6 in SG&G.
boolean lock=false; boolean waiting[n]; /* all initialized to false */
int j;
boolean key;
while (1) {
waiting[i]=true;
key=true;
while (waiting[i] && key)
key = TestAndSet(&lock);
waiting[i]=false;
/* critical section */
j=(i+1)%n;
while ((j!=i) && !waiting[j])
j=(j+1)%n;
if (j==i) lock=false;
else waiting[j]=false;
/* non-critical section */
}
semaphore mutex=1;
while (1) {
wait(mutex);
/* critical section */
signal(mutex);
/* non-critical section */
}
semaphore fullslots, emptyslots, mutex; full=0; empty=n; mutex=1;
while (1) {
produce item;
wait(emptyslots);
wait(mutex);
add item to buffer;
signal(mutex);
signal(fullslots);
}
while (1) {
wait(fullslots);
wait(mutex);
remove item from buffer;
signal(mutex);
signal(emptyslots);
consume item;
}