Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Part IIProcess ManagementChapter 6: Process Synchronization                                1
Process SynchronizationWhy is synchronization needed?Race ConditionsCritical SectionsPure Software SolutionsHardware Suppo...
Why is Synchronization Needed? 1/4        int    Count = 10;        Process 1     Process 2        Count++; Count--;      ...
Why is Synchronization Needed? 2/4   int   Count = 10;   Process 1              Process 2   LOAD Reg, Count        LOAD Re...
Why is Synchronization Needed? 3/4         Process 1                   Process 2  Inst     Reg   Memory    Inst       Reg ...
Why is Synchronization Needed? 4/4         Process 1                   Process 2  Inst     Reg   Memory    Inst       Reg ...
Race Conditions A Race Condition occurs, if   two or more processes/threads access and   manipulate the same data concurre...
Critical Section and Mutual Exclusion   A critical section is a section of code in which a   process accesses shared resou...
The Critical Section Protocoldo {                   A critical section protocol                       consists of two part...
Solutions to the Critical Section Problem Any solution to the critical section problem must satisfy the following three co...
Mutual ExclusionIf a process P is executing in its critical section,then no other processes can be executing in theircriti...
ProgressIf no process is executing in its critical sectionand some processes wish to enter their criticalsections, then   ...
Bounded WaitingAfter a process made a request to enter itscritical section and before it is granted thepermission to enter...
Software Solutions for        Two ProcessesSuppose we have two processes, P0 and P1.Let one process be Pi and the other be...
Algorithm I: 1/2                                            Global variableprocess Pi                                  tur...
Algorithm I: 2/2                                            This solution doesprocess Pi                                  ...
Algorithm II: 1/2bool     flag[2];                                      Variable flag[i]         I am interesteddo {      ...
Algorithm II: 2/2bool   flag[2];                    The correctness of this       I am interested             algorithm is...
Algorithm III: a Combination 1/4            bool flag[2]; // process Pi            int turn;                      I am int...
Algorithm III: Mutual Exclusion 2/4       flag[i] = TRUE;               process Pi       turn = j;       while (flag[j] &&...
Algorithm III: Progress 3/4      flag[i] = TRUE;               process Pi      turn = j;      while (flag[j] && turn == j)...
Algorithm III: Bounded Waiting 4/4        flag[i] = TRUE;               process Pi        turn = j;        while (flag[j] ...
Hardware SupportThere are two types of hardwaresynchronization supports:   Disabling/Enabling interrupts: This is slow  an...
Interrupt Disablingdo {                              Because interrupts are                                  disabled, no ...
Special Machine InstructionsAtomic: These instructions execute as oneuninterruptible unit. More precisely, when suchan ins...
The Test-and-Set Instructionbool TS(bool *key)      bool   lock = FALSE;{   bool save = *key;    do {   *key = TRUE;   ret...
Problems with Software and      Hardware SolutionsAll of these solutions use busy waiting.Busy waiting means a process wai...
Upcoming SlideShare
Loading in …5
×

Synchronization

830 views

Published on

Published in: Technology, Business
  • Be the first to comment

Synchronization

  1. 1. Part IIProcess ManagementChapter 6: Process Synchronization 1
  2. 2. Process SynchronizationWhy is synchronization needed?Race ConditionsCritical SectionsPure Software SolutionsHardware SupportSemaphoresMonitorsMessage Passing 2
  3. 3. Why is Synchronization Needed? 1/4 int Count = 10; Process 1 Process 2 Count++; Count--; Count = ? 9, 10 or 11? 3
  4. 4. Why is Synchronization Needed? 2/4 int Count = 10; Process 1 Process 2 LOAD Reg, Count LOAD Reg, Count ADD #1 SUB #1 STORE Reg, Count STORE Reg, Count The problem is that the execution flow may be switched in the middle! 4
  5. 5. Why is Synchronization Needed? 3/4 Process 1 Process 2 Inst Reg Memory Inst Reg MemoryLOAD 10 10 LOAD 10 10 SUB 9 10ADD 11 10STORE 11 11 erases the previous value 11 STORE 9 9 5
  6. 6. Why is Synchronization Needed? 4/4 Process 1 Process 2 Inst Reg Memory Inst Reg MemoryLOAD 10 10ADD 11 10 LOAD 10 10 SUB 9 10 STORE 9 9STORE 11 11 erases the previous value 9 6
  7. 7. Race Conditions A Race Condition occurs, if two or more processes/threads access and manipulate the same data concurrently, and the outcome of the execution depends on the particular order in which the access takes place.Synchronization is needed to prevent raceconditions from happening.Synchronization is a difficult topic. Don’t missa class; otherwise, you will miss a lot of things. 7
  8. 8. Critical Section and Mutual Exclusion A critical section is a section of code in which a process accesses shared resources. Thus, the execution of critical sections must be mutually exclusive (e.g., at most one process can be in its critical section at any time). The critical-section problem is to design a protocol that processes can use to cooperate. critical sections int count; // sharedcount++; count--; cout << count; 8
  9. 9. The Critical Section Protocoldo { A critical section protocol consists of two parts: an entry section entry section and an exit section. critical section Between them is the exit section critical section that must run in a mutually exclusive way.} while (1); 9
  10. 10. Solutions to the Critical Section Problem Any solution to the critical section problem must satisfy the following three conditions: Mutual Exclusion Progress Bounded Waiting Moreover, the solution cannot depend on CPU’s relative speed and scheduling policy. 10
  11. 11. Mutual ExclusionIf a process P is executing in its critical section,then no other processes can be executing in theircritical sections.The entry protocol should be capable ofblocking processes that wish to enter but cannot.Moreover, when the process that is executing inits critical section exits, the entry protocol mustbe able to know this fact and allows a waitingprocess to enter. 11
  12. 12. ProgressIf no process is executing in its critical sectionand some processes wish to enter their criticalsections, then Only those processes that are waiting to enter can participate in the competition (to enter their critical sections). No other process can influence this decision. This decision cannot be postponed indefinitely. 12
  13. 13. Bounded WaitingAfter a process made a request to enter itscritical section and before it is granted thepermission to enter, there exists a bound on thenumber of times that other processes areallowed to enter.Hence, even though a process may be blockedby other waiting processes, it will not be waitingforever. 13
  14. 14. Software Solutions for Two ProcessesSuppose we have two processes, P0 and P1.Let one process be Pi and the other be Pj, wherej = 1- i. Thus, if i = 0 (resp., i = 1), then j = 1(resp., j = 0).We want to design the enter-exit protocol for acritical section so that mutual exclusion isguaranteed. 14
  15. 15. Algorithm I: 1/2 Global variableprocess Pi turn controls whodo { if it is not my turn, I wait can enter the enter critical section. while (turn != i); Since turn is either 0 or 1, only one can critical section enter. turn = j; exit However, processes are forced to run in} while (1); an alternating way. I am done, it is your turn now 15
  16. 16. Algorithm I: 2/2 This solution doesprocess Pi not fulfill thedo { if it is not my turn, I wait progress condition. enter If Pj exits by setting turn to i and while (turn != i); terminates, Pi can critical section enter but cannot turn = j; exit enter again. Thus, an irrelevant process can block} while (1); other processes from entering a I am done, it is your turn now critical section. 16
  17. 17. Algorithm II: 1/2bool flag[2]; Variable flag[i] I am interesteddo { is the “state” of wait for you process Pi: interested or not- flag[i] = TRUE; enter interested. while (flag[j]); Pi expresses its critical section intention to enter, waits for Pj to exit, flag[i] = FALSE; exit enters its section, and finally changes to “I am out” upon} I am not interested exit. 17
  18. 18. Algorithm II: 2/2bool flag[2]; The correctness of this I am interested algorithm is timingdo { wait for you dependent! If both processes set flag[i] = TRUE; enter flag[i] and while (flag[j]); flag[j] to TRUE at the same time, then critical section both will be looping at flag[i] = FALSE; exit the while forever and no one can enter. Bounded waiting does} I am not interested not hold. 18
  19. 19. Algorithm III: a Combination 1/4 bool flag[2]; // process Pi int turn; I am interested do { yield to you first flag[i] = TRUE; enter turn = j; while (flag[j] && turn == j);I am done critical section wait while you are interested and it is flag[i] = FALSE; exit your turn. } 19
  20. 20. Algorithm III: Mutual Exclusion 2/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j);If both processes are in their critical sections, then flag[j] && turn == j (Pi) and flag[i] && turn == i (Pj) are both FALSE. flag[i] and flag[j] are both TRUE Thus, turn == i and turn == j are FALSE. Since turn can hold one value, only one of turn == i or turn == j is FALSE, but not both. We have a contradiction and Pi and Pj cannot be in their critical sections at the same time. 20
  21. 21. Algorithm III: Progress 3/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j);If Pi is waiting to enter, it must be executing itswhile loop.Suppose Pj is not in its critical section: If Pj is not interested in entering, flag[j] was set to FALSE when Pj exits. Thus, Pi may enter. If Pj wishes to enter and sets flag[j] to TRUE, it will set turn to i and Pi may enter.In both cases, processes that are not waiting do notblock the waiting processes from entering. 21
  22. 22. Algorithm III: Bounded Waiting 4/4 flag[i] = TRUE; process Pi turn = j; while (flag[j] && turn == j); When Pi wishes to enter: If Pj is outside of its critical section, then flag[j] is FALSE and Pi may enter. If Pj is in its critical section, eventually it will set flag[j] to FALSE and Pi may enter. If Pj is in the entry section, Pi may enter if it reaches while first. Otherwise, Pj enters and Pi may enter after Pj sets flag[j] to FALSE and exits. Thus, Pi waits at most one round! 22
  23. 23. Hardware SupportThere are two types of hardwaresynchronization supports: Disabling/Enabling interrupts: This is slow and difficult to implement on multiprocessor systems. Special privileged machine instructions: Test and set (TS) Swap Compare and Swap (CS) 23
  24. 24. Interrupt Disablingdo { Because interrupts are disabled, no context entry switch will occur in a disable interrupts critical section. Infeasible in a critical section multiprocessor system because all CPUs must enable interrupts be informed. exit Some features that} while (1); depend on interrupts (e.g., clock) may not work properly. 24
  25. 25. Special Machine InstructionsAtomic: These instructions execute as oneuninterruptible unit. More precisely, when suchan instruction runs, all other instructions beingexecuted in various stages by the CPUs will bestopped (and perhaps re-issued later) until thisinstruction finishes.Thus, if two such instructions are issued at thesame time, even though on different CPUs, theywill be executed sequentially.Privileged: These instructions are, in general,privileged, meaning they can only execute insupervisor or kernel mode. 25
  26. 26. The Test-and-Set Instructionbool TS(bool *key) bool lock = FALSE;{ bool save = *key; do { *key = TRUE; return save; entry} while (TS(&lock)); critical section Mutual exclusion is obvious as only one TS lock = FALSE; exit instruction can run at a time. However, progress and } while (1); bounded waiting may not be satisfied. 26
  27. 27. Problems with Software and Hardware SolutionsAll of these solutions use busy waiting.Busy waiting means a process waits by executing a tightloop to check the status/value of a variable.Busy waiting may be needed on a multiprocessor system;however, it wastes CPU cycles that some other processesmay use productively.Even though some personal/lower-end systems may allowusers to use some atomic instructions, unless the systemis lightly loaded, CPU and system performance can below, although a programmer may “think” his/herprogram looks more efficient.So, we need a better solution. 27

×