Readers first:
- Reader Process Execution:
- No other reader to write, direct execution
- There are written and so on, but other readers are reading and reading directly
- There are written by the writer, waiting
- Writer Process Execution:
- No other reader and writer, direct execution
- 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:
- If no writer waits at this time, execute directly
- If there is a writer waiting, then wait
- Writer Process Execution:
- If there is no other writer, then execute
- 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