I. Description of the problem
Claim:
1, allows multiple readers can simultaneously perform a read operation on the file.
2, only allows a writer to write the file information.
3, a writer does not allow any other reader or writer to work before the completion of the write operation.
4, writers perform before a write operation, should make the existing readers and writers all out.
Second, the problem analysis
The core issue readers to write the question is how to handle multiple readers can simultaneously read the file.
Third, how to achieve
RW = semaphore . 1 ; // implement exclusive access to the file int count = 0 ; semaphore the mutex = . 1 ; // implement exclusive access to the count variable int I = 0 ; Writer () { the while ( . 1 ) { P ( rw); // before writing the "lock" write file V (rw); // after writing "unlock" } } Reader () { the while ( 1 ) { P (mutex); // each reading process exclusive access count IF (COUNT == 0 ) //The first reading process responsible for the "lock" { P (rw); } COUNT ++; // process accesses the file number +1 V (mutex); read file P (mutex); // each reading process exclusive access cOUNT count--; // process accesses the file number -1 IF (cOUNT == 0 ) // last process is responsible for reading "unlock" { V (rw); } V (mutex); } }