homework6 Version 0 |
|
👤 Author: by 1730854984qqcom 2020-11-06 13:32:36 |
wait(S){
while(S<=0)
,//no-op
S--;
}
signal(S){
S++;
}
int count=0;
semaphore mutex=1; //读者计数锁
semaphore rw=1; //资源访问锁
semaphore w=1; //读写公平抢占锁
writer()
{
while(1)
{
P(w);
P(rw);
writing sth;
V(rw);
V(w);
}
}
reader()
{
while(1)
{
P(w);
P(mutex);
if(count==0)
P(rw);
count++;
V(mutex);
V(w);
reading sth;
P(mutex);
count--;
if(count==0)
V(rw);
V(mutex);
}
}
Please login to reply. Login