CSE325 Principles of Operating Systems Process/Thread Synchronization David Duggan dduggan@sandia.gov March 1, 2011
The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and then open the file again. If the red x still appears, you may have to delete the image and then insert it again. Critical Sections shared double balance; Code for P 1 Code for P 2...... balance = balance + amount; balance = balance - amount;...... balance+=amount balance-=amount balance Is there any problem? 3/3/11 CSE325: Synchronization 2
The Critical Section Problem Consider a system of n processes {P 0, P 1,, P n-1 } Each P i has a segment of code called critical section Change common variables Update table Write file No two processes execute in critical section at same time 3/3/11 CSE325: Synchronization 3
The Critical Section Problem (Cont.) Problem is to design a protocol to allow cooperation Processes must request permission to execute The structure consists of four sections Entry section Critical section Exit section Remainder section 3/3/11 CSE325: Synchronization 4
General Structure of a Process, P i do { entry section critical section exit section remainder section } while (TRUE); 3/3/11 CSE325: Synchronization 5
Critical Section Problems Mutual exclusion: Only one process can be in the critical section at a time There is a race to execute critical sections The sections may be defined by different code in different processes cannot easily detect with static analysis Without mutual exclusion, results of multiple execution are not determinate Need an OS mechanism so programmer can resolve race conditions 3/3/11 CSE325: Synchronization 6
Critical Section Requirements Mutual exclusion If process Pi executing in CS, no other processes can be executing in CS Progress Only allow processes needing to execute in CS to participate in selection of process to allow Bounded waiting There exists a limit on the # of times other processes enter CS after a process has made request to enter CS 3/3/11 CSE325: Synchronization 7
General Approaches In Operating Systems Preemptive kernels Nonpreemptive kernels Issues? Preemptive kernels Allows race conditions Allows system to be more responsive Nonpreemptive kernels Race free on kernel data structures Only when on single processor 3/3/11 CSE325: Synchronization 8
Peterson s Solution Software-based Two processes, P 0 & P 1, alternating execution of CS s Must share: int turn; boolean flag[2]; Variable turn indicates which process is next for CS Array flag indicates if a process is ready to enter CS 3/3/11 CSE325: Synchronization 9
Synchronization Hardware Need support from Hardware Mechanism called lock needed Processes must acquire the lock before entering CS 3/3/11 CSE325: Synchronization 10
Solution using locks do { acquire lock critical section release lock remainder section } while (TRUE); 3/3/11 CSE325: Synchronization 11
Disabling Interrupts /* shared double balance */ Code for P 1 Code for P 2 disableinterrupts(); disableinterrupts(); balance = balance + amount; balance = balance - amount; enableinterrupts(); enableinterrupts(); Interrupts could be disabled arbitrarily long Really only want to prevent P 1 and P 2 from interfering with one another; this blocks all other P i 3/3/11 CSE325: Synchronization 12
Test and Set Instruction Atomic operation that will test a variable and set it without being interrupted boolean TestAndSet(boolean *target) { boolean rv = *target; *target = TRUE; return rv; } 3/3/11 CSE325: Synchronization 13
Use of Test and Set Instruction do { while (TestAndSet(&lock)) ; // just wait and do nothing // run critical section code lock = FALSE; // run remainder section code } while (TRUE); 3/3/11 CSE325: Synchronization 14
Swap Instruction Atomic operation that will swap the values of two variables without being interrupted boolean Swap(boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp; } 3/3/11 CSE325: Synchronization 15
Use of Swap Instruction do { key = TRUE; while (key == TRUE) Swap(&lock, &key); // run critical section code lock = FALSE; // run remainder section code } while (TRUE); 3/3/11 CSE325: Synchronization 16
Discussion Are these viable solutions to the Critical-Section Problem? Mutual exclusion? Yes Progress? Yes Bounded waiting? No 3/3/11 CSE325: Synchronization 17
A Semaphore 3/3/11 CSE325: Synchronization 18
Semaphore Invented by Dijkstra in the 1960s Conceptual OS mechanism, with no specific implementation defined (could be enter()/ exit(), was called P() and V(), or wait () and signal()) Basis of all contemporary OS synchronization mechanisms 3/3/11 CSE325: Synchronization 19
Semaphore (Cont.) wait(s) { while S <=0 ; //no-op S--; } signal(s) { S++; } 3/3/11 CSE325: Synchronization 20
Use of Semophores Usually two types of semaphores Counting semaphore Binary semaphore (also called mutex locks) Binary semaphores can be used for: Critical section problems for multiple processes Counting semaphores can be used for: Controlling access to multiple, but finite set of resources Ordering the running of critical sections 3/3/11 CSE325: Synchronization 21
Semaphore Implementation Issues Is there a problem? Busy waiting wastes resources Redefine the semaphore implementation to allow blocking instead 3/3/11 CSE325: Synchronization 22
Semaphore Implementation Processes are only blocked on their own critical sections (not critical sections that they should not care about) If disabling interrupts, be sure to bound the time they are disabled 3/3/11 CSE325: Synchronization 23
Semaphore Implementation Define a semaphore as a record typedef struct { int value; struct process *L; } semaphore; Assume two simple operations: block suspends the process that invokes it. wakeup(p) resumes the execution of a blocked process P. 3/3/11 CSE325: Synchronization 24
Implementation Semaphore operations now defined as enter(s): S.value--; if (S.value < 0) { } add this process to S.L; block; exit(s): S.value++; if (S.value <= 0) { } remove a process P from S.L; wakeup(p); 3/3/11 CSE325: Synchronization 25