SlideShare a Scribd company logo
University of Liberal Arts Bangladesh 1
OPERATING SYSTEMS
CSE 204
LECTURE 9
PROCESS SYNCHRONIZATION
Chapter 6: Process Synchronization
Operating System Concepts - by Gagne, Silberschatz, and Galvin
Synchronization
īŋŊ Co-operating process – one that can affect / be affected by
other processes
īŋŊ Co-operating processes may either directly share a logical
address space (i.e. code & data), or share data through files
or messages through threads
īŋŊ Concurrent access to shared data can result in inconsistencies
P1 P2 P3
Data
Delete
data
update
data
Read
data
3
Objectives
īŋŊ To introduce the critical-section problem, whose solutions can
be used to ensure the consistency of shared data
īŋŊ To present both software and hardware solutions of the
critical-section problem
4
Background
● Concurrent access to shared data may result in data
inconsistency đŸĄĒ Multiple threads in a single process
īŋŊ Maintaining data consistency requires mechanisms to
ensure the orderly execution of cooperating
processes
5
Background
īŋŊ Disjoint threads use disjoint sets of variables, and do not
use shared resources.
īŋŊ The progress of one thread is independent of all
other threads, to which it is disjoint.
īŋŊ Non-disjoint threads influence each other by using
shared data and/or resources.
īŋŊ Two possibilities:
īŋŊ Competing threads: compete for access to the data
resources
īŋŊ Cooperating threads: e.g. producer / consumer
model
īŋŊ Without synchronization, the effect of non-disjoint
parallel threads influencing each other is not
predictable and not reproducible.
6
Race Conditions
īŋŊ A race condition is where multiple processes/threads
concurrently read and write to a shared memory
location and the result depends on the order of the
execution.
īŋŊ The part of the program, in which race conditions can
occur, is called critical section.
7
Critical Sections
īŋŊ A critical section is a piece of code that accesses a
shared resource (data structure or device) that must not
be concurrently accessed by more than one thread of
execution.
īŋŊ The goal is to provide a mechanism by which only one
instance of a critical section is executing for a particular
shared resource.
īŋŊ Unfortunately, it is often very difficult to detect critical
section bugs
8
Critical Sections
īŋŊ A Critical Section Environment contains:
īŋŊ Entry Section Code requesting entry into the critical
section.
īŋŊ Critical Section Code in which only one process can
execute at any one time.
īŋŊ Exit Section The end of the critical section, releasing or
allowing others in.
īŋŊ Remainder Section Rest of the code AFTER the critical
section.
do {
critical section;
remainder sections;
} while (true);
Entry Section;
Exit Section;
9
Solution to Critical-Section Problem
A solution to the critical-section problem must satisfy the
following requirements:
1. Mutual Exclusion – Only one process can be in its critical
section
e.g. 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
10
Solution to Critical-Section Problem (Cont.)
3. Bounded Waiting - A bound, or limit 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
11
Synchronization Hardware
īŋŊ Hardware can be used to solve the critical section
problem
īŋŊ Many systems provide hardware support for critical
section code
īŋŊ Uniprocessors – could disable interrupts
īŋŊ Currently running code would execute without
preemption
īŋŊ No unexpected modifications would be made to
shared variables
īŋŊ Disabling interrupts in a multi-processor environment is
not feasible
Synchronization Hardware
īŋŊ Instead, we can generally state that any solution to the
critical-section problem requires a simple tool, a LOCK
īŋŊ Race conditions are prevented by requiring that critical
regions be protected by locks
while (true) {
acquire lock
critical section
release lock
reminder
section
}
Synchronization Hardware
īŋŊ Modern machines provide special atomic hardware instructions
īŋŊ Atomic = non-interruptable
īŋŊ Either test memory word and set value (TestAndSet())
īŋŊ Or swap contents of two memory words(Swap())
īŋŊ These instructions allow us either to test & modify the content of a
word, or to swap the contents of two words, atomically
TestAndSet
boolean TestAndSet(boolean *target) {
boolean rv = *target;
*target = true;
return rv;
}
Synchronization Hardware
īŋŊ This instruction is executing atomically
īŋŊ if two TestAndSet instructions are executed
simultaneously (on different CPUs), they will be
executed sequentially
TestAndSet with mutual exclusion
lock=false
do{
while
(TestAndSet(&lock));
// do
nothing
// critical section
lock = false;
// reminder
section
} while(true);
P1 I1
P2 I2
Mutual-exclusion: while P1
is executing, all other
interrupts are disabled
Synchronization Hardware
Swap with mutual exclusion
do{
key = true;
while (key==true)
swap(&lock,
&key);
critical
section
lock = false;
reminder
section
} while(true);
Swap
void swap (boolean *a, boolean *b){
boolean temp = *a;
*a=*b;
*b=temp;
}
Pi L K
F T
swap T F
Pj L K
T T
swap T T
Pj L K
F T
swap T F
Semaphore
īŋŊ Semaphore: a synchronization tool, A flag used to
indicate that a routine cannot proceed if a shared
resource is already in use by another routine.
īŋŊ Semaphore S – integer variable
īŋŊ Two standard operations modify S: wait() and signal()
īŋŊ Semaphore is signaling mechanism – no ownership
īŋŊ Can only be accessed via two indivisible (atomic)
operations
īŋŊ wait (S) {
while S <= 0
; // no-op
S--;
}
īŋŊ signal (S) {
S++;
}
Resource
S = 2
Thread 1
Thread 2
Thread 3
Thread 3: Try to access, but not
allowed, because S <=0 (Thread
1 and Thread 2 occupy S)
īŋŊ Two types:
īŋŊ Counting semaphore – integer value can range over an
unrestricted domain
īŋŊcan allow n processes to access (e.g. database) by
initializing the semaphore to n
īŋŊ Binary semaphore – integer value can range only
between 0 and 1
īŋŊN processes share a semaphore - mutex locks
(provide mutual exclusion)
Semaphore as General Synchronization Tool
Resource
Thread 1
Thread 2
Thread 3
If Thread 2 try to access resource by
accessing mutex object then attempts
to take mutex will return error
Thread 1 accessed resource:
mutex object held by Thread 1:
LOCK(mutex)
Do work
UNLOCK (mutex)
Semaphores Usage
īŋŊ Mutex is an object owned by thread, so
there is a ownership in mutex. Mutex allows
only one thread to access resource
īŋŊ Semaphore is a signaling mechanism. It
allows a number of thread to access to the
resources.
20

More Related Content

PPTX
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
PDF
Ch5 process synchronization
Welly Dian Astika
 
PPTX
synchronization in operating system structure
gaurav77712
 
PDF
CH05.pdf
ImranKhan880955
 
PPT
Ch7
Bilal Arshad
 
PDF
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
PPTX
Operating system 23 process synchronization
Vaibhav Khanna
 
PPTX
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
 
Ch5 process synchronization
Welly Dian Astika
 
synchronization in operating system structure
gaurav77712
 
CH05.pdf
ImranKhan880955
 
OPERATING SYSTEM NOTESS ppt Unit 2.1.pdf
CHETHANKUMAR274045
 
Operating system 23 process synchronization
Vaibhav Khanna
 
MODULE 3 process synchronizationnnn.pptx
senthilkumar969017
 

Similar to Lecture 9 - Process Synchronization.pptx (20)

PPTX
OS 6.pptx
ZainabShahzad9
 
PPT
Mutual exclusion and sync
Dr. C.V. Suresh Babu
 
PDF
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
PDF
OS Process synchronization Unit3 synchronization
subhamchy2005
 
PPT
Section06-Syncopkojiojoijnnjkhuubgfffppt
shubhangimalas1
 
PPT
Process Synchronization -1.ppt
jayverma27
 
PPT
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
PPTX
794985751-Unit-3-Inter-Process-Communication.pptx
vaishnavbhavna17
 
PPTX
Operating system 27 semaphores
Vaibhav Khanna
 
PPTX
Lecture 5 inter process communication
Kumbirai Junior Muzavazi
 
PDF
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
PPTX
process synchronization topic of operating system
jarmanjeetsinghpb786
 
DOCX
Process synchronization
lodhran-hayat
 
PDF
Slides for OS 06-Sync.pdf
GeekyHassan
 
PPTX
Process synchronization
Syed Hassan Ali
 
PPTX
Synchronization in os.pptx
AbdullahBhatti53
 
PPTX
Synchronization
S.M.Mustofa Kauser
 
PPTX
Synchronization hardware
Saeram Butt
 
PDF
Shared Memory
Mustafa Ugur Oduncu
 
PPT
process syn.ppt
Pandiya Rajan
 
OS 6.pptx
ZainabShahzad9
 
Mutual exclusion and sync
Dr. C.V. Suresh Babu
 
Lecture 5- Process Synchonization_revised.pdf
Amanuelmergia
 
OS Process synchronization Unit3 synchronization
subhamchy2005
 
Section06-Syncopkojiojoijnnjkhuubgfffppt
shubhangimalas1
 
Process Synchronization -1.ppt
jayverma27
 
Operating Systems - "Chapter 5 Process Synchronization"
Ra'Fat Al-Msie'deen
 
794985751-Unit-3-Inter-Process-Communication.pptx
vaishnavbhavna17
 
Operating system 27 semaphores
Vaibhav Khanna
 
Lecture 5 inter process communication
Kumbirai Junior Muzavazi
 
Operating Systems - Process Synchronization and Deadlocks
Mukesh Chinta
 
process synchronization topic of operating system
jarmanjeetsinghpb786
 
Process synchronization
lodhran-hayat
 
Slides for OS 06-Sync.pdf
GeekyHassan
 
Process synchronization
Syed Hassan Ali
 
Synchronization in os.pptx
AbdullahBhatti53
 
Synchronization
S.M.Mustofa Kauser
 
Synchronization hardware
Saeram Butt
 
Shared Memory
Mustafa Ugur Oduncu
 
process syn.ppt
Pandiya Rajan
 
Ad

Recently uploaded (20)

PPT
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
PPTX
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PDF
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
DOCX
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PDF
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PPTX
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Understanding the Key Components and Parts of a Drone System.ppt
Siva Reddy
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
sunil mishra pptmmmmmmmmmmmmmmmmmmmmmmmmm
singhamit111
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
STUDY OF NOVEL CHANNEL MATERIALS USING III-V COMPOUNDS WITH VARIOUS GATE DIEL...
ijoejnl
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
SAR - EEEfdfdsdasdsdasdasdasdasdasdasdasda.docx
Kanimozhi676285
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Zero Carbon Building Performance standard
BassemOsman1
 
Machine Learning All topics Covers In This Single Slides
AmritTiwari19
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Construction of a Thermal Vacuum Chamber for Environment Test of Triple CubeS...
2208441
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
IoT_Smart_Agriculture_Presentations.pptx
poojakumari696707
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Ad

Lecture 9 - Process Synchronization.pptx

  • 1. University of Liberal Arts Bangladesh 1 OPERATING SYSTEMS CSE 204 LECTURE 9 PROCESS SYNCHRONIZATION
  • 2. Chapter 6: Process Synchronization Operating System Concepts - by Gagne, Silberschatz, and Galvin
  • 3. Synchronization īŋŊ Co-operating process – one that can affect / be affected by other processes īŋŊ Co-operating processes may either directly share a logical address space (i.e. code & data), or share data through files or messages through threads īŋŊ Concurrent access to shared data can result in inconsistencies P1 P2 P3 Data Delete data update data Read data 3
  • 4. Objectives īŋŊ To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data īŋŊ To present both software and hardware solutions of the critical-section problem 4
  • 5. Background ● Concurrent access to shared data may result in data inconsistency đŸĄĒ Multiple threads in a single process īŋŊ Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes 5
  • 6. Background īŋŊ Disjoint threads use disjoint sets of variables, and do not use shared resources. īŋŊ The progress of one thread is independent of all other threads, to which it is disjoint. īŋŊ Non-disjoint threads influence each other by using shared data and/or resources. īŋŊ Two possibilities: īŋŊ Competing threads: compete for access to the data resources īŋŊ Cooperating threads: e.g. producer / consumer model īŋŊ Without synchronization, the effect of non-disjoint parallel threads influencing each other is not predictable and not reproducible. 6
  • 7. Race Conditions īŋŊ A race condition is where multiple processes/threads concurrently read and write to a shared memory location and the result depends on the order of the execution. īŋŊ The part of the program, in which race conditions can occur, is called critical section. 7
  • 8. Critical Sections īŋŊ A critical section is a piece of code that accesses a shared resource (data structure or device) that must not be concurrently accessed by more than one thread of execution. īŋŊ The goal is to provide a mechanism by which only one instance of a critical section is executing for a particular shared resource. īŋŊ Unfortunately, it is often very difficult to detect critical section bugs 8
  • 9. Critical Sections īŋŊ A Critical Section Environment contains: īŋŊ Entry Section Code requesting entry into the critical section. īŋŊ Critical Section Code in which only one process can execute at any one time. īŋŊ Exit Section The end of the critical section, releasing or allowing others in. īŋŊ Remainder Section Rest of the code AFTER the critical section. do { critical section; remainder sections; } while (true); Entry Section; Exit Section; 9
  • 10. Solution to Critical-Section Problem A solution to the critical-section problem must satisfy the following requirements: 1. Mutual Exclusion – Only one process can be in its critical section e.g. 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 10
  • 11. Solution to Critical-Section Problem (Cont.) 3. Bounded Waiting - A bound, or limit 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 11
  • 12. Synchronization Hardware īŋŊ Hardware can be used to solve the critical section problem īŋŊ Many systems provide hardware support for critical section code īŋŊ Uniprocessors – could disable interrupts īŋŊ Currently running code would execute without preemption īŋŊ No unexpected modifications would be made to shared variables īŋŊ Disabling interrupts in a multi-processor environment is not feasible
  • 13. Synchronization Hardware īŋŊ Instead, we can generally state that any solution to the critical-section problem requires a simple tool, a LOCK īŋŊ Race conditions are prevented by requiring that critical regions be protected by locks while (true) { acquire lock critical section release lock reminder section }
  • 14. Synchronization Hardware īŋŊ Modern machines provide special atomic hardware instructions īŋŊ Atomic = non-interruptable īŋŊ Either test memory word and set value (TestAndSet()) īŋŊ Or swap contents of two memory words(Swap()) īŋŊ These instructions allow us either to test & modify the content of a word, or to swap the contents of two words, atomically TestAndSet boolean TestAndSet(boolean *target) { boolean rv = *target; *target = true; return rv; }
  • 15. Synchronization Hardware īŋŊ This instruction is executing atomically īŋŊ if two TestAndSet instructions are executed simultaneously (on different CPUs), they will be executed sequentially TestAndSet with mutual exclusion lock=false do{ while (TestAndSet(&lock)); // do nothing // critical section lock = false; // reminder section } while(true); P1 I1 P2 I2 Mutual-exclusion: while P1 is executing, all other interrupts are disabled
  • 16. Synchronization Hardware Swap with mutual exclusion do{ key = true; while (key==true) swap(&lock, &key); critical section lock = false; reminder section } while(true); Swap void swap (boolean *a, boolean *b){ boolean temp = *a; *a=*b; *b=temp; } Pi L K F T swap T F Pj L K T T swap T T Pj L K F T swap T F
  • 17. Semaphore īŋŊ Semaphore: a synchronization tool, A flag used to indicate that a routine cannot proceed if a shared resource is already in use by another routine. īŋŊ Semaphore S – integer variable īŋŊ Two standard operations modify S: wait() and signal() īŋŊ Semaphore is signaling mechanism – no ownership īŋŊ Can only be accessed via two indivisible (atomic) operations īŋŊ wait (S) { while S <= 0 ; // no-op S--; } īŋŊ signal (S) { S++; } Resource S = 2 Thread 1 Thread 2 Thread 3 Thread 3: Try to access, but not allowed, because S <=0 (Thread 1 and Thread 2 occupy S)
  • 18. īŋŊ Two types: īŋŊ Counting semaphore – integer value can range over an unrestricted domain īŋŊcan allow n processes to access (e.g. database) by initializing the semaphore to n īŋŊ Binary semaphore – integer value can range only between 0 and 1 īŋŊN processes share a semaphore - mutex locks (provide mutual exclusion) Semaphore as General Synchronization Tool Resource Thread 1 Thread 2 Thread 3 If Thread 2 try to access resource by accessing mutex object then attempts to take mutex will return error Thread 1 accessed resource: mutex object held by Thread 1: LOCK(mutex) Do work UNLOCK (mutex)
  • 19. Semaphores Usage īŋŊ Mutex is an object owned by thread, so there is a ownership in mutex. Mutex allows only one thread to access resource īŋŊ Semaphore is a signaling mechanism. It allows a number of thread to access to the resources.
  • 20. 20