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

5 process synchronization

  • 1.
    Silberschatz, Galvin andGagne ©2013 Operating System Concepts – 9th Edition Chapter 5: Process Synchronization
  • 2.
    5.2 Silberschatz, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand Gagne ©2013 Operating System Concepts – 9th Edition Critical Section  General structure of process Pi
  • 7.
    5.7 Silberschatz, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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, Galvinand 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 andGagne ©2013 Operating System Concepts – 9th Edition End of Chapter 5