0% found this document useful (0 votes)
24 views19 pages

OS L7 PSync 2017

The document discusses process synchronization and solving the critical section problem, where multiple processes need exclusive access to shared resources. It describes race conditions that can occur when processes access shared data concurrently without synchronization. Several algorithms are presented to enforce mutual exclusion when processes enter critical sections, including Peterson's algorithm, which uses shared flags and a turn variable to ensure only one process is in the critical section at a time while allowing the other to progress.

Uploaded by

Riajimin
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)
24 views19 pages

OS L7 PSync 2017

The document discusses process synchronization and solving the critical section problem, where multiple processes need exclusive access to shared resources. It describes race conditions that can occur when processes access shared data concurrently without synchronization. Several algorithms are presented to enforce mutual exclusion when processes enter critical sections, including Peterson's algorithm, which uses shared flags and a turn variable to ensure only one process is in the critical section at a time while allowing the other to progress.

Uploaded by

Riajimin
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/ 19

OPERATING SYSTEMS (THEORY)

LECTURE - 7

K.ARIVUSELVAN
Assistant Professor (SG) (SITE)
VIT University
PROCESS SYNCHRONIZATION
Introduction to Cooperating Processes

Processes within a system may be independent or


cooperating

Independent process cannot affect or be affected by the


execution of another process

Cooperating process can affect or be affected by other


processes, including sharing data
Suppose that two processes A and B have access to a shared
variable Balance:

PROCESS A:

Balance = Balance - 100

PROCESS B:

Balance = Balance - 200

Further, assume that Process A and Process B are executing


concurrently in a time-shared, multi-programmed system.
The statement Balance = Balance 100 is implemented by
several machine level instructions such as:

A1. LOAD R1, BALANCE // load Balance from memory into Register
1 (R1)
A2. SUB R1, 100 // Subtract 100 from R1
A3. STORE BALANCE, R1 // Store R1s contents back to the
memory location of Balance.

B1. LOAD R1, BALANCE // load Balance from memory into Register
1 (R1)
B2. SUB R1, 200 // Subtract 200 from R1
B3. STORE BALANCE, R1 // Store R1s contents back to the
memory location of Balance.
Observe: In a time-shared or multi-processing system the exact
instruction execution order cannot be predicted!
Race Condition:

If Several processes access and manipulate the shared data


concurrently,

Then the outcome of the execution depends on the particular


order in which the access takes place
Race Condition: Solution

To prevent race conditions, concurrent processes must be


synchronized

Ensure that only one process at a time can be manipulating


the shared Data
The Critical-Section

A section of code Common to n cooperating processes, in


which the processes may be accessing common variables

A critical section environment contains:

1. Entry Section: Code requesting entry into the critical section

2. Critical Section: Code in which only one process can execute


at any one time

3. Exit Section: The end of the critical section, releasing or


allowing others in

4. Remainder Section: Rest of the code AFTER the critical


section
General Structure of a Typical Process

do {
entry section

critical section

exit session

remainder section

} while (TRUE);
Solution to Critical-Section Problem

The critical section must ENFORCE all the 3 rules:

(1) Mutual
Exclusion

(2) Progress

(3) Bounded
Waiting
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.

(i.e. no two processes will simultaneously be inside the same CS)

2. Progress the selection of the process 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.
+
processes will remain inside its CS for a short time only, without
blocking
Initial Attempts to Solve Problem

Only 2 processes, Pi and Pj

General structure of process Pi (other process Pj)

do {
entry section
critical section
exit section
remainder section
} while (1);

Processes may share some common variables to


synchronize their actions.
Algorithm 1

Shared variables:

int turn;
initially turn = 0 (or ) 1

Structure of Process Pi :

turn = j (Pj can enter its critical section)

repeat
while (turn != i) do nothing; /*busy wait*/
critical section
turn = i;
remainder section
Until false;
Algorithm 1

This solution guarantees mutual exclusion

Drawback 1: processes must strictly alternate

Drawback 2: if one processes fails other process is


permanently blocked
Algorithm 2
Shared variables:

boolean flag[2];
initially flag [0] = flag [1] = false

flag [i] = true Pi ready to enter its critical section

Structure of Process Pi :

repeat
flag[ i ] := true;
while (flag[ j ]) do nothing;
critical section
flag [j] = false;
remainder section
Until false;
Algorithm 2

This solution guarantees mutual exclusion

Drawback 1:This approach may lead to dead lock

What is wrong with this implementation ?

Dead lock occurs because each process can insist


on its right to enter critical section
Algorithm 3 (PETERSON ALGORITHM)

Combined shared variables of algorithms 1 and 2

Process Pi

do {
flag [i]:= true;
turn = j;
while (flag [j] and turn = j) do nothing;
critical section
flag [i] = false;
remainder section
} while (1);

Meets all three requirements; solves the critical-


section problem for two processes

You might also like