Reader question (semaphore)

Source: Internet
Author: User

Reader Priority Algorithm:

Set two mutex semaphores: rwmutex is used to access Shared data that is mutually exclusive between the writer and other readers/writers.
Rmutex is used to access the reader counter readcount that is mutually exclusive to the reader.

VaR rwmutex, rmutex: semaphore: =; int readcount = 0; cobegin readeri begin // I = ,.... P (rmutex); readcount ++; If (readcount = 1) P (rwmutex); V (rmutex); read data; P (rmutex); readcount --; if (readcount = 0) V (rwmutex); V (rmutex); End
Writerj begin // j = 1, 2 ,.... P (rwmutex); write update; V (rwmutex); End coend

Writer Priority:

Condition:

1) Multiple readers can read at the same time
2) The writer must be mutually exclusive (only one writer is allowed to write, nor can the reader writer simultaneously)
3) The writer takes precedence over the reader (if a writer exists, the reader will have to wait and the writer will be given priority when waking up)

Solution 1: if the number of readers is fixed, we can use the following algorithm:

Rwmutex: used to access Shared data that is mutually exclusive between the writer and other readers/writers.
Rmutex: the initial value of this semaphore is set to 10, indicating that up to 10 reader processes can be simultaneously read.

VaR rwmutex, rmutex: semaphore: = 1, 10;
Cobegin
Readeri begin // I = 1, 2 ,....
P (rwmutex); // the reader and writer are mutually exclusive.
P (rmutex );
V (rwmutex); // release the read/write mutex semaphores and allow other read/write processes to access resources.
Read data;
V (rmutex );
End

Writerj begin // j = 1, 2 ,....
P (rwmutex );
For (I = 1; I <= 10; I ++) P (rmutex); // forbid new readers and wait for them to exit
Write update;
For (I = 1; I <= 10; I ++) V (rmutex); // restores the allowed rmutex value to 10
V (rwmutex );
End
Coend

 

Solution 2: If the number of readers is not fixed, use the following algorithm:

Set three mutex semaphores: rwmutex is used to access Shared data that is mutually exclusive between the writer and other readers/writers.
Rmutex is used to access the reader counter readcount nrmutex for the reader to wait for the reader to exit.

VaR rwmutex, rmutex, nrmutex: semaphore: = 1, 1;
Int readcount = 0;
Cobegin
Readeri begin // I = 1, 2 ,....
P (rwmutex );
P (rmutex );
Readcount ++;
If (readcount = 1) P (nrmutex); // read/write operations are mutually exclusive.
V (rmutex );
V (rwmutex); // release the read/write mutex semaphores in time and allow other read/write processes to apply for resources.
Read data;
P (rmutex );
Readcount --;
If (readcount = 0) V (nrmutex); // all readers exit and allow write update
V (rmutex );
End

Writerj begin // j = 1, 2 ,....
P (rwmutex); // mutually exclusive with other readers and writers
P (nrmutex); // If a reader is reading, wait for all readers to finish reading
Write update;
V (nrmutex); // allows the next new first reader to access and mutex write operations
V (rwmutex); // allows subsequent new readers and other writers
End
Coend

 

Related Article

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.