Title: Chapter 7: Process Synchronization
1Chapter 7 Process Synchronization
- Background
- The Critical-Section Problem
- Synchronization Hardware
- Semaphores
- Classical Problems of Synchronization
- Critical Regions
- Monitors
- Synchronization in Solaris 2 Windows 2000
Modified from Silberschatz, Galvin and Gagne
?2002 W. Stallingss slides
Prepared by Dr. Kocan
2Concurrency in Three Contexts
- Multiple applications
- Multiprogramming
- Structured application
- Application can be a set of concurrent processes
- Operating-system structure
- Operating system is a set of processes or threads
3Difficulties in Concurrency
- Sharing global resources
- Eg. Simultaneous read/write on the same variable
- Management of allocation of resources
- Allocate an I/O channel to process and then
suspends the process - the OS locks the channel ? deadlock possible
- Programming errors difficult to locate
- The results are not deterministic and
reproducible - Valid problems in multiprocessor as well as
uniprocessor systems
4A simple example single copy of echo for all
programs
- void echo()
-
- chin getchar()
- chout chin
- putchar(chout)
-
- void echo()
-
- chin getchar()
- chout chin
- putchar(chout)
-
- P1 invoke red echo and interrupted after
getchar() - Chin x
- P2 invoke blue echo and runs to the conclusion
- Outputs y
- P1 resumed.
- chin is overridden and now has y
- outputs y
5Echo example One process in echo at a time
- P1 invokes echo and interrupted after getchar()
- Chin x
- P2 invokes echo is blocked from entering the
procedure - P2 is suspended/blocked awaiting P1 to release
echo - P1 resumed. Finds chin untouched.
- Displays x
- P1 exits echo and removes the block on P2.
- Lesson Protect echo
6Echo in Multiprocessor system
Process 1
Process 2
- void echo()
-
- .
- chin getchar()
- chout chin
- .
- putchar(chout)
- .
-
- void echo()
-
- chin getchar()
- .
- chout chin
- putchar(chout)
- .
- .
-
Processor 2
Processor 1
- P1 P2 invoke echo at the same time
- Both enter echo
- What happens?
SOLUTION control access to shared resources
7Operating System Concerns
- Keep track of active processes (PCBs and/or TCBs)
- Allocate and deallocate resources
- Processor time (i.e. scheduling)
- Memory
- Files
- I/O devices
- Protect data and resources
- Result of process must be independent of the
speed of execution of other concurrent processes - That is our topic now!
- Next we will elaborate this issue of independent
of the speed
8Process Interaction
- Processes unaware of each other
- They dont have intention to work together
- Eg. Multiprogramming of multiple independent
processes - OS concern competition for resources
- eg. P1 and P2 wants to access the same disk,
printer - Processes indirectly aware of each other
- Share access to the same object (sa. I/O buffer)
- They dont know each other by their process Ids
- Cooperation in sharing the common object
- Process directly aware of each other
- They know each other by their process Ids
- Designed to work jointly on some activity
- Cooperation by communication
9Competition Among Processes for Resources
- Mutual Exclusion
- Critical sections
- Only one program at a time is allowed in its
critical section - Example only one process at a time is allowed to
send command to the printer - Deadlock
- LIVELOCK is different from DEADLOCK
- Starvation
10Cooperation Among Processes by Sharing
- Writing must be mutually exclusive
- Critical sections are used to provide data
integrity - Data coherency
11Cooperation Among Processes by Communication
- Messages are passes
- Mutual exclusion is not a control requirement
- Possible to have deadlock
- Each process waiting for a message from the other
process - Receive(A, message) Receive(B,
message) - Send(B, message) Send(A,
message) - Possible to have starvation
- Two processes sending message to each other while
another process waits for a message
12Cooperation by Sharing
- Concurrent access to shared data may result in
data inconsistency. - Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
processes. - Shared-memory solution to bounded-butter problem
(Chapter 4) allows at most n 1 items in buffer
at the same time. A solution, where all N
buffers are used is not simple. - Suppose that we modify the producer-consumer code
by adding a variable counter, initialized to 0
and incremented each time a new item is added to
the buffer
13Bounded-Buffer
- Shared data
- define BUFFER_SIZE 10
- typedef struct
- . . .
- item
- item bufferBUFFER_SIZE
- int in 0
- int out 0
- int counter 0 ? New
Counter the number of items in the buffer
14Bounded-Buffer
- Producer process
- item nextProduced
- while (1)
- while (counter BUFFER_SIZE)
- / do nothing /
- bufferin nextProduced
- in (in 1) BUFFER_SIZE
- counter
-
15Bounded-Buffer
- Consumer process
- item nextConsumed
- while (1)
- while (counter 0)
- / do nothing /
- nextConsumed bufferout
- out (out 1) BUFFER_SIZE
- counter--
-
-
16Bounded Buffer
- The statementscountercounter--must be
performed atomically. - Atomic operation means an operation that
completes in its entirety without interruption.
17Bounded Buffer
- The statement count may be implemented in
machine language asregister1 counter - register1 register1 1counter register1
- The statement count may be implemented
asregister2 counterregister2 register2
1counter register2
18Bounded Buffer
- If both the producer and consumer attempt to
update the buffer concurrently, the assembly
language statements may get interleaved. - Interleaving depends upon how the producer and
consumer processes are scheduled.
19Bounded Buffer
- Assume counter is initially 5. One interleaving
of statements isproducer register1 counter
(register1 5)producer register1 register1
1 (register1 6)consumer register2 counter
(register2 5)consumer register2 register2
1 (register2 4)producer counter register1
(counter 6)consumer counter register2
(counter 4) - The value of count may be either 4 or 6, where
the correct result should be 5.
20Race Condition
- Race condition The situation where several
processes access and manipulate shared data
concurrently. The final value of the shared data
depends upon which process finishes last. - To prevent race conditions, concurrent processes
must be synchronized.
21The Critical-Section Problem
- n processes all competing to use some shared data
- Each process has a code segment, called critical
section (CS), in which the shared data is
accessed. - Problem ensure that when one process is
executing in its critical section, no other
process is allowed to execute in its critical
section.
22Solution to Critical-Section Problem
- 1. Mutual Exclusion. If process Pi is executing
in its critical section, then no other processes
can be executing in their critical sections. - 2. 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. No DEADLOCK or STARVATION - 3. 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 made a request to enter its critical section
and before that request is granted. - Assume that each process executes at a nonzero
speed - No assumption concerning relative speed of the n
processes.
23Bounded Waiting
- P1 wants to enter CS
- Any Pi enters its CS at most N times after P1s
request - After N entries, P1 must enter its CS
- N must be bounded
24Initial Attempts to Solve Problem(TWO PROCESSES
SOLUTIONS)
- Only 2 processes, P0 and P1
- General structure of process Pi (other process
Pj) - do
- entry section
- critical section
- exit section
- reminder section
- while (1)
- Processes may share some common variables to
synchronize their actions.
25Algorithm 1
- Shared variables
- int turninitially turn 0
- turn - i ? Pi can enter its critical section
- Process Pi
- do
- while (turn ! i)
- critical section
- turn j
- reminder section
- while (1)
- Satisfies mutual exclusion, but not progress
- Pi, Pj, Pi, Pj strict alternation of processes
- Pi leaves, Pj busy with long I/O, Pi comes back
to CS-entry - No one in the CS, but Pi has to wait until Pj to
come to the CS - What if Pj never comes back to CS ????
In MIPS assembly
Load R3, j Load R2, i Loop
Load R1, turn BNE R1, R2, Loop
cs Store R3, turn
rs
atomic
Problem No sufficient information about the
states of the processes
26Algorithm 2
- Shared variables
- boolean flag2initially flag 0 flag 1
false. - flag i true ? Pi ready to enter its critical
section - Process Pi
- do
- flagi true while (flagj)
critical section - flag i false
- remainder section
- while (1)
- Satisfies mutual exclusion, but not progress
requirement.
27Algorithm 2 - illustration
Pj do flagj true while (flagi)
critical section flag j false
remainder section while (1)
Pi do flagi true while (flagj)
critical section flag i false
remainder section while (1)
Pi sets flagi to true Pj sets flagj to
true Indefinite wait deadlock
Algorithm depends on the exact timing of the two
processes
28Algorithm 3
- Combined shared variables of algorithms 1 and 2.
- Process Pi
- do
- flag i true turn j while (flag j
and turn j) - critical section
- flag i false
- remainder section
- while (1)
- Meets all three requirements solves the
critical-section problem for two processes.
29Algorithm 3 - cont.
- Claim Mutual exclusion is preserved
- Proof
- Assume Pi and Pj in CS at the same time
- flagi flagj True
- (I) flagj turn j ? False ? turn i
- (II) flagi turn i ? False ? turn j
- turn cannot be i and j at the same time
30Algorithm 3- cont.
- Claim Progress is met.
- Proof
- Pi stuck at flagj true turn j while
loop - (case 1) Pj is not ready to enter
- flagj false ? Pi can enter
- (case 2) Pj is ready to enter
- Pj set flagj to true and at its while
- Observation turn i XOR turn j
- (case 2.1) turn i ? Pi will enter
- (case 2.2) turn j ? Pj will enter
- (case 2.2.1) Pj leaves CS sets flagj to false
? Pi enters - (case 2.2.2) Pj leaves CS sets flagj to true
- then sets turn to i ? Pi enters
- (Pi at while cannot change turn)
- Pi (Pj) will enter the CS (progress) after at
most one entry by Pj (Pi) (bounded waiting) -
31Bakery Algorithm
Critical section for n processes
- Before entering its critical section, process
receives a number. Holder of the smallest number
enters the critical section. - If processes Pi and Pj receive the same number,
if i lt j, then Pi is served first else Pj is
served first. - Processes know each other by IDs
- The numbering scheme always generates numbers in
increasing order of enumeration i.e.,
1,2,3,3,3,3,4,5...
32Bakery Algorithm
- Notation lt? lexicographical order (ticket ,
process id ) - (a,b) lt c,d) if a lt c or if a c and b lt d
- max (a0,, an-1) is a number, k, such that k ? ai
for i - 0, , n 1 - Shared data
- boolean choosingn
- int numbern
- Data structures are initialized to false and
0 respectively
33Bakery Algorithm
- do
- choosingi true
- numberi max(number0, number1, , number
n 1)1 - choosingi false
- for (j 0 j lt n j)
- while (choosingj)
- while ((numberj ! 0) (numberj,j lt
numberi,i)) -
- critical section
- numberi 0
- remainder section
- while (1)
34Hardware Synchronization
- Uniprocessor environment (see NACHOS!)
- DISABLE INTERRUPTS
- critical section
- ENABLE INTERRUPTS
- Result No preemption
- Multiprocessor environment
- Enable/disable interrupt wont work.
- message passing
- Systems clock may be skewed
- System clock is updated by interrupts
35Synchronization Hardware
- Test and modify the content of a word
atomically. - boolean TestAndSet(boolean target)
- boolean rv target
- tqrget true
- return rv
-
boolean testset(int i) if (i0) i
1 return true else
return false
Atomic uninterruptible
36Mutual Exclusion with Test-and-Set
- Shared data boolean lock false
- Process Pi
- do
- while (TestAndSet(lock))
- critical section
- lock false
- remainder section
-
37Synchronization Hardware
- Atomically swap two variables.
- void Swap(boolean a, boolean b)
- boolean temp a
- a b
- b temp
-
Atomic uninterruptible
38Mutual Exclusion with Swap
- Shared data (initialized to false) boolean
lock - boolean waitingn
- Process Pi
- do
- key true
- while (key true)
- Swap(lock,key)
- critical section
- lock false
- remainder section
-