0% found this document useful (0 votes)
32 views16 pages

OS - Lec - 04 - Process Synchronization

Process Synchronization Operating Systems
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views16 pages

OS - Lec - 04 - Process Synchronization

Process Synchronization Operating Systems
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Operating

Systems
Designed by Mr. M Mudassar
Process
Threads
CPU Scheduling
Process Synchronization
Deadlocks
Memory Management
Outline Virtual Memory
File System
I/O System
Disk Management
Protection
Security
Process Synchronization
Process Synchronization
Producer Consumer Dry run

repeat repeat
… while counter = 0 Counter = 5
produce an item in nextp do no operation
… nextc = buffer [out] T0: producer execute R1 = counter {R1 = 5}
while counter = n out = (out-1) mod n T1: producer execute R1 = R1+1 {R1 = 6}
do no operation counter = counter - 1 T2: consumer execute R2 = counter {R2 = 5}
buffer[in] = nextp … T3: consumer execute R2 = R2 – 1 {R2 = 4}
in = (in+1) mod n Consume the item in nextc T4: producer exec. counter = R1 {counter = 6}
counter = counter + 1 … T5: consumer exec. counter = R2 {counter = 4}
until false until false

Code for procedure Code for consumer


R1 = counter R2 = counter
R1 = R1 + 1 R2 = R2 – 1
counter = R1 counter = R2
Process Synchronization –
Critical Section Problem

A solution to the critical section problem


must satisfy the following three
requirements:
1. Mutual Exclusion
2. Progress
3. Bounded Waiting
Process Synchronization – Mutual Exclusion
using Critical Region
Process Synchronization
– Semaphore

wait(S): while S ≤ 0 do no operation

•S=S–1

Signal(S): S = S + 1

P1:

• Statement1
• Signal(mutex)

P2:

• Wait(mutex) Semaphore variable (Mutex) = 0 or 1


• Statement2
Record Based Semaphore

1 CS is empty, waiting queue is empty


type semaphore = record CS is occupied, waiting queue is empty
value = integer 0
LP = List of processes -3 CS is occupied, 3 processes in queue
end
-4 CS is occupied, 4 processes in queue
The semaphore operations can be defined as:

Wait(S): S.value = S.value – 1 Signal(S): S.value = S.value + 1


if S.value < 0 if S.value ≤ 0
then begin then begin
add this process to S.LP remove a process from S.LP
end end
Binary Semaphore

type binary_semaphore = record


value: (0,1)
Queue = List of processes
end
var S: binary_semaphore
The semaphore operations can be defined as:

Wait B(S): if S.value = 1 Signal B(S): if S.Queue is empty


then S.value=0 then S.value = 1
else begin else begin
place this process to S.Queue remove a process from S.Queue
block this process place process P on ready list
end end
Process Synchronization – Deadlock and
Starvation
• Improper usage of semaphore creates deadlock and
starvation. (variable S and Q values?)
• For example, the two concurrent processes misusing
the semaphore variable such as below:

P1 P2
Wait(S) Wait(Q)
Wait(Q) Wait(S)
… …
Signal(S) Signal(Q)
Signal(Q) Signal(S)

Wait(S): while S≤0 do no-operation


S=S-1
Signal(S): S=S+1
Classical Problems in Process
Synchronization – The Bounded
Buffer Problem
The structure of producer process: The structure of consumer process:
Three variables: mutex=1, empty=n, full=0
Repeat Repeat
… Wait(full)
Produce an item in nextp Wait(mutex)
… …
Wait(empty) Remove an item from the buffer to nextc // CS
Wait(mutex)


Signal(mutex)
Add nextp to the buffer // CS
Signal(empty)


Signal(Mutex)
Signal(full)
Consume the item in nextc

Until End Until End


Classical Problems in Process
Synchronization – The Dinning
Philosopher Problem
The Structure of Dinning Philosopher i:
Variables: chopstick array [0..4] of semaphore, all five
locations initialized with 1.
Repeat
Wait(chopstick[i])
Wait(chopstick[(i+1) mod 5])

Eat

Signal(chopstick[i])
Signal(chopstick[(i+1) mod 5])

Think

Until End
Classical Problems in Process
Synchronization – The Dinning
Philosopher Problem
Remedies for the solution of Dinning Philosopher
Problem:
1. Maximum four philosophers can access the
chopstick at a time.
2. A philosopher will start eating if and only if
both chopsticks(left and right) are available.
3. The philosopher with even number will choose
the right chopstick, and the odd number will
choose the left chopstick.
Classical Problems in Process
Synchronization – Readers and
Writers Problem
Remember: Correctness Criteria:
ar: number of active reader. 1. Scheduling of waiting process
rr: number of running reader.  0 <= rr <= ar
 0 <= rw <= aw
aw: number of active writer.
2. Mutual exclusion of running processes
rw: number of running writer. • Not (rr > 0 & rw>0)
3. No deadlock of active processes
• (rr = 0 & rw = 0 ) & (ar > 0 || aw > 0) implies
Request resource (rr > 0 || rw > 0 ) within a finite time
Use resource 4. Writer will have the priority over
Release resource readers
References
• https://youtu.be/3Kp4GlKqefo
• https://youtu.be/HthluCAXfxs
• https://youtu.be/QYheRZ-S7SY
• https://youtu.be/jM9_u__dr2g
• https://youtu.be/5SBe6rIi8nI
• https://youtu.be/o1Joyj_Ow6M
• https://youtu.be/4Mw_a3IlldM
• https://youtu.be/DLkrIRxuy7k
• https://youtu.be/0g4Z_Ws9y6k
• https://youtu.be/cGM5QtcS4KE
Thank You(Q & A)

You might also like