操作系统实验报告barber实验.docx

上传人:b****4 文档编号:4122608 上传时间:2022-11-28 格式:DOCX 页数:33 大小:64.28KB
下载 相关 举报
操作系统实验报告barber实验.docx_第1页
第1页 / 共33页
操作系统实验报告barber实验.docx_第2页
第2页 / 共33页
操作系统实验报告barber实验.docx_第3页
第3页 / 共33页
操作系统实验报告barber实验.docx_第4页
第4页 / 共33页
操作系统实验报告barber实验.docx_第5页
第5页 / 共33页
点击查看更多>>
下载资源
资源描述

操作系统实验报告barber实验.docx

《操作系统实验报告barber实验.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告barber实验.docx(33页珍藏版)》请在冰豆网上搜索。

操作系统实验报告barber实验.docx

操作系统实验报告barber实验

实验题目:

barber实验

 

1、实验目的

进一步研究和实践操作系统中关于并发进程同步与互斥操作的一些经典问题的解法,加深对于非对称性互斥问题有关概念的理解。

观察和体验非对称性互斥问题的并发控制方法。

2、实验原理

理发店有一个出口一个入口,没顾客的时候理发师在打瞌睡,有顾客的时候理发师在椅子上等待的顾客中选出顾客来理发,顾客进店就坐在椅子上等待(也有做沙发),没有椅子坐的人就站着排队(也有的省去站着排队的环节,以下就是),连站着排队的位置也没有的时候就直接离开。

它的实质是生产者和消费者的问题。

3、实验代码

1.编写Customer.java

publicclassCustomerextendsThread{

privateBarberShopApplettapplet;

privateBarberShopshop;

privateintcid;

intdelay=2500;

intstatus=0;

intcutFinish=0;

intbarberID=0;

intpaid=0;

publicCustomer(BarberShopAppletapplet,BarberShopiq,intid){

shop=iq;

tapplet=applet;

cid=id;

}

publicvoidrun(){

try{

status=0;

tapplet.mc.println(status,"c",cid);

shop.sitSofa(tapplet,cid);

sleep(delay);

shop.sitBarberChair(tapplet,cid);

shop.waitPay(tapplet,cid);

}catch(InterruptedExceptione){

System.err.println("CustomerException"+e.toString());

}

}

}

2.编写Barber.java

publicclassBarberextendsThread{

privateBarberShopshop;

privateBarberShopApplettapplet;

privateintpid;

intdelay=2500;

intstatus=0;

intcustomerID=0;

publicBarber(BarberShopAppletapplet,BarberShopiq,intid){

shop=iq;

tapplet=applet;

pid=id;

}

publicvoidrun(){

while(true){

try{

status=0;

tapplet.mc.println(status,"b",pid);

sleep((int)(Math.random()*delay));

shop.cutHair(tapplet,pid);

sleep((int)(Math.random()*delay));

shop.finishCut(tapplet,pid);

}catch(InterruptedExceptione){

System.err.println("Exception"+e.toString());

}

}

}

}

3.编写BarberShop.java

importjava.awt.*;

publicclassBarberShopextendsCanvas

{

privateintchairSize=3;

privateintsofaSize=4;

privateintframeDelay=3560;

privateint[]customerSofaQ;//thequeuetoholdthecustomersonthesofa

privateint[]customerStandQ;//thequeuetoholdthecustomersonthestandingarea

privateint[]customerChairQ;//thequeuetoholdthecustomersonBarberChairs

privateint[]customerPayQ;//thequeuetoholdthecustomerswaitingforpaying.

privateint[]customerReady;//cutomerReady[i]=1,customeriisreadyforbarber1

privateint[]finishedCustomerQ;//thearraytoholdthecutfinishflags

privateint[]paidCustomerQ;

privateint[]exitArray;//thearrayholdallthecustomersintheordertheyexitshop

privateintsofaTop,sofaBottom;//forcustomerSofaQ

privateintchairTop,chairBottom;//forcustomerChairQ

privateintpayTop,payBottom;//forcustomerPayQ

privateintcustomerTop,customerBottom;//forcustomersQ

privateintcustomerOnSofa;//thecountofcustomersonthesofa

privateintcustomerOnChair;//thecountofcustomersonthebarberchairs

privateintcustomerStandCount;//thecountofcustomersstanding

privateintwantPayCount;//thecountofcustomerswaitingforpaying

privateinthasCashier;

privateintcashierID;//thebarberIDforwhoisperformingasacashier

privateintexitID;//thecustomerIDwhoisleavingthebarbershop

privateintexitTop;

privateintcustomerCount;

privateintsize;

privateint[]customerOut;

privateintoutTop;

privateintoutBottom;

privateintrepaintFlag=0;

privateFontfont;

privateFontMetricsfm;

privateintx;//customerconsumeditem

publicBarberShop(){

size=4;//defaultbuffersize

customerTop=customerBottom=1;

payTop=payBottom=1;

chairTop=chairBottom=0;

sofaTop=sofaBottom=0;

customerCount=0;

customerOnSofa=0;

customerOnChair=0;

customerStandCount=0;

wantPayCount=0;

hasCashier=0;

cashierID=0;

exitID=0;

exitTop=0;

finishedCustomerQ=newint[11];

customerOut=newint[2];

outTop=outBottom=0;

setSize(size);

resize(500,300);

setBackground(Color.white);

font=newFont("TimesRoman",Font.BOLD,18);

fm=getFontMetrics(font);

}

publicvoidsetSize(ints)

{

size=s;

if(size>8)customerStandCount=8;

elsecustomerStandCount=size;

inttmpCount=0;

if(size>8)

{tmpCount=size-8;

System.out.println("thetmpCountis"+tmpCount);

for(inti=0;i

{

customerOut[i]=9+i;

}}

outBottom=0;

outTop=1;

customerSofaQ=newint[sofaSize];

customerChairQ=newint[chairSize+1];

customerPayQ=newint[11];

customerReady=newint[size+1];//themaximumcustomersizeis10

paidCustomerQ=newint[size+1];

exitArray=newint[size];

for(inti=1;i<=size;i++)

{

customerReady[i]=0;

}

repaint();

}

publicsynchronizedbooleanchairFull(){

returncustomerOnChair==chairSize;

}

publicsynchronizedbooleansofaFull(){

returncustomerOnSofa==sofaSize;

}

publicsynchronizedvoidcutHair(BarberShopAppletapplet,intid)

{

if(customerReady[id]==0)getCashierLock(applet,id);

if(cashierID==id)performCashier(applet,id);

while(customerReady[id]==0)//ifthereisnocustomeriswaiting

{

updateBarberStatus(applet,id,4);

try{wait();}catch(InterruptedExceptione){

System.err.println("Exception"+e.toString());

}

}

System.out.println("customerReadyare:

");

for(inti=0;i<=3;i++)

{

System.out.println(Integer.toString(customerReady[i]));

}

intx=customerReady[id];

applet.b[id].customerID=x;

applet.c[x].barberID=id;

System.out.println("xis"+x);

applet.b[id].status=1;

applet.mc.println(applet.b[id].status,"b",id,x);

updateCustomerStatus(applet,x,1);//cuttingHair

//repaint();

notifyAll();

}

publicsynchronizedvoidfinishCut(BarberShopAppletapplet,intid)

{

customerReady[id]=0;

inty=applet.b[id].customerID;

if(applet.haltFlag==1)

{

if(y!

=1)

{

updateCustomerStatus(applet,y,10);

updateBarberStatus(applet,id,1);

}

else

{

while(true)/*tokeepthebarberstatusincuttinghair*/

{

try{wait();}catch(InterruptedExceptione){}

}

}

}

elseif(applet.requestFlag==1)

{

System.out.println("processis"+y);

if(y==1)

{

while(finishedCustomerQ[2]!

=1)

{try{wait();}catch(InterruptedExceptione){}

}

updateCustomerStatus(applet,y,11);

}

elseif(y==3)

{

while(finishedCustomerQ[2]!

=1)

{

try{wait();}catch(InterruptedExceptione){}

}

updateCustomerStatus(applet,y,7);

}

else

{

updateCustomerStatus(applet,y,7);//waitingforpay

}

customerChairQ[id]=0;

applet.b[id].customerID=0;

applet.c[y].barberID=0;

repaint();

finishedCustomerQ[y]=1;

notifyAll();

wantPayCount++;

customerPayQ[y]=y;

repaint();

customerOnChair--;

notifyAll();

if(wantPayCount>0)getCashierLock(applet,id);

if(cashierID==id)performCashier(applet,id);

elseupdateBarberStatus(applet,id,4);

}

else//Tohandletheprocessesinfairsituation

{

updateCustomerStatus(applet,y,7);

customerChairQ[id]=0;

applet.b[id].customerID=0;

applet.c[y].barberID=0;

repaint();

System.out.println("customer"+y+"finishcutting");

finishedCustomerQ[y]=1;

wantPayCount++;

customerPayQ[payTop]=y;

payTop++;

repaint();

customerOnChair--;

notifyAll();

if(wantPayCount>0)getCashierLock(applet,id);

if(cashierID==id)performCashier(applet,id);

elseupdateBarberStatus(applet,id,4);

}

}

publicsynchronizedvoidsitSofa(BarberShopAppletapplet,intid)

{

while(customerBottom!

=id)

{

System.out.println("customer"+id+"iswaitingfortheturn");

try{wait();}catch(InterruptedExceptione){}

}

customerCount++;

notifyAll();

if(id>8)

{customerStandCount++;

outBottom++;

repaint();

}

while(sofaFull())

{

try{wait();}catch(InterruptedExceptione){}

}

customerBottom++;

notifyAll();

customerOnSofa++;

customerStandCount--;

customerSofaQ[sofaTop]=id;

sofaTop=(sofaTop+1)%sofaSize;

repaint();

updateCustomerStatus(applet,id,5);//sittingonsofa

notifyAll();

}

publicsynchronizedvoidsitBarberChair(BarberShopAppletapplet,intid)

{

while(customerSofaQ[sofaBottom]!

=id)

{

System.out.println("Customer"+id+"iswaitingforthechairturn");

try{wait();}catch(InterruptedExceptione){}

}

while(chairFull())

{

try{wait();}catch(InterruptedExceptione){}

}

customerSofaQ[sofaBottom]=0;

sofaBottom=(sofaBottom+1)%sofaSize;//getupfromsofa

customerOnSofa--;

customerOnChair++;

for(inti=1;i<=chairSize;i++)

{

if(customerChairQ[i]==0)

{

customerChairQ[i]=id;

customerReady[i]=id;

i=chairSize;//getoutoftheloop

}

}

updateCustomerStatus(applet,id,6);

repaint();

try{

applet.c[id].sleep((int)(Math.random()*frameDelay));

}catch(InterruptedExceptione){}

notifyAll();

}

publicsynchronizedvoidgetCashierLock(BarberShopAppletapplet,intbid)

{

if((wantPayCount>0)&&(hasCashier!

=1))

{

hasCashier=1;

cashierID=bid;

//updateBarberStatus(applet,bid,9);//acashierrightnow

repaint();

System.out.println("Barber"+bid+"gotthecashierLockrightnow");

notifyAll();

}

}

publicsynchronizedvoidperformCashier(BarberShopAppletapplet,intbid)

{

while(wantPayCount>0)

{

System.out.println("Barber"+bid+"isacashierrightnow");

updateBarberStatus(applet,bid,2);

try{wait();}catch(InterruptedExceptione){}

}

cashierID=0;

hasCashier=0;

notifyAll();

}

publicsynchr

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1