经典读者写者问题
读者写者问题
1、读者优先
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| int readcount=0; semaphore RCSignal=1, fileSrc=1;
P(RCSignal); readcount++; if (readcount == 1) P(fileSrc); V(RCSignal); P(RCSignal); readcount--; if (readcount == 0) V(fileSrc); V(RCSignal);
P(fileSrc); V(fileSrc);
|
2、写者优先
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| int readcount=0, writecount=0; semaphore RCSignal=1, WCSignal=1, read=1, fileSrc;
P(read); P(RCSignal); readcount++; if(readcount == 1 ) P(fileSrc); V(RCSignal); V(read); P(RCSignal); readcount--; if (readcount == 0 ) V(fileSrc); V(RCSignal);
P(WCSingal); writecount++; if (writecount == 1 ) P(read); V(WCSingal); P(fileSrc); V(fileSrc); P(WCSingal); writecount --; if (writecount == 0) V(read); V(WCSingal);
|
参考资料
https://www.bilibili.com/video/av21593625 https://blog.csdn.net/c1194758555/article/details/52805918 https://blog.csdn.net/william_munch/article/details/84256690 (有问题?)