Process synchronization - producer, consumer issues

Synchronization is the process of coordinating multiple related processes on the execution order, so between multiple processes concurrently executing according to certain rules sharing system resources.

1, producers and consumers:

Producer, consumer issues into the buffer Producers, consumers remove the product from the buffer to spend. Does not allow consumers to take the product from an empty buffer, the producers are not allowed to add products to the buffer is full.

 

 2, the process is described:

Producer:

void Producer () {
     the while ( . 1 ) 
    { 
      the while (n-counter ==); // buffer pool is full, no longer performed later 
      Buffer [ in ] = nextp; // the produced product into buffer pools 
      in = ( in + . 1 ) n-%; // the pointer moves 
      counter ++ ;     
    } 
 
}

consumer:

void Consumer () {
     the while ( . 1 ) 
    { 
      the while (counter == 0 ); // buffer pool is empty, the latter operation is no longer performed 
      NEXTC = Buffer [ OUT ]; // remove a product 
      OUT = ( OUT + . 1 ) n-%; // the pointer moves 
      Counter is ; 
    } 
}

Without control of the producer and consumer processes will be different results (correct wrong), that is, not the reproducibility of the process, which is due to the process lost due to closure.

3, process synchronization (semaphore mechanism):

(1) Use wati to achieve access to critical resources () and signal () operation.

the mutex = semaphone . 1 ; 
Pa () { 
    the while ( . 1 ) { 
        the wait (the mutex); // entry area 
        critical region; // access the critical resource code 
        Signal (the mutex); // exit region 
        remaining region; // other portions 
    } 
}

(2) semaphore mechanism to solve the problem of synchronization process:

Producer:

int n = 0 , OUT = 0 ; 
Item Buffer [n]; 
semaphore the mutex = . 1 , empty = n, Full = 0 ; // pool a, n-empty buffer, the buffer 0 full 
void Producer) () {
     the while ( . 1 ) { 
        the wait (empty); // first p performing the buffer is empty prior to execution if is 0, blocking is not necessary to perform later in 
        the wait (the mutex); // slowing pool of the 
        buffer [ in ] nextp =; // the produced product into buffer pools 
        in = ( in + . 1 ) n-%; // the pointer moves 
        Signal (the mutex); //Release of resources, v Operation 
        Signal (Full);     
    } 
}

consumer:

int n = 0 , OUT = 0 ; 
Item Buffer [n]; 
semaphore the mutex = . 1 , empty = n, Full = 0 ; // pool a, n-empty buffer, the buffer 0 full 
void Consumer () {
     the while ( . 1 ) { 
        the wait (full); // first operation performed on the p buffer, if it is 0, the full executed before blocking, in the implementation of the latter does not have to 
        the wait (the mutex); // slowing pool of the 
        buffer [ in ] = nextp; // the consumer product into buffer pools 
        OUT = ( OUT + . 1 ) n-%; // the pointer moves 
        Signal (the mutex); //Release of resources, v Operation 
        Signal (Full);         
        } 
}  

Which, mutex semaphores to ensure that consumers and producers exclusive access to the buffer pool, that is, before the consumer (or producer) to release resources, consumers (or producers) can not use the buffer pool.

(3) the magnitude of the signal meaning:

For example: a semaphore set initial value 1, after a p operation becomes 0, this time, has no resources, but the process is not blocked;

           After the second operation, it changes to -1, which means the resource has no, and another process in a blocked state;

           1 represents the initial value of the number of available resources.

 

Guess you like

Origin www.cnblogs.com/zhai1997/p/11999992.html