当前位置:网站首页>Process synchronization and mutual exclusion problem
Process synchronization and mutual exclusion problem
2022-08-09 08:08:00 【happy learning】
1.生产者- 消费者问题m个消费者 n 个生产者
empty [-n,k]
Producer processes are executed,k到0 ,继续执行,Mutual exclusion conflict,jam to-n
full [-m,k]
同生产者,If the buffer is full,消费者进程执行,同上
当k>=m+n
mutex[-(n+m-1),1]
Producer and consumer processes run concurrently,kwill not be restricted,The minimum value is that the producer and consumer processes are waiting to block at the same time
当k<m+n
mutex[-(k-1),1]
Producer and consumer processes run concurrently,但是受k限制,Producer and consumer processes do not all execute
为什么和m+n比较
Because of the two processesempty+full=k
2.1. Suppose an elementary school has 600 人,Only one reading room is available 100 Individuals read at the same time.Readers enter and
All departures from the reading room must be registered on a registration form at the entrance to the reading room,阅览室有 100 个
座位,Provides that only one person is allowed to register or deregister at a time.
要求:(1)用 P、V Operates algorithms that describe relationships between reader processes.
(2)Indicates the name of the semaphore used in the algorithm、作用、Initial value and range of change
semaphore mutex=1,seat=100;
parbigin
process readeri(i=1,2,3...)
begin
Readers come to the reading room
P(seat)
P(mutex)
在登记表上登记
V(mutex)
Read materials in the reading room
P(mutex)
Log off on the registration form
V(mutex)
V(seat)
The reader leaves
seat[-500,100]
mutex[-99,1]
- Three beverage factories P1、P2、P3 produce orange juice,They have each purchased the three necessary raw materials(水、
糖、浓缩汁)中的两种,After purchasing the third raw material, it can be prepared and sold.There is a supplier that can
Sufficient supply of these raw materials,But only one ingredient can be taken out and put into the container for sale at a time,当容器中有
When the raw material is needed, the beverage factory that needs the raw material can take it away,When the container is empty,Suppliers can also put in an ingredient,
假定:P1 There is sugar and water,P2 Water and juice concentrate are available,P3 Sugar and juice concentrate already available,试用 PV 操
Write a synchronous and mutually exclusive relationship between a supplier and three beverage factories.
semaphore s=1,so=0,ss=0,sw=0;
enum{
sugar,water,orange}container;
cobegin
process Provider{
while(true){
P(s)
Load ingredients into containers;
if(cantainer==suger)V(so);
else if(cantainer==water)V(ss);
else V(sw);
}}
process P2{
while (true){
P(ss);
Take the sugar from the container;
V(s);
Production of orange juice;}}
process P1{
while (true){
P(so);
Take the juice concentrate from the container;
V(s);
Production of orange juice;}}
process P3{
while (true){
P(sw);
Take water from the container;
V(s);
Production of orange juice;}}
- There is a single-plank bridge going east-west,针对以下三种情况,用 P,V The operations are implemented separately for pedestrians Synchronize mutually exclusive relationships: (1)每次只允许一个人过桥; (2)When there are pedestrians on the single-plank bridge,Pedestrians in the same direction can cross the bridge at the same time,The person in the opposite direction must 等待. (3)When there are pedestrians from east to west on the single-plank bridge,Pedestrians in the same direction can cross the bridge at the same time,从西向东 的方向,Only one person is allowed to cross the bridge alone.
(2)
设信号量: mutex=1 //East-west direction is mutually exclusive
MD=1//East to West uses count variables for mutual exclusion
MX=1//West to East use count variables mutually exclusive
Set the integer variable CD=0//Number of people crossing the bridge from east to west
CX=0//The number of people on the bridge from west to east
process traveler j(){
//从东向西
//上桥
P(MD);
CD++;
if(CD==1)P(mutex);At this time, there were people on the bridge from east to west,West to east to this place will block mutually exclusive
V(MD);
过桥
//下桥
P(MD);
CD--;
if(CD==0)V(mutex);
V(MD);
}
(3)
This question is the same as the reader writer question
int read=0;//计数器
semaphore ws=1 ,mutex=1;
cobegin
process EtoW(){
P(mutex);
read++;
if(read==1)P(ws); //这个时候,West to East will conflict
V(mutex);
过桥;
P(mutex);
read--;
if(read==0)V(ws);
V(mutex);}
coend;
process WtoE()
{
P(ws);
过桥
V(ws);
}
边栏推荐
猜你喜欢
随机推荐
Operations in the database (syntax)
EMQ X 消息服务器学习记录——为后面的毕设做准备
Four departments including the Ministry of Industry and Information Technology promote green smart home products to the countryside
db2数据库备份恢复问题
Decimal工具类
C: print the diamond
nvm安装以及管理多版本node教程
Different styles of Flask-restful
H3C_利用策略路由实现出口双线路负载(选路)的部署
[STL]list
JS基础1
EXCEL uses function joint debugging (find, mid, vlookup, xlookup)
不同风格的Flask-restful
scp上传文件到远程服务器
MySql homework practice questions
Luogu P1110 report statistics multiset stl good question
App测试
OpenHarmony开源见面会(南京站)相关笔记
scp upload file to remote server
(二)、灰色预测模型(GM1,1)