SlideShare a Scribd company logo
Process Synchronization
Presented by Dani Mfungo
HD/UDOM/408/T.2015
CS603:computer system programming
Objective and Outcomes
• What happened when Concurrent process shared data
• How to handle data inconsistency
• Investigate a critical section (CS) as a protocol for synchronization
• Algorithmic approach to CS implementation
• Investigate classical process-synchronization problems
• Producer – consumer problem
• Dining –philosopher problems
• Sleeping barber problems
• Cigarette smokers’ problem
• Investigation of tools used to solve process synchronization problems
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Lets consider to approach on handling concurrent
problem
• LANGUAGE STRUCTURE FOR CONCURENCY PROCESS
• Fork – Join
• Cobegin - Coend
• How do they help on construct concurrency program??
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
TRANSLATE THE PRECENDENCE GRAPH TO
Cobegin –Coend structure
1. Begin
2. S1;
3. Cobegin
4. S3;
5. begin
6. S2;
7. Cobegin
8. S4;
9. S5;
10. Coend
11. S6;
12. End
13. Coend
14. S7;
15. End Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
TRANSLATE THE PRECENDENCE GRAPH TO
fork –join structure
1. Begin L1:S3 L2:S6
2. count = 3; goto L3 goto L3
3. S1;
4. fork L1
5. S2;
6. S4;
7. fork L2
8. S5;
9. L3: join count;
10. S7;
11. End
Task of Join Count statement
Count = count -1
If count ≠ 0 𝑡ℎ𝑒𝑛 𝑄𝑢𝑖𝑡
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Which structure is better than Other?
What is solution for a system which
implement
Cobegin – Coend structure ???
• It is impossible to write Cobegin – Coend Here
• Fork –join is the best solution
? Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
PRODUCER / CONSUMER PROBLEM
What will happen if
• the rate to producer is greater than the rate to consume?
• Rate to producer < rate to consume?
• Rate to produce == to Rate to consume?
• Producer generate Items
• Consumer consume item
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
PRODUCER – CONSUMER PROBLEM-> SOLUTION
Two Situation
1. Unbounded Buffer
2. Bounded buffer – limited size
When buffer is buffer is full,
• producer wait until items are consumed
• Rate of consumption > rate of production, result to empty buffer
Producer Consumer problems also known as Bounded Buffer Problem
Buffer (n)
P
c
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
bounded buffer problem
• Solution to this must satisfy the following condition
1. A producer must not overwrite a full buffer
2. Consumer must not consume an empty buffer
3. Consumer and buffer must access buffer in a mutually exclusive manner
• Variable count keep track number of item (N) in buffer
• For Producer
• If count = n -> buffer is full then producer go to sleep
• If count ≠ 𝑛 producer add item and increment count
• For Consumer
• If count = 0 - > buffer empty, consumer go to sleep
• If count ≠ 0 consumer remove an item and decrement counter
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
bounded buffer problem
Is this simple algorithm free of problems??
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Producer Process
count + 1
A: R1 = count
B: R1 = R1+ 1
C: count = R1
Consumer Process
count -1
D: R2 = count
E: R2 = R2-1
F: count = R2
Bounded buffer weakness & race condition
Execution sequence
A R1 =7
B R1=8
D R2=7
E R2=6
C count=8
F count=6
 Also consider Transaction process
 Race condition result a critical
section problems
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
CRITICAL SECTION
• A critical section of a data item d is a section of code which cannot be executed
concurrently with it self or with other critical section(s) for d.
• PROPERTIES OF CS IMPLEMENTATION
• CORRECTENESS (Mutual execution): at most one process may execute a CS at a given
moment
• PROGRESS: A process will take a finite time interval to execute its critical section
• BOUNDED WAIT: Processes wait a finite time interval to enter their critical
sections.
• Absence of deadlock: Processes should not block each other indefinitely
Sunday, July 10, 2016DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Approaches for Implementing Synchronization
• 3 Categories
1. Busy waiting
2. Hardware support
3. Operating System support
1. Dekker’s algorithm and Peterson’s algorithm are used for Busy waiting
2. Disable interrupt for accessing hardware facilities
3. Mechanism and tools such as semaphores and monitors are used by OS
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
CRITICAL SECTION
• Entry section make decision if process should enter CS or not
• Shared Lock mechanism is used for CS
• Process enter a exit section after CS
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
ENTRY SECTION {
} EXIT SECTION {
CRITICAL SECTION
REMINDER SECTION
}
Decision made, locking mechanism
Decision made, locking mechanism (unlock process)
Reminder section (other part of the code)
Simple natural solution
• Synchronization between two process P1 and P2
• Each process wait for the other to leave CS
• Implement shared variable process_turn
• While loop act as entry section and if variable = 1, P1 enter CS otherwise if 2, P2
enter CS
• P1 finish execution, initialize process_turn to 2, P2 execute also.
• What if P2 still in non critical section and don’t want to execute?
• This approach / protocol will satisfy ME but not Progress
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
• Only Mutual execution are granted
• Progress is not work in this situation
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Another Solution
• The algorithm is that it does not save the state of the executing process
• To store the state of the process, two more variables, namely, state_flag_P1 and
state_flag_P2
• If a process enters its critical section, it first sets this state flag to zero,
and after exiting, it sets it to one
• It indicates that if a process is using its CS, then the other process must wait until the state
flag becomes one,
• state flag variables eliminate the problem of progress
• Also this solution suffer from other problem as well – don’t guarantee Mutual execution
since
• In the beginning, when no process is executing, both P1 and P2 will try to enter the critical
section
consider diagram
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
3rd Solution
• Setting state_flag_P1 and state_flag_P2 to 0 before the while loop
• This will solve the problem of mutual exclusion, but there still is one problem
• Suppose, at the moment P1 starts and executes its first statement as state_ flag_P1 =
0, P2 interrupts and gets the execution and executes its first statement, state_ flag_P2
= 0.
• In this situation, if P2 continues and executes its while loop, then it will not be able to
proceed as it is waiting for P1 to set state_ flag_P1 to 1.
• Both the processes are waiting for each other, thereby causing deadlock in the
system.
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
• Two result
• Dead lock and livelock
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Dekker’s Solution
• This algorithm satisfies all the rules of the designed protocol.
• Both the processes will not try to enter simultaneously due to the variable,
process_turn and will satisfy the mutual exclusion property.
• If P1 starts first and finds that state_ flag_P2 = 0, that is, P2 wants to enter the CS, it
will allow P2, only when process_turn = 2.
• Otherwise, it will wait for state_ flag_P2 to be 1 so that P1 can enter its CS. If state_
flag_P2 = 1 initially, then P1 will skip the while loop and straightway enter the CS.
• In this way, there will not be any deadlock or livelock.
• Moreover, the chance is given to only those processes that are waiting in the queue
to enter the CS.
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Dekker’s Solution
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Peterson’s Solution
• easier method compared to Dekker’s solution
var flag: array [0..1] of Boolean;
Turn : 0..1;
Pi
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Flag [i] =true;
Turn =j;
While(flag[i] and turn=j) do skip;
CS
Flag [i] = false;
• process_turn takes the value zero for Process P1 and one for Process P2
• For the process flag, a Boolean array process_ flag[] is taken that consists
of two values, 0 for P1 and 1 for P2
• The variable process_turn maintains the mutual exclusion and process_ flag[]
maintains the state of the process.
• Initially, both the processes make their flags true but to maintain the mutual
exclusion, the processes before entering their critical sections allow other
processes to run
• After exiting the critical section, the process makes its flag false so that the
other process can start if it wants.
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Peterson’s Solution
SEMAPHORE
• The semaphore is used to protect any resource such as global shared memory that needs to be
accessed and updated by many processes simultaneously
• Semaphore acts as a guard or lock on the resource
• Whenever a process needs to access the resource, it first needs to take permission from the
semaphore
• The semaphore is implemented as an integer variable, say as S, and can be initialized with any
positive integer values.
• The semaphore is accessed by only two indivisible operations known as wait and signal
operations, denoted by P and V, respectively
• Whenever a process tries to enter the critical section, it needs to perform wait operation
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
SEMAPHORE cont…
• Initially, the count of semaphore is 1
• It decrement to zero when process accesses the available critical area
• When a process exits the critical section, it performs the signal operation
• The semaphore whose value is either zero or one is known as binary semaphore
• semaphore that takes a value greater than one is known as counting semaphore.
• In binary semaphore,the CS locked by a process may be unlocked by any other
process.
• However, in mutex, only the process that locks the CS can unlock it.
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
one problem in the implementation of such a semaphore
• When a process does not get access to the critical
section, it loops continually waiting for it.
• This does not produce any result but consumes CPU
cycles, thereby wasting the processor time
Mutex: semaphore mutex=1
Pi: P(mutex)
CS
V(mutex)
RS
SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS
USING SEMAPHORES
• PROCESS SYNCHRONIZATION
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
P(sync)
Sj
Si
V(sync)
Si Sj
Sync:semaphore
Sync = 0;
S1
S2
.
.
.
Si
Si+1
Si+2
.
.
U1
U2
.
.
.
Uj
Uj+1
Uj+2
.
.
V(s)
P(s)
Wait for Ui to finish
Sequence: J1 J2
Run
concurrently
SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING
SEMAPHORES
• SOLUTION OF Cobegin – coend synchronization process
Variable a,b,c,d,e,g: semaphore
1. Begin
2. Cobegin
3. S1; V(a);V(b); end
4. P(a); S2;S4;V(c);V(d);
5. P(b); S3; V(e);
6. P(c);S5;V(f);
7. P(d);P(e);S6;V(g);
8. P(f);P(g);S7;
9. coend
10. End
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Type semaphore = record
value= integer;
L: list of processes;
end;
Var S:semaphore;
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
TO OVERCOME A BUSY WAIT PROBLEM
Define a semaphore AS structure or records and not integer
V(S): S.Value=S.Value+1
If S.value ≤ 0 then
Begin
Remove process P from S.L
//put P into ready queue
Wakeup(P);
End;
P(S): S.Value=S.Value-1
If S.value<0 then
Begin
Add this process to S.L
// into wait state
Block;
End;
SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING
SEMAPHORES
• PRODUCER/ CONSUMER PROBLEM
full,empty,mutex:semaphore
nextp, nextc:item
full =0;
empty=n;
mutex=1;
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015
Consumer:
repeat
P(full)
P(mutex)
……
Remove an item from buffer to nextc
……
V(mutex)
V(empty)
Until false;
Producer:
repeat
production on item in nextp;
…..
P(empty)
P(mutex)
……
Add nextp to buffer
……
V(mutex)
V(full)
Until false;
CONSUMERPRODUCER
Demostration
Simulation about Process synchronization
Sunday, July 10,
2016
DANI ELIAS MFUNGO HD/UDOM/408/T.2015

More Related Content

What's hot (20)

PDF
CS6601 DISTRIBUTED SYSTEMS
Kathirvel Ayyaswamy
 
PPT
Distributed Operating System
SanthiNivas
 
PPTX
Desktop and multiprocessor systems
V.V.Vanniaperumal College for Women
 
DOCX
Operating System Process Synchronization
Haziq Naeem
 
DOCX
Complete Operating System notes
Lakshmi Sarvani Videla
 
PPT
Chapter 4 a interprocess communication
AbDul ThaYyal
 
PDF
Processes and Processors in Distributed Systems
Dr Sandeep Kumar Poonia
 
PPTX
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
PPT
distributed shared memory
Ashish Kumar
 
PDF
CS9222 ADVANCED OPERATING SYSTEMS
Kathirvel Ayyaswamy
 
PPTX
Allocating of Frames.pptx
infomerlin
 
PPT
Operating Systems - "Chapter 4: Multithreaded Programming"
Ra'Fat Al-Msie'deen
 
PPTX
Multi processor scheduling
Shashank Kapoor
 
PPT
File models and file accessing models
ishmecse13
 
PPTX
Process scheduling
Deepika Balichwal
 
PPTX
Computer Networks: Quality of service
Kongu Engineering College, Perundurai, Erode
 
PPTX
Critical section problem in operating system.
MOHIT DADU
 
PPTX
Design Goals of Distributed System
Ashish KC
 
PPTX
Global state recording in Distributed Systems
Arsnet
 
PDF
TOC 1 | Introduction to Theory of Computation
Mohammad Imam Hossain
 
CS6601 DISTRIBUTED SYSTEMS
Kathirvel Ayyaswamy
 
Distributed Operating System
SanthiNivas
 
Desktop and multiprocessor systems
V.V.Vanniaperumal College for Women
 
Operating System Process Synchronization
Haziq Naeem
 
Complete Operating System notes
Lakshmi Sarvani Videla
 
Chapter 4 a interprocess communication
AbDul ThaYyal
 
Processes and Processors in Distributed Systems
Dr Sandeep Kumar Poonia
 
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
distributed shared memory
Ashish Kumar
 
CS9222 ADVANCED OPERATING SYSTEMS
Kathirvel Ayyaswamy
 
Allocating of Frames.pptx
infomerlin
 
Operating Systems - "Chapter 4: Multithreaded Programming"
Ra'Fat Al-Msie'deen
 
Multi processor scheduling
Shashank Kapoor
 
File models and file accessing models
ishmecse13
 
Process scheduling
Deepika Balichwal
 
Computer Networks: Quality of service
Kongu Engineering College, Perundurai, Erode
 
Critical section problem in operating system.
MOHIT DADU
 
Design Goals of Distributed System
Ashish KC
 
Global state recording in Distributed Systems
Arsnet
 
TOC 1 | Introduction to Theory of Computation
Mohammad Imam Hossain
 

Viewers also liked (10)

PPTX
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
PPTX
Mutual Exclusion using Peterson's Algorithm
Souvik Roy
 
PDF
Unit II - 3 - Operating System - Process Synchronization
cscarcas
 
PDF
Web technology
Selvin Josy Bai Somu
 
PDF
Operating Systems - Synchronization
Emery Berger
 
PPT
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
PPT
Web Tech
Rupsee
 
PPT
OS Process Synchronization, semaphore and Monitors
sgpraju
 
PPT
Process Synchronization And Deadlocks
tech2click
 
PDF
Soft computing
Dr Sandeep Kumar Poonia
 
Concurrency: Mutual Exclusion and Synchronization
Anas Ebrahim
 
Mutual Exclusion using Peterson's Algorithm
Souvik Roy
 
Unit II - 3 - Operating System - Process Synchronization
cscarcas
 
Web technology
Selvin Josy Bai Somu
 
Operating Systems - Synchronization
Emery Berger
 
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Web Tech
Rupsee
 
OS Process Synchronization, semaphore and Monitors
sgpraju
 
Process Synchronization And Deadlocks
tech2click
 
Soft computing
Dr Sandeep Kumar Poonia
 
Ad

Similar to Process synchronization in operating system (20)

PDF
OS Process synchronization Unit3 synchronization
subhamchy2005
 
PPT
Operating System
Subhasis Dash
 
PDF
Process Synchronization - Part1
Amir Payberah
 
PDF
OperatingSystem-Unit2_Process Management
SonaShaiju1
 
PDF
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
PDF
CH05.pdf
ImranKhan880955
 
PDF
Process Synchronization
Shipra Swati
 
PPTX
operating system notes about deadlock 3.pptx
panditestmail
 
PPTX
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
PDF
Ch5 process synchronization
Welly Dian Astika
 
PPTX
Lecture 8.pptx Operating system lecture
ReelsShortVideo
 
PPT
Ch7
Bilal Arshad
 
PPTX
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
PDF
Process Synchronization
Christalin Nelson
 
PPTX
Synchronization Peterson’s Solution.pptx
Senthil Vit
 
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
vaishnavbhavna17
 
PPTX
Synchronization in os.pptx
AbdullahBhatti53
 
PPT
Process Synchronization -1.ppt
jayverma27
 
PPT
Ipc feb4
yashnand
 
PPTX
9-Operating Systems -Synchronization, interprocess communication, deadlock.pptx
sekkiran
 
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Operating System
Subhasis Dash
 
Process Synchronization - Part1
Amir Payberah
 
OperatingSystem-Unit2_Process Management
SonaShaiju1
 
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
CH05.pdf
ImranKhan880955
 
Process Synchronization
Shipra Swati
 
operating system notes about deadlock 3.pptx
panditestmail
 
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
Ch5 process synchronization
Welly Dian Astika
 
Lecture 8.pptx Operating system lecture
ReelsShortVideo
 
Process Synchronization in operating system | mutex | semaphore | race condition
Shivam Mitra
 
Process Synchronization
Christalin Nelson
 
Synchronization Peterson’s Solution.pptx
Senthil Vit
 
794985751-Unit-3-Inter-Process-Communication.pptx
vaishnavbhavna17
 
Synchronization in os.pptx
AbdullahBhatti53
 
Process Synchronization -1.ppt
jayverma27
 
Ipc feb4
yashnand
 
9-Operating Systems -Synchronization, interprocess communication, deadlock.pptx
sekkiran
 
Ad

Recently uploaded (20)

PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
PPTX
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPSX
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
PDF
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
How to Manage Access Rights & User Types in Odoo 18
Celine George
 
BANDHA (BANDAGES) PPT.pptx ayurveda shalya tantra
rakhan78619
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
HEALTH ASSESSMENT (Community Health Nursing) - GNM 1st Year
Priyanshu Anand
 
'' IMPORTANCE OF EXCLUSIVE BREAST FEEDING ''
SHAHEEN SHAIKH
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Configure Lost Reasons in Odoo 18 CRM
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Zoology (Animal Physiology) practical Manual
raviralanaresh2
 

Process synchronization in operating system

  • 1. Process Synchronization Presented by Dani Mfungo HD/UDOM/408/T.2015 CS603:computer system programming
  • 2. Objective and Outcomes • What happened when Concurrent process shared data • How to handle data inconsistency • Investigate a critical section (CS) as a protocol for synchronization • Algorithmic approach to CS implementation • Investigate classical process-synchronization problems • Producer – consumer problem • Dining –philosopher problems • Sleeping barber problems • Cigarette smokers’ problem • Investigation of tools used to solve process synchronization problems Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 3. Lets consider to approach on handling concurrent problem • LANGUAGE STRUCTURE FOR CONCURENCY PROCESS • Fork – Join • Cobegin - Coend • How do they help on construct concurrency program?? Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 4. TRANSLATE THE PRECENDENCE GRAPH TO Cobegin –Coend structure 1. Begin 2. S1; 3. Cobegin 4. S3; 5. begin 6. S2; 7. Cobegin 8. S4; 9. S5; 10. Coend 11. S6; 12. End 13. Coend 14. S7; 15. End Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 5. TRANSLATE THE PRECENDENCE GRAPH TO fork –join structure 1. Begin L1:S3 L2:S6 2. count = 3; goto L3 goto L3 3. S1; 4. fork L1 5. S2; 6. S4; 7. fork L2 8. S5; 9. L3: join count; 10. S7; 11. End Task of Join Count statement Count = count -1 If count ≠ 0 𝑡ℎ𝑒𝑛 𝑄𝑢𝑖𝑡 Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 6. Which structure is better than Other? What is solution for a system which implement Cobegin – Coend structure ??? • It is impossible to write Cobegin – Coend Here • Fork –join is the best solution ? Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 7. PRODUCER / CONSUMER PROBLEM What will happen if • the rate to producer is greater than the rate to consume? • Rate to producer < rate to consume? • Rate to produce == to Rate to consume? • Producer generate Items • Consumer consume item Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 8. PRODUCER – CONSUMER PROBLEM-> SOLUTION Two Situation 1. Unbounded Buffer 2. Bounded buffer – limited size When buffer is buffer is full, • producer wait until items are consumed • Rate of consumption > rate of production, result to empty buffer Producer Consumer problems also known as Bounded Buffer Problem Buffer (n) P c Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 9. bounded buffer problem • Solution to this must satisfy the following condition 1. A producer must not overwrite a full buffer 2. Consumer must not consume an empty buffer 3. Consumer and buffer must access buffer in a mutually exclusive manner • Variable count keep track number of item (N) in buffer • For Producer • If count = n -> buffer is full then producer go to sleep • If count ≠ 𝑛 producer add item and increment count • For Consumer • If count = 0 - > buffer empty, consumer go to sleep • If count ≠ 0 consumer remove an item and decrement counter Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 10. bounded buffer problem Is this simple algorithm free of problems?? Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 11. Producer Process count + 1 A: R1 = count B: R1 = R1+ 1 C: count = R1 Consumer Process count -1 D: R2 = count E: R2 = R2-1 F: count = R2 Bounded buffer weakness & race condition Execution sequence A R1 =7 B R1=8 D R2=7 E R2=6 C count=8 F count=6  Also consider Transaction process  Race condition result a critical section problems Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 12. CRITICAL SECTION • A critical section of a data item d is a section of code which cannot be executed concurrently with it self or with other critical section(s) for d. • PROPERTIES OF CS IMPLEMENTATION • CORRECTENESS (Mutual execution): at most one process may execute a CS at a given moment • PROGRESS: A process will take a finite time interval to execute its critical section • BOUNDED WAIT: Processes wait a finite time interval to enter their critical sections. • Absence of deadlock: Processes should not block each other indefinitely Sunday, July 10, 2016DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 13. Approaches for Implementing Synchronization • 3 Categories 1. Busy waiting 2. Hardware support 3. Operating System support 1. Dekker’s algorithm and Peterson’s algorithm are used for Busy waiting 2. Disable interrupt for accessing hardware facilities 3. Mechanism and tools such as semaphores and monitors are used by OS Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 14. CRITICAL SECTION • Entry section make decision if process should enter CS or not • Shared Lock mechanism is used for CS • Process enter a exit section after CS Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 ENTRY SECTION { } EXIT SECTION { CRITICAL SECTION REMINDER SECTION } Decision made, locking mechanism Decision made, locking mechanism (unlock process) Reminder section (other part of the code)
  • 15. Simple natural solution • Synchronization between two process P1 and P2 • Each process wait for the other to leave CS • Implement shared variable process_turn • While loop act as entry section and if variable = 1, P1 enter CS otherwise if 2, P2 enter CS • P1 finish execution, initialize process_turn to 2, P2 execute also. • What if P2 still in non critical section and don’t want to execute? • This approach / protocol will satisfy ME but not Progress Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 16. • Only Mutual execution are granted • Progress is not work in this situation Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 17. Another Solution • The algorithm is that it does not save the state of the executing process • To store the state of the process, two more variables, namely, state_flag_P1 and state_flag_P2 • If a process enters its critical section, it first sets this state flag to zero, and after exiting, it sets it to one • It indicates that if a process is using its CS, then the other process must wait until the state flag becomes one, • state flag variables eliminate the problem of progress • Also this solution suffer from other problem as well – don’t guarantee Mutual execution since • In the beginning, when no process is executing, both P1 and P2 will try to enter the critical section consider diagram Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 18. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 19. 3rd Solution • Setting state_flag_P1 and state_flag_P2 to 0 before the while loop • This will solve the problem of mutual exclusion, but there still is one problem • Suppose, at the moment P1 starts and executes its first statement as state_ flag_P1 = 0, P2 interrupts and gets the execution and executes its first statement, state_ flag_P2 = 0. • In this situation, if P2 continues and executes its while loop, then it will not be able to proceed as it is waiting for P1 to set state_ flag_P1 to 1. • Both the processes are waiting for each other, thereby causing deadlock in the system. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 20. • Two result • Dead lock and livelock Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 21. Dekker’s Solution • This algorithm satisfies all the rules of the designed protocol. • Both the processes will not try to enter simultaneously due to the variable, process_turn and will satisfy the mutual exclusion property. • If P1 starts first and finds that state_ flag_P2 = 0, that is, P2 wants to enter the CS, it will allow P2, only when process_turn = 2. • Otherwise, it will wait for state_ flag_P2 to be 1 so that P1 can enter its CS. If state_ flag_P2 = 1 initially, then P1 will skip the while loop and straightway enter the CS. • In this way, there will not be any deadlock or livelock. • Moreover, the chance is given to only those processes that are waiting in the queue to enter the CS. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 22. Dekker’s Solution Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 23. Peterson’s Solution • easier method compared to Dekker’s solution var flag: array [0..1] of Boolean; Turn : 0..1; Pi Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 Flag [i] =true; Turn =j; While(flag[i] and turn=j) do skip; CS Flag [i] = false;
  • 24. • process_turn takes the value zero for Process P1 and one for Process P2 • For the process flag, a Boolean array process_ flag[] is taken that consists of two values, 0 for P1 and 1 for P2 • The variable process_turn maintains the mutual exclusion and process_ flag[] maintains the state of the process. • Initially, both the processes make their flags true but to maintain the mutual exclusion, the processes before entering their critical sections allow other processes to run • After exiting the critical section, the process makes its flag false so that the other process can start if it wants. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 25. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 Peterson’s Solution
  • 26. SEMAPHORE • The semaphore is used to protect any resource such as global shared memory that needs to be accessed and updated by many processes simultaneously • Semaphore acts as a guard or lock on the resource • Whenever a process needs to access the resource, it first needs to take permission from the semaphore • The semaphore is implemented as an integer variable, say as S, and can be initialized with any positive integer values. • The semaphore is accessed by only two indivisible operations known as wait and signal operations, denoted by P and V, respectively • Whenever a process tries to enter the critical section, it needs to perform wait operation Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 27. SEMAPHORE cont… • Initially, the count of semaphore is 1 • It decrement to zero when process accesses the available critical area • When a process exits the critical section, it performs the signal operation • The semaphore whose value is either zero or one is known as binary semaphore • semaphore that takes a value greater than one is known as counting semaphore. • In binary semaphore,the CS locked by a process may be unlocked by any other process. • However, in mutex, only the process that locks the CS can unlock it. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 28. Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 one problem in the implementation of such a semaphore • When a process does not get access to the critical section, it loops continually waiting for it. • This does not produce any result but consumes CPU cycles, thereby wasting the processor time Mutex: semaphore mutex=1 Pi: P(mutex) CS V(mutex) RS
  • 29. SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES • PROCESS SYNCHRONIZATION Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 P(sync) Sj Si V(sync) Si Sj Sync:semaphore Sync = 0; S1 S2 . . . Si Si+1 Si+2 . . U1 U2 . . . Uj Uj+1 Uj+2 . . V(s) P(s) Wait for Ui to finish Sequence: J1 J2 Run concurrently
  • 30. SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES • SOLUTION OF Cobegin – coend synchronization process Variable a,b,c,d,e,g: semaphore 1. Begin 2. Cobegin 3. S1; V(a);V(b); end 4. P(a); S2;S4;V(c);V(d); 5. P(b); S3; V(e); 6. P(c);S5;V(f); 7. P(d);P(e);S6;V(g); 8. P(f);P(g);S7; 9. coend 10. End Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015
  • 31. Type semaphore = record value= integer; L: list of processes; end; Var S:semaphore; Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 TO OVERCOME A BUSY WAIT PROBLEM Define a semaphore AS structure or records and not integer V(S): S.Value=S.Value+1 If S.value ≤ 0 then Begin Remove process P from S.L //put P into ready queue Wakeup(P); End; P(S): S.Value=S.Value-1 If S.value<0 then Begin Add this process to S.L // into wait state Block; End;
  • 32. SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES • PRODUCER/ CONSUMER PROBLEM full,empty,mutex:semaphore nextp, nextc:item full =0; empty=n; mutex=1; Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015 Consumer: repeat P(full) P(mutex) …… Remove an item from buffer to nextc …… V(mutex) V(empty) Until false; Producer: repeat production on item in nextp; ….. P(empty) P(mutex) …… Add nextp to buffer …… V(mutex) V(full) Until false; CONSUMERPRODUCER
  • 33. Demostration Simulation about Process synchronization Sunday, July 10, 2016 DANI ELIAS MFUNGO HD/UDOM/408/T.2015