资源简介
,同步互斥P、V操作。理发店里有5把椅子,顾客进来理发,此时理发师在睡觉,顾客叫醒理发师开始理发,再有顾客进来就在椅子上等候,如果没有椅子了,就离开
代码片段和文件信息
public class idb02251210
{
public static void main(String args[])
{
//Here is the main function
System.out.println(“This is a barber shop with 5 chairs for waiting.“);
System.out.println(“Here is 20 customers want to barber.“);
System.out.println(“Firstthe barber is sleeping for there is no customer.“);
Database server = new Database();
Barber[] barberArray = new Barber[NUM_OF_BARBER];
for (int i = 0; i < NUM_OF_BARBER; i++)
{
barberArray[i] = new Barber(i server);
barberArray[i].start();
try {//this try is for controling the speed of everyone who go into the barber shop
barberArray[i].sleep(1000-20*i);
}
catch (InterruptedException f) {
}
}
}
private static final int NUM_OF_BARBER = 20;
}
class Database
{
public Database() {//set the data
waitCount = 0;
maxCustomer = 5;
mutex = new Semaphore(1);
db = new Semaphore(maxCustomer + 1);
}
// customers and barber will call this to nap
public static void napping() {
int sleepTime = (int) (NAP_TIME * Math.random() );
try { Thread.sleep(sleepTime*100); }
catch(InterruptedException e) {}
}
public int startWait() {//customers begin to go into the barber shop
db.P();
++waitCount;//the number of customers add one
System.out.println(“There are “ + waitCount + “ chairs occupied.“);
if (waitCount < maxCustomer)
{ mutex.P();}
waitCount--;
db.V();//the number of customers substract one
return waitCount;
}
public int endWait() {//there are no cuostomers and barber begin to sleep
db.P();
if (waitCount == 0)
{
System.out.println(“The barber begin to sleep again.“);
}
mutex.V();
db.V();
System.out.println(“There are “ + waitCount + “ chairs occupied.“);
return waitCount;
}
private int waitCount;
public int maxCustomer;
Semaphore mutex; // controls access to readerCount
Semaphore db; // controls access to the database
private static final int NAP_TIME = 25;
}
class Barber extends Thread
{
public Barber(int r Database db)
{
barberNum = r;
server = db;
isFull = false;
}
public void run()
{
int c;
while (true)//people is begin the detail behavior
{
//System.out.println(“customer-“ + barberNum + “ is going.“);
Database.napping();
System.out.println(“customer-“ + barberNum + “ enter shop.“);
c = server.startWait();
if(c >= server.maxCustomer)
{
System.out.println(“customer-“ + barberNum + “have to go away for no chairs.“);
isFull = true;
}
// you have access to have barbering
if(c < server.maxCustomer)
{
System.out
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4001 2004-06-10 22:43 SleepingBarber.java
----------- --------- ---------- ----- ----
4219 2
- 上一篇:数据结构大作业贪吃蛇和实验报告
- 下一篇:A*最短路径算法 最短路径
相关资源
- NUR算法和OPT算法实现-----操作系统实验
- 操作系统模拟实现单级目录的文件系
- FCFS和SJF调度算法C++
- 进程同步操作系统实验三,带实验报
- 操作系统的理发师问题解决文件打包
- 操作系统调度算法c语言实现
- 操作系统实验-计算机进程管理和进程
- 操作系统课程设计之进程调度源代码
- 操作系统 动态分区存储管理方式的
- 《 Linux操作系统下C语言编程入门》
- 操作系统内存分配C++实现
- 操作系统 读者写者问题c++
- 操作系统实习-快速文件系统
- 操作系统实习:动态分区分配C++实现
- 操作系统c语言模拟作业调度实验
- 进程调度时间片轮转+优先级进程调度
- 操作系统课程设计 文件管理系统模拟
- spooling模拟系统代码.docx
- 操作系统实验-----MFC线程--购票系统演
- 分区式存储管理
- 操作系统抢占式短进程优先调度算法
- 请求调页存储管理方式的模拟
- 缓冲池的模拟(C++)
- 操作系统进程调度C++代码实现
- 操作系统请求分页存储器管理C++代码
- 操作系统银行家算法源码
- 进程的同步-吃水果问题
- 编写并调试一个模拟的进程调度程序
- 东北大学操作系统实验1进程的同步与
- 银行家算法C语言实现源文件
评论
共有 条评论