SlideShare a Scribd company logo
Operating Systems: CSE 3204
ASTU
Department of CSE
Lecture 5: Process Synchronization
Chapter Two
Process Management
Content
 Background of Process Synchronization
 The Critical-Section Problem
 Classic Problems of Synchronization
 Semaphores
 Monitors
• Concurrent Processes can be
• Independent processes
• cannot affect or be affected by the execution of another process.
• Cooperating processes
• can affect or be affected by the execution of another process.
• Cooperating processes can either share directly similar address space or
share data through files and messages.
• Concurrent access to shared data may lead to data inconsistency.
• Concurrent execution requires
• process communication and process synchronization
 Process Synchronization
• …mechanisms to ensure the orderly execution of cooperating processes
that share a logical address space, so that data consistency is
maintained.
Background to Process Synchronization
June 24, 2019
• Concurrent processes may have access to shared data and
resources
• Concurrent access to shared data may result in data inconsistency
• If there is no controlled access to shared data, some processes will
obtain an inconsistent view of the shared data
 Consider two processes P1 and P2, accessing shared data. while
P1 is updating data, it is preempted (because of timeout, for
example) so that P2 can run. Then P2 try to read the data, which
are partly modified, which will result in data inconsistency
• In such cases, the outcome of the action performed by concurrent
processes will then depend on the order in which their execution is
interleaved
• Maintaining data consistency requires mechanisms to ensure the
orderly execution of cooperating processes
Background to Process synchronization
June 24, 2019
Synchronization in Producer-Consumer Problem
• producer process produces information that is consumed by a
consumer process.
• The previous solution considered the use of a bounded buffer
• Producer produce items and puts it into the buffer
• Consumer consumes items from the buffer
• Producer and Consumer must synchronize.
• Suppose that we wanted to provide a solution to the consumer-
producer problem that fills all the buffers.
• We can do so by having an integer count that keeps track of the
number of full buffers.
• Initially, count is set to 0.
• It is incremented by the producer after it produces a new buffer
• It is decremented by the consumer after it consumes a buffer
June 24, 2019
Producer - Consumer Problem
Producer
while (true)
{
/* produce an item and put in
nextProduced */
while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
Consumer
while (true)
{
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
/* consume the item innextConsumed
}
June 24, 2019
• Although both producer and consumer routines are correct when executed
separately, they may not properly work when executed concurrently.
•To illustrate this, let’s assume the current value of the count variable is 5
• count++ could be implemented as (in a typical machine language):
register1 = count
register1 = register1 + 1
count = register1
• count-- could be implemented as (in a typical machine language):
register2 = count
register2 = register2 - 1
count = register2
• The concurrent execution of count ++ and count -- is equivalent with the execution
of the statements of each operations where arbitrary interleaving of the statements
occur. A simple arbitrary interleaving of the operations can be as follows:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}
•At S5, we have arrived at the value of counter=4, if we reversed S4 and S5,
we would have arrived at count=6
•We would arrive at the incorrect value of the count variable because we allowed
both processes to manipulate the variable concurrently
Producer Consumer Problem (cont’d)
June 24, 2019
Race condition
• Race condition is the situation where several processes access
and manipulate shared data concurrently
• The final value of the shared data depends upon which process
finishes last.
• The key to preventing trouble here and in many other situations
involving shared memory, shared files, and shared everything
else is to find some way to prohibit more than one process from
reading and writing the shared data at the same time .
• To prevent race conditions, concurrent processes must
coordinate or be synchronized.
June 24, 2019
Example: Race condition updating a variable
June 24, 2019
The Critical-Section Problem
• n processes competing to use some shared data
• Each process has a code segment, called Critical Section (CS), in which the
shared data is accessed
• A critical section is a piece of code in which a process or thread accesses a
common resource
• So each process must first request permission to enter its critical section.
The section of code implementing this request is called the Entry Section
(ES)
• The critical section (CS) might be followed by a Leave/Exit Section (LS)
• The remaining code is the Remainder Section (RS)
• Problem – ensure that when one process is executing in its CS, no other
process is allowed to execute in its CS
• When a process executes code that manipulates shared data (or resource), we
say that the process is in it’s Critical Section (for that shared data)
• The execution of critical sections must be mutually exclusive: at any time,
only one process is allowed to execute in its critical section (even with multiple
processors) 10
June 24, 2019
Critical section to prevent a race condition
June 24, 2019
 Multiprogramming allows logical parallelism, uses devices efficiently
but we lose correctness when there is a race condition.
 So we forbid logical parallelism inside critical section so we lose some
parallelism but we regain correctness.
 So each process must first request permission to enter its critical section.
 The section of code implementing this request is called the Entry Section
(ES).
 The critical section (CS) might be followed by a Leave/Exit Section
(LS).
 The remaining code is the Remainder Section (RS).
 The critical section problem is to design a protocol that the processes can
use so that their action will not depend on the order in which their
execution is interleaved (possibly on many processors).
• General structure of process Pi (other process Pj)
do {
entry section
critical section
leave/exit section
reminder section
} while (1);
The Critical-Section Problem(cont…)
• A solution to a critical section problem must satisfy the following three
requirements:
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. Then only those processes that are not executed in their
remainder sections can participate in the decision on which will enter its
critical section next.
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
Solution to critical section problem
June 24, 2019
Advantages
• Applicable to any number of processes on either a single processor or
multiple processors sharing main memory
• It is simple and therefore easy to verify
• It can be used to support multiple critical sections
Disadvantages
• Busy-waiting consumes processor time
• Starvation is possible when a process leaves a critical section and
more than one process is waiting
• Deadlock
• If a low priority process has the critical region and a higher priority
process needs, the higher priority process will obtain the
processor(CPU) to wait for the critical region
Solution to CS problem: Mutual Exclusion
June 24, 2019
• A Special variable, S called a semaphore if it is
used for signaling
• Semaphore is a variable that has an integer value
• May be initialized to a nonnegative number
• Wait operation decrements the semaphore
value
• Signal operation increments semaphore value
• Two standard operations modify S: wait ( ) and
signal ( )
• Wait and signal operations cannot be interrupted
• Queue is used to hold processes waiting on the
semaphore
• If a process is waiting for a signal, it is suspended
until that signal is sent
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
Semaphore
• Less complicated than the hardware based solutions (using TESTandSET() and
swap ( ) instructions
June 24, 2019
 Two Types of Semaphores
• 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
• Can implement a counting semaphore S as a
binary semaphore
• Provides mutual exclusion
• Modifications to the integer value of the
semaphore in the wait and signal operations must
be executed indivisibly.
Semaphore S;
// initialized to 1
wait (S);
Critical Section
signal (S);
Semaphores as General Synchronization Tool
June 24, 2019
Semaphore Implementation
• Must guarantee that no two processes can execute wait () and
signal () on the same semaphore at the same time
• Thus, implementation becomes the critical section problem where
the wait and signal code are placed in the critical section
• Could now have busy waiting in critical section implementation
• But implementation code of the critical section is short
• Little busy waiting if critical section rarely occupied
• Note that applications may spend lots of time in critical sections
and therefore this is not a good solution
June 24, 2019
Semaphore Implementation with no Busy waiting
• With each semaphore there is an
associated waiting queue. Each entry in a
waiting queue has two data items:
• value (of type integer)
• pointer to next record in the list
• Two operations:
• block – place the process invoking the
operation on the appropriate
waiting queue.
• wakeup – remove one of the processes
in the waiting queue and place it in the
ready queue.
 Implementation of wait:
wait (S){
value--;
if (value < 0) {
add this process to waiting
queue
block(); }
}
 Implementation of signal:
Signal (S){
value++;
if (value <= 0) {
remove a process P from the
waiting queue
wakeup(P); }
}
June 24, 2019
• Semaphores provide a powerful tool for enforcing mutual exclusion and
coordinate processes, But wait (S) and signal (S) are scattered among
several processes. Hence, difficult to understand their effects
• Usage must be correct in all the processes (correct order, correct variables,
no omissions)
• One bad (or malicious) process can fail the entire collection of processes
• Deadlock and Starvation
• Deadlock – two or more processes are waiting indefinitely for an event
that can be caused by only one of the waiting processes.
Let S and Q be two semaphores initialized to 1
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
 
signal(S); signal(Q);
signal(Q) signal(S);
• Starvation – indefinite blocking. A process may never be removed from
the semaphore queue in which it is suspended.
Problems with Semaphores
June 24, 2019
Classical Problems of Synchronization
• Bounded-Buffer Problem
• Readers and Writers Problem
• Dining-Philosophers Problem
• The Sleeping Barber Problem
(Read Chapter 6.6 of the text book)
June 24, 2019
Bounded-Buffer Problem
• N buffers, each can hold one item
• Semaphore mutex initialized to the value 1
• Semaphore full initialized to the value 0
• Semaphore empty initialized to the value N.
 The structure of the producer process
while (true) {
// produce an item
wait (empty);
wait (mutex);
// add the item to the buffer
signal (mutex);
signal (full);
}
 The structure of the consumer process
while (true) {
wait (full);
wait (mutex);
// remove an item from buffer
signal (mutex);
signal (empty);
// consume the removed
item
}
Readers-Writers Problem
• A data set is shared among a number of concurrent processes
• Readers – only read the data set; they do not perform any
updates
• Writers – can both read and write.
Problem – allow multiple readers to read at the same time. Only
one single writer can access the shared data at the same time.
• Shared Data
• Data set
• Semaphore mutex initialized to 1.
• Semaphore wrt initialized to 1.
• Integer readcount initialized to 0.
Readers-Writers Problem (Cont.)
 The structure of a writer process
while (true) {
wait (wrt) ;
// writing is performed
signal (wrt) ;
}
 The structure of a reader process
while (true) {
wait (mutex) ;
readcount ++ ;
if (readcount == 1) wait (wrt) ;
signal (mutex)
// reading is performed
wait (mutex) ;
readcount - - ;
if (readcount == 0) signal (wrt) ;
signal (mutex) ;
}
Dining-Philosophers Problem
• Shared data
• Bowl of rice (data set)
• Semaphore chopstick [5] initialized to 1
Dining-Philosophers Problem (Cont.)
• The structure of Philosopher i:
While (true) {
wait ( chopstick[i] );
wait ( chopStick[ (i + 1) % 5] );
// eat
signal ( chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
}
Problems with Semaphores
• Incorrect use of semaphore operations:
• signal (mutex) …. wait (mutex)
• wait (mutex) … wait (mutex)
• Omitting of wait (mutex) or signal (mutex)
(or both)
Monitors
• A high-level abstraction that provides a convenient and effective
mechanism for process synchronization
• A monitor is a software module consisting of sets of procedures and local
data variables
• The main characteristics of monitors are:
 Local variables of a monitor are only accessible by the monitor’s
procedures (no external procedure can access variables of the
monitor)
 Only one process may be active within the monitor at a time
 A process enters the monitor by invoking one of its procedures
 By only allowing one process to execute at a time, the monitor
provides a mutual exclusion facility
 Thus, a shared data structure can be included as part of a monitor
and will be protected
 If the data in the monitor represents a shared resource, then the
monitor provides a mutual exclusion facility on the resource
June 24, 2019
Monitor (contd.)
monitor monitor-name
{
// shared variable
declarations
procedure P1 (…) { …. }
…
procedure Pn (…) {……}
Initialization code ( ….) { … }
…
}
}
June 24, 2019
Monitors: Condition Variables
• A monitor supports synchronization by the use of condition variables
that are contained within the monitor and accessible only within the
monitor
• Condition variables are a special data type in monitors,
• Conditon x, y;
• The only operations that can be invoked on condition variables are wait()
and signal()
• The operation x.wait () means a process that invokes the operation is
suspended until another process invokes the x.signal() operation
• x.signal () – resumes one of the suspended processes. If there are no
suspended operations, then the operation will have no effect.
• Note that: The wait() and signal() operations in monitor and semaphore
are different. If a process in a monitor signals and no task is waiting on
the condition variable, the signal is lost. However, in semaphores, the
signal operation will always have effect on the state/value of the
semaphore variable.
June 24, 2019
Reading Assignments
 Software Solutions to Synchronization problems
• Peterson’s solution
 Hardware based solutions
• Swap
• Test and Set
• Lock
Ad

Recommended

794985751-Unit-3-Inter-Process-Communication.pptx
794985751-Unit-3-Inter-Process-Communication.pptx
vaishnavbhavna17
 
OS Process synchronization Unit3 synchronization
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
Ch5 process synchronization
Ch5 process synchronization
Welly Dian Astika
 
Operating system 23 process synchronization
Operating system 23 process synchronization
Vaibhav Khanna
 
CH05.pdf
CH05.pdf
ImranKhan880955
 
synchronization in operating system structure
synchronization in operating system structure
gaurav77712
 
Lecture16-17.ppt
Lecture16-17.ppt
ssuserf67e3a
 
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
RajanikanthM4
 
operating system notes about deadlock 3.pptx
operating system notes about deadlock 3.pptx
panditestmail
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Lecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptx
EhteshamulIslam1
 
Process Synchronization
Process Synchronization
Shipra Swati
 
Process Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
Process synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Synchronization in os.pptx
Synchronization in os.pptx
AbdullahBhatti53
 
Process synchronization(deepa)
Process synchronization(deepa)
Nagarajan
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
U3-PPT-1 (1).ppt
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
Ch7
Ch7
Bilal Arshad
 
Ipc feb4
Ipc feb4
yashnand
 
Chapter 5 - Operating Synchronization.pptx
Chapter 5 - Operating Synchronization.pptx
MujtabaVlogs
 
process synchronization topic of operating system
process synchronization topic of operating system
jarmanjeetsinghpb786
 
Mutual exclusion and sync
Mutual exclusion and sync
Dr. C.V. Suresh Babu
 
Os3
Os3
gopal10scs185
 
Interprocess Communication important topic in iOS .pptx
Interprocess Communication important topic in iOS .pptx
thetale999
 
Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptx
Amanuelmergia
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptx
Amanuelmergia
 

More Related Content

Similar to Lecture 5- Process Synchonization_revised.pdf (20)

os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
RajanikanthM4
 
operating system notes about deadlock 3.pptx
operating system notes about deadlock 3.pptx
panditestmail
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Lecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptx
EhteshamulIslam1
 
Process Synchronization
Process Synchronization
Shipra Swati
 
Process Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
Process synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Synchronization in os.pptx
Synchronization in os.pptx
AbdullahBhatti53
 
Process synchronization(deepa)
Process synchronization(deepa)
Nagarajan
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
U3-PPT-1 (1).ppt
U3-PPT-1 (1).ppt
AJAYVISHALRP
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
Ch7
Ch7
Bilal Arshad
 
Ipc feb4
Ipc feb4
yashnand
 
Chapter 5 - Operating Synchronization.pptx
Chapter 5 - Operating Synchronization.pptx
MujtabaVlogs
 
process synchronization topic of operating system
process synchronization topic of operating system
jarmanjeetsinghpb786
 
Mutual exclusion and sync
Mutual exclusion and sync
Dr. C.V. Suresh Babu
 
Os3
Os3
gopal10scs185
 
Interprocess Communication important topic in iOS .pptx
Interprocess Communication important topic in iOS .pptx
thetale999
 
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
os dfgdfgdfgdgdfgdfgdfgdfgdfgdfgdfgdfg df gdf.pptx
RajanikanthM4
 
operating system notes about deadlock 3.pptx
operating system notes about deadlock 3.pptx
panditestmail
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Chapter 5-Process Synchronization (Unit 2).ppt
Chapter 5-Process Synchronization (Unit 2).ppt
ssuser09d6cd1
 
Lecture 9 - Process Synchronization.pptx
Lecture 9 - Process Synchronization.pptx
EhteshamulIslam1
 
Process Synchronization
Process Synchronization
Shipra Swati
 
Process Synchronization -1.ppt
Process Synchronization -1.ppt
jayverma27
 
Process synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Synchronization in os.pptx
Synchronization in os.pptx
AbdullahBhatti53
 
Process synchronization(deepa)
Process synchronization(deepa)
Nagarajan
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
MODULE 3 process synchronizationnnn.pptx
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
Chapter 5 - Operating Synchronization.pptx
Chapter 5 - Operating Synchronization.pptx
MujtabaVlogs
 
process synchronization topic of operating system
process synchronization topic of operating system
jarmanjeetsinghpb786
 
Interprocess Communication important topic in iOS .pptx
Interprocess Communication important topic in iOS .pptx
thetale999
 

More from Amanuelmergia (14)

Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptx
Amanuelmergia
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptx
Amanuelmergia
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptx
Amanuelmergia
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
Amanuelmergia
 
OS course Outilne 2021.doc
OS course Outilne 2021.doc
Amanuelmergia
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdf
Amanuelmergia
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdf
Amanuelmergia
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdf
Amanuelmergia
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptx
Amanuelmergia
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptx
Amanuelmergia
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
Amanuelmergia
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptx
Amanuelmergia
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
Amanuelmergia
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
Amanuelmergia
 
Lecture 4 - Process Scheduling (1).pptx
Lecture 4 - Process Scheduling (1).pptx
Amanuelmergia
 
Operating Systems chap 2_updated2 (1).pptx
Operating Systems chap 2_updated2 (1).pptx
Amanuelmergia
 
Operating Systems chap 2_updated2.pptx
Operating Systems chap 2_updated2.pptx
Amanuelmergia
 
Lecture-7 Main Memroy.pptx
Lecture-7 Main Memroy.pptx
Amanuelmergia
 
OS course Outilne 2021.doc
OS course Outilne 2021.doc
Amanuelmergia
 
Lecture 3- Threads.pdf
Lecture 3- Threads.pdf
Amanuelmergia
 
Lecture 2- Processes.pdf
Lecture 2- Processes.pdf
Amanuelmergia
 
Lecture 1- Introduction to Operating Systems.pdf
Lecture 1- Introduction to Operating Systems.pdf
Amanuelmergia
 
Lecture 6- Deadlocks (1) (1).pptx
Lecture 6- Deadlocks (1) (1).pptx
Amanuelmergia
 
Lecture 4 - Process Scheduling.pptx
Lecture 4 - Process Scheduling.pptx
Amanuelmergia
 
Lecture 5- Process Synchronization (1).pptx
Lecture 5- Process Synchronization (1).pptx
Amanuelmergia
 
Lecture 6- Deadlocks.pptx
Lecture 6- Deadlocks.pptx
Amanuelmergia
 
Lecture 8- Virtual Memory Final.pptx
Lecture 8- Virtual Memory Final.pptx
Amanuelmergia
 
Lecture 3- Threads (1).pptx
Lecture 3- Threads (1).pptx
Amanuelmergia
 
Ad

Recently uploaded (20)

FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Data Validation and System Interoperability
Data Validation and System Interoperability
Safe Software
 
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
Tech-ASan: Two-stage check for Address Sanitizer - Yixuan Cao.pdf
caoyixuan2019
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC and Open Hackathons Monthly Highlights June 2025
OpenACC
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Seminar: Evolving Landscape of Post-Quantum Cryptography.pptx
FIDO Alliance
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Seminar: New Data: Passkey Adoption in the Workforce.pptx
FIDO Alliance
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Reducing Conflicts and Increasing Safety Along the Cycling Networks of East-F...
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Ad

Lecture 5- Process Synchonization_revised.pdf

  • 1. Operating Systems: CSE 3204 ASTU Department of CSE Lecture 5: Process Synchronization Chapter Two Process Management
  • 2. Content  Background of Process Synchronization  The Critical-Section Problem  Classic Problems of Synchronization  Semaphores  Monitors
  • 3. • Concurrent Processes can be • Independent processes • cannot affect or be affected by the execution of another process. • Cooperating processes • can affect or be affected by the execution of another process. • Cooperating processes can either share directly similar address space or share data through files and messages. • Concurrent access to shared data may lead to data inconsistency. • Concurrent execution requires • process communication and process synchronization  Process Synchronization • …mechanisms to ensure the orderly execution of cooperating processes that share a logical address space, so that data consistency is maintained. Background to Process Synchronization June 24, 2019
  • 4. • Concurrent processes may have access to shared data and resources • Concurrent access to shared data may result in data inconsistency • If there is no controlled access to shared data, some processes will obtain an inconsistent view of the shared data  Consider two processes P1 and P2, accessing shared data. while P1 is updating data, it is preempted (because of timeout, for example) so that P2 can run. Then P2 try to read the data, which are partly modified, which will result in data inconsistency • In such cases, the outcome of the action performed by concurrent processes will then depend on the order in which their execution is interleaved • Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Background to Process synchronization June 24, 2019
  • 5. Synchronization in Producer-Consumer Problem • producer process produces information that is consumed by a consumer process. • The previous solution considered the use of a bounded buffer • Producer produce items and puts it into the buffer • Consumer consumes items from the buffer • Producer and Consumer must synchronize. • Suppose that we wanted to provide a solution to the consumer- producer problem that fills all the buffers. • We can do so by having an integer count that keeps track of the number of full buffers. • Initially, count is set to 0. • It is incremented by the producer after it produces a new buffer • It is decremented by the consumer after it consumes a buffer June 24, 2019
  • 6. Producer - Consumer Problem Producer while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; } Consumer while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; /* consume the item innextConsumed } June 24, 2019 • Although both producer and consumer routines are correct when executed separately, they may not properly work when executed concurrently. •To illustrate this, let’s assume the current value of the count variable is 5
  • 7. • count++ could be implemented as (in a typical machine language): register1 = count register1 = register1 + 1 count = register1 • count-- could be implemented as (in a typical machine language): register2 = count register2 = register2 - 1 count = register2 • The concurrent execution of count ++ and count -- is equivalent with the execution of the statements of each operations where arbitrary interleaving of the statements occur. A simple arbitrary interleaving of the operations can be as follows: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4} •At S5, we have arrived at the value of counter=4, if we reversed S4 and S5, we would have arrived at count=6 •We would arrive at the incorrect value of the count variable because we allowed both processes to manipulate the variable concurrently Producer Consumer Problem (cont’d) June 24, 2019
  • 8. Race condition • Race condition is the situation where several processes access and manipulate shared data concurrently • The final value of the shared data depends upon which process finishes last. • The key to preventing trouble here and in many other situations involving shared memory, shared files, and shared everything else is to find some way to prohibit more than one process from reading and writing the shared data at the same time . • To prevent race conditions, concurrent processes must coordinate or be synchronized. June 24, 2019
  • 9. Example: Race condition updating a variable June 24, 2019
  • 10. The Critical-Section Problem • n processes competing to use some shared data • Each process has a code segment, called Critical Section (CS), in which the shared data is accessed • A critical section is a piece of code in which a process or thread accesses a common resource • So each process must first request permission to enter its critical section. The section of code implementing this request is called the Entry Section (ES) • The critical section (CS) might be followed by a Leave/Exit Section (LS) • The remaining code is the Remainder Section (RS) • Problem – ensure that when one process is executing in its CS, no other process is allowed to execute in its CS • When a process executes code that manipulates shared data (or resource), we say that the process is in it’s Critical Section (for that shared data) • The execution of critical sections must be mutually exclusive: at any time, only one process is allowed to execute in its critical section (even with multiple processors) 10 June 24, 2019
  • 11. Critical section to prevent a race condition June 24, 2019  Multiprogramming allows logical parallelism, uses devices efficiently but we lose correctness when there is a race condition.  So we forbid logical parallelism inside critical section so we lose some parallelism but we regain correctness.
  • 12.  So each process must first request permission to enter its critical section.  The section of code implementing this request is called the Entry Section (ES).  The critical section (CS) might be followed by a Leave/Exit Section (LS).  The remaining code is the Remainder Section (RS).  The critical section problem is to design a protocol that the processes can use so that their action will not depend on the order in which their execution is interleaved (possibly on many processors). • General structure of process Pi (other process Pj) do { entry section critical section leave/exit section reminder section } while (1); The Critical-Section Problem(cont…)
  • 13. • A solution to a critical section problem must satisfy the following three requirements: 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. Then only those processes that are not executed in their remainder sections can participate in the decision on which will enter its critical section next. 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 Solution to critical section problem June 24, 2019
  • 14. Advantages • Applicable to any number of processes on either a single processor or multiple processors sharing main memory • It is simple and therefore easy to verify • It can be used to support multiple critical sections Disadvantages • Busy-waiting consumes processor time • Starvation is possible when a process leaves a critical section and more than one process is waiting • Deadlock • If a low priority process has the critical region and a higher priority process needs, the higher priority process will obtain the processor(CPU) to wait for the critical region Solution to CS problem: Mutual Exclusion June 24, 2019
  • 15. • A Special variable, S called a semaphore if it is used for signaling • Semaphore is a variable that has an integer value • May be initialized to a nonnegative number • Wait operation decrements the semaphore value • Signal operation increments semaphore value • Two standard operations modify S: wait ( ) and signal ( ) • Wait and signal operations cannot be interrupted • Queue is used to hold processes waiting on the semaphore • If a process is waiting for a signal, it is suspended until that signal is sent wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; } Semaphore • Less complicated than the hardware based solutions (using TESTandSET() and swap ( ) instructions June 24, 2019
  • 16.  Two Types of Semaphores • 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 • Can implement a counting semaphore S as a binary semaphore • Provides mutual exclusion • Modifications to the integer value of the semaphore in the wait and signal operations must be executed indivisibly. Semaphore S; // initialized to 1 wait (S); Critical Section signal (S); Semaphores as General Synchronization Tool June 24, 2019
  • 17. Semaphore Implementation • Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time • Thus, implementation becomes the critical section problem where the wait and signal code are placed in the critical section • Could now have busy waiting in critical section implementation • But implementation code of the critical section is short • Little busy waiting if critical section rarely occupied • Note that applications may spend lots of time in critical sections and therefore this is not a good solution June 24, 2019
  • 18. Semaphore Implementation with no Busy waiting • With each semaphore there is an associated waiting queue. Each entry in a waiting queue has two data items: • value (of type integer) • pointer to next record in the list • Two operations: • block – place the process invoking the operation on the appropriate waiting queue. • wakeup – remove one of the processes in the waiting queue and place it in the ready queue.  Implementation of wait: wait (S){ value--; if (value < 0) { add this process to waiting queue block(); } }  Implementation of signal: Signal (S){ value++; if (value <= 0) { remove a process P from the waiting queue wakeup(P); } } June 24, 2019
  • 19. • Semaphores provide a powerful tool for enforcing mutual exclusion and coordinate processes, But wait (S) and signal (S) are scattered among several processes. Hence, difficult to understand their effects • Usage must be correct in all the processes (correct order, correct variables, no omissions) • One bad (or malicious) process can fail the entire collection of processes • Deadlock and Starvation • Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. Let S and Q be two semaphores initialized to 1 P0 P1 wait(S); wait(Q); wait(Q); wait(S);   signal(S); signal(Q); signal(Q) signal(S); • Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. Problems with Semaphores June 24, 2019
  • 20. Classical Problems of Synchronization • Bounded-Buffer Problem • Readers and Writers Problem • Dining-Philosophers Problem • The Sleeping Barber Problem (Read Chapter 6.6 of the text book) June 24, 2019
  • 21. Bounded-Buffer Problem • N buffers, each can hold one item • Semaphore mutex initialized to the value 1 • Semaphore full initialized to the value 0 • Semaphore empty initialized to the value N.  The structure of the producer process while (true) { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); }  The structure of the consumer process while (true) { wait (full); wait (mutex); // remove an item from buffer signal (mutex); signal (empty); // consume the removed item }
  • 22. Readers-Writers Problem • A data set is shared among a number of concurrent processes • Readers – only read the data set; they do not perform any updates • Writers – can both read and write. Problem – allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. • Shared Data • Data set • Semaphore mutex initialized to 1. • Semaphore wrt initialized to 1. • Integer readcount initialized to 0.
  • 23. Readers-Writers Problem (Cont.)  The structure of a writer process while (true) { wait (wrt) ; // writing is performed signal (wrt) ; }  The structure of a reader process while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex) // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; }
  • 24. Dining-Philosophers Problem • Shared data • Bowl of rice (data set) • Semaphore chopstick [5] initialized to 1
  • 25. Dining-Philosophers Problem (Cont.) • The structure of Philosopher i: While (true) { wait ( chopstick[i] ); wait ( chopStick[ (i + 1) % 5] ); // eat signal ( chopstick[i] ); signal (chopstick[ (i + 1) % 5] ); // think }
  • 26. Problems with Semaphores • Incorrect use of semaphore operations: • signal (mutex) …. wait (mutex) • wait (mutex) … wait (mutex) • Omitting of wait (mutex) or signal (mutex) (or both)
  • 27. Monitors • A high-level abstraction that provides a convenient and effective mechanism for process synchronization • A monitor is a software module consisting of sets of procedures and local data variables • The main characteristics of monitors are:  Local variables of a monitor are only accessible by the monitor’s procedures (no external procedure can access variables of the monitor)  Only one process may be active within the monitor at a time  A process enters the monitor by invoking one of its procedures  By only allowing one process to execute at a time, the monitor provides a mutual exclusion facility  Thus, a shared data structure can be included as part of a monitor and will be protected  If the data in the monitor represents a shared resource, then the monitor provides a mutual exclusion facility on the resource June 24, 2019
  • 28. Monitor (contd.) monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … } } June 24, 2019
  • 29. Monitors: Condition Variables • A monitor supports synchronization by the use of condition variables that are contained within the monitor and accessible only within the monitor • Condition variables are a special data type in monitors, • Conditon x, y; • The only operations that can be invoked on condition variables are wait() and signal() • The operation x.wait () means a process that invokes the operation is suspended until another process invokes the x.signal() operation • x.signal () – resumes one of the suspended processes. If there are no suspended operations, then the operation will have no effect. • Note that: The wait() and signal() operations in monitor and semaphore are different. If a process in a monitor signals and no task is waiting on the condition variable, the signal is lost. However, in semaphores, the signal operation will always have effect on the state/value of the semaphore variable. June 24, 2019
  • 30. Reading Assignments  Software Solutions to Synchronization problems • Peterson’s solution  Hardware based solutions • Swap • Test and Set • Lock