semaphore rw = 1;//对共享文件的互斥访问
semaphore mutexr = 1;//保证同时仅有一个读进程访问countr
semaphore mutexw = 1;//保证同时仅有一个写进程访问countw
semaphore prior = 1;//保证写进程优先
int countr = 0;//读进程数量
int countw = 0;//写进程数量
writer(){
while(1){
p(mutexw);
if(countw = 0){
p(prior);
countw += 1;
}
v(mutexw);
p(rw);
写文件;
v(rw);
p(mutexw);
countw -= 1;
if(countw = 0){
v(prior);
}
v(mutexw);
}
}
reader(){
while(1){
p(prior);
p(mutexr);
countr += 1;
if(countr == 1){
p(rw);
}
v(mutexr);
v(prior);
读文件;
p(mutexr);
countr -= 1;
if(countr == 0){
v(rw);
}
v(mutexr);
}
}
读者写者问题写优先
于 2022-11-17 15:47:21 首次发布