Lecture Concurrency Mutual Exclusion and Synchronization Operating System
- Slides: 33
Lecture : Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2008 1
Concurrency n n An OS has many concurrent processes that run in parallel but share common access Race Condition: A situation where several processes access and manipulate the same data concurrently and the outcome of the execution depends on the particular order in which the access takes place. 2
Example for Race condition n Suppose a customer wants to book a seat on UAL 56. Ticket agent will check the #-of-seats. If it is greater than 0, he will grab a seat and decrement #-of-seats by 1. UAL 56: #-of-seats=12 Terminal Ticket Agent 1 Ticket Agent 2 … Main memory Terminal Ticket Agent n 3
Example for Race condition(cont. ) Ticket Agent 1 Ticket Agent 2 Ticket Agent 3 P 1: LOAD #-of-seats P 2: DEC 1 P 3: STORE #-of-seats Q 1: LOAD #-of-seats Q 2: DEC 1 Q 3: STORE #-of-seats R 1: LOAD #-of-seats R 2: DEC 1 R 3: STORE #-of-seats Suppose, initially, #-of-seats=12 Suppose instructions are interleaved as P 1, Q 1, R 1, P 2, Q 2, R 2, P 3, Q 3, R 3 The result would be #-of-seats=11, instead of 9 To solve the above problem, we must make sure that: P 1, P 2, P 3 must be completely executed before we execute Q 1 or R 1, or Q 1, Q 2, Q 3 must be completely executed before we execute P 1 or R 1, R 2, R 3 must be completely executed before we execute P 1 or Q 1. 4
Critical Section Problem Critical section: a segment of code in which the process may be changing common variables, updating a table, writing a file, and so on. P 0 P 1 Pn-1 Prefix 0 Prefix 1 Prefixn-1 CS 0 CS 1 Suffix 0 Suffix 1 … CSn-1 Suffixn-1 Goal: To program the processes so that, at any moment of time, at most one of the processes is in its critical section. 5
Solution to Critical-Section Problem Any facility to provide support for mutual exclusion should meet the following requirements: n 1. 2. 3. 4. 5. 6. Mutual exclusion must be enforced: Only one process at a time is allowed into its critical section A process that halts in its noncritical section must do so without interfering with other processes. A process waiting to enter its critical section cannot be delayed infinitely When no process is in a critical section, any process that requests entry to its critical section must be permitted to enter without delay. No assumption are made about the relative process speeds or the number of processors. A process remains inside its critical section for a finite time only. 6
Three Environments 1. 2. 3. There is no central program to coordinate the processes. The processes communicate with each other through global variable. Special hardware instructions There is a central program to coordinate the processes. 7
Three Environments 1. 2. 3. There is no central program to coordinate the processes. The processes communicate with each other through global variable. Special hardware instructions There is a central program to coordinate the processes. 8
1 st Attempt Start with just 2 processes, P 0 and p 1 Global variable turn, initially turn=0 Prefix 0 While (turn 0) do {} CS 0 turn=1 suffix 0 Prefix 1 While (turn 1) do {} CS 1 turn=0 suffix 1 The processes take turn to enter its critical section If turn=0, P 0 enters If turn=1, P 1 enters This solution guarantees mutual exclusion. But the drawback is that the method is not fair, because P 0 is priviledged. Worse yet, until P 0 executed its CS, P 1 is blocked. 9
2 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 While (flag[1]) do {} flag[0]=true CS 0 flag[0]=false suffix 0 Prefix 1 While (flag[0]) do {} flag[1]=true CS 1 flag[1]= false suffix 1 If P 0 is in critical section, flag[0] is true; If P 1 is in critical section, flag[1] is true If one process leaves the system, it will not block the other process. However, mutual exclusion is not guaranteed. P 0 executes the while statement and finds that flag[1] is false; P 1 executes the while statement and finds that flag[0] is false. P 0 sets flag[0] to true and enters its critical section; P 1 sets flag[1] to true and enters its critical section. 10
3 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 flag[0]=true While (flag[1]) do {} CS 0 flag[0]=false suffix 0 Prefix 1 flag[1]=true While (flag[0]) do {} CS 1 flag[1]= false suffix 1 If P 0 is in critical section, flag[0] is true; If P 1 is in critical section, flag[1] is true Guarantees mutual exclusion. But mutual blocking can occur. P 0 sets flag[0] to be true; P 1 sets flag[1] to be true; Both will be hung in the while loop. 11
4 st Attempt Global variable flag[0] and flag[1], initially flag[0] and flag[1] are both false Prefix 0 L 0: flag[0]=true If (flag[1]) then { flag[0]=false; goto L 0} CS 0 flag[0]=false suffix 0 Prefix 1 L 1: flag[1]=true If (flag[0]) then { flag[1]=false; goto L 1}} CS 1 flag[1]= false suffix 1 Guarantees mutual exclusion. mutual blocking can occur if they execute at the same speed. 12
Correct Solution (Dekker’s Alg) – The first correct mutual exclusion alg (early 1960’s) Initially, flag[0]=flag[1]=false; turn=0 Prefix 0 flag[0]=true while (flag[1]) do { if (turn=1){ flag[0]=false; while(turn=1) do{} flag[0]=true; } } CS 0 turn=1 flag[0]=false suffix 0 Prefix 1 flag[1]=true while (flag[0]) do { if (turn=0){ flag[1]=false; while(turn=0) do{} flag[1]=true; } } CS 1 turn=0 flag[1]=false suffix 1 13
Peterson’s Algorithm for 2 processes – The simplest and most compact mutual exclusion alg. Initially, flag[0]=flag[1]=false Prefix 0 flag[0]=true turn=1 while (flag[1] and turn=1) do{} CS 0 flag[0]=false suffix 0 Prefix 1 flag[1]=true turn=0 while (flag[0] and turn=0) do{} CS 1 flag[1]=false suffix 1 14
Solution for n processes Global Variable n 1. Flag[0. . n-1] – array of size n. Flag[i]= 2. Idle Want-in in-CS if Pi is outside Csi if Pi wants to be in CSi if Pi is in CSi Turn. Initially, Turn=some no. between 0 and n-1 15
Solutions for n processes Pi Prefixi Repeat Flag[i]=want-in; j=Turn; while j i do {if Flag[j] idle then j=Turn else j=(j+1) mod n} Flag[i]=in-CS j=0 while (j<n) and (j=i or Flag[j] in-CS) do {j=j+1} Until (j n) and (Turn=i or Flag[Turn]=idle) Turn=i; CSi j=(Turn+1)mod n While (j Turn) and (Flag[j]=idle) do{j=(i+1) mod n} Turn=j Flag[i]=idle 16
Three Environments 1. 2. 3. There is no central program to coordinate the processes. The processes communicate with each other through global variable. Special hardware instructions There is a central program to coordinate the processes. 17
Hardware Support Disable interrupt CS Enable interrupt Won’t work if we have multiprocessors 18
Special Machine Instructions n Modern machines provide special atomic hardware instructions 4 Atomic = non-interruptable l Either test memory word and set value l Or swap contents of two memory words 19
TS – Test and Set Boolean TS(i)= true if i=0; it will also set i to 1 false if i=1 Initially, lock=0 Pi Prefixi While(¬ TS(lock)) do {} CSi Lock=0 suffixi It is possible that a process may starve if 2 processes enter the critical section arbitrarily often. 20
Three Environments 1. 2. 3. There is no central program to coordinate the processes. The processes communicate with each other through global variable. Special hardware instructions There is a central program to coordinate the processes. 21
Semaphores A variable that has an integer value upon which 3 operations are defined. Three operations: n n 1. 2. 3. n A semaphore may be initialized to a nonnegative value The wait operation decrements the semaphore value. If the value becomes negative, then the process executing the wait is blocked The signal operation increments the semaphore value. If the value is not positive, then a process blocked by a wait operation is unblocked. Other than these 3 operations, there is no way to inspect or manipulate semaphores. 22
Wait(s) and Signal(s) n n Wait(s) – is also called P(s) { s=s-1; if (s<0) {place this process in a waiting queue} } Signal(s) – is also called V(s) { s=s+1; if(s 0) {remove a process from the waiting queue} } 23
Semaphore as General Synchronization Tool n n Counting semaphore – integer value can range over an unrestricted domain Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement n n Also known as mutex locks Wait B(s) s is a binary semaphore { if s=1 then s=0 else block this process } Signal B(s) { if there is a blocked process then unblock a process else s=1 } Can implement a counting semaphore S as a binary semaphore 24
Note n The wait and signal primitives are assumed to be atomic; they cannot be interrupted and each routine can be treated as an indivisible step. 25
Two classical examples n n Producer and Consumer Problem Readers/Writers Problem 26
Two classical examples n n Producer and Consumer Problem Readers/Writers Problem 27
Producer and Consumer Problem n n n Producer can only put something in when there is an empty buffer Consumer can only take something out when there is a full buffer Producer and consumer are concurrent processes 0 consumer producer N buffers … N-1 28
Producer and Consumer Problem(cont. ) Global Variable n 1. 2. 3. B[0. . N-1] – an array of size N (Buffer) P – a semaphore, initialized to N C – a semaphore, initialized to 0 Local Variable n 1. 2. In – a ptr(integer) used by the producer, in=0 initially Out – a ptr(integer) used by the consumer, out=0 initially Producer Process producer: produce(w) wait(p) B[in]=w in=(in+1)mod N signal(c) goto producer W is a local buffer used by the producer to produce Consumer Process consumer: wait(c) w=B[out] out=(out+1)mod N signal(p) consume(w) goto consumer W is a local buffer used by the consumer 29 to store the item to be consumed
Two classical examples n n Producer and Consumer Problem Readers/Writers Problem 30
Readers/Writers Problem n Suppose a data object is to be shared among several concurrent processes. Some of these processes want only to read the data object, while others want to update (both read and write) n n Readers – Processes that read only Writers – processes that read and write If a reader process is using the data object, then other reader processes are allowed to use it at the same time. If a writer process is using the data object, then no other process (reader or writer) is allowed to use it simultaneously. 31
Solve Readers/Writers Problem using wait and signal primitives(cont. ) n Global Variable: n n Wrt is a binary semaphore, initialized to 1; Wrt is used by both readers and writers For Reader Processes: n n Mutex is a binary semaphore, initialized to 1; Readcount is an integer variable, initialized to 0 Mutex and readcount used by readers only Reader Processes Wait(mutex) Readcount=readcount+1 If readcount=1 then wait(wrt) Signal(mutex); … Reading is performed … Wait(mutex) Readcount=readcount-1 If readcount=0 then signal(wrt) Signal(mutex) Writer Processes Wait(wrt) … Writing is performed … Signal(wrt) 32
End of lecture 6 Thank you! 33
- Design fifo and non fifo execution
- Classical synchronization problems
- Synchronization tools in os
- Which one of the following is a synchronization tool?
- Singhal heuristic algorithm
- Mutual exclusion.
- Performance metrics for mutual exclusion algorithm
- Metode pembentukan mutual exclusion
- Mutual exclusion principle
- Dijkstra mutual exclusion algorithm
- Mutual exclusion in distributed systems
- Operating system lecture notes
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Clock synchronization in distributed system
- Why was life on the manor often harsh for peasants
- Mutual aid box alarm system
- Synchronization algorithms and concurrent programming
- Buddy system memory
- File system in operating system
- File system in operating system
- File system in operating system
- Concurrency control and recovery
- Concurrency control and recovery
- Communication and concurrency
- Safety and liveness in concurrency
- Transactions and concurrency control in distributed systems
- Transaction management and concurrency control
- Example of concurrency control in dbms
- Special lines in triangles
- Transaction management and concurrency control
- Problem statement for project
- Example of inclusion and exclusion criteria
- What is the inclusion and exclusion criteria in research
- Exclusion crisis in absalom and achitophel