Typical issue of Process Synchronization 1: reader writer (writers give priority to fair competition)

Source: Internet
Author: User
Source: http://dantvt.spaces.live.com reader-Writer Problem Writer Priority and fair competition multi-process mutual access to shared resources and Process Synchronization of the classic problem has a file F, access is required for multiple concurrent read and write processes. Requirements: (1) read/write mutex (2) Write/Write mutex (3) the record-type semaphore mechanism is used to allow simultaneous access by multiple read processes: Semaphore fmutex = 1, rdcntmutex = 1;
// Fmutex --> access to file; rdcntmutex --> access to readcount
Int readcount = 0;
Void reader (){
While (1 ){
Wait (rdcntmutex );
If (0 = readcount) Wait (fmutex );
Readcount = readcount + 1;
Signal (rdcntmutex );
// Do read operation...
Wait (rdcntmutex );
Readcount = readcount-1;
If (0 = readcount) signal (fmutex );
Signal (rdcntmutex );
}
}
Void writer (){
While (1 ){
Wait (fmutex );
// Do write operation...
Signal (fmutex );
}
} As long as other read processes see files being accessed, the read process can continue to read the files. The write process must wait until all read processes do not access the files, even if the write process may submit an application earlier than some read processes. Therefore, the above solution is the preferred solution for readers. If reading is frequently accessed, it may cause the write process to be unable to access files all the time... to solve the above problems, it is necessary to increase the priority of the write process. Here we add another queuing semaphore: queue. Before a read/write process accesses a file, it must be queued on this semaphore. by treating the read/write process differently, the priority of the write process can be increased. Add another writecount to record the total number of write access requests and processes being written: Semaphore fmutex = 1, rdcntmutex = 1, wtcntmutex = 1, queue = 1;
// Fmutex --> access to file; rdcntmutex --> access to readcount // wtcntmutex --> access to writecount
Int readcount = 0, writecount = 0;
Void reader (){
While (1 ){
Wait (Queue );
Wait (rdcntmutex );
If (0 = readcount) Wait (fmutex );
Readcount = readcount + 1;
Signal (rdcntmutex );
Signal (Queue );
// Do read operation...
Wait (rdcntmutex );
Readcount = readcount-1;
If (0 = readcount) signal (fmutex );
Signal (rdcntmutex );
}
}
Void writer (){
While (1 ){
Wait (wtcntmutex );
If (0 = writecount) Wait (Queue );
Writecount = writecount + 1;
Signal (wtcntmutex );
Wait (fmutex );
// Do write operation...
Signal (fmutex );
Wait (wtcntmutex );
Writecount = writecount-1;
If (0 = writecount) signal (Queue );
Signal (wtcntmutex );
}
} Each read process needs to apply for a queue semaphore at the very beginning, and then make the request before the read operation (so that the write process can apply for a queue at any time ). However, only the first write process needs to apply for a queue, which will remain occupied until all write processes are completed. This means that the read process queuing is prohibited as long as a write process applies, increasing the priority of the write process in disguise. Using similar ideas, we can achieve fair competition in the read/write process: Semaphore fmutex = 1, rdcntmutex = 1, queue = 1;
// Fmutex --> access to file; rdcntmutex --> access to readcount
Int readcount = 0;
Void reader (){
While (1 ){
Wait (Queue );
Wait (rdcntmutex );
If (0 = readcount) Wait (fmutex );
Readcount = readcount + 1;
Signal (rdcntmutex );
Signal (Queue );
// Do read operation...
Wait (rdcntmutex );
Readcount = readcount-1;
If (0 = readcount) signal (fmutex );
Signal (rdcntmutex );
}
}
Void writer (){
While (1 ){
Wait (Queue );
Wait (fmutex); signal (Queue );
// Do write operation...
Signal (fmutex );}
} The read process remains unchanged, and the write process waits for the queue semaphore before each write operation. Generally, only the first solution can be written in the textbook. After reading the last two methods, we can find that in the first solution, the fmutex semaphore is actually a dual identity. First, mutual access to the file is realized, second, it plays the same role as the queue of the queued semaphores, except that the sort can only give priority to readers. If you have read the last two solutions, you should have a clearer understanding.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.