<
理发师
>
semphore mutex_i
=
1
,consumer
=
0
,baber
=
0
;
int
i
=
0
;
seat
=
n
consumer
()
{
while
(
1
)
{
P
(mutex_i);
if
(i
<
n)
//有座位
{
i
++
;
V
(costomer);
V
(mutex_i);
P
()
}
else
{
走
V
(mutex_i);
}
}
}
<!--
叫号问题
-->
mutex_i
<!--
防止顾客拿到一个号
-->
mutex_j
<!--
防止
-->
semphore mutex_i
=
1
;
consumer
()
{
取号
P
(mutex_i)
i
++
;
V
(mutex_i)
waiting...
}
seller
()
{
P
(mutex_j)
if
(j
<
i)
{
selling...
j
++
V
(mutex_j)
}
else
{
V
(mutex_j)
休息。。。
}
}
<!--
银行
-->
semphore mutex_i;
consumer
()
{
P
(mutex_i)
if
(i
<
10
)
{
i
++
}
}
<!--
前趋图
-->
procecss
P1
()
{
P
(s1)
...
V
(s2)
V
(s3)
//释放 2 个
}
P2
()
{
P
(s2)
...
V
(s4)
}
P3
()
{
P
(s3)
...
V
(s4)
}
P4
()
{
P
(s4)
P
(s4)
//只有s4=2才可以
...
}
<!--
十字路口
-->
semaphore s
=
1
;
process1
()
{
while
(
1
)
{
P
(s)
...
V
(s)
}
}
process2
()
{
while
(
1
)
{
P
(s);
...
V
(s);
}
}
semaphore s
=
100
// 初始可以进入的人的个数
Process_i
()
{
P
(s);
进入售票厅
出售票厅
V
(s);
}
假设初始的车是停 门关闭
semaphore run
=
1
//可以跑
semphore open
=
0
diver
()
//司机进程
{
while
(
1
)
{
P
(run)
开车..
停车..
V
(open)
}
}
seller
()
{
while
(
1
)
{
P
(open)
开门...
上乘客
关门...
V
(run)
}
}
<!--
老和尚喝水
-->
semaphore empty
=
n,full
=
0
;
well
=
1
//互斥访问井
pail
=
3
//三个水桶
vat
=
1
//水缸
取水()
{
while
(
1
)
{
P
(empty)
//井中取水
P
(pail)
P
(well)
井中打水
V
(well)
P
(vat)
倒入缸中
V
(vat)
V
(pail)
V
(full)
}
}
喝水()
{
while
(
1
)
{
P
(full)
P
(pail)
P
(vat)
//互斥水缸
取水...
V
(vat)
V
(pail)
V
(empty)
}
}
<!--
南大三个消费者生产者问题
-->
semaphore empty
=
n,full
=
0
,mutex
=
1
;
R
()
//读入者
{
P
(empty)
P
(metex)
读入缓冲区...
i
++
;
V
(metex)
V
(full_1)
}
M
()
//检验" "->";"
{
P
(full_1)
P
(metex)
if
(B[i]
==
" "
)
B[i]
=
";"
V
(metex)
V
(full_2)
}
P
()
//打印者
{
P
(full_2)
P
(mutex)
cout
<<
B[i]
V
(mutex)
V
(empty)
}
<!--
理发师问题
-->
int
wait
=
0
;
//等待的人数
int
chairs
=
n;
//椅子
semaphore s_barber
=
1
//理发师这个资源可用
semphore s_customer
=
0
;
mutex
=
1
;
//互斥的座位的访问
Customer
()
{
while
(
1
)
{
P
(mutex)
waiting
++
;
if
(waiting
>
chairs)
//先看座位满不满
{
离开...
V
(mutex);
}
else
{
waiting
++
;
//坐下
V
(mutex);
P
(s_barber)
//request a haircut
V
(s_consumer);
}
}
}
Barber
()
{
while
(
1
)
{
P
(s_customer);
P
(mutex)
waiting
--
;
剪发...
V
(mutex)
V
(s_barber);
}
}
<!--
吸烟者问题的变型:零件问题
-->
semaphore offer1
=
0
//AB
offer2
=
0
//BC
offer3
=
0
//AC
finish
=
0
;
//并未完成一次操作
Master
()
//师傅进程
{
int
i
=
random
()
%
3
+
1
;
//写出随机函数
if
(i
==
1
)
{
V
(offer1);
}
else
if
(i
==
2
)
{
V
(offer2)
}
else
{
V
(offer3)
}
放到desk上...
P
(finish)
}
A
()
{
while
(
1
)
P
(offer2)
加工
V
(finish)
}
B
()
{
while
(
1
)
P
(offer3)
加工
V
(finish)
}
C
()
{
while
(
1
)
P
(offer1)
加工....
V
(finish)
}
<!
复旦
>
文件必须有2个经理签字 方可发出
semphore sem
=
2
//还需要2个经理签字
A
()
{
while
(
1
)
{
P
(mutex)
//互斥访问这2次 必须一起签完
P
(sem)
P
(sem)
//签字2次
V
(mutex)
发出
V
(sem)
V
(sem)
}
}
<!--
北京工业大学 标志块更改
-
生产者消费者
-->
semaphore empty
=
64
, full
=
0
//初始64bit均是不可消费的
mutex
=
1
;
flag[
64
];
PUT
()
//生产者进程
{
P
(empty)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
1
)
{
flag[i]
=
0
;
break
;
}
}
V
(mutex)
V
(full)
}
GET
()
//消费者进程
{
P
(full)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
0
)
{
flag[i]
==
1
;
break
;
}
}
V
(mutex)
V
(empty)
}
<!--
北大 读写进程
-->
semaphore write
=
1
;
semaphore read
=
1
;
semaphore mutex
=
1
;
int
count
=
0
;
//读者的个数
P2
()
//只写
{
P
(write)
write...
V
(write)
}
P1
()
//只读
{
while
(
1
)
{
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
}
}
//读和写进程
P3
()
{
while
(
1
)
{
//先读
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
//后写
P
(write)
writing...
V
(write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist
=
3
;
//当前的牙医资源可用
semaphore mutex
=
1
;
//互斥访问座位
int
waiting
=
0
;
//起始没人等候
semaphore paient
=
0
;
//等待就医的病人
Patient
()
{
P
(mutex)
if
(waiting
>=
10
)
//先检查座位
{
离开
V
(mutex)
}
else
{
坐下
waiting
++
V
(mutex)
P
(dentist)
//请求就医唤醒牙医
V
(pateint)
}
}
dentist
()
{
P
(patient)
P
(mutex)
waiting
--
;
//等候顾客减少一个
V
(mutex);
看病....
V
(dentist)
}
//北大酒吧听音乐问题
semaphore offer1
=
0
//电池+音乐磁带
semaphore offer2
=
0
//电池+随身听
semaphore offer3
=
0
//随身听+音乐磁带
semaphore finish
=
0
;
//是否完成听歌
Boss
()
//老板进程
{
while
(
1
)
{
int
i
=
radom
()
%
3
+
1
;
//产生随机数1-3
if
(i
==
1
)
{
V
(offer1)
放置电池
+
音乐磁带....
}
else
if
(i
==
2
)
{
V
(offer2)
放置电池
+
随身听...
}
else
{
V
(offer3)
放置随身听
+
音乐磁带
}
//执行顺序是在这里转到:Listener进程
P
(finish);
//这里的P操作为等待听完
}
}
Listener1
()
{
while
(
1
)
{
P
(offer1)
//消耗资源1
listen to the music...
V
(finish)
//宣告结束听歌
}
}
Listener2
()
{
while
(
1
)
{
P
(offer2)
//消耗2类资源
listen to the music...
V
(finish)
}
}
Listener3
()
{
while
(
1
)
{
P
(offer3)
listen to the music....
V
(finish)
}
}
//华中科技大
North
()
{
P
(mutex)
go thourgh the bridge...
V
(mutex)
}
South
()
{
P
(mutex)
go through the street...
V
(mutex)
}
//可以过多辆车
semaphore bridge
=
1
;
//桥的信号量
semaphore mutex_north
=
1
;
//互斥信号量
semophore mutex_south
=
1
;
int
count
=
0
;
North
()
{
P
(mutex_north)
if
(count
==
0
)
P
(bridge)
// 将桥上锁
count
++
;
V
(mutex_north)
go though the bridge...
P
(mutex_north)
count
--
;
if
(count
==
0
)
V
(brigde);
V
(mutex_north)
}
South
()
{
P
(mutex_south)
if
(count
==
0
)
P
(bridge)
count
++
;
V
(mutex_south)
go through the bridge...
P
(mutex_south)
count
--
;
if
(count
==
0
)
V
(bridge)
V
(mutex_south)
}
//南大 存钱取钱流程
semaphore mutex
=
1
;
//互斥访问账户的信号量
semphore take save;
save
()
{
while
(
1
)
{
P
(mutex)
m1
=
amount
m1
=+
10
amount
=+
m1
V
(mutex)
V
(take)
//可以取钱
}
}
take
()
{
while
(
1
)
{
P
(take)
//请求取钱
P
(mutex)
m2
=
amount
m2
=+
10
amount
=
m2
V
(mutex)
}
}
<
理发师
>
semphore mutex_i
=
1
,consumer
=
0
,baber
=
0
;
int
i
=
0
;
seat
=
n
consumer
()
{
while
(
1
)
{
P
(mutex_i);
if
(i
<
n)
//有座位
{
i
++
;
V
(costomer);
V
(mutex_i);
P
()
}
else
{
走
V
(mutex_i);
}
}
}
<!--
叫号问题
-->
mutex_i
<!--
防止顾客拿到一个号
-->
mutex_j
<!--
防止
-->
semphore mutex_i
=
1
;
consumer
()
{
取号
P
(mutex_i)
i
++
;
V
(mutex_i)
waiting...
}
seller
()
{
P
(mutex_j)
if
(j
<
i)
{
selling...
j
++
V
(mutex_j)
}
else
{
V
(mutex_j)
休息。。。
}
}
<!--
银行
-->
semphore mutex_i;
consumer
()
{
P
(mutex_i)
if
(i
<
10
)
{
i
++
}
}
<!--
前趋图
-->
procecss
P1
()
{
P
(s1)
...
V
(s2)
V
(s3)
//释放 2 个
}
P2
()
{
P
(s2)
...
V
(s4)
}
P3
()
{
P
(s3)
...
V
(s4)
}
P4
()
{
P
(s4)
P
(s4)
//只有s4=2才可以
...
}
<!--
十字路口
-->
semaphore s
=
1
;
process1
()
{
while
(
1
)
{
P
(s)
...
V
(s)
}
}
process2
()
{
while
(
1
)
{
P
(s);
...
V
(s);
}
}
semaphore s
=
100
// 初始可以进入的人的个数
Process_i
()
{
P
(s);
进入售票厅
出售票厅
V
(s);
}
假设初始的车是停 门关闭
semaphore run
=
1
//可以跑
semphore open
=
0
diver
()
//司机进程
{
while
(
1
)
{
P
(run)
开车..
停车..
V
(open)
}
}
seller
()
{
while
(
1
)
{
P
(open)
开门...
上乘客
关门...
V
(run)
}
}
<!--
老和尚喝水
-->
semaphore empty
=
n,full
=
0
;
well
=
1
//互斥访问井
pail
=
3
//三个水桶
vat
=
1
//水缸
取水()
{
while
(
1
)
{
P
(empty)
//井中取水
P
(pail)
P
(well)
井中打水
V
(well)
P
(vat)
倒入缸中
V
(vat)
V
(pail)
V
(full)
}
}
喝水()
{
while
(
1
)
{
P
(full)
P
(pail)
P
(vat)
//互斥水缸
取水...
V
(vat)
V
(pail)
V
(empty)
}
}
<!--
南大三个消费者生产者问题
-->
semaphore empty
=
n,full
=
0
,mutex
=
1
;
R
()
//读入者
{
P
(empty)
P
(metex)
读入缓冲区...
i
++
;
V
(metex)
V
(full_1)
}
M
()
//检验" "->";"
{
P
(full_1)
P
(metex)
if
(B[i]
==
" "
)
B[i]
=
";"
V
(metex)
V
(full_2)
}
P
()
//打印者
{
P
(full_2)
P
(mutex)
cout
<<
B[i]
V
(mutex)
V
(empty)
}
<!--
理发师问题
-->
int
wait
=
0
;
//等待的人数
int
chairs
=
n;
//椅子
semaphore s_barber
=
1
//理发师这个资源可用
semphore s_customer
=
0
;
mutex
=
1
;
//互斥的座位的访问
Customer
()
{
while
(
1
)
{
P
(mutex)
waiting
++
;
if
(waiting
>
chairs)
//先看座位满不满
{
离开...
V
(mutex);
}
else
{
waiting
++
;
//坐下
V
(mutex);
P
(s_barber)
//request a haircut
V
(s_consumer);
}
}
}
Barber
()
{
while
(
1
)
{
P
(s_customer);
P
(mutex)
waiting
--
;
剪发...
V
(mutex)
V
(s_barber);
}
}
<!--
吸烟者问题的变型:零件问题
-->
semaphore offer1
=
0
//AB
offer2
=
0
//BC
offer3
=
0
//AC
finish
=
0
;
//并未完成一次操作
Master
()
//师傅进程
{
int
i
=
random
()
%
3
+
1
;
//写出随机函数
if
(i
==
1
)
{
V
(offer1);
}
else
if
(i
==
2
)
{
V
(offer2)
}
else
{
V
(offer3)
}
放到desk上...
P
(finish)
}
A
()
{
while
(
1
)
P
(offer2)
加工
V
(finish)
}
B
()
{
while
(
1
)
P
(offer3)
加工
V
(finish)
}
C
()
{
while
(
1
)
P
(offer1)
加工....
V
(finish)
}
<!
复旦
>
文件必须有2个经理签字 方可发出
semphore sem
=
2
//还需要2个经理签字
A
()
{
while
(
1
)
{
P
(mutex)
//互斥访问这2次 必须一起签完
P
(sem)
P
(sem)
//签字2次
V
(mutex)
发出
V
(sem)
V
(sem)
}
}
<!--
北京工业大学 标志块更改
-
生产者消费者
-->
semaphore empty
=
64
, full
=
0
//初始64bit均是不可消费的
mutex
=
1
;
flag[
64
];
PUT
()
//生产者进程
{
P
(empty)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
1
)
{
flag[i]
=
0
;
break
;
}
}
V
(mutex)
V
(full)
}
GET
()
//消费者进程
{
P
(full)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
0
)
{
flag[i]
==
1
;
break
;
}
}
V
(mutex)
V
(empty)
}
<!--
北大 读写进程
-->
semaphore write
=
1
;
semaphore read
=
1
;
semaphore mutex
=
1
;
int
count
=
0
;
//读者的个数
P2
()
//只写
{
P
(write)
write...
V
(write)
}
P1
()
//只读
{
while
(
1
)
{
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
}
}
//读和写进程
P3
()
{
while
(
1
)
{
//先读
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
//后写
P
(write)
writing...
V
(write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist
=
3
;
//当前的牙医资源可用
semaphore mutex
=
1
;
//互斥访问座位
int
waiting
=
0
;
//起始没人等候
semaphore paient
=
0
;
//等待就医的病人
Patient
()
{
P
(mutex)
if
(waiting
>=
10
)
//先检查座位
{
离开
V
(mutex)
}
else
{
坐下
waiting
++
V
(mutex)
P
(dentist)
//请求就医唤醒牙医
V
(pateint)
}
}
dentist
()
{
P
(patient)
P
(mutex)
waiting
--
;
//等候顾客减少一个
V
(mutex);
看病....
V
(dentist)
}
//北大酒吧听音乐问题
semaphore offer1
=
0
//电池+音乐磁带
semaphore offer2
=
0
//电池+随身听
semaphore offer3
=
0
//随身听+音乐磁带
semaphore finish
=
0
;
//是否完成听歌
Boss
()
//老板进程
{
while
(
1
)
{
int
i
=
radom
()
%
3
+
1
;
//产生随机数1-3
if
(i
==
1
)
{
V
(offer1)
放置电池
+
音乐磁带....
}
else
if
(i
==
2
)
{
V
(offer2)
放置电池
+
随身听...
}
else
{
V
(offer3)
放置随身听
+
音乐磁带
}
//执行顺序是在这里转到:Listener进程
P
(finish);
//这里的P操作为等待听完
}
}
Listener1
()
{
while
(
1
)
{
P
(offer1)
//消耗资源1
listen to the music...
V
(finish)
//宣告结束听歌
}
}
Listener2
()
{
while
(
1
)
{
P
(offer2)
//消耗2类资源
listen to the music...
V
(finish)
}
}
Listener3
()
{
while
(
1
)
{
P
(offer3)
listen to the music....
V
(finish)
}
}
//华中科技大
North
()
{
P
(mutex)
go thourgh the bridge...
V
(mutex)
}
South
()
{
P
(mutex)
go through the street...
V
(mutex)
}
//可以过多辆车
semaphore bridge
=
1
;
//桥的信号量
semaphore mutex_north
=
1
;
//互斥信号量
semophore mutex_south
=
1
;
int
count
=
0
;
North
()
{
P
(mutex_north)
if
(count
==
0
)
P
(bridge)
// 将桥上锁
count
++
;
V
(mutex_north)
go though the bridge...
P
(mutex_north)
count
--
;
if
(count
==
0
)
V
(brigde);
V
(mutex_north)
}
South
()
{
P
(mutex_south)
if
(count
==
0
)
P
(bridge)
count
++
;
V
(mutex_south)
go through the bridge...
P
(mutex_south)
count
--
;
if
(count
==
0
)
V
(bridge)
V
(mutex_south)
}
//南大 存钱取钱流程
semaphore mutex
=
1
;
//互斥访问账户的信号量
semphore take save;
save
()
{
while
(
1
)
{
P
(mutex)
m1
=
amount
m1
=+
10
amount
=+
m1
V
(mutex)
V
(take)
//可以取钱
}
}
take
()
{
while
(
1
)
{
P
(take)
//请求取钱
P
(mutex)
m2
=
amount
m2
=+
10
amount
=
m2
V
(mutex)
}
}
<
理发师
>
semphore mutex_i
=
1
,consumer
=
0
,baber
=
0
;
int
i
=
0
;
seat
=
n
consumer
()
{
while
(
1
)
{
P
(mutex_i);
if
(i
<
n)
//有座位
{
i
++
;
V
(costomer);
V
(mutex_i);
P
()
}
else
{
走
V
(mutex_i);
}
}
}
<!--
叫号问题
-->
mutex_i
<!--
防止顾客拿到一个号
-->
mutex_j
<!--
防止
-->
semphore mutex_i
=
1
;
consumer
()
{
取号
P
(mutex_i)
i
++
;
V
(mutex_i)
waiting...
}
seller
()
{
P
(mutex_j)
if
(j
<
i)
{
selling...
j
++
V
(mutex_j)
}
else
{
V
(mutex_j)
休息。。。
}
}
<!--
银行
-->
semphore mutex_i;
consumer
()
{
P
(mutex_i)
if
(i
<
10
)
{
i
++
}
}
<!--
前趋图
-->
procecss
P1
()
{
P
(s1)
...
V
(s2)
V
(s3)
//释放 2 个
}
P2
()
{
P
(s2)
...
V
(s4)
}
P3
()
{
P
(s3)
...
V
(s4)
}
P4
()
{
P
(s4)
P
(s4)
//只有s4=2才可以
...
}
<!--
十字路口
-->
semaphore s
=
1
;
process1
()
{
while
(
1
)
{
P
(s)
...
V
(s)
}
}
process2
()
{
while
(
1
)
{
P
(s);
...
V
(s);
}
}
semaphore s
=
100
// 初始可以进入的人的个数
Process_i
()
{
P
(s);
进入售票厅
出售票厅
V
(s);
}
假设初始的车是停 门关闭
semaphore run
=
1
//可以跑
semphore open
=
0
diver
()
//司机进程
{
while
(
1
)
{
P
(run)
开车..
停车..
V
(open)
}
}
seller
()
{
while
(
1
)
{
P
(open)
开门...
上乘客
关门...
V
(run)
}
}
<!--
老和尚喝水
-->
semaphore empty
=
n,full
=
0
;
well
=
1
//互斥访问井
pail
=
3
//三个水桶
vat
=
1
//水缸
取水()
{
while
(
1
)
{
P
(empty)
//井中取水
P
(pail)
P
(well)
井中打水
V
(well)
P
(vat)
倒入缸中
V
(vat)
V
(pail)
V
(full)
}
}
喝水()
{
while
(
1
)
{
P
(full)
P
(pail)
P
(vat)
//互斥水缸
取水...
V
(vat)
V
(pail)
V
(empty)
}
}
<!--
南大三个消费者生产者问题
-->
semaphore empty
=
n,full
=
0
,mutex
=
1
;
R
()
//读入者
{
P
(empty)
P
(metex)
读入缓冲区...
i
++
;
V
(metex)
V
(full_1)
}
M
()
//检验" "->";"
{
P
(full_1)
P
(metex)
if
(B[i]
==
" "
)
B[i]
=
";"
V
(metex)
V
(full_2)
}
P
()
//打印者
{
P
(full_2)
P
(mutex)
cout
<<
B[i]
V
(mutex)
V
(empty)
}
<!--
理发师问题
-->
int
wait
=
0
;
//等待的人数
int
chairs
=
n;
//椅子
semaphore s_barber
=
1
//理发师这个资源可用
semphore s_customer
=
0
;
mutex
=
1
;
//互斥的座位的访问
Customer
()
{
while
(
1
)
{
P
(mutex)
waiting
++
;
if
(waiting
>
chairs)
//先看座位满不满
{
离开...
V
(mutex);
}
else
{
waiting
++
;
//坐下
V
(mutex);
P
(s_barber)
//request a haircut
V
(s_consumer);
}
}
}
Barber
()
{
while
(
1
)
{
P
(s_customer);
P
(mutex)
waiting
--
;
剪发...
V
(mutex)
V
(s_barber);
}
}
<!--
吸烟者问题的变型:零件问题
-->
semaphore offer1
=
0
//AB
offer2
=
0
//BC
offer3
=
0
//AC
finish
=
0
;
//并未完成一次操作
Master
()
//师傅进程
{
int
i
=
random
()
%
3
+
1
;
//写出随机函数
if
(i
==
1
)
{
V
(offer1);
}
else
if
(i
==
2
)
{
V
(offer2)
}
else
{
V
(offer3)
}
放到desk上...
P
(finish)
}
A
()
{
while
(
1
)
P
(offer2)
加工
V
(finish)
}
B
()
{
while
(
1
)
P
(offer3)
加工
V
(finish)
}
C
()
{
while
(
1
)
P
(offer1)
加工....
V
(finish)
}
<!
复旦
>
文件必须有2个经理签字 方可发出
semphore sem
=
2
//还需要2个经理签字
A
()
{
while
(
1
)
{
P
(mutex)
//互斥访问这2次 必须一起签完
P
(sem)
P
(sem)
//签字2次
V
(mutex)
发出
V
(sem)
V
(sem)
}
}
<!--
北京工业大学 标志块更改
-
生产者消费者
-->
semaphore empty
=
64
, full
=
0
//初始64bit均是不可消费的
mutex
=
1
;
flag[
64
];
PUT
()
//生产者进程
{
P
(empty)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
1
)
{
flag[i]
=
0
;
break
;
}
}
V
(mutex)
V
(full)
}
GET
()
//消费者进程
{
P
(full)
P
(mutex)
for
(
int
i
=
0
;i
<
64
;i
++
)
{
if
(flag[i]
==
0
)
{
flag[i]
==
1
;
break
;
}
}
V
(mutex)
V
(empty)
}
<!--
北大 读写进程
-->
semaphore write
=
1
;
semaphore read
=
1
;
semaphore mutex
=
1
;
int
count
=
0
;
//读者的个数
P2
()
//只写
{
P
(write)
write...
V
(write)
}
P1
()
//只读
{
while
(
1
)
{
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
}
}
//读和写进程
P3
()
{
while
(
1
)
{
//先读
P
(mutex)
if
(count
==
0
)
P
(write)
//第一个关写门
count
++
;
V
(mutex)
reading...
//读完
P
(mutex)
count
--
;
if
(count
==
0
)
V
(write);
V
(mutex)
//后写
P
(write)
writing...
V
(write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist
=
3
;
//当前的牙医资源可用
semaphore mutex
=
1
;
//互斥访问座位
int
waiting
=
0
;
//起始没人等候
semaphore paient
=
0
;
//等待就医的病人
Patient
()
{
P
(mutex)
if
(waiting
>=
10
)
//先检查座位
{
离开
V
(mutex)
}
else
{
坐下
waiting
++
V
(mutex)
P
(dentist)
//请求就医唤醒牙医
V
(pateint)
}
}
dentist
()
{
P
(patient)
P
(mutex)
waiting
--
;
//等候顾客减少一个
V
(mutex);
看病....
V
(dentist)
}
//北大酒吧听音乐问题
semaphore offer1
=
0
//电池+音乐磁带
semaphore offer2
=
0
//电池+随身听
semaphore offer3
=
0
//随身听+音乐磁带
semaphore finish
=
0
;
//是否完成听歌
Boss
()
//老板进程
{
while
(
1
)
{
int
i
=
radom
()
%
3
+
1
;
//产生随机数1-3
if
(i
==
1
)
{
V
(offer1)
放置电池
+
音乐磁带....
}
else
if
(i
==
2
)
{
V
(offer2)
放置电池
+
随身听...
}
else
{
V
(offer3)
放置随身听
+
音乐磁带
}
//执行顺序是在这里转到:Listener进程
P
(finish);
//这里的P操作为等待听完
}
}
Listener1
()
{
while
(
1
)
{
P
(offer1)
//消耗资源1
listen to the music...
V
(finish)
//宣告结束听歌
}
}
Listener2
()
{
while
(
1
)
{
P
(offer2)
//消耗2类资源
listen to the music...
V
(finish)
}
}
Listener3
()
{
while
(
1
)
{
P
(offer3)
listen to the music....
V
(finish)
}
}
//华中科技大
North
()
{
P
(mutex)
go thourgh the bridge...
V
(mutex)
}
South
()
{
P
(mutex)
go through the street...
V
(mutex)
}
//可以过多辆车
semaphore bridge
=
1
;
//桥的信号量
semaphore mutex_north
=
1
;
//互斥信号量
semophore mutex_south
=
1
;
int
count
=
0
;
North
()
{
P
(mutex_north)
if
(count
==
0
)
P
(bridge)
// 将桥上锁
count
++
;
V
(mutex_north)
go though the bridge...
P
(mutex_north)
count
--
;
if
(count
==
0
)
V
(brigde);
V
(mutex_north)
}
South
()
{
P
(mutex_south)
if
(count
==
0
)
P
(bridge)
count
++
;
V
(mutex_south)
go through the bridge...
P
(mutex_south)
count
--
;
if
(count
==
0
)
V
(bridge)
V
(mutex_south)
}
//南大 存钱取钱流程
semaphore mutex
=
1
;
//互斥访问账户的信号量
semphore take save;
save
()
{
while
(
1
)
{
P
(mutex)
m1
=
amount
m1
=+
10
amount
=+
m1
V
(mutex)
V
(take)
//可以取钱
}
}
take
()
{
while
(
1
)
{
P
(take)
//请求取钱
P
(mutex)
m2
=
amount
m2
=+
10
amount
=
m2
V
(mutex)
}
}