操作系统-经典PV代码操作总结

< 理发师 >
semphore mutex_i = 1 ,consumer = 0 ,baber = 0 ;
int i = 0 ;
seat = n
consumer ()
{
while ( 1 )
{

P (mutex_i);
if (i < n) //有座位
{
i ++ ;
V (costomer);
V (mutex_i);
P ()
}
else
{
V (mutex_i);
}
}

}
<!-- 叫号问题 -->
mutex_i <!-- 防止顾客拿到一个号 -->
mutex_j <!-- 防止 -->

semphore mutex_i = 1 ;
consumer ()
{
取号
P (mutex_i)
i ++ ;
V (mutex_i)
waiting...
}

seller ()
{
P (mutex_j)
if (j < i)
{
selling...
j ++
V (mutex_j)
}
else
{
V (mutex_j)
休息。。。
}
}

<!-- 银行 -->
semphore mutex_i;
consumer ()
{
P (mutex_i)
if (i < 10 )
{
i ++
}
}

<!-- 前趋图 -->
procecss P1 ()
{
P (s1)
...
V (s2)
V (s3) //释放 2 个
}
P2 ()
{
P (s2)
...
V (s4)
}
P3 ()
{
P (s3)
...
V (s4)
}
P4 ()
{
P (s4)
P (s4) //只有s4=2才可以
...
}
<!-- 十字路口 -->
semaphore s = 1 ;
process1 ()
{
while ( 1 )
{
P (s)
...
V (s)
}
}
process2 ()
{
while ( 1 )
{
P (s);
...
V (s);
}
}

semaphore s = 100 // 初始可以进入的人的个数

Process_i ()
{
P (s);
进入售票厅
出售票厅
V (s);
}





假设初始的车是停 门关闭
semaphore run = 1 //可以跑
semphore open = 0
diver () //司机进程
{
while ( 1 )
{
P (run)
开车..
停车..
V (open)
}
}

seller ()
{
while ( 1 )
{
P (open)
开门...
上乘客
关门...
V (run)
}
}

<!-- 老和尚喝水 -->
semaphore empty = n,full = 0 ;
well = 1 //互斥访问井
pail = 3 //三个水桶
vat = 1 //水缸

取水()
{
while ( 1 )
{
P (empty)
//井中取水
P (pail)
P (well)
井中打水
V (well)
P (vat)
倒入缸中
V (vat)

V (pail)
V (full)
}
}

喝水()
{
while ( 1 )
{
P (full)
P (pail)
P (vat) //互斥水缸
取水...
V (vat)
V (pail)
V (empty)

}
}

<!-- 南大三个消费者生产者问题 -->
semaphore empty = n,full = 0 ,mutex = 1 ;
R () //读入者
{
P (empty)
P (metex)
读入缓冲区...
i ++ ;
V (metex)
V (full_1)
}


M () //检验" "->";"
{
P (full_1)
P (metex)
if (B[i] == " " )
B[i] = ";"
V (metex)
V (full_2)
}

P () //打印者
{
P (full_2)
P (mutex)
cout << B[i]
V (mutex)
V (empty)
}

<!-- 理发师问题 -->
int wait = 0 ; //等待的人数
int chairs = n; //椅子
semaphore s_barber = 1 //理发师这个资源可用
semphore s_customer = 0 ;
mutex = 1 ; //互斥的座位的访问

Customer ()
{
while ( 1 )
{
P (mutex)
waiting ++ ;
if (waiting > chairs) //先看座位满不满
{
离开...
V (mutex);
}
else
{
waiting ++ ; //坐下
V (mutex);
P (s_barber) //request a haircut

V (s_consumer);
}
}
}





Barber ()
{
while ( 1 )
{
P (s_customer);
P (mutex)
waiting -- ;
剪发...
V (mutex)
V (s_barber);
}
}













<!-- 吸烟者问题的变型:零件问题 -->
semaphore offer1 = 0 //AB
offer2 = 0 //BC
offer3 = 0 //AC
finish = 0 ; //并未完成一次操作

Master () //师傅进程
{
int i = random () % 3 + 1 ; //写出随机函数
if (i == 1 )
{
V (offer1);
}
else if (i == 2 )
{
V (offer2)
}
else
{
V (offer3)
}
放到desk上...
P (finish)
}

A ()
{
while ( 1 )
P (offer2)
加工
V (finish)
}

B ()
{
while ( 1 )
P (offer3)
加工
V (finish)
}

C ()
{
while ( 1 )
P (offer1)
加工....
V (finish)
}

<! 复旦 > 文件必须有2个经理签字 方可发出
semphore sem = 2 //还需要2个经理签字
A ()
{
while ( 1 )
{
P (mutex) //互斥访问这2次 必须一起签完
P (sem)
P (sem) //签字2次
V (mutex)
发出
V (sem)
V (sem)
}
}





<!-- 北京工业大学 标志块更改 - 生产者消费者 -->
semaphore empty = 64 , full = 0 //初始64bit均是不可消费的
mutex = 1 ;
flag[ 64 ];
PUT () //生产者进程
{
P (empty)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 1 )
{
flag[i] = 0 ;
break ;
}
}
V (mutex)
V (full)
}

GET () //消费者进程
{
P (full)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 0 )
{
flag[i] == 1 ;
break ;
}
}
V (mutex)
V (empty)
}

<!-- 北大 读写进程 -->
semaphore write = 1 ;
semaphore read = 1 ;
semaphore mutex = 1 ;
int count = 0 ; //读者的个数
P2 () //只写
{
P (write)
write...
V (write)
}

P1 () //只读
{
while ( 1 )
{
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...
//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)
}
}

//读和写进程
P3 ()
{
while ( 1 )
{
//先读
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...

//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)

//后写
P (write)
writing...
V (write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist = 3 ; //当前的牙医资源可用
semaphore mutex = 1 ; //互斥访问座位
int waiting = 0 ; //起始没人等候
semaphore paient = 0 ; //等待就医的病人
Patient ()
{
P (mutex)
if (waiting >= 10 ) //先检查座位
{
离开
V (mutex)
}
else
{
坐下
waiting ++
V (mutex)
P (dentist) //请求就医唤醒牙医

V (pateint)
}
}





dentist ()
{
P (patient)
P (mutex)
waiting -- ; //等候顾客减少一个
V (mutex);
看病....
V (dentist)
}


//北大酒吧听音乐问题
semaphore offer1 = 0 //电池+音乐磁带
semaphore offer2 = 0 //电池+随身听
semaphore offer3 = 0 //随身听+音乐磁带
semaphore finish = 0 ; //是否完成听歌
Boss () //老板进程
{
while ( 1 )
{
int i = radom () % 3 + 1 ; //产生随机数1-3
if (i == 1 )
{
V (offer1)
放置电池 + 音乐磁带....
}
else if (i == 2 )
{
V (offer2)
放置电池 + 随身听...
}
else
{
V (offer3)
放置随身听 + 音乐磁带
} //执行顺序是在这里转到:Listener进程
P (finish); //这里的P操作为等待听完
}
}

Listener1 ()
{
while ( 1 )
{
P (offer1) //消耗资源1
listen to the music...
V (finish) //宣告结束听歌
}
}
Listener2 ()
{
while ( 1 )
{
P (offer2) //消耗2类资源
listen to the music...
V (finish)
}
}
Listener3 ()
{
while ( 1 )
{
P (offer3)
listen to the music....
V (finish)
}
}

//华中科技大
North ()
{
P (mutex)
go thourgh the bridge...
V (mutex)
}
South ()
{
P (mutex)
go through the street...
V (mutex)
}





//可以过多辆车
semaphore bridge = 1 ; //桥的信号量
semaphore mutex_north = 1 ; //互斥信号量
semophore mutex_south = 1 ;
int count = 0 ;
North ()
{
P (mutex_north)

if (count == 0 )
P (bridge) // 将桥上锁
count ++ ;

V (mutex_north)
go though the bridge...

P (mutex_north)

count -- ;
if (count == 0 )
V (brigde);

V (mutex_north)
}

South ()
{
P (mutex_south)
if (count == 0 )
P (bridge)
count ++ ;
V (mutex_south)

go through the bridge...

P (mutex_south)
count -- ;
if (count == 0 )
V (bridge)
V (mutex_south)
}


//南大 存钱取钱流程
semaphore mutex = 1 ; //互斥访问账户的信号量
semphore take save;
save ()
{
while ( 1 )
{

P (mutex)

m1 = amount
m1 =+ 10
amount =+ m1
V (mutex)
V (take) //可以取钱
}
}









take ()
{
while ( 1 )
{
P (take) //请求取钱
P (mutex)

m2 = amount
m2 =+ 10
amount = m2

V (mutex)
}

}
< 理发师 >
semphore mutex_i = 1 ,consumer = 0 ,baber = 0 ;
int i = 0 ;
seat = n
consumer ()
{
while ( 1 )
{

P (mutex_i);
if (i < n) //有座位
{
i ++ ;
V (costomer);
V (mutex_i);
P ()
}
else
{
V (mutex_i);
}
}

}
<!-- 叫号问题 -->
mutex_i <!-- 防止顾客拿到一个号 -->
mutex_j <!-- 防止 -->

semphore mutex_i = 1 ;
consumer ()
{
取号
P (mutex_i)
i ++ ;
V (mutex_i)
waiting...
}

seller ()
{
P (mutex_j)
if (j < i)
{
selling...
j ++
V (mutex_j)
}
else
{
V (mutex_j)
休息。。。
}
}

<!-- 银行 -->
semphore mutex_i;
consumer ()
{
P (mutex_i)
if (i < 10 )
{
i ++
}
}

<!-- 前趋图 -->
procecss P1 ()
{
P (s1)
...
V (s2)
V (s3) //释放 2 个
}
P2 ()
{
P (s2)
...
V (s4)
}
P3 ()
{
P (s3)
...
V (s4)
}
P4 ()
{
P (s4)
P (s4) //只有s4=2才可以
...
}
<!-- 十字路口 -->
semaphore s = 1 ;
process1 ()
{
while ( 1 )
{
P (s)
...
V (s)
}
}
process2 ()
{
while ( 1 )
{
P (s);
...
V (s);
}
}

semaphore s = 100 // 初始可以进入的人的个数

Process_i ()
{
P (s);
进入售票厅
出售票厅
V (s);
}





假设初始的车是停 门关闭
semaphore run = 1 //可以跑
semphore open = 0
diver () //司机进程
{
while ( 1 )
{
P (run)
开车..
停车..
V (open)
}
}

seller ()
{
while ( 1 )
{
P (open)
开门...
上乘客
关门...
V (run)
}
}

<!-- 老和尚喝水 -->
semaphore empty = n,full = 0 ;
well = 1 //互斥访问井
pail = 3 //三个水桶
vat = 1 //水缸

取水()
{
while ( 1 )
{
P (empty)
//井中取水
P (pail)
P (well)
井中打水
V (well)
P (vat)
倒入缸中
V (vat)

V (pail)
V (full)
}
}

喝水()
{
while ( 1 )
{
P (full)
P (pail)
P (vat) //互斥水缸
取水...
V (vat)
V (pail)
V (empty)

}
}

<!-- 南大三个消费者生产者问题 -->
semaphore empty = n,full = 0 ,mutex = 1 ;
R () //读入者
{
P (empty)
P (metex)
读入缓冲区...
i ++ ;
V (metex)
V (full_1)
}


M () //检验" "->";"
{
P (full_1)
P (metex)
if (B[i] == " " )
B[i] = ";"
V (metex)
V (full_2)
}

P () //打印者
{
P (full_2)
P (mutex)
cout << B[i]
V (mutex)
V (empty)
}

<!-- 理发师问题 -->
int wait = 0 ; //等待的人数
int chairs = n; //椅子
semaphore s_barber = 1 //理发师这个资源可用
semphore s_customer = 0 ;
mutex = 1 ; //互斥的座位的访问

Customer ()
{
while ( 1 )
{
P (mutex)
waiting ++ ;
if (waiting > chairs) //先看座位满不满
{
离开...
V (mutex);
}
else
{
waiting ++ ; //坐下
V (mutex);
P (s_barber) //request a haircut

V (s_consumer);
}
}
}





Barber ()
{
while ( 1 )
{
P (s_customer);
P (mutex)
waiting -- ;
剪发...
V (mutex)
V (s_barber);
}
}













<!-- 吸烟者问题的变型:零件问题 -->
semaphore offer1 = 0 //AB
offer2 = 0 //BC
offer3 = 0 //AC
finish = 0 ; //并未完成一次操作

Master () //师傅进程
{
int i = random () % 3 + 1 ; //写出随机函数
if (i == 1 )
{
V (offer1);
}
else if (i == 2 )
{
V (offer2)
}
else
{
V (offer3)
}
放到desk上...
P (finish)
}

A ()
{
while ( 1 )
P (offer2)
加工
V (finish)
}

B ()
{
while ( 1 )
P (offer3)
加工
V (finish)
}

C ()
{
while ( 1 )
P (offer1)
加工....
V (finish)
}

<! 复旦 > 文件必须有2个经理签字 方可发出
semphore sem = 2 //还需要2个经理签字
A ()
{
while ( 1 )
{
P (mutex) //互斥访问这2次 必须一起签完
P (sem)
P (sem) //签字2次
V (mutex)
发出
V (sem)
V (sem)
}
}





<!-- 北京工业大学 标志块更改 - 生产者消费者 -->
semaphore empty = 64 , full = 0 //初始64bit均是不可消费的
mutex = 1 ;
flag[ 64 ];
PUT () //生产者进程
{
P (empty)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 1 )
{
flag[i] = 0 ;
break ;
}
}
V (mutex)
V (full)
}

GET () //消费者进程
{
P (full)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 0 )
{
flag[i] == 1 ;
break ;
}
}
V (mutex)
V (empty)
}

<!-- 北大 读写进程 -->
semaphore write = 1 ;
semaphore read = 1 ;
semaphore mutex = 1 ;
int count = 0 ; //读者的个数
P2 () //只写
{
P (write)
write...
V (write)
}

P1 () //只读
{
while ( 1 )
{
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...
//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)
}
}

//读和写进程
P3 ()
{
while ( 1 )
{
//先读
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...

//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)

//后写
P (write)
writing...
V (write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist = 3 ; //当前的牙医资源可用
semaphore mutex = 1 ; //互斥访问座位
int waiting = 0 ; //起始没人等候
semaphore paient = 0 ; //等待就医的病人
Patient ()
{
P (mutex)
if (waiting >= 10 ) //先检查座位
{
离开
V (mutex)
}
else
{
坐下
waiting ++
V (mutex)
P (dentist) //请求就医唤醒牙医

V (pateint)
}
}





dentist ()
{
P (patient)
P (mutex)
waiting -- ; //等候顾客减少一个
V (mutex);
看病....
V (dentist)
}


//北大酒吧听音乐问题
semaphore offer1 = 0 //电池+音乐磁带
semaphore offer2 = 0 //电池+随身听
semaphore offer3 = 0 //随身听+音乐磁带
semaphore finish = 0 ; //是否完成听歌
Boss () //老板进程
{
while ( 1 )
{
int i = radom () % 3 + 1 ; //产生随机数1-3
if (i == 1 )
{
V (offer1)
放置电池 + 音乐磁带....
}
else if (i == 2 )
{
V (offer2)
放置电池 + 随身听...
}
else
{
V (offer3)
放置随身听 + 音乐磁带
} //执行顺序是在这里转到:Listener进程
P (finish); //这里的P操作为等待听完
}
}

Listener1 ()
{
while ( 1 )
{
P (offer1) //消耗资源1
listen to the music...
V (finish) //宣告结束听歌
}
}
Listener2 ()
{
while ( 1 )
{
P (offer2) //消耗2类资源
listen to the music...
V (finish)
}
}
Listener3 ()
{
while ( 1 )
{
P (offer3)
listen to the music....
V (finish)
}
}

//华中科技大
North ()
{
P (mutex)
go thourgh the bridge...
V (mutex)
}
South ()
{
P (mutex)
go through the street...
V (mutex)
}





//可以过多辆车
semaphore bridge = 1 ; //桥的信号量
semaphore mutex_north = 1 ; //互斥信号量
semophore mutex_south = 1 ;
int count = 0 ;
North ()
{
P (mutex_north)

if (count == 0 )
P (bridge) // 将桥上锁
count ++ ;

V (mutex_north)
go though the bridge...

P (mutex_north)

count -- ;
if (count == 0 )
V (brigde);

V (mutex_north)
}

South ()
{
P (mutex_south)
if (count == 0 )
P (bridge)
count ++ ;
V (mutex_south)

go through the bridge...

P (mutex_south)
count -- ;
if (count == 0 )
V (bridge)
V (mutex_south)
}


//南大 存钱取钱流程
semaphore mutex = 1 ; //互斥访问账户的信号量
semphore take save;
save ()
{
while ( 1 )
{

P (mutex)

m1 = amount
m1 =+ 10
amount =+ m1
V (mutex)
V (take) //可以取钱
}
}









take ()
{
while ( 1 )
{
P (take) //请求取钱
P (mutex)

m2 = amount
m2 =+ 10
amount = m2

V (mutex)
}

}
< 理发师 >
semphore mutex_i = 1 ,consumer = 0 ,baber = 0 ;
int i = 0 ;
seat = n
consumer ()
{
while ( 1 )
{

P (mutex_i);
if (i < n) //有座位
{
i ++ ;
V (costomer);
V (mutex_i);
P ()
}
else
{
V (mutex_i);
}
}

}
<!-- 叫号问题 -->
mutex_i <!-- 防止顾客拿到一个号 -->
mutex_j <!-- 防止 -->

semphore mutex_i = 1 ;
consumer ()
{
取号
P (mutex_i)
i ++ ;
V (mutex_i)
waiting...
}

seller ()
{
P (mutex_j)
if (j < i)
{
selling...
j ++
V (mutex_j)
}
else
{
V (mutex_j)
休息。。。
}
}

<!-- 银行 -->
semphore mutex_i;
consumer ()
{
P (mutex_i)
if (i < 10 )
{
i ++
}
}

<!-- 前趋图 -->
procecss P1 ()
{
P (s1)
...
V (s2)
V (s3) //释放 2 个
}
P2 ()
{
P (s2)
...
V (s4)
}
P3 ()
{
P (s3)
...
V (s4)
}
P4 ()
{
P (s4)
P (s4) //只有s4=2才可以
...
}
<!-- 十字路口 -->
semaphore s = 1 ;
process1 ()
{
while ( 1 )
{
P (s)
...
V (s)
}
}
process2 ()
{
while ( 1 )
{
P (s);
...
V (s);
}
}

semaphore s = 100 // 初始可以进入的人的个数

Process_i ()
{
P (s);
进入售票厅
出售票厅
V (s);
}





假设初始的车是停 门关闭
semaphore run = 1 //可以跑
semphore open = 0
diver () //司机进程
{
while ( 1 )
{
P (run)
开车..
停车..
V (open)
}
}

seller ()
{
while ( 1 )
{
P (open)
开门...
上乘客
关门...
V (run)
}
}

<!-- 老和尚喝水 -->
semaphore empty = n,full = 0 ;
well = 1 //互斥访问井
pail = 3 //三个水桶
vat = 1 //水缸

取水()
{
while ( 1 )
{
P (empty)
//井中取水
P (pail)
P (well)
井中打水
V (well)
P (vat)
倒入缸中
V (vat)

V (pail)
V (full)
}
}

喝水()
{
while ( 1 )
{
P (full)
P (pail)
P (vat) //互斥水缸
取水...
V (vat)
V (pail)
V (empty)

}
}

<!-- 南大三个消费者生产者问题 -->
semaphore empty = n,full = 0 ,mutex = 1 ;
R () //读入者
{
P (empty)
P (metex)
读入缓冲区...
i ++ ;
V (metex)
V (full_1)
}


M () //检验" "->";"
{
P (full_1)
P (metex)
if (B[i] == " " )
B[i] = ";"
V (metex)
V (full_2)
}

P () //打印者
{
P (full_2)
P (mutex)
cout << B[i]
V (mutex)
V (empty)
}

<!-- 理发师问题 -->
int wait = 0 ; //等待的人数
int chairs = n; //椅子
semaphore s_barber = 1 //理发师这个资源可用
semphore s_customer = 0 ;
mutex = 1 ; //互斥的座位的访问

Customer ()
{
while ( 1 )
{
P (mutex)
waiting ++ ;
if (waiting > chairs) //先看座位满不满
{
离开...
V (mutex);
}
else
{
waiting ++ ; //坐下
V (mutex);
P (s_barber) //request a haircut

V (s_consumer);
}
}
}





Barber ()
{
while ( 1 )
{
P (s_customer);
P (mutex)
waiting -- ;
剪发...
V (mutex)
V (s_barber);
}
}













<!-- 吸烟者问题的变型:零件问题 -->
semaphore offer1 = 0 //AB
offer2 = 0 //BC
offer3 = 0 //AC
finish = 0 ; //并未完成一次操作

Master () //师傅进程
{
int i = random () % 3 + 1 ; //写出随机函数
if (i == 1 )
{
V (offer1);
}
else if (i == 2 )
{
V (offer2)
}
else
{
V (offer3)
}
放到desk上...
P (finish)
}

A ()
{
while ( 1 )
P (offer2)
加工
V (finish)
}

B ()
{
while ( 1 )
P (offer3)
加工
V (finish)
}

C ()
{
while ( 1 )
P (offer1)
加工....
V (finish)
}

<! 复旦 > 文件必须有2个经理签字 方可发出
semphore sem = 2 //还需要2个经理签字
A ()
{
while ( 1 )
{
P (mutex) //互斥访问这2次 必须一起签完
P (sem)
P (sem) //签字2次
V (mutex)
发出
V (sem)
V (sem)
}
}





<!-- 北京工业大学 标志块更改 - 生产者消费者 -->
semaphore empty = 64 , full = 0 //初始64bit均是不可消费的
mutex = 1 ;
flag[ 64 ];
PUT () //生产者进程
{
P (empty)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 1 )
{
flag[i] = 0 ;
break ;
}
}
V (mutex)
V (full)
}

GET () //消费者进程
{
P (full)
P (mutex)
for ( int i = 0 ;i < 64 ;i ++ )
{
if (flag[i] == 0 )
{
flag[i] == 1 ;
break ;
}
}
V (mutex)
V (empty)
}

<!-- 北大 读写进程 -->
semaphore write = 1 ;
semaphore read = 1 ;
semaphore mutex = 1 ;
int count = 0 ; //读者的个数
P2 () //只写
{
P (write)
write...
V (write)
}

P1 () //只读
{
while ( 1 )
{
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...
//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)
}
}

//读和写进程
P3 ()
{
while ( 1 )
{
//先读
P (mutex)
if (count == 0 ) P (write) //第一个关写门
count ++ ;
V (mutex)

reading...

//读完
P (mutex)
count -- ;
if (count == 0 ) V (write);
V (mutex)

//后写
P (write)
writing...
V (write)
}
}
//理发师问题的变型: 北大牙医问题:
semaphore dentist = 3 ; //当前的牙医资源可用
semaphore mutex = 1 ; //互斥访问座位
int waiting = 0 ; //起始没人等候
semaphore paient = 0 ; //等待就医的病人
Patient ()
{
P (mutex)
if (waiting >= 10 ) //先检查座位
{
离开
V (mutex)
}
else
{
坐下
waiting ++
V (mutex)
P (dentist) //请求就医唤醒牙医

V (pateint)
}
}





dentist ()
{
P (patient)
P (mutex)
waiting -- ; //等候顾客减少一个
V (mutex);
看病....
V (dentist)
}


//北大酒吧听音乐问题
semaphore offer1 = 0 //电池+音乐磁带
semaphore offer2 = 0 //电池+随身听
semaphore offer3 = 0 //随身听+音乐磁带
semaphore finish = 0 ; //是否完成听歌
Boss () //老板进程
{
while ( 1 )
{
int i = radom () % 3 + 1 ; //产生随机数1-3
if (i == 1 )
{
V (offer1)
放置电池 + 音乐磁带....
}
else if (i == 2 )
{
V (offer2)
放置电池 + 随身听...
}
else
{
V (offer3)
放置随身听 + 音乐磁带
} //执行顺序是在这里转到:Listener进程
P (finish); //这里的P操作为等待听完
}
}

Listener1 ()
{
while ( 1 )
{
P (offer1) //消耗资源1
listen to the music...
V (finish) //宣告结束听歌
}
}
Listener2 ()
{
while ( 1 )
{
P (offer2) //消耗2类资源
listen to the music...
V (finish)
}
}
Listener3 ()
{
while ( 1 )
{
P (offer3)
listen to the music....
V (finish)
}
}

//华中科技大
North ()
{
P (mutex)
go thourgh the bridge...
V (mutex)
}
South ()
{
P (mutex)
go through the street...
V (mutex)
}





//可以过多辆车
semaphore bridge = 1 ; //桥的信号量
semaphore mutex_north = 1 ; //互斥信号量
semophore mutex_south = 1 ;
int count = 0 ;
North ()
{
P (mutex_north)

if (count == 0 )
P (bridge) // 将桥上锁
count ++ ;

V (mutex_north)
go though the bridge...

P (mutex_north)

count -- ;
if (count == 0 )
V (brigde);

V (mutex_north)
}

South ()
{
P (mutex_south)
if (count == 0 )
P (bridge)
count ++ ;
V (mutex_south)

go through the bridge...

P (mutex_south)
count -- ;
if (count == 0 )
V (bridge)
V (mutex_south)
}


//南大 存钱取钱流程
semaphore mutex = 1 ; //互斥访问账户的信号量
semphore take save;
save ()
{
while ( 1 )
{

P (mutex)

m1 = amount
m1 =+ 10
amount =+ m1
V (mutex)
V (take) //可以取钱
}
}









take ()
{
while ( 1 )
{
P (take) //请求取钱
P (mutex)

m2 = amount
m2 =+ 10
amount = m2

V (mutex)
}

}

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Linux操作系统,可以使用信号量(Semaphore)来实现进程间的互斥访问共享资源,例如打印机。下面是一个简单的示例代码,演示了如何使用PV操作来实现多个进程共享一台打印机的互斥访问[^1]: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/sem.h> #define KEY 1234 void P(int semid) { struct sembuf buf; buf.sem_num = 0; buf.sem_op = -1; buf.sem_flg = SEM_UNDO; semop(semid, &buf, 1); } void V(int semid) { struct sembuf buf; buf.sem_num = 0; buf.sem_op = 1; buf.sem_flg = SEM_UNDO; semop(semid, &buf, 1); } int main() { int semid; pid_t pid; // 创建信号量 semid = semget(KEY, 1, IPC_CREAT | 0666); if (semid == -1) { perror("semget"); exit(1); } // 初始化信号量 semctl(semid, 0, SETVAL, 1); // 创建子进程 pid = fork(); if (pid == -1) { perror("fork"); exit(1); } if (pid == 0) { // 子进程 P(semid); // 进入临界区 printf("Child process is printing...\n"); sleep(2); // 模拟打印过程 printf("Child process finished printing.\n"); V(semid); // 离开临界区 } else { // 父进程 P(semid); // 进入临界区 printf("Parent process is printing...\n"); sleep(2); // 模拟打印过程 printf("Parent process finished printing.\n"); V(semid); // 离开临界区 } // 删除信号量 semctl(semid, 0, IPC_RMID); return 0; } ``` 这段代码使用了System V信号量机制,通过`semget`函数创建一个信号量集合,然后使用`semctl`函数初始化信号量的值为1。在子进程和父进程,分别调用`P`函数和`V`函数来实现进入和离开临界区的操作。其,`P`函数将信号量的值减1,如果信号量的值小于0,则进程被阻塞;`V`函数将信号量的值加1,如果有进程因为等待信号量而被阻塞,则唤醒其一个进程。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值