Process synchronization in operating system

33
Process Synchronization Presented by Dani Mfungo HD/UDOM/408/T.2015 CS603:computer system programming

Transcript of Process synchronization in operating system

Page 1: Process synchronization in operating system

Process Synchronization

Presented by Dani MfungoHD/UDOM/408/T.2015

CS603:computer system programming

Page 2: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Objective and Outcomes• What happened when Concurrent process shared data

• How to handle data inconsistency

• Investigate a critical section (CS) as a protocol for synchronization

• Algorithmic approach to CS implementation

• Investigate classical process-synchronization problems

• Producer – consumer problem

• Dining –philosopher problems

• Sleeping barber problems

• Cigarette smokers’ problem

• Investigation of tools used to solve process synchronization problems

Page 3: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Lets consider to approach on handling concurrent problem

• LANGUAGE STRUCTURE FOR CONCURENCY PROCESS

• Fork – Join

• Cobegin - Coend

• How do they help on construct concurrency program??

Page 4: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

TRANSLATE THE PRECENDENCE GRAPH TO

Cobegin –Coend structure

1. Begin2. S1;3. Cobegin4. S3;5. begin6. S2;7. Cobegin8. S4;9. S5;10. Coend11. S6;12. End13. Coend14. S7;15. End

Page 5: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

TRANSLATE THE PRECENDENCE GRAPH TO

fork –join structure

1. Begin L1:S3 L2:S62. count = 3; goto L3 goto L33. S1;4. fork L15. S2;6. S4;7. fork L28. S5;9. L3: join count;10. S7;11. End

Task of Join Count statementCount = count -1If count

Page 6: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Which structure is better than Other?

What is solution for a system which implement

Cobegin – Coend structure ???

• It is impossible to write Cobegin – Coend Here

• Fork –join is the best solution

?

Page 7: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

PRODUCER / CONSUMER PROBLEM

What will happen if

• the rate to producer is greater than the rate to consume?

• Rate to producer < rate to consume?

• Rate to produce == to Rate to consume?

• Producer generate Items

• Consumer consume item

Page 8: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

PRODUCER – CONSUMER PROBLEM-> SOLUTION

Two Situation

1. Unbounded Buffer

2. Bounded buffer – limited size

When buffer is buffer is full,

• producer wait until items are consumed

• Rate of consumption > rate of production, result to empty buffer

Producer Consumer problems also known as Bounded Buffer Problem

Buffer (n)

Pc

Page 9: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

bounded buffer problem

• Solution to this must satisfy the following condition

1. A producer must not overwrite a full buffer

2. Consumer must not consume an empty buffer

3. Consumer and buffer must access buffer in a mutually exclusive manner

• Variable count keep track number of item (N) in buffer

• For Producer

• If count = n -> buffer is full then producer go to sleep

• If count producer add item and increment count

• For Consumer

• If count = 0 - > buffer empty, consumer go to sleep

• If count consumer remove an item and decrement counter

Page 10: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

bounded buffer problem

Is this simple algorithm free of problems??

Page 11: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Producer Processcount + 1

A: R1 = count

B: R1 = R1+ 1

C: count = R1

Consumer Processcount -1

D: R2 = count

E: R2 = R2-1

F: count = R2

Bounded buffer weakness & race condition

Execution sequenceA R1 =7B R1=8D R2=7E R2=6C count=8F count=6

Also consider Transaction process

Race condition result a critical section problems

Page 12: Process synchronization in operating system

CRITICAL SECTION• A critical section of a data item d is a section of code which cannot be executed

concurrently with it self or with other critical section(s) for d.

• PROPERTIES OF CS IMPLEMENTATION

• CORRECTENESS (Mutual execution): at most one process may execute a CS at a given moment

• PROGRESS: A process will take a finite time interval to execute its critical section

• BOUNDED WAIT: Processes wait a finite time interval to enter their criticalsections.

• Absence of deadlock: Processes should not block each other indefinitely

Tuesday, May 2, 2023DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Page 13: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Approaches for Implementing Synchronization

• 3 Categories

1. Busy waiting

2. Hardware support

3. Operating System support

1. Dekker’s algorithm and Peterson’s algorithm are used for Busy waiting

2. Disable interrupt for accessing hardware facilities

3. Mechanism and tools such as semaphores and monitors are used by OS

Page 14: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

CRITICAL SECTION• Entry section make decision if process should enter CS or not

• Shared Lock mechanism is used for CS

• Process enter a exit section after CS

ENTRY SECTION {

} EXIT SECTION {

CRITICAL SECTION

REMINDER SECTION }

Decision made, locking mechanism

Decision made, locking mechanism (unlock process)

Reminder section (other part of the code)

Page 15: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Simple natural solution• Synchronization between two process P1 and P2

• Each process wait for the other to leave CS

• Implement shared variable process_turn

• While loop act as entry section and if variable = 1, P1 enter CS otherwise if 2, P2 enter CS

• P1 finish execution, initialize process_turn to 2, P2 execute also.

• What if P2 still in non critical section and don’t want to execute?

• This approach / protocol will satisfy ME but not Progress

Page 16: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

• Only Mutual execution are granted

• Progress is not work in this situation

Page 17: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Another Solution• The algorithm is that it does not save the state of the executing process

• To store the state of the process, two more variables, namely, state_flag_P1 and state_flag_P2

• If a process enters its critical section, it first sets this state flag to zero,and after exiting, it sets it to one

• It indicates that if a process is using its CS, then the other process must wait until the state flag becomes one,

• state flag variables eliminate the problem of progress

• Also this solution suffer from other problem as well – don’t guarantee Mutual execution since

• In the beginning, when no process is executing, both P1 and P2 will try to enter the critical section

consider diagram

Page 18: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Page 19: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

3rd Solution • Setting state_flag_P1 and state_flag_P2 to 0 before the while loop

• This will solve the problem of mutual exclusion, but there still is one problem

• Suppose, at the moment P1 starts and executes its first statement as state_ flag_P1 = 0, P2 interrupts and gets the execution and executes its first statement, state_ flag_P2 = 0.

• In this situation, if P2 continues and executes its while loop, then it will not be able to proceed as it is waiting for P1 to set state_ flag_P1 to 1.

• Both the processes are waiting for each other, thereby causing deadlock in the system.

Page 20: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

• Two result

• Dead lock and livelock

Page 21: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Dekker’s Solution• This algorithm satisfies all the rules of the designed protocol.

• Both the processes will not try to enter simultaneously due to the variable, process_turn and will satisfy the mutual exclusion property.

• If P1 starts first and finds that state_ flag_P2 = 0, that is, P2 wants to enter the CS, it will allow P2, only when process_turn = 2.

• Otherwise, it will wait for state_ flag_P2 to be 1 so that P1 can enter its CS. If state_ flag_P2 = 1 initially, then P1 will skip the while loop and straightway enter the CS.

• In this way, there will not be any deadlock or livelock.

• Moreover, the chance is given to only those processes that are waiting in the queue to enter the CS.

Page 22: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Dekker’s Solution

Page 23: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Peterson’s Solution

• easier method compared to Dekker’s solution

var flag: array [0..1] of Boolean;

Turn : 0..1;

Pi Flag [i] =true;Turn =j;

While(flag[i] and turn=j) do skip;

CS

Flag [i] = false;

Page 24: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

• process_turn takes the value zero for Process P1 and one for Process P2

• For the process flag, a Boolean array process_ flag[] is taken that consistsof two values, 0 for P1 and 1 for P2

• The variable process_turn maintains the mutual exclusion and process_ flag[] maintains the state of the process.

• Initially, both the processes make their flags true but to maintain the mutual exclusion, the processes before entering their critical sections allow other processes to run

• After exiting the critical section, the process makes its flag false so that theother process can start if it wants.

Page 25: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Peterson’s Solution

Page 26: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

SEMAPHORE• The semaphore is used to protect any resource such as global shared memory that needs to be

accessed and updated by many processes simultaneously

• Semaphore acts as a guard or lock on the resource

• Whenever a process needs to access the resource, it first needs to take permission from the semaphore

• The semaphore is implemented as an integer variable, say as S, and can be initialized with any positive integer values.

• The semaphore is accessed by only two indivisible operations known as wait and signal operations, denoted by P and V, respectively

• Whenever a process tries to enter the critical section, it needs to perform wait operation

Page 27: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

SEMAPHORE cont…• Initially, the count of semaphore is 1

• It decrement to zero when process accesses the available critical area

• When a process exits the critical section, it performs the signal operation

• The semaphore whose value is either zero or one is known as binary semaphore

• semaphore that takes a value greater than one is known as counting semaphore.

• In binary semaphore,the CS locked by a process may be unlocked by any other process.

• However, in mutex, only the process that locks the CS can unlock it.

Page 28: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

one problem in the implementation of such a semaphore• When a process does not get access to the

critical section, it loops continually waiting for it.

• This does not produce any result but consumes CPU cycles, thereby wasting the processor time

Mutex: semaphore mutex=1Pi: P(mutex)

CSV(mutex)

RS

Page 29: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES

• PROCESS SYNCHRONIZATION

P(sync)Sj

SiV(sync)

Si Sj

Sync:semaphoreSync = 0;

S1S2...

SiSi+1Si+2

.

.

U1U2

.

.

.Uj

Uj+1Uj+2

.

.

V(s)

P(s)

Wait for Ui to finish

Sequence: J1 J2

Run concurrently

Page 30: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES

• SOLUTION OF Cobegin – coend synchronization process

Variable a,b,c,d,e,g: semaphore

1. Begin

2. Cobegin

3. S1; V(a);V(b); end

4. P(a); S2;S4;V(c);V(d);

5. P(b); S3; V(e);

6. P(c);S5;V(f);

7. P(d);P(e);S6;V(g);

8. P(f);P(g);S7;

9. coend

10. End

Page 31: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Type semaphore = record

value= integer;

L: list of processes;

end;

Var S:semaphore;

TO OVERCOME A BUSY WAIT PROBLEM

Define a semaphore AS structure or records and not integer

V(S): S.Value=S.Value+1If S.value 0 then

BeginRemove process P from S.L//put P into ready queue

Wakeup(P);End;

P(S): S.Value=S.Value-1If S.value<0 then

BeginAdd this process to S.L

// into wait stateBlock;End;

Page 32: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

SOLUTION OF CLASSIC SYNCHRONIZATION PROBLEMS USING SEMAPHORES

• PRODUCER/ CONSUMER PROBLEM

full,empty,mutex:semaphore

nextp, nextc:item

full =0;

empty=n;

mutex=1;Consumer: repeat

P(full)P(mutex)

……Remove an item from buffer to nextc

……V(mutex)V(empty)

Until false;

Producer: repeat

production on item in nextp;…..

P(empty)P(mutex)

……Add nextp to buffer

……V(mutex)

V(full) Until false;

CONSUMERPRODUCER

Page 33: Process synchronization in operating system

Tuesday, May 2, 2023

DANI ELIAS MFUNGO HD/UDOM/408/T.2015

Demostration

Simulation about Process synchronization