Process synchronization - readers to write the problem

A data file that can be shared by multiple processes, the process requires only read files become "Reader process", other processes called "Writer process", allows multiple processes to read, but does not allow a reading process and other processes with Writer or write.

reader:

Readers in question, the readcount as a mutex, before executing readcount and reduction or every point p Gaga operation.

RMutex = semaphore 1 , wmutex = 1 ;
 // RMutex semaphore Reader processes to ensure more access to exclusive resources, where the readcount as the exclusive resource 
 // wmutex semaphore mutex to ensure that the process of reading and writing   
int readcount = 0 ; // number was reading process 
void Reader () {
     the while ( 1 ) { 
        the wait (RMutex); 
        IF (readercount == 0 ) 
        the wait (wmutex); // only when there is no process to read, execute p operation, not allow other processes to write,
                      // because the semaphore is 1, the value of the semaphore after performing the operation becomes p 0 
        the ReadCounter ++ ; 
        signal (RMutex); 
        ... 
        ...
        the wait (RMutex);
        ReadCount - ;
         IF (ReadCount == 0 ) // represents the last reader, the reader takes up no 
        Signal (wmutex); 
        Signal (RMutex); 
    } 
}

Writer:

wmutex = semaphore . 1 ; // wmutex ensure mutual exclusion semaphore read process   
void Writer () {
     the while ( . 1 ) { 
        the wait (wmutex); // check whether it was read or write 
        a write operation; 
        Signal (wmutex); 
    } 
}

 

 

 

 

 

Guess you like

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