《操作系统实验报告barber实验.docx》由会员分享,可在线阅读,更多相关《操作系统实验报告barber实验.docx(25页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、操作系统实验报告实验题目:barber实验一、 实验目的进一步研究和实践操作系统中关于并发进程同步与互斥操作的一些经典问题的解法,加深对于非对称性互斥问题有关概念的理解。观察和体验非对称性互斥问题的并发控制方法。 二、 实验原理 理发店有一个出口一个入口,没顾客的时候理发师在打瞌睡,有顾客的时候理发师在椅子上等待的顾客中选出顾客来理发,顾客进店就坐在椅子上等待(也有做沙发),没有椅子坐的人就站着排队(也有的省去站着排队的环节,以下就是),连站着排队的位置也没有的时候就直接离开。它的实质是生产者和消费者的问题。三、 实验代码1. 编写Customer.javapublic class Custo
2、mer extends Threadprivate BarberShopApplet tapplet;private BarberShop shop;private int cid;int delay = 2500;int status= 0;int cutFinish = 0;int barberID = 0;int paid = 0;public Customer(BarberShopApplet applet, BarberShop iq, int id)shop = iq;tapplet = applet;cid = id;public void run() try status =
3、0; tapplet.mc.println(status, c, cid); shop.sitSofa(tapplet, cid); sleep(delay); shop.sitBarberChair(tapplet, cid); shop.waitPay(tapplet, cid); catch(InterruptedException e)System.err.println(Customer Exception + e.toString();2. 编写Barber.javapublic class Barber extends Threadprivate BarberShop shop;
4、private BarberShopApplet tapplet;private int pid;int delay = 2500;int status = 0;int customerID = 0;public Barber(BarberShopApplet applet, BarberShop iq, int id)shop = iq;tapplet = applet;pid = id;public void run()while(true)trystatus = 0;tapplet.mc.println(status, b, pid);sleep(int)(Math.random()*d
5、elay);shop.cutHair(tapplet, pid);sleep(int) (Math.random()*delay);shop.finishCut(tapplet, pid); catch(InterruptedException e)System.err.println(Exception + e.toString();3. 编写BarberShop.javaimport java.awt.*;public class BarberShop extends Canvasprivate int chairSize = 3;private int sofaSize = 4;priv
6、ate int frameDelay = 3560;private int customerSofaQ;/the queue to hold the customers on the sofaprivate int customerStandQ;/the queue to hold the customers on the standing areaprivate int customerChairQ;/the queue to hold the customers on Barber Chairsprivate int customerPayQ;/the queue to hold the
7、customers waiting for paying.private int customerReady; /cutomerReadyi = 1, customer i is ready for barber 1private int finishedCustomerQ; /the array to hold the cut finish flagsprivate int paidCustomerQ;private int exitArray; /the array hold all the customers in the order they exit shopprivate int
8、sofaTop, sofaBottom;/for customerSofaQprivate int chairTop, chairBottom;/for customerChairQprivate int payTop, payBottom;/for customerPayQprivate int customerTop, customerBottom;/for customersQprivate int customerOnSofa; /the count of customers on the sofaprivate int customerOnChair;/the count of cu
9、stomers on the barber chairsprivate int customerStandCount; /the count of customers standingprivate int wantPayCount; /the count of customers waiting for payingprivate int hasCashier;private int cashierID;/the barber ID for who is performing as a cashierprivate int exitID;/the customer ID who is lea
10、ving the barbershopprivate int exitTop;private int customerCount;private int size;private int customerOut;private int outTop;private int outBottom;private int repaintFlag = 0;private Font font;private FontMetrics fm;private int x; /customer consumed itempublic BarberShop( )size = 4; /default buffer
11、sizecustomerTop = 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 = new int11;customerOut
12、 = new int2;outTop = outBottom = 0;setSize(size);resize(500, 300);setBackground(Color.white);font = new Font(TimesRoman, Font.BOLD, 18);fm = getFontMetrics(font); public void setSize(int s)size = s;if(size 8) customerStandCount = 8;else customerStandCount = size;int tmpCount = 0;if(size 8) tmpCount
13、= size - 8; System.out.println(the tmpCount is + tmpCount); for(int i = 0; i tmpCount; i+) customerOuti = 9+i; outBottom = 0;outTop = 1;customerSofaQ = new intsofaSize;customerChairQ = new intchairSize+1;customerPayQ = new int11;customerReady = new intsize+1; /the maximum customer size is 10paidCust
14、omerQ = new intsize+1;exitArray = new intsize;for(int i = 1; i =size ; i+)customerReadyi = 0;repaint();public synchronized boolean chairFull()return customerOnChair = chairSize;public synchronized boolean sofaFull()return customerOnSofa = sofaSize;public synchronized void cutHair(BarberShopApplet ap
15、plet, int id)if(customerReadyid = 0) getCashierLock(applet, id);if(cashierID = id) performCashier(applet, id);while(customerReadyid = 0)/if there is no customer is waitingupdateBarberStatus(applet, id, 4);try wait(); catch(InterruptedException e)System.err.println(Exception + e.toString(); System.ou
16、t.println(customerReady are: );for(int i = 0; i 0) getCashierLock(applet, id);if(cashierID = id) performCashier(applet, id);else updateBarberStatus(applet, id, 4);else/ To handle the processes in fair situationupdateCustomerStatus(applet, y, 7);customerChairQid= 0;applet.bid.customerID = 0;applet.cy
17、.barberID = 0;repaint();System.out.println(customer + y + finish cutting);finishedCustomerQy = 1;wantPayCount +;customerPayQpayTop = y;payTop+;repaint();customerOnChair -;notifyAll();if(wantPayCount 0) getCashierLock(applet, id);if(cashierID = id) performCashier(applet, id);else updateBarberStatus(a
18、pplet, id, 4);public synchronized void sitSofa(BarberShopApplet applet, int id)while(customerBottom != id)System.out.println(customer + id + is waiting for the turn);try wait(); catch(InterruptedException e) customerCount+;notifyAll();if(id 8) customerStandCount +; outBottom +; repaint(); while(sofa
19、Full()try wait();catch(InterruptedException e) customerBottom+;notifyAll();customerOnSofa +;customerStandCount -;customerSofaQsofaTop = id;sofaTop =(sofaTop+1)%sofaSize;repaint();updateCustomerStatus(applet, id, 5); /sitting on sofanotifyAll();public synchronized void sitBarberChair(BarberShopApplet
20、 applet, int id) while(customerSofaQsofaBottom != id) System.out.println(Customer + id + is waiting for the chair turn); try wait(); catch(InterruptedException e) while(chairFull()trywait();catch(InterruptedException e) customerSofaQsofaBottom = 0;sofaBottom =(sofaBottom+1)%sofaSize;/get up from sof
21、acustomerOnSofa -;customerOnChair +;for(int i = 1; i 0) & (hasCashier!= 1) hasCashier= 1; cashierID = bid; /updateBarberStatus(applet, bid, 9); / a cashier right now repaint(); System.out.println(Barber + bid + got the cashier Lock right now); notifyAll(); public synchronized void performCashier(Bar
22、berShopApplet applet, int bid) while(wantPayCount 0) System.out.println(Barber + bid + is a cashier right now); updateBarberStatus(applet, bid, 2); try wait(); catch(InterruptedException e) cashierID = 0; hasCashier= 0; notifyAll(); public synchronized void waitPay(BarberShopApplet applet, int cid)
23、while(customerPayQpayBottom != cid) if(applet.requestFlag = 1) & (cid = 3) repaintFlag = 1; try wait(); catch(InterruptedException e) if(applet.requestFlag = 1)/ for 1st process in unfair situationwhile(true)try applet.ccid.sleep(int) (Math.random()*frameDelay); catch(InterruptedException e) while(h
24、asCashier!= 1) try wait(); catch(InterruptedException e) try applet.ccid.sleep(int) (Math.random()*frameDelay); catch(InterruptedException e) updateCustomerStatus(applet, cid, 9);payBottom+;wantPayCount -;exitID = cid;exitArrayexitTop = cid;exitTop +;customerCount -;repaint();notifyAll(); public syn
25、chronized void updateCustomerStatus(BarberShopApplet applet, int cid, int status) applet.ccid.status = status; applet.mc.println(status, c, cid); public synchronized void updateBarberStatus(BarberShopApplet applet, int bid, int status) applet.bbid.status = status; applet.mc.println(status, b, bid);
26、public void clear() size = 4; /default buffer size customerCount = 0; payTop = payBottom = 1; chairTop = chairBottom = 0; sofaTop = sofaBottom = 0; customerTop= customerBottom= 1; outTop = outBottom = 0; finishedCustomerQ = new int11; customerOut= new int2; customerOnSofa = 0;/the count of customers
27、 on the sofa customerOnChair = 0;/the count of customers on the barber chairs customerStandCount = 0;/the count of customers standing wantPayCount = 0;/the count of customers waiting for paying hasCashier= 0; cashierID = 0; /the barber ID for who is performing as a cashier exitID = 0; exitTop= 0; re
28、paintFlag = 0; public void paint(Graphics g)g.setFont(new Font(TimesRoman, Font.BOLD, 12);g.setColor(Color.blue);int xpos = 120;int ypos = 10;g.setFont(new Font(TimesRoman, Font.BOLD, 18);g.drawString(Barber Chairs, xpos+150, ypos+5);for(int i = 1; i = chairSize; i+)g.draw3DRect(xpos+100+70*(i-1), y
29、pos+20, 28, 28, true);if(i != cashierID)g.drawString(B+i, xpos+103+70*(i-1), ypos+70);g.setColor(Color.red);for(int j=1; j = chairSize; j +)if(customerChairQj != 0)g.drawString(Integer.toString(customerChairQj), xpos + 105 + 70*(j-1), ypos+35);g.draw3DRect(xpos+100+70*(j-1), ypos+20, 28, 28, true);g
30、.setColor(Color.blue);g.drawString(Cashier, xpos+410, ypos+45);g.setFont(new Font(TimesRoman, Font.BOLD, 14);if(cashierID != 0)g.drawString(B +cashierID, xpos+430, ypos+20);g.draw3DRect(xpos+410, ypos+60, 60, 20, true);g.setFont(new Font(TimesRoman, Font.BOLD, 12);int b = payBottom;System.out.printl
31、n(wantPaycount is + wantPayCount);if(repaintFlag = 1)for(int i = 0; i 3; i+)if(customerPayQi+b != 0)g.drawString(C+customerPayQi+b, xpos+430, ypos+100);ypos += 20;elsefor(int i = 0; i wantPayCount; i+)if(customerPayQi+b != 0)g.drawString(C+customerPayQi+b, xpos+430, ypos+100);ypos += 20;g.setFont(ne
32、w Font(TimesRoman, Font.BOLD, 12);ypos = 10;g.drawString(Standing Room Area, xpos-100, ypos+160);b = customerBottom;for(int i = 0; i , xpos-110, ypos+105);g.setColor(Color.red);System.out.println(outTop is: + outTop);for(int i = outBottom; i 0)g.drawString(C +customerOuti, xpos-80, ypos+80-20*i);g.s
33、etColor(Color.red);g.drawString(Exit, xpos+530, ypos+10);for(int i = 0; i exitTop; i+)if(exitArrayi != 0)g.drawString(C + exitArrayi, xpos+530, ypos+25+15*i);xpos = 100;ypos = 10;g.setColor(Color.blue);g.drawString(Sofa, xpos+225, ypos+185);for(int i = 0; i sofaSize; i+)g.draw3DRect(xpos+180+28*(i), ypos+140, 28, 28, true);g.setColor(Color.red);int k = sofaBottom;for(int j=0; j customerOnSofa; j+)g.drawString(Integer.toString(customerSofaQk), xpos + (190+28*3) -28*(j), ypos+155);g.draw3DRect(xpos+180+28*3 - 28*(j), ypos+140, 28, 28, true);k = (k+1)%sofaSize;4. BarberShopApp
限制150内