SlideShare a Scribd company logo
1 of 20
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

Similar to Lecture 9 - Process Synchronization.pptx

Processscheduling 161001112521
Processscheduling 161001112521Processscheduling 161001112521
Processscheduling 161001112521marangburu42
 
critical section problem.pptx
critical section problem.pptxcritical section problem.pptx
critical section problem.pptxvtu19163
 
Notes about concurrent and distributed systems & x86 virtualization
Notes about concurrent and distributed systems & x86 virtualizationNotes about concurrent and distributed systems & x86 virtualization
Notes about concurrent and distributed systems & x86 virtualizationAlessio Villardita
 
Processes, Threads and Scheduler
Processes, Threads and SchedulerProcesses, Threads and Scheduler
Processes, Threads and SchedulerMunazza-Mah-Jabeen
 
Operating System- INTERPROCESS COMMUNICATION.docx
Operating System- INTERPROCESS COMMUNICATION.docxOperating System- INTERPROCESS COMMUNICATION.docx
Operating System- INTERPROCESS COMMUNICATION.docxminaltmv
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronizationlodhran-hayat
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.pptaminnezarat
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfBhanuCharan9
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.MOHIT DADU
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptKadri20
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in androidRakesh Jha
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algosAkhil Sharma
 

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

Processscheduling 161001112521
Processscheduling 161001112521Processscheduling 161001112521
Processscheduling 161001112521
 
critical section problem.pptx
critical section problem.pptxcritical section problem.pptx
critical section problem.pptx
 
Notes about concurrent and distributed systems & x86 virtualization
Notes about concurrent and distributed systems & x86 virtualizationNotes about concurrent and distributed systems & x86 virtualization
Notes about concurrent and distributed systems & x86 virtualization
 
Processes, Threads and Scheduler
Processes, Threads and SchedulerProcesses, Threads and Scheduler
Processes, Threads and Scheduler
 
Operating System- INTERPROCESS COMMUNICATION.docx
Operating System- INTERPROCESS COMMUNICATION.docxOperating System- INTERPROCESS COMMUNICATION.docx
Operating System- INTERPROCESS COMMUNICATION.docx
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Chapter 3 chapter reading task
Chapter 3 chapter reading taskChapter 3 chapter reading task
Chapter 3 chapter reading task
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
slides8 SharedMemory.ppt
slides8 SharedMemory.pptslides8 SharedMemory.ppt
slides8 SharedMemory.ppt
 
criticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdfcriticalsectionproblem-160905215747.pdf
criticalsectionproblem-160905215747.pdf
 
Critical section problem in operating system.
Critical section problem in operating system.Critical section problem in operating system.
Critical section problem in operating system.
 
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.pptBIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
BIL406-Chapter-9-Synchronization and Communication in MIMD Systems.ppt
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Mutual exclusion and sync
Mutual exclusion and syncMutual exclusion and sync
Mutual exclusion and sync
 
CH05.pdf
CH05.pdfCH05.pdf
CH05.pdf
 
dos mutual exclusion algos
dos mutual exclusion algosdos mutual exclusion algos
dos mutual exclusion algos
 
OS UNIT3.pptx
OS UNIT3.pptxOS UNIT3.pptx
OS UNIT3.pptx
 
Operating System lab
Operating System labOperating System lab
Operating System lab
 
Operating Systems
Operating SystemsOperating Systems
Operating Systems
 

Recently uploaded

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 

Recently uploaded (20)

Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 

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