Computer Science 432 |
Lecture 07: Cooperating Processes, Critical Sections, Process Synchronization
Date: September 28, 2006
Due at the start of class, Tuesday, October 3.
SG&G 6.1
Please turn in a hard copy (typeset or handwritten are OK). We will discuss Dekker's algorithm and how it solves the critical-section problem during class, so no late submissions are accepted.
The readings for next time are SG&G Sections 6.4-6.6.
struct node { ... struct node *next; } struct node *head, *tail; void insert(val) { struct node *newnode; newnode = getnode(); newnode->next = NULL; if (head == NULL){ head = tail = newnode; } else { // <==== THE WRONG PLACE tail->next = newnode; tail = newnode; } } void remove() { // ... code to remove value ... head = head->next; if (head == NULL) tail = NULL; return (value); }If the process executing insert is interrupted at "the wrong place" and then another process calls remove until the list is empty, when the insert process resumes, it will be operating on invalid assumptions and the list will be corrupted.
int turn=0;
while (1) { while (turn!=i); /* busy wait */ /* critical section */ turn=j; /* non-critical section */ }
boolean flag[2]; flag[0]=false; flag[1]=false;
while (1) { flag[i]=true; while (flag[j]); /* critical section */ flag[i]=false; /* non-critical section */ }
int turn=0; boolean flag[2]; flag[0]=false; flag[1]=false;
while (1) { flag[i]=true; turn=j; while (flag[j] && turn==j); /* critical section */ flag[i]=false; /* non-critical section */ }
boolean choosing[n]; int number[n];
while (1) { choosing[i]=true; number[i]=max(number[0],number[i],...,number[n-1])+1; choosing[i]=false; for (j=0; j<n; j++) { while (choosing[j]); while ((number[j]!=0) && ((number[j],j) < (number[i],i))); } /* critical section */ number[i]=0; /* non-critical section */ }