【一道操作系统原理题,请高手作答】

来源:百度知道 编辑:UC知道 时间:2024/05/12 04:12:19
我们的教授姓韩,自称微软退休的工程师,他说只要做出下面一道题来的学生他在毕业的时候会推荐给微软,可是这道题市涉及编程的,我们都不会,请百度高手指教

用PV操作实现 读者----写者 问题,在PC系统里,有些文件是可以供若干进程共享的,假使有某个共享文件F,系统允许进程对文件F读或写,但是规定 1.多个进程可以同时读文件F, 2.在进程对文件F进行修改的时候不允许其他进程对文件进行读或写, 3.当有进程在读文件的时候不准任何进城去修改文件

设置互斥信号量wmutex 表示写者间、读者和写者间互斥
用readcount变量来记录读者数

Var rmutex,wmutex: semaphore:=1,1 ;
readcount :integer :=0 ;
begin
parbegin

reader:begin
repeat
P(rmutex)
if readcount=0 then P(wmutex);
readcount=readcount+1;
V(rmutex)
read text
P(rmutex)
readcount=readcount+1;
if readcount=0 then V(wmutex);
V(rmutex)
until false

writer:begin
repeat
P(wmutex);
write text;
V(wmutex);
until false
end

parend
end