29
PROCESS SYNCHRONIZATION

SYNCHRONIZATION IN MULTIPROCESSING

Embed Size (px)

DESCRIPTION

IT IS ABOUT MULTIPROCESSING,COMMUNICATION BETWEEN THE PROCESS THROUGH MESSAGE PASSING AND SHARED MEMORY.SYNCHRONIZATION MECHANISM AND SYNCHRONIZATION USING SEMAPHORE

Citation preview

Page 1: SYNCHRONIZATION IN MULTIPROCESSING

PROCESS SYNCHRONIZATION

Page 2: SYNCHRONIZATION IN MULTIPROCESSING

MULTIPROCESSOR SYSTEM

• Multiprocessor System is an interconnection of 2 or more CPU with memory and input-output equipments.

• Multiprocessors are classified as MIMD system(Multiple Instruction Multiple Data Stream).

Page 3: SYNCHRONIZATION IN MULTIPROCESSING

PROCESS

• A process is program in execution.• A process can be either– Independent process– Cooperative process

Independent Process:– A process is independent if it is not affected by

other process executing in the system.– Process will not share data with other process.

Page 4: SYNCHRONIZATION IN MULTIPROCESSING

Cooperative process:– A process can affect or be affected by other

process executing in the system.– Process share data with other process.

• Cooperative process requires interprocess communication(IPC) mechanism to exchange data and information.

• 2 fundamental models:– Shared memory–Message massing

Page 5: SYNCHRONIZATION IN MULTIPROCESSING

Shared Memory

• A region of memory that is shared by cooperative process is established .

• Process can exchange information by reading and writing to shared memory.

• It allows maximum speed.• Processor communicates with shared address

space.• The main problem in shared memory is process

synchronization

Page 6: SYNCHRONIZATION IN MULTIPROCESSING
Page 7: SYNCHRONIZATION IN MULTIPROCESSING

MESSGAE PASSING

• Communication takes place by means of message exchange between cooperating process.

• It is useful for exchanging smaller amount of data.• It is time consuming as there is kernel intervention.• The process will be having separate address space.• Process have private memories.

Page 8: SYNCHRONIZATION IN MULTIPROCESSING
Page 9: SYNCHRONIZATION IN MULTIPROCESSING

PROCESS SYNCHRONIZATION MECHSNISMS

• Mainly 2 types of synchronizations are used:–Mutual exclusion– Condition synchronization

• Mutual Exclusion: If a process is executing its critical section then no other process can execute in their critical section.– A critical section is a piece of code that accesses a

shared resource that must not be concurrently accessed.

Page 10: SYNCHRONIZATION IN MULTIPROCESSING

• Condition synchronization:–When a shared data object is in a state that is

inappropriate for executing a given operation.– Any process which attempt such an operation

should be delayed until the state of the data object changes to the desired value as a result of other process being executed.

Page 11: SYNCHRONIZATION IN MULTIPROCESSING

• The mutual -exclusive execution of a critical section,S,whose access is controlled by a variable gate can be enforced by :

• an entry protocol denoted by MUTEXBEGIN(gate)

• and an exit protocol denoted by MUTEXEND (gate).

Page 12: SYNCHRONIZATION IN MULTIPROCESSING

• Execution of the MUTEXBEGIN statement should detect the status of the critical section.

• If it is busy, the process attempting to enter the critical section must wait.

• Execution of the MUTEXEND statement should reset the status of the critical section to idle and provide a mechanism to schedule the waiting process to use the critical section.

Page 13: SYNCHRONIZATION IN MULTIPROCESSING

IMPLEMENTATION

LOCK and UNLOCK operations :• consider that there is a single gate that each

process must pass through to enter a CS and also leave it.

• If a process attempting to enter the CS finds the gate unlocked (open) it locks (closes) it as it enters the CS so that all other processes attempting to enter the CS will find the gate locked.

• On completion, the process unlocks the gate and exits from the CS.

Page 14: SYNCHRONIZATION IN MULTIPROCESSING

• Assuming that the variable• gate =0 means that the gate is open • gate=1 means that the gate is closed,• the access to a CS controlled by the gate can

be written as:

LOCK (gate)

execute critical section

UNLOCK (gate)

Page 15: SYNCHRONIZATION IN MULTIPROCESSING

• The LOCK(x) operation may be implemented as follows:

Var x:shared integer;

LOCK (x):begin

var y: integer;

y x;

While y =1 do yx;//wait until gate is open //

x1 //set gate to unavailable status //

end

Page 16: SYNCHRONIZATION IN MULTIPROCESSING

• The UNLOCK(x) operation may be implemented as

UNLOCK(x): x 0;

DISADVANTAGE OF LOCK MECHANISM:• two or more processes may find x=0 before

one reaches the x1 statement. • processes attempting to enter critical sections

are busy accessing common variables. • This is called busy –wait or spin -lock, which

results in performance degradation.

Page 17: SYNCHRONIZATION IN MULTIPROCESSING

• An important property of locks is that :• a process does not relinquish the processor on

which it is executing while it is waiting for a lock held by another process.

• Thus, it is able to resume execution very quickly when the lock becomes available.

Page 18: SYNCHRONIZATION IN MULTIPROCESSING

SYNCHRONIZATION WITH SEMAPHORE

• Dijkstra invented the two operations P and V, which can be shared by many processes and which implement the mutual -exclusion mechanism efficiently.

• The P and V operations are called primitives .• They operate on a special common variable

called a semaphore, which indicates the number of processes attempting to use the critical section.

Page 19: SYNCHRONIZATION IN MULTIPROCESSING

• var s: semaphore• P(s): MUTEXBEGIN (s)

s s-1;

If s < 0 then

begin

Block the process executing the P(s) and put it in a FIFO queue associated with the semaphore s;

end

MUTEXEND

Page 20: SYNCHRONIZATION IN MULTIPROCESSING

• V(s): MUTEXBEGIN (s)

SS + 1;

If s <= 0 then

begin

if an inactive process associated with semaphore s exists, then wake up the highest priority blocked process associated with s and

put it in a ready list.

end

MUTEXEND

Page 21: SYNCHRONIZATION IN MULTIPROCESSING

• When s can take values of 0 or 1, it is called a

binary semaphore. • If s takes any integer value, it is called a

counting semaphore

Page 22: SYNCHRONIZATION IN MULTIPROCESSING

• Eg.Producer Consumer Problem• consider a finite buffer BUFFER of size n arranged

as a circular queue in which the slot positions are named 0, 1,…., n-1.

• There are the two pointers c and p, which correspond to the "head" and "tail" of a circular queue, respectively.

• The consumer consumes the message from the head c by updating c and then retrieving the message.

• The producer adds a message to the buffer by updating p before the add operation.

Page 23: SYNCHRONIZATION IN MULTIPROCESSING

• Initially, p= c = 0, which indicates that the buffer is empty.

• Let the variables empty and full be used to indicate the number of empty slots and occupied slots, respectively.

Page 24: SYNCHRONIZATION IN MULTIPROCESSING
Page 25: SYNCHRONIZATION IN MULTIPROCESSING

• The empty variable is used to inform the producer of the number of available slots.

• the full variable informs the consumer of the �number of messages needed to be consumed.

• The producer or consumer will be suspended when empty=0 or full = 0, respectively.

Page 26: SYNCHRONIZATION IN MULTIPROCESSING

• shared record

begin

var p, c: integer;

var empty, full: semaphore;

var BUFFER [0:n -1]: message;

end

initial empty = n, full = 0, p = 0, c = 0;

Page 27: SYNCHRONIZATION IN MULTIPROCESSING

• Cobegin

Producer: begin

var m: message;

Cycle

begin

Produce a message m;

P(emptv);

p(p+1)modn;

BUFFER [p] m; // place message in buffer//

V(full)

end

end

Page 28: SYNCHRONIZATION IN MULTIPROCESSING

• Consumer: begin

var m: message;

Cycle

begin

P (full);

c(c+1) mod n;

m BUFFER [c]; // remove message from buffer //

V (emptv);

Consume message m;

end

end

coend

Page 29: SYNCHRONIZATION IN MULTIPROCESSING

THANK YOU