我无法理解下面的这段代码,因为我并不是100%理解信号量的概念。你能解释一下这段代码中发生了什么吗?具体使用wait()和signal()函数吗?
共享数据
semaphore mutexR , mutexW, writePending , readBlock , writeBlock;
int readCount , writeCount ;
mutexR = 1 , mutexW = 1 , writePending = 1 ,
readBlock = 1 , writeBlock = 1 ;
readCount = 0 , writeCount = 0 ;
•作家流程
wait (mutexW ) ;
writeCount++:
if ( writeCount == 1 ) {
wait ( readBlock ) ;
}
signal (mutexW ) ;
wait ( write Block ) ;
/∗ Do Writing /Make Reservation etc ∗/
signal ( write Block ) ;
wait (mutexW ) ;
writeCount −−;
if ( writeCount == 0 ){
signal ( readBlock ) ;
}
signal (mutexW ) ;
读者流程
wait ( writePending ) ;
wait ( readBlock ) ;
wait (mutexR ) ;
readCount++;
if ( readCount == 1 ){
wait ( write Block ) ;
}
signal (mutexR ) ;
signal ( readBl ock ) ;
signal ( writePending ) ;
/∗Do reading /Browse etc ∗/
wait (mutexR ) ;
readCount−−;
if ( readCount == 0 ){
signal ( write Block ) ;
}
signal (mutexR ) ;