Title: Lecture 6: Concurrency: Mutual Exclusion and Synchronization
1Lecture 6 Concurrency Mutual Exclusion and
Synchronization
- Operating System
- Fall 2006
2Concurrency
- 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.
3Example for Race condition
- 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.
4Example for Race condition(cont.)
Ticket Agent 1 P1 LOAD -of-seats P2 DEC 1 P3
STORE -of-seats
Ticket Agent 2 Q1 LOAD -of-seats Q2 DEC 1 Q3
STORE -of-seats
Ticket Agent 3 R1 LOAD -of-seats R2 DEC 1 R3
STORE -of-seats
Suppose, initially, -of-seats12 Suppose
instructions are interleaved as
P1,Q1,R1,P2,Q2,R2,P3,Q3,R3 The result would be
-of-seats11, instead of 9
To solve the above problem, we must make sure
that P1,P2,P3 must be completely executed before
we execute Q1 or R1, or Q1,Q2,Q3 must be
completely executed before we execute P1 or R1,
or R1,R2,R3 must be completely executed before we
execute P1 or Q1.
5Critical 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.
Goal To program the processes so that, at any
moment of time, at most one of the processes is
in its critical section.
6Solution to Critical-Section Problem
- Any facility to provide support for mutual
exclusion should meet the following requirements - 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 indefinitely - 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.
7Three Environments
- 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.
8Three Environments
- 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.
91st Attempt
Start with just 2 processes, P0 and p1 Global
variable turn, initially turn0
Prefix0 While (turn?0) do
CS0 turn1 suffix0
Prefix1 While (turn?1) do
CS1 turn0 suffix1
The processes take turn to enter its critical
section If turn0, P0 enters If turn1, P1 enters
This solution guarantees mutual exclusion.
But the drawback is that if one process leaves
the system or fails, the other will be blocked
permanently.
102st Attempt
Global variable flag0 and flag1, initially
flag0 and flag1 are both false
Prefix0 While (flag1) do flag0true
CS0 flag0false suffix0
Prefix1 While (flag0) do flag1true
CS1 flag1 false suffix1
If P0 is in critical section, flag0 is true
If P1 is in critical section, flag1 is true
If one process leaves the system, it will not
block the other process.
However, mutual exclusion is not guaranteed. P0
executes the while statement and finds that
flag1 is false P1 executes the while statement
and finds that flag0 is false. P0 sets flag0
to true and enters its critical section P1 sets
flag1 to true and enters its critical section.
113st Attempt
Global variable flag0 and flag1, initially
flag0 and flag1 are both false
Prefix0 flag0true While (flag1) do
CS0 flag0false suffix0
Prefix1 flag1true While (flag0) do
CS1 flag1 false suffix1
If P0 is in critical section, flag0 is true
If P1 is in critical section, flag1 is true
Guarantees mutual exclusion.
But mutual blocking can occur. P0 sets flag0 to
be true P1 sets flag1 to be true Both will be
hung in the while loop.
124st Attempt
Global variable flag0 and flag1, initially
flag0 and flag1 are both false
Prefix0 L0 flag0true If (flag1) then
flag0false goto L0
CS0 flag0false suffix0
Prefix1 L1 flag1true If (flag0) then
flag1false goto L1 CS1 flag1 false
suffix1
Guarantees mutual exclusion.
mutual blocking can occur if they execute at the
same speed.
13Correct Solution
Initially, flag0flag1false turn0
Prefix0 flag0true while (flag1) do if
(turn1) flag0false while(turn1)
do flag0true CS0 turn1 flag0fals
e suffix0
Prefix1 flag1true while (flag0) do if
(turn0) flag1false while(turn0)
do flag1true CS1 turn0 flag1fals
e suffix1
14Petersons Algorithm for 2 processes
Initially, flag0flag1false
Prefix0 flag0true turn1 while (flag1 and
turn1) do CS0 flag0false suffix0
Prefix1 flag1true turn0 while (flag0 and
turn0) do CS1 flag1false suffix1
15Solution for n processes
- Global Variable
- Flag0..n-1 array of size n.
- Turn. Initially, Turnsome no. between 0 and n-1
Idle if Pi is outside Csi
Want-in if Pi wants to be in CSi
Flagi
in-CS if Pi is in CSi
16Solutions for n processes
Pi Prefixi
Repeat Flagiwant-in jTurn while
j?i do if Flagj?idle then jTurn else j(j1)
mod n Flagiin-CS j0 while (jltn)
and (ji or Flagj?in-CS) do jj1 Until (j?n)
and (Turni or FlagTurnidle) Turni
CSi
j(Turn1)mod n While (j?Turn) and (Flagjidle)
doj(i1) mod n Turnj Flagiidle
17Three Environments
- 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.
18Hardware Support
Disable interrupt CS Enable interrupt
Wont work if we have multiprocessors
19Special Machine Instructions
- Modern machines provide special atomic hardware
instructions - Atomic non-interruptable
- Either test memory word and set value
- Or swap contents of two memory words
20TS Test and Set
Boolean TS(i) true if i0 it will also set i
to 1 false if i1
Initially, lock0
Pi Prefixi While( TS(lock)) do
CSi Lock0 suffixi
It is possible that a process may starve if 2
processes enter the critical section arbitrarily
often.
21TS Test and Set (cont.)
To avoid the starvation problem, we can do the
following
Global variables waiting0..n-1 (boolean)
lock (integer) Local variable
Keyi (boolean)
Initially, lock0 and waitingifalse for all
0?i?n-1
Pi Waitingitrue Keyitrue While(waiting
i and Keyi) do Keyi TS(lock) Waitingifals
e CSi j(i1) mod n While(j?i and
waitingj) do j(j1) mod n if (ji) then
lock0 else Waitingjfalse
22Exchange(int register, int memory)
Exchange the contents of a register with that of
memory.
Shared Variable lock, initially 0 local
variable key
Process Pi Prefixi Keyi1 While(Keyi?0)
do exchange(Keyi,lock) CSi Lock0
suffixi
23Three Environments
- 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.
24Semaphores
- A variable that has an integer value upon which 3
operations are defined. - Three operations
- 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.
25Wait(s) and Signal(s)
- Wait(s) is also called P(s)
-
- ss-1
- if (slt0) place this process in a waiting
queue -
- Signal(s) is also called V(s)
-
- ss1
- if(s?0) remove a process from the waiting
queue -
26Semaphore as General Synchronization Tool
- 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 - Also known as mutex locks
- Wait B(s) s is a binary semaphore
-
- if s1 then s0 else block this process
-
- Signal B(s)
-
- if there is a blocked process then unblock
a process else s1 -
- Can implement a counting semaphore S as a binary
semaphore
27Note
- The wait and signal primitives are assumed to be
atomic they cannot be interrupted and each
routine can be treated as an indivisible step.
28Mutual Exclusion provided by Semaphores
- Semaphore S // initialized to 1
- Pi
- prefixi
- wait (S)
- CSi
- signal (S)
- suffixi
- Signal B(s)
-
- if there is a blocked process
- then unblock a process
- else s1
-
- Wait B(s) s is a binary semaphore
-
- if s1
- then s0
- else block this process
-
29Message Passing
- Direct Addressing
- Specific identifier of source and destination
processes - Send(destination, message)
- Receive(source, message)
- Indirect Addressing
- Messages are not sent directly from sender to
receiver but rather are sent to a shared data
structure consisting of queues that can
temporarily hold messages. - Such queues are generally referred to as
mailboxes. - Thus, for 2 processes to communicate, one process
sends a message to the appropriate mailbox and
the other process picks up the message from the
mailbox.
30Message Passing (cont.)
- When a send primitive is executed in a process,
there are 2 possibilities - Either the sending process is blocked until the
message is received - Or it is not
- When a receive primitive is executed in a
process, there are 2 possibilities - If a message has previously been sent, the
message is received and execution continues - If there is no waiting message, then either
- The process is blocked until a message arrives,
or - The process continues to execute, abandoning the
attempt to receive. - Blocking send, blocking receive
- Nonblocking send, blocking receive
- Nonblocking send , nonblocking receive
31Mutual Exclusion
- Create_mailbox(mutex)
- Send(mutex, null)
- Pi
- Prefixi
- Receive(mutex,msg)
- CSi
- Send(mutex,msg)
- Suffixi
Main process Mailbox name is mutex
32Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
33Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
34Producer and Consumer Problem
- 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
35Producer and Consumer Problem(cont.)
- Global Variable
- B0..N-1 an array of size N (Buffer)
- P a semaphore, initialized to N
- C a semaphore, initialized to 0
- Local Variable
- In a ptr(integer) used by the producer, in0
initially - Out a ptr(integer) used by the consumer, out0
initially
36Two classical examples
- Producer and Consumer Problem
- Readers/Writers Problem
37Readers/Writers Problem
- 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) - 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.
38Solve Readers/Writers Problem using wait and
signal primitives(cont.)
- Global Variable
- Wrt is a binary semaphore, initialized to 1 Wrt
is used by both readers and writers - For Reader Processes
- Mutex is a binary semaphore, initialized to
1Readcount is an integer variable, initialized
to 0 - Mutex and readcount used by readers only
39End of lecture 6
Thank you!