当前位置:网站首页>PV操作每日一题-橘子苹果问题(初阶版)
PV操作每日一题-橘子苹果问题(初阶版)
2022-06-10 10:14:00 【liangsena】
不得不说这个问题的变式还是蛮多的,后面还有一类独木桥问题,也是PV操作的宠儿。话不多说,搞起~
一、问题描述
桌上有一个盘子,每次只能放入一个水果,爸爸专门向盘子放入苹果,妈妈专门向盘子放入橘子;有一儿一女,儿子只吃橘子,女儿只吃苹果。请使用信号量和PV操作求解该问题。
二、问题求解
:
semaphore plate=1;
semaphore orange=0;
semaphore apple=0;
Father()
{
while(1)
{
P(plate);
放一个苹果;
V(apple);
}
}
Mom()
{
while(1)
{
P(plate);
放一个橘子;
V(orange);
}
}
Son()
{
while(1)
{
P(orange);
拿走盘中的橘子;
V(plate);
}
}
Daughter()
{
while(1)
{
P(apple);
拿走盘中的苹果;
V(plate);
}
}
三、思考
在这里我们是否需要设置一个互斥信号量来互斥访问盘子资源呢?
其实在本题中,我们可以尝试去设置,一个互斥信号量,但是写完后你会发现其实没有必要,这也给了我们一个提醒: 在 缓 冲 区 大 小 为 1 时 可 能 不 需 要 设 置 互 斥 信 号 量 . \color{red}在缓冲区大小为1时可能不需要设置互斥信号量. 在缓冲区大小为1时可能不需要设置互斥信号量.
好啦,今天就到这里啦,后面还会有中阶版和高阶版的橘子和苹果问题哈~
边栏推荐
- 随机生成数字字母(大写)组合
- Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
- 9、 Delegation mode
- 2023王道C语言训练营(二叉查找树-顺序查找-折半查找)
- leetcode18子集问题
- 51定时器初值计算
- 装饰器模式
- Some problems in using message queue service in thinkphp6
- mdb转换为db文件
- Troubleshoot typescript files being recognized as video
猜你喜欢

协程asyncio异步编程

Print: Entry, ':CFBundleIdentifier', Does Not Exist

The winning list of "talking tom cat family - time light chaser" 3D digital collection was announced

2021 ciscn-pwn 初赛

62. different paths - Dynamic Planning
![[cloud native weapon cilium] what is cilium](/img/e5/6faeb76d6c111afb974155a7e16400.png)
[cloud native weapon cilium] what is cilium

phpstrom 将项目上传码云

dried food! Training method of machinetranslation model based on mask label smoothing

2023王道C语言训练营(二叉查找树-顺序查找-折半查找)

“会说话的汤姆猫家族-时代逐光者”3D数字藏品中奖名单公布
随机推荐
Google Earth engine (GEE) - gpwv411: data set of average administrative unit area
2021 ciscn PWN preliminary
【兴趣阅读】DeepInf: Social Influence Prediction with Deep Learning
mdb转换为db文件
万物生长,各自高贵
Force buckle 1037 Effective boomerang
EG2131测试电路
“大写意花鸟画宗师李苦禅先生”重磅数字藏品全网首发
Test question bank and simulation test for operation certificate of ordinary scaffolder (special type of construction work) in 2022
MAC下安装MySQL+Django详细步骤
最大和的连续子数组
Phpstorm configuration database connection
[image denoising] image denoising based on MATLAB bdcnn [including Matlab source code 1866]
OSG basic operation
「诗经」主题文化数字藏品中奖名单公布
Question bank and answers of 2022 metal and nonmetal mine hoist operation examination
Requirements and business model analysis - Requirements 17- requirements management
[cloud native weapon cilium] what is cilium
Download and use of finalshell
Selenium distributed testing