Sunday, 3 June 2018

Classical Synchronization Problem in Operating System

Classical Synchronization Problem in Operating System:

  1.  Producer Consumer Problem:
  1. Dining Philosopher Problem:

Producer Consumer Problem:

The Producer Consumer Problem defines the need for synchronization in systems where many processes share a common resource in the problem two processes shared a fixed size buffer.

i.  One process produces the item and woods it in the buffer, while other process consume item from the buffer. which are produces by producer. These processes do not take terms accessing the buffer, they both work concurrently. here in likes the problem what happens if the producer tries to woods and item into a full buffer?


ii.    what happens if the consumer tries to taken an item from an empty buffer?

Solution : 
In order to synchronize these processes we will block the producer when the buffer is full and we will block the consumer when the buffer is empty.

Algorithm Through Semaphore:


int N =10;              // buffer size
semaphore empty = N;  //N no. of slots are ready to fill
semaphore full = 0;
semaphore mutex = 1;    // binary semaphore
int p, c ;  // counter variable  & P is producer process and C -consumer process

int buffer [N] ;

At Producer End-

wait ( & empty );       //wait for empty slot

wait ( & mutex ) ;         //reserve the buffer for producing an item.

buffer [ p] =item;
p = ( p + 1) mod N;
signal ( & mutex ) ;
signal ( & full );

} while ( 1) ;

At Consumer End-

wait ( & full );    //wait untill buffer is full
wait ( & mutex 0 ; 
item =buffer [c ];
c = ( c+1 ) mod N ;
signal ( & mutex );
signal (& empty ) ;

} while (1 );

No comments:

Post a comment

Popular Posts