当前位置:网站首页>PV operation daily question - single log bridge problem
PV operation daily question - single log bridge problem
2022-06-10 16:11:00 【liangsena】
Single wooden bridge problem

One 、 Problem description
The car passes the single wooden bridge in the east-west direction , To ensure safety , As long as there is no car on the bridge , Then one side's car is allowed to cross the bridge , When all the cars of the other side have passed , The other side's car is allowed to cross the bridge .
It's not hard to find out , It's a reader writer problem
Two 、 problem solving
:
int eastCount=0; // The current number of cars on the bridge from the East
int westCount=0; // The current number of cars on the bridge from the West
semaphore bridge=1; // The cars on both sides are mutually exclusive
semaphore eastMutex=1; // Exclusive access eastCount
semaphore westMutex=1; // Exclusive access westCount
East()
{
while(1)
{
P(eastMutex);
if(eastCount==0)
{
P(bridge);
}
eastCount++;
V(eastMutex);
Cross the bridge from the East ;
P(eastMutex);
eastCount--;
if(eastCount==0)
{
P(bridge);
}
V(eastMutex);
}
}
West()
{
while(1)
{
P(westMutex);
if(westCount==0)
{
P(bridge);
}
westCount++;
V(eastMutex);
Cross the bridge from the West ;
P(westMutex);
westCount--;
if(westCount==0)
{
P(bridge);
}
V(westMutex);
}
}
3、 ... and 、 twitter
There isn't much fresh , No more than is first The car on the bridge bridge resources , The last one to get off the bridge Car release for bridge resources , visit xCount You can use mutually exclusive access .
That's it , Slip away ~
边栏推荐
- MapReduce之Map阶段的join操作案例
- 【MySQL基础】
- Join operation cases in the map phase of MapReduce
- 智能家居(3)智能交互的竞品分析
- Jerry's ble PB2 cannot be hardware grounded or connected to triode base [chapter]
- 软件测试架构师,给后辈的16条忠告,快看看别错过
- [untitled]
- Code implementation of sorting and serializing cases in MapReduce
- 硬件仪器的使用
- Baidu open source ice-ba installation and operation summary
猜你喜欢

Recommend an easy-to-use designer navigation website

RGB颜色空间、色调、饱和度、亮度、HSV颜色空间详解

Rk3308--8 channels changed to dual channels + recording gain

广和通携手中国移动、惠普、联发科、英特尔合作打造5G全互联PC泛终端系列产品

安霸CV2FS/CV22FS获得ASIL C芯片功能安全认证,超越市场同类芯片水平

2D pose estimation for pose estimation - (openpose) realtime multi person 2D pose estimation using part affinity fields

Sorting of MapReduce cases

2290. Minimum Obstacle Removal to Reach Corner

Analysis of different dimensions of enterprise reviewers: enterprise growth of Hunan Great Wall Science and Technology Information Co., Ltd

Save a window with a specific size, resolution, or background color
随机推荐
Save a window with a specific size, resolution, or background color
MapReduce之排序及序列化案例的代码实现
Query-Convert QuickView是灰掉的解决办法(转)_SAP刘梦
uniapp中常用到的方法(部分) - 时间戳问题及富文本解析图片问题
剑指 Offer 06. 从尾到头打印链表
SVM and ANN of OpenCV neural network library_ Use of MLP
姿态估计之2D人体姿态估计 - Simple Baseline(SBL)
Rk3308 key and LED light
【MySQL基础】
Jerry's ble IO port interrupt and flip [chapter]
[MySQL basics]
广和通携手中国移动、惠普、联发科、英特尔合作打造5G全互联PC泛终端系列产品
从“初代播种”到“落地生花”,广和通在5G商用三年间做了什么?
C # game prototype character map dual movement
RK3308--固件编译
Jerry's ble transmission rate [chapter]
[untitled] audio Bluetooth voice chip, wt2605c-32n real-time recording upload technical scheme introduction
Code implementation of partition case of MapReduce
服务器运维环境安全体系(下篇)
Sword finger offer 06 Print linked list from end to end