1自行车生产线上有一箱子.ppt

1自行车生产线上有一箱子.ppt

ID:48141744

大小:135.50 KB

页数:27页

时间:2020-01-17

1自行车生产线上有一箱子.ppt_第1页
1自行车生产线上有一箱子.ppt_第2页
1自行车生产线上有一箱子.ppt_第3页
1自行车生产线上有一箱子.ppt_第4页
1自行车生产线上有一箱子.ppt_第5页
资源描述:

《1自行车生产线上有一箱子.ppt》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、习题课1.自行车生产线上有一箱子,其中有N个位置(N≥3),每个位置可放一个车轮或一个车架;又设有3个工人P1,P2,P3其活动分别为:P1:P2;P3:加工一车架;加工一个车轮;箱中取1车架;将车架放入箱中;将车轮放入箱中;箱中取2车轮;组装为一辆车;用PV操作实现三个工人的合作(不考虑死锁)Semaphareempty=N,wheel=0,frame=0;P1:P2:P3:加工一车架;加工一车轮;p(frame);取一车架;P(empty);p(empty);v(empty);车架放入箱中;车轮放入箱中;p(wheel);P(wheel);

2、取2个车轮;v(frame);v(wheel);v(empty);v(empty)组装一车子;死锁分析当1工人推进速度快,箱中有N个车架或有N-1个车架,且P3已拿到一个车架同时申请2个车轮时,出现死锁;同理当箱中有N个车轮,且P3尚未拿到车架时也出现死锁.考虑死锁的算法Semaphoreempty=N;frame=0;wheel=0;n1=N-2;n2=N-1;P1:P2:P3:加工一车架;加工一车轮;p(frame);P(n1);p(n2);取一车架;P(empty);p(empty);v(empty);车架放入箱中;车轮放入箱中;v(n1

3、);V(frame);v(wheel);p(wheel);p(wheel);取2车轮;v(empty);v(empty)v(n2);v(n2)组装一个车子;用管程实现三个工人的合作TypeBox=monitor;enumitem={frame,wheel,null}Varbox:array[0….N]ofitem;count1,count2:integer;full,frameempty,wheelempty:condition;in,out:integer;Defineputin-frame,putin-wheel,takeout-frame

4、,takeout-wheelProduceputin-frame(x:frame)beginif(count1+count2)≥Nthenwait(full);ifcount1>N-2thenwait(full);count1:=count1+1;fori=1toNifbox[i]=nullthenbreak;in:=i;box[in]:=x;signal(frameempty);endProduceputin-wheel(y:wheel)beginif(count1+count2)≥Nthenwait(full);ifcount2>N-1th

5、enwait(full);count2:=count2+1;fori=1toNifbox[i]=nullthenbreak;in:=i;box[in]:=y;signal(wheelempty);endProducetakeout-frame(x:frame)beginifcount1<1thenwait(bodyempty);count1:=count1-1;fori=1toNifbox[i]=framethenbreakout:=i;x:=box[out];signal(full);endProducetakeout-wheel(y:whe

6、el)beginifcount2<1thenwait(wheelempty);count2:=count2-1;fori=1toNifbox[i]=wheelthenbreakout:=i;y:=box[out];signal(full);endProduceworkeroneProduceworkerthreevarx:frame;varx:frame;beginy:wheel;repeatbeginproduce(x);repeatBox.putin-frame(x);Box.takeout-frame(x);foreverBox.take

7、out-wheel(y);Box.takeout-wheel(y);endmakebike;Produceworkertwoforevervary:wheel;endbeginCobeginrepeatworkerone;produce(y);workertwo;Box.putin-wheel(y);workerthree;foreverCoendend用会合实现三个工人的合作Procedurethree-workeristasktypeworkerone;taskworkertwo;taskworkerthree;taskcaseisentr

8、ydeliverframe;entrydeliverwheel;entrytakeframe;entrytakewheel;endcaseTaskbo

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。