Reader Writer's question

Source: Internet
Author: User
Tags semaphore

Readers first:

    • Reader Process Execution:
      1. No other reader to write, direct execution
      2. There are written and so on, but other readers are reading and reading directly
      3. There are written by the writer, waiting
    • Writer Process Execution:
      1. No other reader and writer, direct execution
      2. There are other readers who are waiting for

Pseudo Code Description:

//R: Semaphore, initial value of 1, for synchronizing between readers
MUTX: semaphore, initial value of 1, for mutually exclusive reader and writer, or writer and writer
RC: Used to count how many reader processes are currently in progress
voidReader () { while(TRUE) {P (R); RC= RC +1; if(rc = =1) P (mutex); V (R); Read Operation P (R); RC= RC-1; if(rc = =0) V (mutex); V (R); Other actions}}voidwriter () { while(TRUE) {P (mutex); Write operation V (mutex); }}

Write a priority (this is what I think out, not the standard answer, looking to point out the error):

    • Reader Process Execution:
      1. If no writer waits at this time, execute directly
      2. If there is a writer waiting, then wait
    • Writer Process Execution:
      1. If there is no other writer, then execute
      2. If there are other readers, wait

Pseudo Code Description:

//Mutex semaphore: initial value of 1, Control access to critical section//W semaphore: Initial value of 1 for synchronization between the writer//R semaphore: Initial value of 1 for synchronization between readers//RW semaphore: Initial value of 1 for synchronization between reader and writer//Rcount Initial value is 0: used to count the current number of readers//Wcount Initial value is 0: used to count how many people are currently writtenvoidReader () { while(true) {P (RW);                P (R); Rcount++ ; if(Rcount = =1) P (mutex);        V (R);            V (RW);            Read Operation P (R); Rcount-- ; if(Rcount = =0) V (mutex);    V (R); }}voidwriter () { while(true) {P (W); Wcount++ ; if(Wcount = =1) P (RW);        V (W);            P (Mutex) write operation V (mutex) p (W); Wcount-- ; if(Wcount = =0) V (RW);    V (W); }}

Reader Writer's question

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.