文章
166
粉丝
0
获赞
0
访问
10.1k
seats = 10 // 表示剩余座位数
mutex = 1 // 互斥锁,保护共享变量
customersWaiting = 0 // 当前等待的顾客数
nextCustomer = 0 // 排队等待的顾客编号
calledCustomerID = -1 // 正在叫号的顾客编号
served = 0 // 作用于服务完成的同步
顾客流程:
1. 加入队列:
P(mutex)
if (seats > 0) {
seats = seats - 1
customersWaiting = customersWaiting + 1
共享变量记录:顾客编号, 以及等待状态
} else {
// 座位满,顾客离开或等待
退出
}
V(mutex)
2. 等待叫号通知(条件变量或信号量):
等待叫号信号
获得叫号后,开始“获得服务”
3. 被服务完毕:
顾客离开,释放座位:
P(mutex)
seats = seats + 1
V(mutex)
营业员流程:
while (true) {
P(mutex)
if (customersWaiting > 0) {
叫号:选取一个等待的顾客(可能用队列)
叫号信号发给该顾客
customersWaiting = customersWaiting - 1
}
V(mutex)
// 为叫到的顾客服务(同步等待)
// 之后循环,等待下一轮叫号
}
评分及理由
(1)得分及理由(满分8分)
得分:5分
理由:
登录后发布评论
暂无评论,来抢沙发