Very useful for reference! Reader Writer's question--the reader's priority and the writer's priority program analysis using the semaphore

Source: Internet
Author: User
Tags semaphore

Transfer from http://www.linuxso.com/linuxbiancheng/13098.html

It is important to note that Readcount,writecount is to be set as a shared variable (because it is a process), otherwise it may cause a deadlock

The so-called priority question, I think, is mainly reflected in the following two points:

1. When a low-priority process (thread) obtains a critical section, the high-priority process can quickly grab access from a low-priority process.

2. When a high-priority process obtains critical section access, the low priority waits until the idle time of the high-priority full access to gain access.

In these two points, the 1th must be, otherwise it can not reflect the meaning of priority.

The 2nd can be determined according to the needs of the program.

Program excerpt from: "Operating system essence and Design principle"

/* Program Readersandwriters*///Reader preferred, after I modified a bit int readcount = 0;semaphore x = 1,z = 1, Wsem = 1;voID Read ER () {while (TRUE) {semwait (x); readcount + +; if (readcount = = 1) semwait (Wsem); semsignal (x); Readunit ();//Read Data semwait (x); Readcount--;if (readcount = 0) semsignal (wsem); semsignal (x);}} void writer () {while (true) {semwait (z);//added, but this reduces the efficiency of the write operation, but satisfies the above 2 points. After the z signal is removed, only the 1th is satisfied, and Analysis Semwait (WSEM) can be analyzed. Writeunit ();//write Data semsignal (WSEM); semsignal (z);}}

Semwait in the program refers to the signal volume P operation, semsignal means V

First of all understand a bit if the initial value of a signal is 1, and in a function is the p,v operation is in pairs appear, then this semaphore is used to act as a mutex!

The semaphore x is used to guarantee the atomicity of the Readcount self-decrement operation and the IF condition statement operation.

To analyze why this program is read first?

1. Consider this situation: if the reader has access to the critical section, then when the reader is continuous (that is, the middle interval is small), that is, when a process is finished reading, another process has entered the critical section. At this point, any time in the critical section of the write process will be greater than one. Even if the write process, there can only be one writer blocked in the Wsem signal, the rest of the entire block in the Z signal ... Until a moment when no reader is in the critical section, there is a chance to write the critical section.

2. Consider a situation in which the write process has gained access to the critical section and the write process is dense. Can the read process prioritize access without waiting for the write process to complete?

The answer is yes. Write process a lot, then only one time can have a write process Access critical section. The remaining write processes are blocked in the Z-signal, queued and.   If the read process also wants to be accessed, the read process will block the queue in the Wsem signal, and only the read process will block in the Wsem. Note: When the write process at this time completes access to the critical section writeunit. The next sentence semsignal (Wsem), good, at this time the blocking in the Wsem queue of the read process to grab access to the critical section.

In summary: In the reader Priority program: The write process only waits for no reader to access the critical section of the moment, to get access, the read process can be accessed at any time.

The block has a maximum of one writer in the Wsem queue, and the rest of the writer blocks in the z signal. The Wsem queue is of course likely to have a limited number of writes (because the writer can get access within a very short time)

Written by priority

/*program readersandwriters*///Writer Precedence int readcount, Writecount;semaphore x = 1, y = 1, z = 1, Wsem = 1, Rsem = 1;void Read ER () {while (true) {semwait (z);//z signals are used to ensure that the blocking of the RSEM signal is at most one of the readers. The rest of the block is on Z. Semwait (Rsem); semwait (x);//guarantee The following 3-sentence operation of the atomic Readcount ++;//here, when the writer appears, because of the Rsem block, the value of readcount will not be greater than 1if (Readcount = = i)// I should be 1 to semwait (WSEM);//For the real execution of Readunit () when no one disturbs semsignal (x); semsignal (Rsem);//The time when the writer preemption access!semsignal (z); Readunit (); semwait (x); readcount--; Since the above readcount can only be 1, the following statement is bound to execute if (Readcount = = 0) semsignal (wsem); semsignal (x);}} void writer () {while (true) {semwait (y); Writecount ++;if (Writecount = = 1) semwait (RSEM);//The first writer blocks the place semsignal (y); Semwait (Wsem);//For the real execution of Writeunit () when no one disturbs writeunit (); semsignal (Wsem); semwait (y); Writecount--;if (writecount = 0)// The last writer to release Rsem, then the reader has the opportunity to visit, the reader also has the opportunity to semwait (WSEM)                                              , otherwise in no chance semwait (Wsem), the writer of course can continue semsignal (RSEM); semsignal (y);}}


As with the previous face, Y is used to protect the writecount of the writer from the atomic nature of the operation.

1. When the reader has access to the critical section, and the reader process is very dense (that is, many readers need access), how the writer grabs access.

When a reader is granted access, a process is executing the readuint operation. Similar to the one written in the previous reader's priority program, if there is no writer, then the reader can reach the critical section one after another. However, because of the mutual exclusion of the z signal and the RSEM signal before the readuint operation. Make the reader one only one through this section of code, the rest of the people block in the z signal. When entering the critical section, it is entered sequentially (but it is not necessarily a FIFO). Once a writer comes in, he blocks in the RSEM signal. At this time when the reader has just finished semsignal (Rsem), then the writer will get RSEM signal can continue to execute. Then the writer just waits for the reader to enter the critical section to be able to execute.

2. When the writer obtains access to the critical section, the reader can only get access to the critical section until the critical section is idle.

Because when the writer obtains the critical section, all readers are blocked in the z signal and the RSEM signal. And only the last writer accesses the critical section, Semsignal (Rsem), so that the only reader blocking in Rsem access to the critical section.

The function of the z signal is that when the reader obtains the permission, this time the writer does not have to wait for all the readers in front of him to visit. Because the z signal allows at most one reader to block in the Rsem signal queue

Reprint Annotated Source

(turn) is very useful for reference! Reader Writer's question--the reader's priority and the writer's priority program analysis using the semaphore

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.