操作系统实验报告barber实验Word下载.docx
- 文档编号:6796329
- 上传时间:2023-05-07
- 格式:DOCX
- 页数:38
- 大小:64.22KB
操作系统实验报告barber实验Word下载.docx
《操作系统实验报告barber实验Word下载.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告barber实验Word下载.docx(38页珍藏版)》请在冰点文库上搜索。
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{
privateintpid;
intcustomerID=0;
publicBarber(BarberShopAppletapplet,BarberShopiq,intid){
shop=iq;
pid=id;
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);
Exception"
}
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;
8)
{tmpCount=size-8;
System.out.println("
thetmpCountis"
+tmpCount);
for(inti=0;
i<
tmpCount;
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;
=size;
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){
customerReadyare:
"
);
=3;
System.out.println(Integer.toString(customerReady[i]));
intx=customerReady[id];
applet.b[id].customerID=x;
applet.c[x].barberID=id;
xis"
+x);
applet.b[id].status=1;
applet.mc.println(applet.b[id].status,"
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]!
{try{wait();
updateCustomerStatus(applet,y,11);
elseif(y==3)
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;
customerOnChair--;
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();
customer"
+y+"
finishcutting"
finishedCustomerQ[y]=1;
wantPayCount++;
customerPayQ[payTop]=y;
payTop++;
customerOnChair--;
notifyAll();
if(wantPayCount>
if(cashierID==id)performCashier(applet,id);
elseupdateBarberStatus(applet,id,4);
publicsynchronizedvoidsitSofa(BarberShopAppletapplet,intid)
while(customerBottom!
=id)
+id+"
iswaitingfortheturn"
customerCount++;
if(id>
{customerStandCount++;
outBottom++;
while(sofaFull())
try{wait();
}catch(InterruptedExceptione){}
customerBottom++;
customerOnSofa++;
customerStandCount--;
customerSofaQ[sofaTop]=id;
sofaTop=(sofaTop+1)%sofaSize;
updateCustomerStatus(applet,id,5);
//sittingonsofa
publicsynchronizedvoidsitBarberChair(BarberShopAppletapplet,intid)
while(customerSofaQ[sofaBottom]!
Customer"
iswaitingforthechairturn"
}catch(InterruptedExceptione){}
while(chairFull())
try{wait();
customerSofaQ[sofaBottom]=0;
sofaBottom=(sofaBottom+1)%sofaSize;
//getupfromsofa
customerOnSofa--;
customerOnChair++;
for(inti=1;
=chairSize;
if(customerChairQ[i]==0)
customerChairQ[i]=id;
customerReady[i]=id;
i=chairSize;
//getoutoftheloop
updateCustomerStatus(applet,id,6);
applet.c[id].sleep((int)(Math.random()*frameDelay));
}catch(InterruptedExceptione){}
publicsynchronizedvoidgetCashierLock(BarberShopAppletapplet,intbid)
if((wantPayCount>
0)&
&
(hasCashier!
=1))
hasCashier=1;
cashierID=bid;
//updateBarberStatus(applet,bid,9);
//acashierrightnow
Barber"
+bid+"
gotthecashierLockrightnow"
publicsynchronizedvoidperformCashier(BarberShopAppletapplet,intbid)
while(wantPayCount>
0)
isacashierrightnow"
updateBarberStatus(applet,bid,2);
cashierID=0;
hasCashier=0;
publicsynchr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 操作系统 实验 报告 barber