The writer's priority algorithm for reader-writer problem

Source: Internet
Author: User
Tags mutex semaphore

Transfer from http://blog.csdn.net/zoudaokou2006/article/details/3966694

The reader-writer problem is a classic process synchronization problem that is implemented with semaphores. In the system, a data set (such as a file or record) is shared by several concurrent processes, which are divided into two categories, one of which requires only re-operation, called "Reader", and the other requires write or modify operations, which we call "writer".

In general, for a data set, in order to ensure the integrity and correctness of the data, allowing multiple reader processes to access simultaneously, but does not allow a writer process with any other process (reader or writer) simultaneous access, and this kind of problem is called "reader-writer" problem.

Reader-preferred algorithms are described in the operating system-related books, which is the simplest solution: when no write process is accessing the shared dataset, the read process can access it, otherwise it must wait. The reader-first algorithm has the problem of a "starved writer" thread: As long as there are readers coming in, the writer will wait until all the readers have read and no new readers arrive to write the data set. In many cases we need to avoid "starving the writer", so we use the writer's preferred algorithm:

In the writer-first algorithm, the goals we want to achieve are:

1. To allow the reader and the writer, as well as the writer and writer of the question to be mutually exclusive to the same data set;

2. Each reader can access the data set at the same time when no write process arrives;

3. It is preferred to write when the reader and the writer are waiting for the interview.

An algorithm implementation:
We will use two different mutually exclusive signals to achieve mutual exclusion between the reader and the writer and the mutual exclusion between the writer process: to realize the mutual exclusion of each writer's question by mutual-exclusion Semaphore Wmutex, the mutual-exclusion Semaphore Rmutex realizes the mutual exclusion of the readers and the writer's question;
Set the two integer variables Wcount and rcount to record the number of written waits and the number of readers that are being read, respectively, because Wcount, rcount are shared variables, so set two mutually exclusive semaphores mutl and MUT2 to achieve the most mutually exclusive access for the process to the two.

A writer-first algorithm implemented with semaphore mechanisms such as:

[C-Sharp ] View plain Copyvar mut1,mut2,wmutex,fmutex:semaphore; Rcount,wcount:integer; MUT1:=mut2:=wmutex:=fmutex:=1; Rcount:=wcount:=0; //Fmutex--reader-writer Mutex//Wmutex-to- write mutex//MUT1--access to Rcount && competition Fmutex//Mut2 ---access to Wcountwriter:begin Wait (MUT1); Wcount:=wcount+1; If Wcount=1Then Wait (Fmutex);//If there are readers, the writer is blocked hereSignal (MUT1); Wait (Wmutex); 

Written by priority

Wait (MUT2);   Rcount:=rcount+1; If Rcount=1//

Read operation:

Wait (MUT2); Rcount:=rcount-1; If rcount=0//

Code Explanation:

1:
The readers and the writer apply for the semaphore in the order of MUT1 and Fmutex.

2:
After the writer obtains the Fmutex, until the last writer releases the Fmutex, the reader is blocked.
After the reader obtains the Fmutex, the writer is blocked until the last reader (not including the reader blocked by MUT1) releases the Fmutex.

3.
The reader is released immediately after applying to MUT1;
When the writer applies to the MUT1, it has been occupied until the application to Fmutex.

4. Summary
The function of Fmutex is the mutual exclusion between writer and reader;
MUT1 's extra role is to help the writer to compete first to Fmutex.

The writer's priority algorithm for reader-writer problem

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.