当前位置:网站首页>PV operation daily question - orange apple question (advanced version)
PV operation daily question - orange apple question (advanced version)
2022-06-10 10:46:00 【liangsena】
Orange apple problem - Advanced version
Finally, we have to wait for the advanced version , Come on !
One 、 Problem description
There is a plate on the table , It can hold up to two fruits , Only put or take out at a time A fruit , Dad put apples on the plate , Mom put oranges on the plate , Two people required alternate Add fruit ; Yes Two sons and two daughters , My son eats one orange at a time , My daughter eats one apple at a time . Please use semaphores and PV Operate to solve the problem .
The change of the title is nothing more than :
1. The buffer size is 2, stay 1 and N Between
2. Alternate the fruit , Do not put the same fruit twice in a row
( A topic will be updated later , There are no restrictions on alternate placement , For the time being, it is called a higher-order variant )
Two 、 problem solving
:
semaphore singal=1; // Two people put in the fruit alternately
semaphore mutex=1;
semaphore appleFull=0;
semaphore orangeFull=0;
semaphore appleEmpty=1;
semaphore orangeEmpty=1;
// In fact, this problem can be seen as two people accessing two sizes of... Respectively 1 Orange and apple buffers
// Take it apart and make it
Father()
{
while(1)
{
P(singal);
P(appleEmpty);
P(mutex);
Put in an apple ;
V(mutex);
V(appleFull); // Tell the two daughters : You can eat apples
V(singal); // Send a signal to mom : You can put oranges
}
}
Mother()
{
while(1)
{
P(singal);
P(orangeEmpty);
P(mutex);
Put in an orange ;
V(mutex);
V(orangeFull); // Tell the two sons : You can eat oranges
V(singal); // Send a signal to Dad : You can put apples
}
}
Son-i(i=1,2)
{
while(1)
{
P(orangeFull);
P(mutex);
Take an orange ;
V(mutex);
P(orangeEmpty);
}
}
Datughter-i(i=1,2)
{
while(1)
{
P(appleFull);
P(mutex);
Take an apple ;
V(mutex);
V(appleEmpty);
}
}
See here , Maybe someone is confused , But don't be afraid of , Experience it over and over again PV The subtlety of the operation ~
3、 ... and 、 reflection
Imagine , Let two daughters or two sons mutually exclusive to get fruit ?
Is it necessary to set another semaphore ?
That's all for today , Slip away ~
边栏推荐
- "Bookofsongs" themed cultural digital collection winners announced
- 杰理之获取 BLE 广播包、profile 数据的接口【篇】
- Delphi中的冷门知识点
- Question bank and answers of 2022 metal and nonmetal mine hoist operation examination
- Conversion between binary, octal, decimal and hexadecimal (integer plus decimal)
- Multithreading implementation
- 山东大学软件学院项目实训-创新实训-网络安全靶场实验平台(十七)
- Today, 19:30 | graphics special session - Gao Lin, teacher team of Institute of computing technology, Chinese Academy of Sciences
- Unpopular knowledge points in Delphi
- 蔚来季报图解:营收99亿同比增24% 经营亏损近22亿
猜你喜欢

上企评家,看各央企、上市公司企业成长性评价

珈创生物冲刺创业板:年营收1.3亿 实控人曾为武大教授

For details of imitated Taobao products, pull down to switch to picture details, and pull up to switch to graphic introduction

62. different paths - Dynamic Planning

Industrial Internet Architecture

基于分层自监督学习将视觉Transformer扩展到千兆像素图像

Axure add drop-down menu linkage

Implementation code of several recent good papers (with source code download)

学术讲座: 多标签主动学习之 MASP
常用Shell命令 - 02 压缩和解压缩
随机推荐
Splishsplash架构介绍
2022 underground coal mine electrical examination question bank and online simulation examination
数组目标值target两个整数,并返回它们的数组下标
2023 Wangdao C language training camp (binary search tree - sequential search - half search)
MySQL实战45讲_8_从一个问题来加深对 mysql 可重复读的理解
Is the online account opening channel reliable? Is it safe?
Array to determine any duplicate value
Extending visual transformer to gigapixel images based on hierarchical self supervised learning
Selenium distributed testing
Industrial Internet Architecture
PV operation daily question - ticket sales
Sublime text - 01 code tracking
【无标题】
Delphi中的冷门知识点
Pat class a 1126 Euler path
Binary find a specific value in an ordered array
PAT甲级 1126 欧拉路径
B站季报图解:营收51亿同比增30% 月均活跃用户近3亿
Unpopular knowledge points in Delphi
二分查找有序数组中的特定值