You are on page 1of 19

CS 303

Operating Systems Concepts


Semester I – 2019/2020

Chapter 05 – Process Synchronization

CS 303 - Operating Systems


Department of Statistics & Computer Science 1
Concepts
Content
• Background Knowledge
• Race Condition
• The Critical Section Problem
• Peterson’s Solution
• Synchronization Hardware

CS 303 - Operating Systems


Department of Statistics & Computer Science 2
Concepts
Background Knowledge
• 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
▪ A cooperating process is one that can be affect or be affected by other
processes executing in the system

CS 303 - Operating Systems


Department of Statistics & Computer Science 3
Concepts
Race Condition
• Race condition occurs with multiple processes of threads reads and write data
so that the final results depend on the order of execution of instruction in the
multiple processes
• For instance, consider processes P1 and P2 sharing the global variable a
▪ At some point in execution P1 updates a to value 2
▪ At another point of execution P2 updates a to value 5
▪ Thus, the two task are in a race to write variable a. The looser of the race
which the process that updates last determine the final value of a

CS 303 - Operating Systems


Department of Statistics & Computer Science 4
Concepts
The Critical Section Problem
• Consider a system consisting of n processes {P0, P1, P2, …, Pn-1}
• Each process has a segment of code, called a critical section
• Process may be changing common variables, updating a table, writing a
file, and so on
• When one process is executing its critical section, no other process is
allowed to execute in its critical section
• The critical-section problem is to design a protocol that the processes can use
to cooperate
• Each process must request permission to enter its critical section
▪ The section of code implementing this request is the entry section
▪ The critical section may be followed by an exit section
▪ The remaining code is the remainder section

CS 303 - Operating Systems


Department of Statistics & Computer Science 5
Concepts
The Critical Section Problem (cntd…)
do {
entry section

critical section

exit section

remainder section

} while (true);

Figure 5.1: General structure of a typical process Pi

CS 303 - Operating Systems


Department of Statistics & Computer Science 6
Concepts
The Critical Section Problem (cntd…)
A solution to the Critical-Section Problem must satisfy the
following 3 requirements:
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 some
processes wish to enter their critical sections, then only those processes
that are not executing in their remainder sections can participate in
deciding which will enter its critical section next, and this selection cannot
be postponed indefinitely
3. Bounded waiting – There exists bound, or limit, 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
requested is granted
▪ Assume that each process executes at a nonzero speed
▪ No assumption concerning relative speed of the n process

CS 303 - Operating Systems


Department of Statistics & Computer Science 7
Concepts
The Critical Section Problem (cntd…)
Critical-Section Handling in OS:
• Two general approaches are used to handle critical sections in OSs:
▪ Preemptive kernels – Allows a process to be preempted while it is running
in kernel mode
▪ Nonpreemptive kernels – Does not allow a process running in kernel mode
to be preempted; a kernel-mode process will run until it exits kernel mode,
blocks, or voluntarily yields control of the CPU
➢ Essentially free from race conditions on kernel data structures, as only
one process is active in the kernel at a time

CS 303 - Operating Systems


Department of Statistics & Computer Science 8
Concepts
Peterson’s Solution
• Restricted to two processes that alternate execution between their critical
sections and remainder sections
• The processes are numbered P0 and P1 ( For convenience, when presenting Pi,
we use Pj to denote the other process; that is, j = 1 – i)
• Peterson’s solution requires two data items to be shared between the two
processes
int turn;
boolean flag[2];
• The variable turn indicates whose turn it is to enter its critical section
▪ If turn == i, then process Pi is allowed to execute in its critical section
• The flag array is used to indicate if a process is ready to enter its critical section
• For instance, flag[i] = true implies that process Pi is ready to enter
its critical section

CS 303 - Operating Systems


Department of Statistics & Computer Science 9
Concepts
Peterson’s Solution (cntd…)
do {

flag[i] = true;
turn = j;
while (flag[j] && turn == j);

critical section

flag[i] = false;

remainder section

} while (true);

Figure 5.2: The structure of process Pi in Peterson’s solution

CS 303 - Operating Systems


Department of Statistics & Computer Science 10
Concepts
Peterson’s Solution (cntd…)
Explanation for the structure of process Pi in Peterson’s solution:
• To enter the critical section, process Pi first sets flag[i] to be true and then
sets turn to the value j, thereby asserting that if the other process wishes to
enter the critical section, it can do so.
• If both processes try to enter at the same time, turn will be set to both i and j at
roughly the same time.
• Only one of these assignments will last; the other will occur but will be
overwritten immediately.
• The eventual value of turn determines which of the two processes is allowed
to enter its critical section first.

CS 303 - Operating Systems


Department of Statistics & Computer Science 11
Concepts
Synchronization Hardware
• Many systems provide hardware support for implementing the critical section
code
• All solutions below based on idea of locking
▪ Protecting critical regions though the use of locks
• Uniprocessors – Could disable interrupts
▪ Currently running code would execute without preemption
▪ Generally too inefficient on multiprocessor systems
➢ OSs using this not broadly scalable
• Modern machines provide special atomic hardware instructions
➢ Atomic = Non-interruptible
▪ Either test memory word and set value
▪ Or swap contents of two memory words

CS 303 - Operating Systems


Department of Statistics & Computer Science 12
Concepts
Synchronization Hardware (cntd…)
Solution to Critical-Section Problem using Locks:

do {

acquire lock

critical section

release lock

remainder section

} while (TRUE);

Figure 5.3: The general structure of the solution to Critical-Section Problem using Locks

CS 303 - Operating Systems


Department of Statistics & Computer Science 13
Concepts
Synchronization Hardware (cntd…)
test_and_set Instruction:
• Definition:
boolean test_and_set (boolean *target) {
boolean rv = *target;
*target = TRUE;

return rv:
}
1. Executed atomically
2. Returns the original value of passed parameter
3. Set the new value of passed parameter to “TRUE”

CS 303 - Operating Systems


Department of Statistics & Computer Science 14
Concepts
Synchronization Hardware (cntd…)
Solution using test_and_set():
• Shared Boolean variable lock, initialized to FALSE
• Solution:
do {
while (test_and_set(&lock))
; /* do nothing */
/* critical section */
lock = false;
/* remainder section */
} while (true);

CS 303 - Operating Systems


Department of Statistics & Computer Science 15
Concepts
Synchronization Hardware (cntd…)
compare_and_swap Instruction:
• Definition:
int compare_and_swap(int *value, int expected, int new_value) {
int temp = *value;

if (*value == expected)
*value = new_value;

return temp;
}

1. Executed atomically
2. Returns the original value of passed parameter “value”
3. Set the variable “value” the value of the passed parameter “new_value”
but only if “value” ==“expected”. That is, the swap takes place only under
this condition.

CS 303 - Operating Systems


Department of Statistics & Computer Science 16
Concepts
Synchronization Hardware (cntd…)
Solution using compare_and_swap():
• Shared integer variable lock, initialized to 0;
• Solution:
do {
while (compare_and_swap(&lock, 0, 1) != 0)
; /* do nothing */
/* critical section */
lock = 0;
/* remainder section */
} while (true);

CS 303 - Operating Systems


Department of Statistics & Computer Science 17
Concepts
Synchronization Hardware (cntd…)
Bounded-Waiting Mutual Exclusion with test_and_set():
do {
waiting[i] = true;
key = true;
while (waiting[i] && key)
key = test_and_set(&lock);
waiting[i] = false;
/* critical section */
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
lock = false;
else
waiting[j] = false;
/* remainder section */
} while (true);

CS 303 - Operating Systems


Department of Statistics & Computer Science 18
Concepts
End of Chapter 05

CS 303 - Operating Systems


Department of Statistics & Computer Science 19
Concepts

You might also like