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

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
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
 
(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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
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
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Recently uploaded (20)

Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
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
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
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
 
(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
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
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 )
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

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