chapter 6 process synchronization copyright © 2008
TRANSCRIPT
Chapter 6
Process SynchronizationCopyright © 2008
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.2Operating Systems, by Dhananjay Dhamdhere 2
Introduction
• What is Process Synchronization?• Race Conditions• Critical Sections• Control Synchronization and Indivisible Operations• Synchronization Approaches• Structure of Concurrent Systems
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.3Operating Systems, by Dhananjay Dhamdhere 3
Introduction (continued)
• Classic Process Synchronization Problems• Algorithmic Approach to Implementing Critical Sections• Semaphores• Monitors• Case Studies of Process Synchronization
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.4Operating Systems, by Dhananjay Dhamdhere 4
What is Process Synchronization?
• The term process is a generic term for both a process and a thread
• Processes that do not interact are independent processes
• Process synchronization is a generic term for the techniques used to delay and resume processes to implement process interactions
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.5Operating Systems, by Dhananjay Dhamdhere 5
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.6Operating Systems, by Dhananjay Dhamdhere 6
Race Conditions
• Uncoordinated accesses to shared data may affect consistency of data
• Consider processes Pi and Pj that update the value of ds through operations ai and aj , respectively:
Operation ai : ds := ds + 10; Let fi(ds) represent its result
Operation aj : ds := ds + 5; Let fj(ds) represent its result
– What situation could arise if they execute concurrently?
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.7Operating Systems, by Dhananjay Dhamdhere 7
An Example of Race Condition
Race conditionsin cases 2 and 3
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.8Operating Systems, by Dhananjay Dhamdhere 8
Race Conditions (continued)
• Race conditions in Figure 6.2 are prevented by ensuring that operations ai and aj of Definition 6.2 do not execute concurrently– This is called mutual exclusion
• Data access synchronization is coordination of processes to implement mutual exclusion over shared data
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.9Operating Systems, by Dhananjay Dhamdhere 9
Critical Sections
• Mutual exclusion is implemented by using critical sections (CS) of code
• Using CSs causes delays in operation of processes– Process must not execute for too long inside CS
– Process must not make system calls while in the CS that might put it in blocked state
– Kernel must not preempt a process that is engaged in executing a CS
• We use a dashed box in the code to mark a CS
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.10Operating Systems, by Dhananjay Dhamdhere 10
Critical Sections (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.11Operating Systems, by Dhananjay Dhamdhere 11
Properties of a Critical Section Implementation
• The progress and bounded wait properties together prevent starvation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.12Operating Systems, by Dhananjay Dhamdhere 12
Control Synchronization and Indivisible Operations
• Interacting processes need to coordinate their execution with respect to one another, to perform their actions in a desired order– Requirement met through control synchronization
– Signaling is a general technique of control synchronization
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.13Operating Systems, by Dhananjay Dhamdhere 13
Control Synchronization and Indivisible Operations (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.14Operating Systems, by Dhananjay Dhamdhere 14
Control Synchronization and Indivisible Operations (continued)
• Naive signaling in previous example does not work– Pi may face indefinite blocking in some situations
• Use indivisible or atomic operations instead
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.15Operating Systems, by Dhananjay Dhamdhere 15
Control Synchronization and Indivisible Operations (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.16Operating Systems, by Dhananjay Dhamdhere 16
Synchronization Approaches
• Looping versus Blocking• Hardware Support for Process Synchronization• Algorithmic Approaches, Synchronization Primitives,
and Concurrent Programming Constructs
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.17Operating Systems, by Dhananjay Dhamdhere 17
Looping versus Blocking
• Busy wait:
• A busy wait has many consequences– Cannot provide the bounded wait property– System performance degradation due to looping– Deadlock– Priority inversion
• Typically addressed through the priority inheritance protocol
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.18Operating Systems, by Dhananjay Dhamdhere 18
Looping versus Blocking (continued)
• To avoid busy waits, a process waiting for entry to a CS is put in blocked state– Changed to ready only when it can enter the CS
• Process decides to loop or block– Decision is subject to race conditions. Avoided through
• Algorithmic approach• Use of computer hardware features
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.19Operating Systems, by Dhananjay Dhamdhere 19
Hardware Support for Process Synchronization
• Indivisible instructions– Avoid race conditions on memory locations
• Used with a lock variable to implement CS and indivisible operations
– entry_test performed with indivisible instruction• Test-and-set (TS) instruction• Swap instruction
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.20Operating Systems, by Dhananjay Dhamdhere 20
Hardware Support for Process Synchronization (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.21Operating Systems, by Dhananjay Dhamdhere 21
Algorithmic Approaches, Synchronization Primitives, and
Concurrent Programming Constructs• Algorithmic Approaches
– For implementing mutual exclusion
– Independent of hardware or software platform• Busy waiting for synchronization
• Synchronization Primitives– Implemented using indivisible instructions
– E.g., wait and signal of semaphores• Problem: can be used haphazardly
• Concurrent Programming Constructs– Monitors
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.22Operating Systems, by Dhananjay Dhamdhere 22
Structure of Concurrent Systems
• Three key components:– Shared data
• Application data used and manipulated by processes • Synchronization data
– Operations on shared data• Convenient unit of code which accesses and manipulates
shared data– A synchronization operation is on synchronization data
– Interacting processes
• A snapshot of a concurrent system is a view of the system at a specific time instant
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.23Operating Systems, by Dhananjay Dhamdhere 23
Structure of Concurrent Systems (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.24Operating Systems, by Dhananjay Dhamdhere 24
Example: Snapshots of a Concurrent System
(a) Pi performs operation check_aj(b) Pi is blocked; Pj performs operation post_aj(c) post_aj activates Pi; Pj exits post_aj
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.25Operating Systems, by Dhananjay Dhamdhere 25
Classic Process Synchronization Problems
• A solution to a process synchronization problem should meet three important criteria:– Correctness
– Maximum concurrency
– No busy waits
• Some classic problems:– Producers-Consumers with Bounded Buffers
– Readers and Writers
– Dining Philosophers
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.26Operating Systems, by Dhananjay Dhamdhere 26
Producers-Consumers with Bounded Buffers
• A solution must satisfy the following:1. A producer must not overwrite a full buffer
2. A consumer must not consume an empty buffer
3. Producers and consumers must access buffers in a mutually exclusive manner
4. (Optional) Information must be consumed in the same order in which it is put into the buffers
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.27Operating Systems, by Dhananjay Dhamdhere 27
Producers-Consumers with Bounded Buffers (continued)
• Suffers from two problems:– Poor concurrency and busy waits
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.28Operating Systems, by Dhananjay Dhamdhere 28
Producers-Consumers with Bounded Buffers (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.29Operating Systems, by Dhananjay Dhamdhere 29
Producers-Consumers with Bounded Buffers (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.30Operating Systems, by Dhananjay Dhamdhere 30
Readers and Writers
• A solution must satisfy the following:1. Many readers can perform reading concurrently
2. Reading is prohibited while a writer is writing
3. Only one writer can perform writing at any time
4. (optional) A reader has a nonpreemptive priority over writers─ Called readers preferred readers–writers system
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.31Operating Systems, by Dhananjay Dhamdhere 31
Readers and Writers (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.32Operating Systems, by Dhananjay Dhamdhere 32
Dining Philosophers
• Design processes to represent the philosophers so that each philosopher can eat when hungry and none dies of hunger– Solution shouldn’t suffer from deadlocks or livelocks
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.33Operating Systems, by Dhananjay Dhamdhere 33
Dining Philosophers (continued)
• Prone to deadlocks and race conditions, unless:– If right fork is unavailable, release left fork, retry later
• It suffers from livelocks
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.34Operating Systems, by Dhananjay Dhamdhere 34
Dining Philosophers (continued)
• Problem: loop causes a busy wait condition
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.35Operating Systems, by Dhananjay Dhamdhere 35
Algorithmic Approach to Implementing Critical Sections
• Two-Process Algorithms• n-Process Algorithms
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.36Operating Systems, by Dhananjay Dhamdhere 36
Two-Process Algorithms
• Violates the progress condition
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.37Operating Systems, by Dhananjay Dhamdhere 37
Two-Process Algorithms (continued)
• Violates mutual exclusion condition• Can lead to deadlock
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.38Operating Systems, by Dhananjay Dhamdhere 38
Turn is effective only when Both processes try to enter
CS at the same time
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.39Operating Systems, by Dhananjay Dhamdhere 39
Two-Process Algorithms (continued)
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.40Operating Systems, by Dhananjay Dhamdhere 40
n-Process Algorithms
Contains a certain form of unfairness since processes do not enter their CSs in the same order in which they requested entry to a CS.
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.41Operating Systems, by Dhananjay Dhamdhere 41
(number[j],j) < number[i],i) if number[j] < number[i], or number[j] = number[i] and j < i
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.42Operating Systems, by Dhananjay Dhamdhere 42
Semaphores
• Also called counting semaphores due to operations on S
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.43Operating Systems, by Dhananjay Dhamdhere 43
Uses of Semaphores in Concurrent Systems
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.44Operating Systems, by Dhananjay Dhamdhere 44
Uses: Mutual Exclusion
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.45Operating Systems, by Dhananjay Dhamdhere 45
Uses: Bounded Concurrency
• Implemented by initializing a semaphore sem_c to c
• Every process wishing to perform opi performs a wait(sem_c) before performing opi and a signal(sem_c) after performing it
• Up to c processes can concurrently perform opi
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.46Operating Systems, by Dhananjay Dhamdhere 46
Uses: Signaling between Processes
• Race conditions cannot arise because the wait and signal operations are indivisible
• Binary semaphore: Can have values 0 and 1 only
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.47Operating Systems, by Dhananjay Dhamdhere 47
Producers-Consumers Using Semaphores
• Avoids busy waits since semaphores are used to check for empty or full buffers
• Total concurrency in system is 1
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.48Operating Systems, by Dhananjay Dhamdhere 48
Example: Producers-Consumers with a Single Buffer through Semaphores
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.49Operating Systems, by Dhananjay Dhamdhere 49
Producers-Consumers Using Semaphores (continued)
• Solution to n-buffer producers–consumers problem:
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.50Operating Systems, by Dhananjay Dhamdhere 50
Readers-Writers Using Semaphores
• Significance of counters: – runread: Number of readers reading – totread: Number of readers wishing to read or reading – Similarly runwrite and totwrite
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.52Operating Systems, by Dhananjay Dhamdhere 52
Implementation of Semaphores
Implementations of wait and signal:• Kernel-Level: Through system calls• User-Level: System calls only to block/activate• Hybrid: Combination of the two
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.53Operating Systems, by Dhananjay Dhamdhere 53
Monitors
• A monitor type resembles a class in a language like C++ or Java– Contains declarations of shared data– Its procedures encode operations that manipulate shared
data and implement process synchronization• A condition is a situation of interest in a monitor• A condition variable is associated with a condition • A process executing a wait operation on condition variable
is blocked until some process performs a signal operation
• In a concurrent system, processes share data by creating a monitor object– We call it simply monitor
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.54Operating Systems, by Dhananjay Dhamdhere 54
Example: Monitor Implementation of a Binary Semaphore
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.55Operating Systems, by Dhananjay Dhamdhere 55
Example: Producers-Consumers Using Monitors
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.56Operating Systems, by Dhananjay Dhamdhere 56
Monitors in Java
• A Java class becomes a monitor type when the attribute synchronized is associated with one or more methods in the class
• An object of such a class is a monitor• Each monitor contains a single unnamed condition
variable– Can lead to busy waits in an application that has many
conditions
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.57Operating Systems, by Dhananjay Dhamdhere 57
Case Studies of Process Synchronization
• Synchronization of POSIX Threads• Process Synchronization in Unix• Process Synchronization in Linux• Process Synchronization in Solaris• Process Synchronization in Windows
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.58Operating Systems, by Dhananjay Dhamdhere 58
Synchronization of POSIX Threads
• POSIX threads provide:– Mutexes for mutual exclusion
• A mutex is a binary semaphore
– Condition variables for control synchronization between processes
• An OS may implement POSIX threads as kernel-level threads or user-level threads
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.59Operating Systems, by Dhananjay Dhamdhere 59
Process Synchronization in Unix
• Unix system V provides a kernel-level implementation of semaphores– Name of a semaphore is called a key
• Key is associated with an array of semaphores• Processes share a semaphore by using the same key
• Unix SVR4 keeps track of all operations performed by a process on each semaphore used by it– Performs an undo on them when process terminates– It helps to make programs more reliable
• Unix 4.4BSD places a semaphore in shared memory areas and uses a hybrid implementation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.60Operating Systems, by Dhananjay Dhamdhere 60
Process Synchronization in Linux
• Linux has Unix-like semaphore for user processes• Also provides semaphores for use by the kernel:
– Conventional semaphore• Uses efficient kernel-level implementation scheme
– Reader–writer semaphore
• Kernels older than 2.6 implemented mutual exclusion in the kernel space through system calls
• 2.6 kernel has a fast user space mutex called futex– Uses a hybrid implementation
– Provides time-bound wait operation
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.61Operating Systems, by Dhananjay Dhamdhere 61
Process Synchronization in Solaris
• Interesting features:– Reader–writer semaphores
• Analogous to the one in Linux
– Adaptive mutexes• Useful in a multiprocessor OS
– Uses the priority inheritance protocol to reduce synchronization delays
– Data structure called a turnstile• Holds information concerning threads that are blocked on a
mutex or reader–writer semaphore– Used for synchronization and priority inheritance
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.62
Process synchronization in Windows
• Dispatcher object is embedded in every object over which synchronization is needed– It can be in signaled/nonsignaled state
– Dispatcher objects used in process, file, etc. (next slide)
• Provides spinlock, queued spinlock, fast mutex and push locks
• Vista provides a reader-writer lock
Operating Systems, by Dhananjay Dhamdhere 62
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.63Operating Systems, by Dhananjay Dhamdhere 63
Process Synchronization in Windows
Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 6.64Operating Systems, by Dhananjay Dhamdhere 64
Summary
• Process synchronization is a generic term for data access synchronization and control synchronization
• A race condition occurs when actions of concurrent processes may have unexpected consequences– Avoided through mutual exclusion
• Avoidance of race conditions is a primary issue in process synchronization
• Critical section: section of code that accesses some shared data in a mutually exclusive manner
• Synchronization achieved through: indivisible instructions, semaphores, monitors