(I) Problem descr iption
There are two groups of concurrent processes, readers and writers, sharing a file. When two or more readers access the Shared data at the same time, there will be no side effects. However, if one writer and other processes (readers or writers) access the Shared data at the same time, it may cause errors of inconsistent data. So requirements: allow multiple readers to perform read operations on the file at the same time; Only one writer is allowed to write information to the file; (3) Any writer shall not allow any other reader or writer to work until he/she has finished writing; (4) write to execute write operation before, should let the existing reader and write all exit.
(2) Problem analysis
Unlike a consumer process, a reader process does not empty or change data after reading it. This allows multiple readers to access the Shared data simultaneously. The reading and writing processes share data at the same time, which can cause problems with inconsistent data read out. Two write processes share data at the same time, which can cause problems with data error overwriting
There are two types of processes: write processes and read processes
Mutually exclusive relationship: write process - write process, write process - read process. There is no mutual exclusion between readers and processes.
Writer contention and any process are mutually exclusive. You can set a mutex, rW, to indicate if another process is accessing the Shared file at this time. Before the write process accesses the Shared file, it can perform a P operation and then a V operation to release the lock on the Shared file after it has finished accessing the Shared file.
The reader process and the writer process are also mutually exclusive, so before the reader accesses the Shared file, the mutex rW needs to be locked by P operation and unlocked by V operation after reading the file.
Although the reader and the writer are mutually exclusive, this sharing can be accessed simultaneously between readers and readers. If the first reader process P before attempting to read the file, the second reader process will be blocked if it tries to do the same, so there is no way for both readers to access the Shared file.
Logically, the PV operation on rW, a mutex, can be understood as locking and unlocking a file. So you can set up a plastic variable called count to record how many readers are currently accessing this file. If the first reader tries to access the file, it should be locked. If the second reader tries to access the file, the second reader should not be allowed to lock the file again after judgment. The second reader should be allowed to skip the P operation and access the file directly.
(3) Problem realization
If two readers execute concurrently, then if count=0, both processes may perform P(rW), causing the second reader to block.
The problem is that the count variable cannot be checked and assigned at the same time, so another mutex can be set to ensure that count access by each reader process is mutually exclusive.