Download presentation
Presentation is loading. Please wait.
1
Chapter 6: Process Synchronization
2
Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions
3
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Example: Producer Consumer Problem in the following slides
4
Producer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE); // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
5
Consumer while (true) { while (count == 0); // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; // consume the item in nextConsumed }
6
Race Conditions in Producer Consumer Functions count++ could be implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2
7
Race Condition in Producer Consumer Functions Consider this execution interleaving with “count = 5” initially: S0: Producer register1 = count {register1 = 5} S1: Producer register1 = register1 + 1 {register1 = 6} S2: Consumer register2 = count {register2 = 5} S3: Consumer register2 = register2 - 1 {register2 = 4} S4: Producer count = register1 {count = 6 } S5: Consumer count = register2 {count = 4} Wrong results could be produced due to race conditions
8
Critical Section Problem n processes {P 0, P 1,..., P n-1 } have a segment of code, called a critical section, in which a process may change a common variable, update a shared table or a file,... When one process is in a CS, ensure no other process is allowed to enter the CS Entry section: code segment to request the permission to enter a CS Exit section: code segment to exit the CS Remainder section: remaining code
9
Solution to Critical Section Problem: 3 Conditions to meet C1.Mutual Exclusion: If a process is executing in its critical section, then no other processes can be executing in their critical sections C2. Progress: If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely
10
Solution to Critical Section Problem: 3 Conditions to meet C3. Bounded Waiting: A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has requested to enter its critical section and before that request is granted
11
Peterson’s Solution Two process solution Assume LOAD and STORE instructions are atomic; that is, they cannot be interrupted Two processes share two variables: –int turn: whose turn it is to enter the CS –Boolean flag[2]: flag[i] = true implies that process P i is ready to enter the CS
12
Algorithm for Process P i while (true) { flag[i] = TRUE; turn = j; while (flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } C1, C2, C3 all satisfied! atomic
13
N process solution Bakery algorithm –On entering the store, each customer receives a number –The customer with the lowest number is served next –If two customers have the same number, serve the one with the smaller ID number
14
Process P i in bakery algorithm while(1) { choosing[i] = true; number[i] = max(number[0], number[1],..., number[n-1]) + 1; choosing[i] = false; for k=0 to n-1 { while (choosing[k]) nop; while (number[k] ≠ 0 and (number[k], k) < (number[i], i)) nop; } Critical section number[i] = 0; remainder section }
15
Synchronization Hardware For efficiency and convenience of programming, modern machines provide special atomic hardware instructions –Atomic = non-interruptible –test-and-set –swap
16
TestAndndSet Instruction boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } Remember this instruction is atomic!
17
Solution using TestAndSet Initially shared variable lock = false while (1) { while (TestAndSet (&lock)) nop; critical section lock = FALSE; remainder section } C1 & C2 supported C3 not supported
18
Swap Instruction void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } This instruction is also atomic!
19
Solution using Swap Initially shared boolean variable lock = false Each process has a local boolean variable key while (true) { key = TRUE; while ( key == TRUE) swap (&lock, &key ) nop; // critical section lock = FALSE; // remainder section } C1 & C2 supported C3 not supported
20
Semaphore Much simpler than the other solutions Semaphore S: integer variable Only two atomic operations can modify S –wait() and signal() –Originally called P() and V() wait (S) { while S ≤ 0 nop; //busy waiting S-- } signal (S) { S++; }
21
Semaphore as General Synchronization Tool Binary semaphore –Special case where the value of semaphore S is either 0 or 1 –Simpler to implement Counting semaphore –Semaphore can have integer values –When a process does wait(S) and finds that S->value < 0, the process is added to the waiting list for S
22
Binary Semaphore Usages Provides mutual exclusion – called mutex semaphore Semaphore S; // initialized to 1 wait (S); Critical Section signal (S);
23
Binary Semaphore Usages Solve synchronization problems –Example: Ensure process P2 executes only after process P1 completes –Initialize binary semaphore synch = 0 P1P2 S1;wait(synch); signal(synch); S2;
24
Semaphore Implementation with no Busy waiting Each semaphore has a waiting queue Two operations: –block: Place the process invoking the operation on the appropriate waiting queue –wakeup: Remove one of processes in the waiting queue and place it in the ready queue
25
Semaphore Implementation with no Busy waiting (Cont.) wait (S) { S.value--; if (value < 0) add this process to waiting queue S.waiting_q block(); } signal (S) { S.value++; if (value ≤ 0) { remove a process P from S.waiting_q wakeup(P); }
26
How to make wait and signal atomic? Uniprocessor –Disable interrupt Multiprocessor –Instructions of different processes running on different processors may interleave in an arbitrary manner –Spinlock (busy waiting) is necessary
27
Deadlock Deadlock: Two or more processes are waiting indefinitely for an event that can be caused by only one of the other waiting processes
28
Deadlock Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);.. signal (S); signal (Q); signal (Q); signal (S);
29
Starvation Indefinite blocking –A process may never be removed from the semaphore queue in which it is suspended –Can happen if processes are removed from the waiting queue in LIFO order
30
Classical Problems of Synchronization Bounded-Buffer Problem Readers and Writers Problem Dining-Philosophers Problem
31
Bounded-Buffer Problem N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N.
32
Bounded Buffer Problem Producer while (true) { // produce an item wait (empty); wait (mutex); // add the item to the // buffer signal (mutex); signal (full); } Consumer while (true) { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item }
33
Readers-Writers Problem A data set is shared among a number of concurrent processes –Readers only read the data set; they do not perform any updates –Writers can both read and write. Multiple readers to read at the same time Only a single writer can access the shared data at the same time. Shared Data –Semaphore mutex initialized to 1 –Semaphore wrt initialized to 1 –Integer readcount initialized to 0
34
Readers-Writers Problem Writer while (true) { wait (wrt) ; // writing is performed signal (wrt) ; } Reader while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount- - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; }
35
Dining-Philosophers Problem Shared data –Bowl of rice (data set) –Semaphore chopstick [5] initialized to 1
36
Dining-Philosophers Problem Philosopher i while (true) { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think }
37
Dining-Philosophers Problem What happens if every philosopher becomes hungry at the same time? Each philosopher picks his/her right chopstic: chopsitc[1..5] = 0 They are delayed forever Solutions –Allow at most four philosophers –Pick up only if two chopsticks are available at the same time –An odd philosopher picks up the left chopstick first, while an even philosopher picks up the right one first
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.