Chapter 6: Process Synchronization
Background
- Processes can be executed concurrently
- Shared data may be changed by different process leading to data inconsistency
- Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes
Race condition
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter– could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
Consider this execution interleaving with “count = 5” initially:
- A situation like this, where several process access and manipulate data concurrently and the outcome of the execution depends on the particular order in which the access take place is called, race condition.
The Critical-section problem
-
Each process has critical section segment of code
-
When one process in critical section, no other may be in its critical section
-
Each process must ask permission to enter critical section in entry section, may follow critical section with exit section, then remainder section
-
General structure of typical process
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Solution requirement:
Peterson's Solution
- Two Process solution
- Assume that the load and store instructions are atomic i.e. cannot be interrupted
- The two processes share two variables:
- int turn;
- turn indicates whose turn it is to enter the critical section
- Boolean flag[2]
- The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready
Structure of process in Peterson's solution
- This solution is correct because:
- Mutual exclusion is preserved
- The progress requirement is satisfied
- The bounded-waiting requirement is met
Hardware support for synchronization
Memory Barriers
-
How a computer architecture determines what memory guarantees it will provide to an application is called memory model.
-
two categories of memory model
- Strongly ordered: A memory modification on one processor is immediately visible to all other processors
- Weakly ordered: A memory modification on one processor is not immediately visible to all other processors
-
Memory barriers fences: Instructions to force any changes in memory to be propagated to all other processors.
Hardware Instructions
Concepts
test_and_set()
- Shared boolean variable lock, initialized to FALSE
using test_and_set()
Bounded-waiting Mutual Exclusion with test_and_set
Bounded-waiting compare_and_swap Instruction
- Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key
Mutex Locks(aka spinlock)
- mutex: short of mutual exclusion
- Simplest to implement
- Product critical regions with it by first acquire() a lock then release() it
- requires busy waiting
Semaphore
Usage
- synchronization tool that does not require busy waiting
- Semaphore S – integer variable
- Two standard operations modify S: wait() and signal()
- Consider P1 and P2 that require S1 to happen before S2
implementation
- Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same time
- implementation is the critical section problem where the wait and signal code are placed in the critical section
- applications may spend lots of time in critical sections and therefore this is not a good solution
Semaphore without busy waiting
- With each semaphore there is an associated waiting queue
- Each entry in a waiting queue has two data items:
- value (of type integer)
- pointer to next record in the list
- Two operations:
- block – place the process invoking the operation on the appropriate waiting queue
- wakeup – remove one of processes in the waiting queue and place it in the ready queue
Use semaphore to solve bounding buffer
- n buffers, each can hold one item
- Semaphore mutex initialized to the value 1
- Semaphore full initialized to the value 0
- Semaphore empty initialized to the value n


Monitors
- A high-level abstraction that provides a convenient and effective mechanism for process synchronization
Usage
- Is an Abstract data type, internal variables only accessible by code within the procedure
- Only one process may be active within the monitor at a time
Schematic view of a monitor

-
condition x, y;
-
Two operations on a condition variable:
- x.wait () – a process that invokes the operation is suspended until x.signal ()
- x.signal () – resumes one of processes (if any) that invoked x.wait ()
- If no x.wait () on the variable, then it has no effect on the variable
-
If process P invokes x.signal (), with Q in x.wait () state, then Q is resumed, P must wait
-
Options include
- Signal and wait – P waits until Q leaves monitor or waits for another condition
- Signal and continue – Q waits until P leaves the monitor or waits for another condition
-
Both have pros and cons – language implementer can decide
-
Monitors implemented in Concurrent Pascal compromise P executing signal immediately leaves the monitor, Q is resumed
Implementing a monitor using semaphores
Each procedure F will be replaced by
- For each condition variable x, we have:
- The operation x.wait can be implemented as:
- The operation x.signal can be implemented as:
- A Monitor to Allocate Single Resource
Resuming Processes within a Monitor
- If several processes queued on condition x, and x.signal() executed, which should be resumed?
- First-come, first-serve not adequate
- conditional-wait construct of the form x.wait©
- c is priority number
- Process with lowest number (highest priority) is scheduled next
Readers-Writers Problem
dining-philosophers problem
- don’t interact with their neighbors, occasionally try to pick up 2 chopsticks (one at a time) to eat from bowl
- Need both chopsticks to eat, then release both when done
Solution to Dining Philosophers
avoid deadlock
- Don't allow all philosophers to sit and eat/think at once.
- Pick up both chopsticks in a critical section
- Alternate choice of first chopstick
Liveness
- Liveness refers to a set of properties that a system must satisfy to ensure that processes make progress during their execution life.
Deadlock
- two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes
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
Evaluation
Review Question
出率高
ANS
- several process access and manipulate data concurrently making the outcome depends on the order in which the access take place
- Consider this execution interleaving with “count = 5” initially:
出率高
ANS
- Mutual exclusion: Only one process can access critical section by a time.
- Progress: If there is no process in critical section, and some process wants in. A process will be selected in.
- Bounded Waiting: Time waiting to get in critical section is limited
ANS
- Mutual exclusion: the second one will be stuck in while loop
- Progress: once the first one gets out, the second one gets in.
- Bounded Waiting: The second one gets in after the first one gets out.
ANS
- bounded-waiting mutual exclusion
- Mutual Exclusion: while (waiting[i] && key)
- Passing:
- Bound waiting:
ANS
- mutual exclusion and progress. No bound waiting
- Mutual exclusion: Only one can get into critical section
- Progress: If a process gets out, other processes will be able to get in.
ANS
- Product critical regions with it by first acquire() a lock then release() it
- Spinlock will repeatedly ask for permission rapidly if it can't get into critical section.
ANS
- Synchronization tool that allow multiple process to access and don't require busy waiting.
- Incoming process is put into a queue to wait. Don't have to check repeatedly
ANS
- n buffers, each can hold one item
- Semaphore mutex initialized to the value 1
- Semaphore full initialized to the value 0
- Semaphore empty initialized to the value n


ANS
- Shared data:
- Data set
- Semaphore rw_mutex initialized to 1
- Semaphore mutex initialized to 1
- Integer read_count initialized to 0
ANS
END