60
Process Synchronization Process Synchronization By CHITRAKANT BANCHHOR IT, MITCOE, Pune 1

Process synchronization 1

Embed Size (px)

Citation preview

Page 1: Process synchronization 1

Process SynchronizationProcess Synchronization

ByCHITRAKANT BANCHHOR

IT, MITCOE, Pune

ByCHITRAKANT BANCHHOR

IT, MITCOE, Pune

1

Page 2: Process synchronization 1

ReferencesReferences

•• Operating system concepts byOperating system concepts by silbertchatzsilbertchatz andand galvingalvin..

•• Operating system by WilliamOperating system by William stallingsstallings..

22

Page 3: Process synchronization 1

What is process synchronizationWhat is process synchronization

Synchronization means to make things happen at the same time Synchronization means to make things happen at the same timeEnglishEnglish

process synchronization is when one process waits for notification of anevent that will occur in another process

process synchronization is when one process waits for notification of anevent that will occur in another process

ComputerScience

ComputerScience

process synchronization is when one process waits for notification of anevent that will occur in another process

process synchronization is when one process waits for notification of anevent that will occur in another process

ComputerScience

ComputerScience

Page 4: Process synchronization 1

Concurrent ProcessesConcurrent Processes

CHITRAKANT BANCHHOR

Page 5: Process synchronization 1

ParallelismParallelism

Parallelism is the quality of occurring at the same timeParallelism is the quality of occurring at the same time

Paralleltasks

Two tasks are parallel if they are performed at the same time

Page 6: Process synchronization 1

ConcurrencyConcurrency

Concurrency is an illusion of parallelism.Concurrency is an illusion of parallelism.

ConcurrentTasks

Two tasks are concurrent if there is an illusion that they are being performed in parallel

Page 7: Process synchronization 1

Concurrent ProcessesConcurrent Processes

Two processes are concurrent if their program executions overlap in real time.Two processes are concurrent if their program executions overlap in real time.

P1

7

P2

Page 8: Process synchronization 1

Principleof

concurrency

8

Concurrencyin

single processor machine

Concurrencyin

multiple processor machine

Page 9: Process synchronization 1

Principleof

concurrency

Concurrencyin

single processor machine

Concurrencyin

multiple processor machine

9

Concurrencyin

single processor machine

Concurrencyin

multiple processor machine

Processes interleaved in time to yield the appearance of simultaneous execution.Processes interleaved in time to yield the appearance of simultaneous execution.

Page 10: Process synchronization 1

Process 1:

Process 2:

Concurrency in Single processor systemConcurrency in Single processor system

Figure:Figure: Concurrent processing in Uniprocessor system

Time

Process 2:

Process 3:

Page 11: Process synchronization 1

Principleof

concurrency

Concurrencyin

single processor machine

Concurrencyin

multiple processor machine

11

Concurrencyin

single processor machine

Concurrencyin

multiple processor machine

Modern Operating Systems are multiprocessor systems.Modern Operating Systems are multiprocessor systems.

They run many process concurrently to achieve better resource utilization.They run many process concurrently to achieve better resource utilization.

Page 12: Process synchronization 1

Process 1:

Process 2:

Concurrent processing in Multiprocessor systemConcurrent processing in Multiprocessor system

Time

Process 3:

Page 13: Process synchronization 1

Racing ProblemRacing Problem

CHITRAKANT BANCHHOR

Page 14: Process synchronization 1

Initially A = 1000

P0:Read(A)A = A – 100Write(A)

P0:Read(A)A = A – 100Write(A)

P1:

Read(A)A = A + 200Write(A)

P1:

Read(A)A = A + 200Write(A)

Suppose two processes P0P0 and P1P1 are accessing a common integer variable AA Suppose two processes P0P0 and P1P1 are accessing a common integer variable AA

Page 15: Process synchronization 1

The expected result of the execution of these two processes is A = 1100 The expected result of the execution of these two processes is A = 1100

This is only possible if execution is:

a) P0 followed by P1.

b) P1 followed by P0.

This is only possible if execution is:

a) P0 followed by P1.

b) P1 followed by P0.

This is only possible if execution is:

a) P0 followed by P1.

b) P1 followed by P0.

This is only possible if execution is:

a) P0 followed by P1.

b) P1 followed by P0.

Page 16: Process synchronization 1

Suppose P0 and P1 are permitted to execute in any arbitrary fashion

Possibility 1: P0, P1, P0, P1

Possibility 2: P0, P1, P0

Page 17: Process synchronization 1

Possibility - 1Possibility - 1 Possibility - 2Possibility - 2

P0:Read(A);

A = A – 100;Write(A);

P1:Read(A);A = A + 200;Write(A);

P0:Read(A);

A = A – 100;Write(A);

P1:Read(A);A = A + 200;Write(A);

P0:Read(A);

A = A – 100;Write(A);

P1:Read(A);A = A + 200;Write(A);

Possibility of execution in the sequence P0,P1, P0, P1. Now, the end value of A will be1200, which is wrong.

Possibility of execution in the sequence P0,P1, P0, P1. Now, the end value of A will be1200, which is wrong.

Possibility of execution in the sequence P0,P1, P0. Now, the end value of A will be 900,which is wrong.

Possibility of execution in the sequence P0,P1, P0. Now, the end value of A will be 900,which is wrong.

P0:Read(A);

A = A – 100;Write(A);

P1:Read(A);A = A + 200;Write(A);

Page 18: Process synchronization 1

Racingproblem

Situation when the end-result of execution of two or more concurrentprocesses is arbitrary and depends on the relative order of their execution,is called a racing problem.

Situation when the end-result of execution of two or more concurrentprocesses is arbitrary and depends on the relative order of their execution,is called a racing problem.

Page 19: Process synchronization 1

Racing ProblemRacing Problem

Situation when the endSituation when the end--result of execution of two or more concurrentresult of execution of two or more concurrentprocesses is arbitrary and depends on the relative order of their execution,processes is arbitrary and depends on the relative order of their execution,is called a racing problem.is called a racing problem.

19

Page 20: Process synchronization 1

Critical Section of ProcessesCritical Section of Processes

20

Critical Section of ProcessesCritical Section of Processes

Page 21: Process synchronization 1

Critical SectionCritical Section

Critical section refers to the code segment of a process, whereby itCritical section refers to the code segment of a process, whereby itaccesses a shared resourceaccesses a shared resource

21

Page 22: Process synchronization 1

P1

Shared Variable: A

Modify : A Modify : A

P0 P1

Processes P0 and P1 are executing their respective critical sections to modify the value of A.Processes P0 and P1 are executing their respective critical sections to modify the value of A.

Page 23: Process synchronization 1
Page 24: Process synchronization 1

Critical Section: ExampleCritical Section: Example

A database application updates parts of a database file in critical section.A database application updates parts of a database file in critical section.

24

Page 25: Process synchronization 1

How to avoid the Racing Problem?How to avoid the Racing Problem?

In previous example the end result is correct, if the execution follows the sequence: In previous example the end result is correct, if the execution follows the sequence:

(a)(a) P0 followed by P1.P0 followed by P1.

(b) P1 followed by P0.(b) P1 followed by P0.

Mutual exclusion: at a time only one of the processes should be executing in its criticalsection, not both.

Mutual exclusion: at a time only one of the processes should be executing in its criticalsection, not both.

Page 26: Process synchronization 1

Cooperating ProcessesCooperating Processes

Two or more concurrent processes.Two or more concurrent processes.

Sharing a common resourceSharing a common resource

P1 P2 Pn

26

These processes have to follow someThese processes have to follow some wellwell –– defined protocolsdefined protocols avoid racing problem.avoid racing problem.

Common Resource

Page 27: Process synchronization 1

P1 P2 Pn

Consider a set of concurrent processesConsider a set of concurrent processes {P0, P1, P2,....,Pn{P0, P1, P2,....,Pn--1}1}

Sharing a common resourceSharing a common resource RR, through the execution of their critical sections., through the execution of their critical sections.

Critical Section ProblemCritical Section Problem

Common Resource R

These processes have to cooperate with each other towards the completion ofThese processes have to cooperate with each other towards the completion oftheir execution.their execution.

Page 28: Process synchronization 1

Critical Section SolutionCritical Section Solution

CHITRAKANT BANCHHOR

Page 29: Process synchronization 1

General structure for Critical Section SolutionsGeneral structure for Critical Section Solutions

dodo{{

Entry Section

Critical Section

Reminder Section

Exit Section

Critical Section

} while(1);} while(1);

Page 30: Process synchronization 1

Entry section

Refers to the code segment of a process that is executed when the process intends toenter its critical section.

Exit section

This section of code will be executed by the process immediately after its exit from thecritical section.

Page 31: Process synchronization 1

Remainder section

This is the remaining part of a process's code.

When a process is executing in this section, it implies that it is not waitingto enter its critical section.

When a process is executing in this section, it implies that it is not waitingto enter its critical section.

So, the processes, executing in their remainder sections, are notconsidered as candidates for entry into their critical sections.

Page 32: Process synchronization 1
Page 33: Process synchronization 1

Critical SectionSolution

Requirements of a Critical SolutionRequirements of a Critical Solution

1. Mutual exclusion

2. Progress

3. Bounded waiting

Page 34: Process synchronization 1

Critical SectionSolution

1. Mutual exclusion

2. Progress

3. Bounded waiting

2. Progress

At any time, at most one of the cooperating processes should be executingAt any time, at most one of the cooperating processes should be executingin its critical section.in its critical section.

Page 35: Process synchronization 1

Critical SectionSolution

1. Mutual exclusion

2. Progress

3. Bounded waiting

2. Progress

If no process is in critical section, can decide quickly who entersIf no process is in critical section, can decide quickly who enters

Only one process can enter the critical section so in practice, others are put on the queueOnly one process can enter the critical section so in practice, others are put on the queue

Page 36: Process synchronization 1

Critical SectionSolution

1. Mutual exclusion

2. Progress

3. Bounded waiting

2. Progress

There must exist a finite upper bound on the number of times that other cooperating processesThere must exist a finite upper bound on the number of times that other cooperating processescan enter their critical section, after a process P1 has requested entry into its critical section andcan enter their critical section, after a process P1 has requested entry into its critical section andbefore the request is granted.before the request is granted.

Normally the upper bound is 1.Normally the upper bound is 1.

Page 37: Process synchronization 1

Critical Section AlgorithmsCritical Section Algorithms

CHITRAKANT BANCHHOR

Page 38: Process synchronization 1

An ideal algorithm to solve a critical section problem should meet the three requirements.

Mutual Exclusion

Progress

Bounded Waiting

Page 39: Process synchronization 1

AlgorithmAlgorithm -- 11

Two processes {P0, P1}, are cooperating through a shared integer variable “turn”.

At a time, turn’s value will be either 0 or 1.

1. If turn = 0 then P0 can enter in its critical section2. If turn = 1 then P1 can enter in its critical section

Page 40: Process synchronization 1

int turn = 0; /* Initial value of turn can be set to 0 or 1*/

P0:P0:do{

while (turn == 1); /* Keep looping as long as turn equals 1*//* This is entry section*/

<Critical Section><Critical Section>turn = 1; /*Enable P1 to enter Critical Section*/

/*This is exit section*/<Remainder Section><Remainder Section>

}while(1);

P1:P1:do{

while (turn == 0); /* Keep looping as long as turn equals 0*//* This is the entry section*/

<Critical Section><Critical Section>turn = 0; /*Enable P0 to enter Critical Section*/

/*This is the exit section*/<Remainder Section><Remainder Section>

}while(1);

Page 41: Process synchronization 1

•• Three requirements of critical section solutionThree requirements of critical section solution

Mutual ExclusionMutual Exclusion Mutual ExclusionMutual Exclusion

ProgressProgress ProgressProgress ProgressProgress

Bounded waitingBounded waiting Bounded waitingBounded waiting

Page 42: Process synchronization 1

MutualExclusion

When turn equals 0, then P1 can not enter its critical section

When turn equals 1, then P0 can not enter its critical section When turn equals 1, then P0 can not enter its critical section

Thus at a time at most one of the cooperating processes can enter its critical section.Thus at a time at most one of the cooperating processes can enter its critical section.

Thus the requirement of mutual exclusion is satisfiedThus the requirement of mutual exclusion is satisfied

Page 43: Process synchronization 1

Progress

1. Processes can enter in their critical section strictly alternately only.

Page 44: Process synchronization 1

2.2. Consider some situations:Consider some situations:

SITUATION - I :

initially turn = 0;

but P1 ------- intends to enter in CS earlier than P0.

( Can not )

initially turn = 0;

but P1 ------- intends to enter in CS earlier than P0.

( Can not )

Page 45: Process synchronization 1

SITUATION I - 2:

P0 ------ exits CS and changes value of turn to 1suppose at this point: P1 -- executing in its remainder section and has no

intention of entry into its CS.

P0 ---- finishes from remainder section---- intends to enter in CS

can not : reason is turn = 1 ( set by P0 earlier )

P0 ------ exits CS and changes value of turn to 1suppose at this point: P1 -- executing in its remainder section and has no

intention of entry into its CS.

P0 ---- finishes from remainder section---- intends to enter in CS

can not : reason is turn = 1 ( set by P0 earlier )

Thus, this is the situation than no cooperating process is in critical section, still P0can not enter its critical section. So, the requirement of progress is not satisfied.

Page 46: Process synchronization 1

Algorithm - 2

Two processes {P0, P1}, are cooperating through boolean variables “flag[0]” and“flag[1]”.

The flags initially set to false, are accessible to both the processes. The flags initially set to false, are accessible to both the processes.

Page 47: Process synchronization 1

typedef enum boolean { false, true };boolean flag[2];

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/while(flag[1]); /*Keep looping, as long as flag[1] is true*/

/* This is Entry Section*/<Critical Section><Critical Section>flag[0] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/while(flag[1]); /*Keep looping, as long as flag[1] is true*/

/* This is Entry Section*/<Critical Section><Critical Section>flag[0] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/while(flag[1]); /*Keep looping, as long as flag[1] is true*/

/* This is Entry Section*/<Critical Section><Critical Section>flag[0] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/while(flag[1]); /*Keep looping, as long as flag[1] is true*/

/* This is Entry Section*/<Critical Section><Critical Section>flag[0] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

P1:P1:do{

flag[1] = true;while(flag[0]); /* Keep looping, as long as flag[0] */

/* This is Entry SectionEntry Section*/<Critical Section><Critical Section>flag[1] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

P1:P1:do{

flag[1] = true;while(flag[0]); /* Keep looping, as long as flag[0] */

/* This is Entry SectionEntry Section*/<Critical Section><Critical Section>flag[1] = false; /*Exiting from critical section*/<Remainder section><Remainder section>

}while(1);

Page 48: Process synchronization 1

MutualExclusion

If flag[1] is set and P1P1 is executing in its critical sectioncritical section, then P0P0 can notenter critical sectioncritical section

If flag[0]flag[0] is set and P0P0 is executing in its critical sectioncritical section then P1P1 can'tenter critical sectioncritical section.

Mutual Exclusion satisfied.Mutual Exclusion satisfied.

Page 49: Process synchronization 1

Progress

1. P0 executes the statement flag[0] = true

2. Then P0 is preempted thereafter prior to execution of “while” statement.

3. Suppose now P1 is dispatched to RUN. P1 sets its flag[1] = true.

P0 intends to enter in its critical section:

P1 intends to enter in its critical section:

3. Suppose now P1 is dispatched to RUN. P1 sets its flag[1] = true.

Now both have set their flags to true but none has entered critical section.

This is a deadlock situation, when both P0 and P1 will continue looping forever intheir while statements

Page 50: Process synchronization 1

Algorithm - 3

Peterson's algorithm

This algorithm has additional information to break the tie in case of deadlock.

That is achieved by Peterson's Algorithm. That is achieved by Peterson's Algorithm.

Page 51: Process synchronization 1

int turn; /*Initial Value does not matter*/boolean flag[2]; /*Initially set to false*/

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/turn = 1;while(flag[1] && turn == 1); /*Keep looping, as long as flag[1] is set to true && turn equals 1*/

/* This is Entry SectionThis is Entry Section*/<Critical Section><Critical Section>

flag[0] = false; /*Exiting from critical sectionExiting from critical section*/<Remainder section><Remainder section>

}while(1);

P0:P0:do{

flag[0] = true; /*Intend to enter critical section*/turn = 1;while(flag[1] && turn == 1); /*Keep looping, as long as flag[1] is set to true && turn equals 1*/

/* This is Entry SectionThis is Entry Section*/<Critical Section><Critical Section>

flag[0] = false; /*Exiting from critical sectionExiting from critical section*/<Remainder section><Remainder section>

}while(1);

P1:P1:do{

turn = 0;flag[1] = true; /*Intend to enter critical section*/while(flag[0] && turn == 0); /*Keep looping, as long as flag[0] is set to true && turn equals 0 */

/* This is Entry SectionThis is Entry Section*/<Critical Section><Critical Section>flag[1] = false; /*Exiting from critical section*/

<Remainder section><Remainder section>}while(1);

P1:P1:do{

turn = 0;flag[1] = true; /*Intend to enter critical section*/while(flag[0] && turn == 0); /*Keep looping, as long as flag[0] is set to true && turn equals 0 */

/* This is Entry SectionThis is Entry Section*/<Critical Section><Critical Section>flag[1] = false; /*Exiting from critical section*/

<Remainder section><Remainder section>}while(1);

Page 52: Process synchronization 1

This algorithm meets all the three requirements of an ideal critical section solution.

Page 53: Process synchronization 1

OS tools for SynchronizationOS tools for Synchronization

CHITRAKANT BANCHHOR

Page 54: Process synchronization 1

SemaphoresSemaphores

Semaphores are the OS tools for synchronization.

Semaphoretypes

Binary Semaphore

Counting Semaphore

Page 55: Process synchronization 1

BinarySemaphore

A binary semaphore is an integer variable.

A binary semaphore can have value either 0 or 1.

A binary semaphore is initialized by the OS to 1.

Page 56: Process synchronization 1

A binary semaphore is accessed by the two primitives: wait and signal.

int S = 1; /* Let S be a Binary semaphore, initialized to 1 */

Page 57: Process synchronization 1

wait

The first process invoking will make the semaphore value 0 and proceed to enter itscritical section.

void wait( int *S ){

while( *S == 0 );/* Keep looping here as long as S is 0*/

*S - -}

Page 58: Process synchronization 1

signal

This primitive is invoked by a cooperating process, when it is exiting fromcritical section.

void signal ( int *S ){

*S++;}

Page 59: Process synchronization 1

A process Pi can be synchronized for accessing of its critical sectionas follows:

do{

wait(&S);

<Critical Section>

signal(&S);

<Remainder Section>

} while(1);

do{

wait(&S);

<Critical Section>

signal(&S);

<Remainder Section>

} while(1);

do{

wait(&S);

<Critical Section>

signal(&S);

<Remainder Section>

} while(1);

do{

wait(&S);

<Critical Section>

signal(&S);

<Remainder Section>

} while(1);

Page 60: Process synchronization 1

Thank You!Thank You!

CHITRAKANT BANCHHORCHITRAKANT BANCHHOR

Thank You!Thank You!

60