操作系统-PV习题
(1)阅览室问题:加入阅览室入口有一本登记册,每个人都必须按顺序签名进去。
struct {
char name[];
int number;
}A[];
semaphore mutex = ; //控制A[]被修改的信号量
semaphore seatcount = ; //剩余的座位数
int i;
for(i=; i<; i++) {
A[i].number=i;
A[i].nume=null;
}
cobegin
process readeri(char readername[]) {
P(seatcount);
P(mutex);
for(int i=; i<; i++) {
if(A[i].name==null) A[i].name=readername;
reader get the seat number i;
}
V(mutex);
{进入阅览室座位号i,坐下读书};
P(mutex);
A[i].name = null;
V(mutex);
V(seatcount);
}
coend
semaphore mutex1 = mutex2 = mutex3 = mutex0 = ; //约束每个信箱同一时间只能有一个进程操作
semaphore empty0 = , empty1 = , empty2 = empty3 =; //信箱剩余空间
semaphore full0 = , full1 = full2 = full3 =; //信箱剩余信件数
int in0 = in1 = in2 = in3 = out0 = out1 = out2 = out3 =; //信箱中存、取信的位置,初始均为0
cobegin
process P0( ) {
while(true) {
P(full0);
P(mutex0);
{从M0[out0]取一条消息};
out0 = ( out0 + ) % ;
V(mutex0);
V(empty0);
{加工消息};
P(empty1);
P(mutex1);
{消息存M1[in1]};
in1 = ( in1 + ) % ;
V(mutex1);
V(full1);
}
}
/*P1-P3略*/
coend
semaphore waits = ;
Semaphore mutex = ;
int numbersum = ;
cobegin
process readeri(int number) {
P(mutex);
while(numbersum+number>=M) {
V(mutex);
P(waits);
}
numbersum = numbersum + number;
V(mutex);
Read file;
P(mutex);
numbersum = numbersum - number;
V(waits);
V(mutex);
}
coend
semaphore s1 = ; //可捡白子
semaphore s2 = ; //可捡黑子
process p1() {
while(true) {
P(s1);
捡白子;
V(s2);
}
}
process p2() {
while(true) {
P(s2);
捡白子;
V(s1);
}
}
Semaphore allow = -;
Semaphore mutex = ;
int count = count_black = count_black = ;
process A(){
while(true){
P(mutex);
get(black);
V(mutex);
if(取到了){count_black++;}
else break;
}
V(allow);
}
process B(){
while(true){
P(mutex);
get(write);
V(mutex);
if(取到了){count_write++;}
else break;
}
V(allow);
}
process C(){
P(allow);
count = count_black + count_write;
printf();
}
semaphore mutex , empty[n2] , full[n2];
int i;
mutex = ;
for(i=; i<n2; i++) {
empty[i]=m;
full[i]=;
}
cobegin
send() {
for(int i=;i<n2;i++) {
P(empty[i]);
P(mutex);
{将消息放入缓冲区;}
V(mutex);
}
for(int i=;i<n2;i++){
V(full[i]);
}
}
receive() {
for(int i=;i<n2;i++) {
P(full[i]);
P(mutex);
{从缓冲区中取出信息;}
V(mutex);
}
for(int i=;i<n2;i++) {
V(empty[i]);
}
}
coend
int buf[];
int count = getptr = putptr = ;
semaphore s1 = s2 = ;
Semaphore sput = ;
Semaphore sget = ;
cobegin
process producer_i() {
while(true) {
P(sput);
P(s1);
buf[putptr] = ;
putptr = ( putptr + ) % ;
buf[putptr] = ;
putptr = ( putptr + ) % ;
buf[putptr] = ;
putptr = ( putptr + ) % ;
V(sget);
V(sget);
V(sget);
V(s1);
}
}
process consumer_j() {
int y;
while(true) {
P(sget);
P(s2);
y = buf[getptr];
getptr = ( getptr + ) % ;
count++;
if(count==){
count = ;
V(sput);
}
V(s2);
消费整数y;
}
}
coend
semaphore mutex1 = mutex2 = ;
semaphore s1 = N/ , s2 = 4N/ , s3 = s4 = ;
int count = ;
int in1 = in2 = out1 = out2 = ;
cobegin
process worker1() {
while(true) {
加工一个车架;
P(s1);
P(mutex1);
车架放入box[in1];
in1 = (in1 +) % (N/);
V(mutex1);
V(s3);
}
process worker2() {
while(true) {
加工一个车轮;
P(s2);
P(mutex2);
车架放入box[in2];
in2 = (in2 +) % (4N/);
count = count + ;
if(count==) {
count = ;
V(s4);
}
V(mutex2);
}
process worker3() {
while(true) {
P(s3);
P(mutex1);
从box[out1]拿车架;
out1 = (out1 + ) % (N/);
V(mutex1);
V(s1);
P(s4);
P(mutex2);
从box[out2]开始取四个车轮;
out2 = (out2 + ) % (4N/);
V(s2);
V(mutex2);
装配车子;
}
}
ceend
semaphore mutex=;
semaphore sx = M-;
semaphore sy = N-;
cobegin
process storeX( ) {
while(true) {
P(sx);
P(mutex);
{将X产品入库};
V(mutex);
V(sy);
}
}
process storeY( ) {
while(true) {
P(sy);
P(mutex);
{将Y产品入库};
V(mutex);
V(sx);
}
}
coend
semaphore empty1 , empty2 , full1 , full2 , mutex , sa , sb;
int in1 , in2 , out1 , out2;
empty1 = empty2 = m; //剩余可放空间
full1 = full2 = ; //剩余产品数
sa = sb = n;
mutex = ;
in1 = in2 = out1 = out2 = ;
cobegin
process put_A() {
while(true) {
P(empty1);
P(sa);
P(mutex);
把A零件放入buffA[in1];
in1 = (in1 + ) % m;
V(mutex);
V(sb);
V(full1);
}
}
process put_B() {
while(true) {
P(empty2);
P(sb);
P(mutex);
把B零件放入buffB[in2];
in2 = (in2 + ) % m;
V(mutex);
V(sa);
V(full2);
}
}
process get() {
while(true) {
P(full1);
P(full2);
P(mutex);
从buffA[out1]取A零件;
从buffB[out2]取B零件;
out1 = (out1 + ) % m;
out2 = (out2 + ) % m;
V(mutex);
V(empty1);
V(empty2);
把A和B装配成产品;
}
}
coend
Semaphore empty = ;
Semaphore mutex = ;
cobegin
process_i(){
P(empty);
P(mutex);
进门;
V(mutex);
参观;
P(mutex);
出门;
V(mutex);
V(empty);
}
typedef struct{
float a;
float b;
}cnum;
cnum x , y ,z;
cnum add(cnum p ,cnum q){
cnum s;
s.a = p.a + q.a;
s.b = s.b + q.b;
return s;
}
Semaphore mutex_y1 = ; //线程1、3对y的互斥访问
Semaphore mutex_y2 = ; //线程2、3对y的互斥访问
Semaphore mutex_z = ; //线程2、3对z的互斥访问 thread1{
cnum w;
P(mutex_y1);
w = add(x , y);
V(mutex_y1);
}
thread2{
cnum w;
P(mutex_y2);
P(mutex_z);
w = add(y , z);
V(mutex_z);
V(mutex_y2);
}
thread3{
cnum w;
w.a = ;
w.b = ;
P(mutex_z);
z = add(z , w);
V(mutex_z);
P(mutex_y1);
P(mutex_y2)
y = add(y , z);
V(mutex_y1);
V(mutex_y2);
}
Semaphore mutex = ;
Sempahore full = ;
Semaphore empty = ;
process P(){
P(empty);
P(mutex);
read20();
V(mutex);
V(full);
}
Process Q1(){
P(full); get20();
P(full); get20();
comp40t30();
V(empty); print15;
V(empty); print15;
}
操作系统-PV习题的更多相关文章
- linux操作系统 - 综合习题
登录超级用户,完成以下操作: [linux@slave053 ~]$ su - 1.用户和组群管理(本大题共5小题,共10分) (1)创建两个用户tangseng,monkey,并指定密码为12345 ...
- 操作系统-PV操作的原理和几种常见问题
信号量是一种变量类型,用一个记录型数据结构表示,有两个分量:信号量的值和信号量队列指针 除了赋初值外,信号量仅能通过同步原语PV对其进行操作 s.value为正时,此值为封锁进程前对s信号量可施行的P ...
- 操作系统PV编程题目总结一
1.今有一个文件F供进程共享,现把这些进程分为A.B两组,规定同组的进程可以同时读文件F:但当有A组(或B组)的进程在读文件F时就不允许B组(或A组)的进程读文件F.试用P.V操作(记录型信号量)来进 ...
- 操作系统实验——PV操作实现生产者消费者模型
操作系统PV操作之--生产者消费者模型 个人博客主页 参考资料: Java实现PV操作 | 生产者与消费者 浙大公开课 在操作系统的多进程.多线程操作中经常会有因为同步.互斥等等问题引发出的一系列问题 ...
- Python笔记(十一):多线程
(二)和(三)不感兴趣的可以跳过,这里参考了<深入理解计算机系统>第一章和<Python核心编程>第四章 (一) 多线程编程 一个程序包含多个子任务,并且子任务之间相 ...
- 零基础一年拿下BAT三家offer
背景 1.本人本科一本双非垫底的那种,硕士211.本硕电子通信,完全0基础,转行一年. 2.研一上第一学期上课+外派到老师合作公司写MATLAB.去年4月开始学习Java. 起步 1.实话说,刚决定转 ...
- log4j 多进程配置要注意的
多进程写日志文件 方法一: 解决log4j公用配置文件,多进程同时写同一个log文件,因存在操作系统pv操作问题, 导致部分日志丢失.解决方案是不同的进程写不同的log文件 测试于:Log4j 1.2 ...
- Operating System:信号量
pv原语操作(1)操作系统PV意思:PV操作与信号量的处理相关,P表示通过的意度思,V表示释放的意思.(2)p操作和v操作是不可中断问的程序段,称为原语.如果将信号量看作共享变量,则pv操作为其临界区 ...
- 整型信号量和PV操作(计算机操作系统)
在整型信号量机制中,信号量被定义为一个整形变量.除初始化外,仅能通过两个标准的原子操作Wait(S)和Signal(S)来访问.其通常分别被称为P.V操作. 描述如下: P操作:S=S-1:如果S小于 ...
随机推荐
- kafka笔记——kafka启动
1. 准备 阿里云Linux服务器一个(Centos7) 腾讯云Linux服务器一个(CentOs7) zookeeper,kafka压缩包 Java环境配置好 要死....脚本之家 2. 安装 zo ...
- 使用vuex做列表数据过滤
功能需求 展示一个表格,表格包含选项有" 姓名 年龄 是否显示"三个列表项 是否显示列表项是可操作开关,点击切换打开/关闭 将表格中开关为打开状态的列表项,在另一个表格中显示 需求 ...
- ref以及传值传址的理解
ref(也包括out)关键字肯定都会用,传值调用和传址调用也是初学写代码时都已经历过的话题,与这相关的还有一些话题,比如值类型和引用类型有什么区别等,但是如果不仔细,可能有一些概念的混淆或者理解不够清 ...
- PHP date_timezone_set() 函数
------------恢复内容开始------------ 实例 设置 DateTime 对象的时区: <?php$date=date_create("2013-05-25" ...
- PHP is_resource() 函数
is_resource() 函数用于检测变量是否为资源类型. PHP 版本要求: PHP 4, P+-HP 5, PHP 7高佣联盟 www.cgewang.com 语法 bool is_resour ...
- PHP asin() 函数
实例 返回不同数的反正弦: <?phpecho(asin(0.64) . "<br>");echo(asin(-0.4) . "<br>&q ...
- 记录一次线上实施snmp
公司要实施一个部级的项目,我们公司的提供的产品要对接下客户的一个平台监控平台,该监控平台使用snmp,我们公司的产品不支持snmp,所以由我负责在现网实施snmp,记录这次现网 一.生成编译规则 1. ...
- maven个人配置
settings.xml 文件修改一下内容 本地 <localRepository>D:\maven\repository</localRepository> 远程:修改成国内 ...
- 一个简单的CPP处理框架
好久没有在csdn上写过东西了,这么多年,一方面是工作忙,下班到家也没有开过电脑了,要陪小孩玩: 下面分享一段代码,是用CPP做的一个简单的消息(协议)处理框架: 是通过成员函数指针+map来实现的: ...
- Python最好IDE:Pycharm使用小技巧总结,让你写代码更为舒适