当前位置:网站首页>PV操作每日一题-黑白棋子问题
PV操作每日一题-黑白棋子问题
2022-06-10 10:14:00 【liangsena】
温馨提示,这个题目的代码可能很简单,但是有点绕,类似于前面的橘子苹果问题的交替放入水果。好啦,开始!
一、问题描述
有一个盒子中放有数量相等的黑白棋子,现在用自动分拣系统将黑白棋子分开,系统中有两个进程P1和P2。P1负责白棋子的分拣,P2则负责黑棋子的分拣,两者必须交替进行分拣,且分拣结束前不得停止。用PV操作解决该问题。
二、问题求解
:这么去理解,把P1和P2看作两个资源,互斥进行访问,与橘子苹果问题正好相反。
semaphore flag1=1;
semaphore flag2=0;
P1()
{
while(1)
{
P(flag1);
分拣一个白棋子;
V(flag2); //发信号给P2
}
}
P2()
{
while(1)
{
P(flag2);
分拣一个黑棋子;
V(flag1); //发信号给P1
}
}
三、碎碎念
理解之后,看完代码是不是感觉题目超级简单~
边栏推荐
- Install the latest version of cocoapods tutorial
- 数组目标值target两个整数,并返回它们的数组下标
- Personnalisation de l'équipement d'essai de Chengdu Introduction préliminaire aux types de données du langage C du micro - ordinateur à puce unique
- Fastadmin uses phpexcel to export tabular data to excel
- Xcode8.3.2 automatic packaging script
- 2023王道C语言训练营(二叉查找树-顺序查找-折半查找)
- 2023 Wangdao C language training camp (binary search tree - sequential search - half search)
- Requirements and business model analysis - Requirements 17- requirements management
- 怎样简洁明了地说清楚产品需求?
- 学术讲座: 多标签主动学习之 MASP
猜你喜欢

Leetcode 2000. 反转单词前缀

2023 Wangdao C language training camp (clue binary tree)

Unpopular knowledge points in Delphi

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

“会说话的汤姆猫家族-时代逐光者”3D数字藏品中奖名单公布

Axure add drop-down menu linkage

MDB to DB file

Requirements and business model analysis - Requirements 17- requirements management

axure添加下拉菜单联动

Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences
随机推荐
Leetcode 2000. 反转单词前缀
ADB log fetching
2021 ciscn-pwn 初赛
stm32 printf乱码
单片机开发需要的工具以及软件有哪些
Phpstrom uploads the project to the code cloud
Chengdu test equipment customization_ A preliminary introduction to the data types of C language of single chip microcomputer
"Isolation insurance" has become a "net red" product, but many policyholders say that it is difficult to settle claims
Neo hacker song's award-winning list has been announced. Who owns tens of thousands of dollars of gold?
组合模式例子
ADB 日志抓取
Eg2131 test circuit
「诗经」主题文化数字藏品中奖名单公布
Create swift color class
八、链模式
Fastadmin uses phpexcel to export tabular data to excel
一个独特的简历生成器,开源了!
Phpstorm configuration database connection
Introduction to development technology of multi-target application on SAP cloud platform
phpstrom 將項目上傳碼雲