问题 问答题


多个进程共享一个文件,其中只读文件的称为读者,其中只写文件的称为写者。读者可以同时读,但是写者只能独立地写。请问:

用P、V操作写出其同步算法。

答案

参考答案:

解析:用P、V操作的同步算法如下: Begin Integer mutex1,mutex2,rc; mutex1:=1; mutex2:=1; rc:=0; Cobegin Reader; Begin P(mutex1); rc:=rc+1; if rc_1 then p(mutex2); V(mutex1); Reading the file; rc:=rc-1; if rc=0 then V(mutex2); V(mutex1); End Writer: Begin p(mutex2); Writing the file; V(mutex2); End Coend End

单项选择题
单项选择题