




































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The critical section problem in computer science and various synchronization techniques such as test and set, semaphore, and interrupts to ensure mutual exclusion and prevent data inconsistency. Different versions of using shared global variables, un-interruptable test & set, and semaphores. It also discusses the producer/consumer problem and readers-writers problem as classical synchronization problems.
Typology: Study notes
1 / 44
This page cannot be seen from the preview
Don't miss anything!





































Need for Synchronization
Critical Section Problem
/* Code schema for p1 */
..
balance = balance + amount;
..
/* Code schema for p2 */ .. balance = balance - amount; ..
/* Schema for p1 / / X == balance */ load R1, X load R2, Y add R1, R store R1, X
/* Schema for p2 / / X == balance */ load R1, X load R2, Y sub R1, R store R1, X
shared float balance;
Critical Section Problem…
/* Schema for p1 */ load R1, X load R2, Y add R1, R store R1, X
/* Schema for p2 */ load R1, X load R2, Y sub R1, R store R1, X
Using Shared Global Variables – Ver 2
procedure processone; begin while true do begin while p2inside do ; p1inside := true; criticalsectionone; p1inside := false; otherstuffone; end end
procedure processtwo; begin while true do begin while p1inside do ; p2inside := true; criticalsectiontwo; p2inside := false; otherstufftwo; end end
Shared boolean: p1inside <= false, p2inside <= false;
Using Shared Global Variables – Ver 3
procedure processone; begin while true do begin p1wantsin := true; while p2wantsin do ; criticalsectionone; p1wantsin := false; otherstuffone; end end
procedure processtwo; begin while true do begin p2wantsin := true; while p1wantsin do ; criticalsectiontwo; p2wantsin := false; otherstufftwo; end end
Shared boolean: p1wantsin <= false, p2wantsin <= false;
Using Interrupts…
Using Shared Variable to Synchronize
/ Program for P1 / .. /* Acquire lock / while(lock) {NULL;}; lock = TRUE; / Execute critical section / balance = balance + amount; / Release lock */ lock = FALSE; ..
/ Program for P2 / .. /* Acquire lock / while(lock) {NULL;}; lock = TRUE; / Execute critical section / balance = balance - amount; / Release lock */ lock = FALSE; ..
shared boolean lock <= FALSE; shared float balance;
Un-interruptable Test & Set
enter( lock ) {
disableInterrupts(); /* Loop until lock TRUE / while (lock) { / Let interrupts occur */ enableInterrupts(); disableInterrupts(); } lock = TRUE; enableInterrupts();
}
exit( lock ) { disableInterrupts(); lock = FALSE; enableInterrupts(); }
Enable interrupts so that the OS, I/O can use them
Re-disable interrupts when ready to test again
Un-interruptable Test & Set…
enter(lock); balance = balance + amount; exit(lock);
enter(lock); balance = balance - amount; exit(lock);
/ Program for P1 / enter(listLK); < delete element >; exit(listLK);
Protecting Multiple Components: 1st^ try
/ Program for P2 / enter(lngthLK); < update length >; exit(lngthLK);
/ Program for P1 / enter(listLK); < delete element >;
Protecting Multiple Components: 2nd^ try
/ Program for P2 / enter(lngthLK); < update length >;
Solution to Synchronization
Semaphore