SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 5: Process
Synchronization
5.2 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Chapter 5: Process Synchronization
 Background
 The Critical-Section Problem
 Peterson’s Solution
 Synchronization Hardware
 Mutex Locks
 Semaphores
 Classic Problems of Synchronization
 Monitors
 Synchronization Examples
 Alternative Approaches
5.3 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Objectives
 To present the concept of process synchronization.
 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
 To examine several classical process-synchronization
problems
 To explore several tools that are used to solve process
synchronization problems
5.4 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Background
 Processes can execute concurrently
 May be interrupted at any time, partially completing
execution
 Concurrent access to shared data may result in data
inconsistency
 Maintaining data consistency requires mechanisms to ensure
the orderly execution of cooperating processes
 Illustration of the problem:
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 counter that keeps track of the
number of full buffers. Initially, counter is set to 0. It is
incremented by the producer after it produces a new buffer
and is decremented by the consumer after it consumes a
buffer.
5.5 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Critical Section Problem
 The critical section is a code segment where the shared
variables can be accessed. An atomic action is required in
a critical section i.e. only one process can execute in
its critical section at a time.
 Consider system of n processes {p0, p1, … pn-1}
 Each process has critical section segment of code
 Process may be changing common variables, updating
table, writing file, etc
 When one process in critical section, no other may be in its
critical section
 Critical section problem is to design protocol to solve this
 Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section,
then remainder section
5.6 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Critical Section
 General structure of process Pi
5.7 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Solution to Critical-Section Problem
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
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
5.8 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or non-
preemptive
 Preemptive – allows preemption of process when running
in kernel mode
 Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
Essentially free of race conditions in kernel mode
5.9 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Synchronization Hardware
 Many systems provide hardware support for implementing the
critical section code.
 All solutions below based on idea of locking
 Protecting critical regions via locks
 Uniprocessors – could disable interrupts
 Currently running code would execute without preemption
 Generally too inefficient on multiprocessor systems
 Operating systems using this not broadly scalable
 Modern machines provide special atomic hardware instructions
 Atomic = non-interruptible
5.10 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Solution to Critical-section Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
5.11 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Mutex Locks
 Previous solutions are complicated and generally inaccessible
to application programmers
 OS designers build software tools to solve critical section
problem
 Simplest is mutex lock
 Protect a critical section by first acquire() a lock then
release() the lock
 Boolean variable indicating if lock is available or not
 Calls to acquire() and release() must be atomic
 Usually implemented via hardware atomic instructions
 But this solution requires busy waiting
 This lock therefore called a spinlock
5.12 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Semaphore
 Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for process
synchronization.
 The definitions of wait and signal are as follows −
 Wait The wait operation decrements the value of its argument S, if it is positive. If
S is negative or zero, then no operation is performed.
 wait(S)
 {
 while (S<=0);
 S--;
 }
 Signal The signal operation increments the value of its argument S.
 signal(S)
 {
 S++;
 }
5.13 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Types of Semaphores
 There are two main types of semaphores i.e. counting semaphores and
binary semaphores. Details about these are given as follows −
 Counting Semaphores These are integer value semaphores and have
an unrestricted value domain. These semaphores are used to coordinate
the resource access, where the semaphore count is the number of
available resources. If the resources are added, semaphore count
automatically incremented and if the resources are removed, the count is
decremented.
 Binary Semaphores The binary semaphores are like counting
semaphores but their value is restricted to 0 and 1. The wait operation
only works when the semaphore is 1 and the signal operation succeeds
when semaphore is 0. It is sometimes easier to implement binary
semaphores than counting semaphores.
5.14 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Semaphore Implementation
 Must guarantee that no two processes can execute the wait()
and signal() on the same semaphore at the same time
 Thus, the 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 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
5.15 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
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
 Priority Inversion – Scheduling problem when lower-priority process
holds a lock needed by higher-priority process
 Solved via priority-inheritance protocol
5.16 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Classical Problems of Synchronization
 Semaphore can be used in other synchronization problems besides
Mutual Exclusion.
 Below are some of the classical problem depicting flaws of process
synchronaization in systems where cooperating processes are
present.
 We will discuss the following three problems:
 Bounded Buffer (Producer-Consumer) Problem
 Dining Philosophers Problem
 The Readers Writers Problem
5.17 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Bounded-Buffer Problem
 This problem is generalised in terms of the Producer
Consumer problem, where a finite buffer pool is used to
exchange messages between producer and consumer
processes.
 Because the buffer pool has a maximum size, this problem
is often called the Bounded buffer problem.
 Solution to this problem is, creating two counting
semaphores "full" and "empty" to keep track of the current
number of full and empty buffers respectively.
5.18 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
Dining Philosophers Problem
 The dining philosopher's problem involves the allocation of limited resources
to a group of processes in a deadlock-free and starvation-free manner.
5.19 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
The Readers Writers Problem
 In this problem there are some processes(called readers) that only read the
shared data, and never change it, and there are other
processes(called writers) who may change the data in addition to reading,
or instead of reading it.
Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition
End of Chapter 5

Weitere ähnliche Inhalte

Was ist angesagt?

Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
Wayne Jones Jnr
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
tech2click
 

Was ist angesagt? (20)

Ch7 Process Synchronization galvin
Ch7 Process Synchronization galvinCh7 Process Synchronization galvin
Ch7 Process Synchronization galvin
 
Chapter 3: Processes
Chapter 3: ProcessesChapter 3: Processes
Chapter 3: Processes
 
Memory Management in OS
Memory Management in OSMemory Management in OS
Memory Management in OS
 
Chapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating SystemChapter 1: Introduction to Operating System
Chapter 1: Introduction to Operating System
 
Operating System
Operating SystemOperating System
Operating System
 
Chapter 7
Chapter 7Chapter 7
Chapter 7
 
Chapter 6 - Process Synchronization
Chapter 6 - Process SynchronizationChapter 6 - Process Synchronization
Chapter 6 - Process Synchronization
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Operating System Chapter 1
Operating System Chapter 1Operating System Chapter 1
Operating System Chapter 1
 
Chapter 2: Operating System Structures
Chapter 2: Operating System StructuresChapter 2: Operating System Structures
Chapter 2: Operating System Structures
 
Operating Systems: Process Scheduling
Operating Systems: Process SchedulingOperating Systems: Process Scheduling
Operating Systems: Process Scheduling
 
Operating System 3
Operating System 3Operating System 3
Operating System 3
 
Operating System-Ch8 memory management
Operating System-Ch8 memory managementOperating System-Ch8 memory management
Operating System-Ch8 memory management
 
Chapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocksChapter 8 Operating Systems silberschatz : deadlocks
Chapter 8 Operating Systems silberschatz : deadlocks
 
7 Deadlocks
7 Deadlocks7 Deadlocks
7 Deadlocks
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Ch1-Operating System Concepts
Ch1-Operating System ConceptsCh1-Operating System Concepts
Ch1-Operating System Concepts
 
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
 
OS - Process Concepts
OS - Process ConceptsOS - Process Concepts
OS - Process Concepts
 
Process scheduling : operating system ( Btech cse )
Process scheduling : operating system ( Btech cse )Process scheduling : operating system ( Btech cse )
Process scheduling : operating system ( Btech cse )
 

Ähnlich wie 5 process synchronization

Process Synchronisation Operating System
Process Synchronisation Operating SystemProcess Synchronisation Operating System
Process Synchronisation Operating System
DRAnithaSofiaLizCSES
 

Ähnlich wie 5 process synchronization (20)

ch5 [Autosaved].ppt
ch5 [Autosaved].pptch5 [Autosaved].ppt
ch5 [Autosaved].ppt
 
Operating System-Process Synchronization
Operating System-Process SynchronizationOperating System-Process Synchronization
Operating System-Process Synchronization
 
ch05.ppt
ch05.pptch05.ppt
ch05.ppt
 
Chapter 6
Chapter 6Chapter 6
Chapter 6
 
process synchronisation operating system
process synchronisation operating systemprocess synchronisation operating system
process synchronisation operating system
 
Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )Process synchonization : operating system ( Btech cse )
Process synchonization : operating system ( Btech cse )
 
chapter4.pptx
chapter4.pptxchapter4.pptx
chapter4.pptx
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
ch5.ppt
ch5.pptch5.ppt
ch5.ppt
 
ch5.ppt operating system
ch5.ppt operating systemch5.ppt operating system
ch5.ppt operating system
 
Process Synchronization
Process SynchronizationProcess Synchronization
Process Synchronization
 
chapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio pptchapter5 processes of synchronizatio ppt
chapter5 processes of synchronizatio ppt
 
ch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdfch5-Process_Synchronization.pdf
ch5-Process_Synchronization.pdf
 
تعليم البرمجه.pdf
تعليم البرمجه.pdfتعليم البرمجه.pdf
تعليم البرمجه.pdf
 
ch6.ppt
ch6.pptch6.ppt
ch6.ppt
 
Process Synchronisation Operating System
Process Synchronisation Operating SystemProcess Synchronisation Operating System
Process Synchronisation Operating System
 
05-Process-Synchronization.pdf
05-Process-Synchronization.pdf05-Process-Synchronization.pdf
05-Process-Synchronization.pdf
 
ch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu studentch5.ppt Operating System Ppt for ptu student
ch5.ppt Operating System Ppt for ptu student
 
ch6-1.ppt
ch6-1.pptch6-1.ppt
ch6-1.ppt
 
PS.ppt
PS.pptPS.ppt
PS.ppt
 

Mehr von BaliThorat1 (20)

Lec15 sfm
Lec15 sfmLec15 sfm
Lec15 sfm
 
Lec14 multiview stereo
Lec14 multiview stereoLec14 multiview stereo
Lec14 multiview stereo
 
Lec13 stereo converted
Lec13 stereo convertedLec13 stereo converted
Lec13 stereo converted
 
Lec12 epipolar
Lec12 epipolarLec12 epipolar
Lec12 epipolar
 
Lec11 single view-converted
Lec11 single view-convertedLec11 single view-converted
Lec11 single view-converted
 
Lec10 alignment
Lec10 alignmentLec10 alignment
Lec10 alignment
 
Lec09 hough
Lec09 houghLec09 hough
Lec09 hough
 
Lec08 fitting
Lec08 fittingLec08 fitting
Lec08 fitting
 
8 operating system concept
8 operating system concept8 operating system concept
8 operating system concept
 
7 processor
7 processor7 processor
7 processor
 
6 input output devices
6 input output devices6 input output devices
6 input output devices
 
2 windows operating system
2 windows operating system2 windows operating system
2 windows operating system
 
5 computer memory
5 computer memory5 computer memory
5 computer memory
 
4 computer languages
4 computer languages4 computer languages
4 computer languages
 
1 fundamentals of computer
1 fundamentals of computer1 fundamentals of computer
1 fundamentals of computer
 
1 fundamentals of computer system
1 fundamentals of computer system1 fundamentals of computer system
1 fundamentals of computer system
 
Computer generation and classification
Computer generation and classificationComputer generation and classification
Computer generation and classification
 
Algorithm and flowchart
Algorithm and flowchartAlgorithm and flowchart
Algorithm and flowchart
 
6 cpu scheduling
6 cpu scheduling6 cpu scheduling
6 cpu scheduling
 
4 threads
4 threads4 threads
4 threads
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 

5 process synchronization

  • 1. Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Chapter 5: Process Synchronization
  • 2. 5.2 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Chapter 5: Process Synchronization  Background  The Critical-Section Problem  Peterson’s Solution  Synchronization Hardware  Mutex Locks  Semaphores  Classic Problems of Synchronization  Monitors  Synchronization Examples  Alternative Approaches
  • 3. 5.3 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Objectives  To present the concept of process synchronization.  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  To examine several classical process-synchronization problems  To explore several tools that are used to solve process synchronization problems
  • 4. 5.4 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Background  Processes can execute concurrently  May be interrupted at any time, partially completing execution  Concurrent access to shared data may result in data inconsistency  Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes  Illustration of the problem: 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 counter that keeps track of the number of full buffers. Initially, counter is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
  • 5. 5.5 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Critical Section Problem  The critical section is a code segment where the shared variables can be accessed. An atomic action is required in a critical section i.e. only one process can execute in its critical section at a time.  Consider system of n processes {p0, p1, … pn-1}  Each process has critical section segment of code  Process may be changing common variables, updating table, writing file, etc  When one process in critical section, no other may be in its critical section  Critical section problem is to design protocol to solve this  Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section
  • 6. 5.6 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Critical Section  General structure of process Pi
  • 7. 5.7 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Solution to Critical-Section Problem 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 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
  • 8. 5.8 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Critical-Section Handling in OS Two approaches depending on if kernel is preemptive or non- preemptive  Preemptive – allows preemption of process when running in kernel mode  Non-preemptive – runs until exits kernel mode, blocks, or voluntarily yields CPU Essentially free of race conditions in kernel mode
  • 9. 5.9 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Synchronization Hardware  Many systems provide hardware support for implementing the critical section code.  All solutions below based on idea of locking  Protecting critical regions via locks  Uniprocessors – could disable interrupts  Currently running code would execute without preemption  Generally too inefficient on multiprocessor systems  Operating systems using this not broadly scalable  Modern machines provide special atomic hardware instructions  Atomic = non-interruptible
  • 10. 5.10 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Solution to Critical-section Problem Using Locks do { acquire lock critical section release lock remainder section } while (TRUE);
  • 11. 5.11 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Mutex Locks  Previous solutions are complicated and generally inaccessible to application programmers  OS designers build software tools to solve critical section problem  Simplest is mutex lock  Protect a critical section by first acquire() a lock then release() the lock  Boolean variable indicating if lock is available or not  Calls to acquire() and release() must be atomic  Usually implemented via hardware atomic instructions  But this solution requires busy waiting  This lock therefore called a spinlock
  • 12. 5.12 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Semaphore  Semaphores are integer variables that are used to solve the critical section problem by using two atomic operations, wait and signal that are used for process synchronization.  The definitions of wait and signal are as follows −  Wait The wait operation decrements the value of its argument S, if it is positive. If S is negative or zero, then no operation is performed.  wait(S)  {  while (S<=0);  S--;  }  Signal The signal operation increments the value of its argument S.  signal(S)  {  S++;  }
  • 13. 5.13 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Types of Semaphores  There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details about these are given as follows −  Counting Semaphores These are integer value semaphores and have an unrestricted value domain. These semaphores are used to coordinate the resource access, where the semaphore count is the number of available resources. If the resources are added, semaphore count automatically incremented and if the resources are removed, the count is decremented.  Binary Semaphores The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The wait operation only works when the semaphore is 1 and the signal operation succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores than counting semaphores.
  • 14. 5.14 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Semaphore Implementation  Must guarantee that no two processes can execute the wait() and signal() on the same semaphore at the same time  Thus, the 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 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
  • 15. 5.15 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition 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  Priority Inversion – Scheduling problem when lower-priority process holds a lock needed by higher-priority process  Solved via priority-inheritance protocol
  • 16. 5.16 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Classical Problems of Synchronization  Semaphore can be used in other synchronization problems besides Mutual Exclusion.  Below are some of the classical problem depicting flaws of process synchronaization in systems where cooperating processes are present.  We will discuss the following three problems:  Bounded Buffer (Producer-Consumer) Problem  Dining Philosophers Problem  The Readers Writers Problem
  • 17. 5.17 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Bounded-Buffer Problem  This problem is generalised in terms of the Producer Consumer problem, where a finite buffer pool is used to exchange messages between producer and consumer processes.  Because the buffer pool has a maximum size, this problem is often called the Bounded buffer problem.  Solution to this problem is, creating two counting semaphores "full" and "empty" to keep track of the current number of full and empty buffers respectively.
  • 18. 5.18 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition Dining Philosophers Problem  The dining philosopher's problem involves the allocation of limited resources to a group of processes in a deadlock-free and starvation-free manner.
  • 19. 5.19 Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition The Readers Writers Problem  In this problem there are some processes(called readers) that only read the shared data, and never change it, and there are other processes(called writers) who may change the data in addition to reading, or instead of reading it.
  • 20. Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9th Edition End of Chapter 5