0% found this document useful (0 votes)
18 views25 pages

CS211 Lec 14

The document discusses process synchronization and solutions to the critical section problem. It describes the bounded buffer problem and issues that can arise from concurrent processes updating shared data. Several algorithms for solving the critical section problem for two processes are presented, including ones using shared variables like a turn indicator or flags to control access to the critical section.

Uploaded by

Muhammad Rafay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views25 pages

CS211 Lec 14

The document discusses process synchronization and solutions to the critical section problem. It describes the bounded buffer problem and issues that can arise from concurrent processes updating shared data. Several algorithms for solving the critical section problem for two processes are presented, including ones using shared variables like a turn indicator or flags to control access to the critical section.

Uploaded by

Muhammad Rafay
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Operating Systems

Course Code: CS211


Instructor: Dr. Sarah Iqbal

Lecture # 14

1
Agenda for Today
 Process synchronization
 The Critical Section Problem
 Conditions for a Good Solution
 2-Process Critical Section Problem Solutions

2
Bounded-Buffer Problem
Producer process
item nextProduced;

while (1) {
nextProduced = getNewItem();
while (counter == BUFFER_SIZE) ;
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
3
Bounded-Buffer Problem
Consumer process
item nextConsumed;
while (1) {
while (counter == 0) ;
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
}
4
Bounded-Buffer Problem
 “counter++” in assembly language
MOV R1, counter
INC R1
MOV counter, R1

 “counter--” in assembly language


MOV R2, counter
DEC R2
MOV counter, R2
5
Bounded-Buffer Problem
 If both the producer and consumer attempt to
update the buffer concurrently, the machine
language statements may get interleaved.
 Interleaving depends upon how the producer and
consumer processes are scheduled.

6
Bounded-Buffer Problem
 Assume counter is initially 5. One
interleaving of statements is:

producer: MOV R1, counter (R1 = 5)


INC R1 (R1 = 6)
consumer: MOV R2, counter (R2 = 5)
DEC R2 (R2 = 4)
producer: MOV counter, R1 (counter = 6)
consumer: MOV counter, R2 (counter = 4)

 The value of count may be either 4 or 6,


where the correct result should be 5.
7
Process
Synchronization
 Race Condition: The situation where
several processes access and
manipulate shared data concurrently,
the final value of the data depends on
which process finishes last.

8
Process
Synchronization
 Critical Section: A piece of code in a
cooperating process in which the
process may updates shared data
(variable, file, database, etc.).
 Critical Section Problem: Serialize
executions of critical sections in
cooperating processes
9
Process
Synchronization
 More Examples
 Bank transactions
 Airline reservation

10
Bank Transactions

D Balance W

Deposit Withdrawal
MOV A, Balance MOV B, Balance
ADD A, Deposited SUB B, Withdrawn
MOV Balance, A MOV Balance, B

11
Solution of the Critical
Problem
 Software based solutions
 Hardware based solutions
 Operating system based solution

12
Structure of Solution
do {
entry section
critical section
exit section

reminder section
} while (1);
13
Solution to Critical-Section
Problem
2-Process Critical Section Problem
N-Process Critical Section Problem
Conditions for a good solution:
1. Mutual Exclusion: If a process is
executing in its critical section, then no
other processes can be executing in their
critical sections.

14
Solution to Critical-Section
Problem
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 decide which
process will enter its critical section next,
and this decision cannot be postponed
indefinitely.

15
Solution to Critical-Section
Problem
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.

16
Solution to Critical-Section
Problem
Assumptions
Assume that each process executes
at a nonzero speed
No assumption can be made
regarding the relative speeds of
the N processes.

17
Possible Solutions
Only 2 processes, P0 and P1
Processes may share some common
variables to synchronize their actions.
General structure of process Pi
do {
entry section
critical section
exit section
remainder section
} while (1);
18
Algorithm 1
Shared variables:
int turn;
initially turn = 0
turn = i  Pi can enter its
critical section

19
Algorithm 1
 Process Pi
do {
while (turn != i);

critical section
turn = j;
remainder section
} while (1);
 Does not satisfy the progress condition
20
Algorithm 2
Shared variables
boolean flag[2]; // Set to false
flag [i] = true  Pi ready to enter its critical section

21
Algorithm 2
 Process Pi
do {
flag[i] = true;
while (flag[j]);
critical section
flag[i] = false;
remainder section
} while (1);
 Does not satisfy the progress condition 22
Algorithm 3
 Combined shared variables of
algorithms 1 and 2.
 boolean flag[2]; // Set to false
 int turn=0;

23
Algorithm 3
 Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j) ;
critical section
flag[i] = false;
remainder section
} while (1);
24
End of

Lecture # 14

25

You might also like