semaphore reader = 0; #记录读者个数
semaphore writer = 1; #记录空闲的写者个数
writer(){
while(1){ #当读者为0时,进行写操作
if(reader.value == 0 ){
P(writer);
writing;
V(writer);
}
}
}
reader(){
while(1){ #当写者为1时,进行读操作
if(writer.value == 1 ){
V(reader);
reading;
P(reader);
}
}
}
想问问为什么操作系统里的读者写者问题一定要定义一个count变量,能像我这个代码吗,我感觉好像没什么问题。。。
该文描述了一个使用信号量实现的并发控制模型,其中semaforereader用于记录读者数量,semaphorewriter记录空闲的写者。当没有读者时,写者可以执行写操作;当没有写者时,读者可以执行读操作。P和V操作用于协调读者和写者的活动,确保资源的安全访问。

被折叠的 条评论
为什么被折叠?



