#include"stdafx.h"
#include"windows.h"
#include"iostream.h"
#include"math.h"
int long waiting(0);//等待理发的顾客人数
int chairs;//店中椅子的总数目
char open_door; //开门
charclose_door;//关门
int count(0);//顾客的序号
int finish(0);//已经理完发的顾客人数
DWORD a;
(无符号整数32位)
HANDLE Mutex=::CreateMutex(NULL,FALSE,"Mutex");//用来实现进程的互斥
HANDLE barbers=::CreateSemaphore(NULL,1,1,"barbers");//定义信号量来进行线程间的同步
HANDLE customers=::CreateSemaphore(NULL,0,3,"customers");//定义信号量来进行线程间的同步
int random()//
定义随机函数来产生顾客,并使两个顾客间的时间少于15秒 {
return(rand()*15000)/RAND_MAX;
}
void cuthair()//理发师的理发函数,用时15秒
{
::Sleep (15000);
cout<<"理发结束!"<<endl;
}
void?gethaircut()?//?顾客被理发的函数
{
::Sleep?(15001);//顾客被理发的函数,为了和理发师之间有所区别,比理
发师理发时间长1毫秒
cout<<"第"<<finish<<"个顾客理发完毕,离开"<<endl;
}
DWORD?WINAPI?customer(LPVOID?pParm2)
//顾客线程
{
::WaitForSingleObject(Mutex?,INFINITE);//P(mutex)来进行互斥操作
count++;//来的是第几个顾客
cout<<"顾客敲门!第"<<count<<"?个顾客到来"<<endl;
if(waiting<chairs)//如果有空椅子
{
if(waiting!=0)
{
cout<<"现在有"<<waiting?<<"?个人在等待理发"<<endl;
}
else
cout<<"无人在等待理发"<<endl;//输出有多少人在等待
waiting++;
cout<<"
剩余"<<chairs-waiting+1<<"个座位"<<endl;
cout<<"有空位,顾客已坐下"<<endl;
::ReleaseSemaphore(customers,1,NULL);//V(customer)
::ResumeThread(customers);//唤醒理发师进程
::ReleaseMutex(Mutex);//释放互斥量,以便其他线程使用
::WaitForSingleObject(barbers,INFINITE);//等待理发
gethaircut();//理发并离开
}
else
{
cout<<"没有空椅子,第"<<count<<"个顾客离开理发店"<<endl;//没有椅子,顾
客直接离开
::ReleaseMutex(Mutex);
}
return 0;
}
DWORD?WINAPI?barber(LPVOID?pParm1)?//理发师线程
{
while(true)?//外循环
{
::WaitForSingleObject(customers,INFINITE);//p(customers),等待顾客
::WaitForSingleObject(Mutex,INFINITE);//等待互斥量
waiting--;//等待的人数减1
::ReleaseSemaphore(barbers,1,NULL);//释放信号量
::ResumeThread(barbers);//唤醒顾客进程
::ReleaseMutex(Mutex);
?//v(mutex);
cuthair();//理发
finish++;//理发完毕的顾客数目加1
}
return?0;
}
int?main(int?argc,?char*?argv[])//实现线程的操作
{
cout<<"输入理发店中的椅子个数:";
cin>>chairs;
cout<<"店中有"<<chairs<<"把椅子"<<endl;//设置椅子数目
cout<<"是否开始接待顾客?Y/N"<<endl;//是否开门营业
cin>>open_door;
while?(open_door!='Y')
{
cout<<endl<<"尚未营业!"<<endl;
cout<<"是否开始接待顾客?Y/N"<<endl;
cin>>open_door;
}
HANDLE hThread1;
HADLE?hThread2;
hThread2=::CreateThread?(NULL,0,barber,NULL,0,NULL);//产生一个理发师进程
while(open_door='y') {
while(close_door!='y')
{
::Sleep(random());//函数实现顾客随机到来
hThread1=::CreateThread(NULL,0,customer,NULL,a,NULL);
cout<<endl<<"正在营业,请进!"<<endl;
if(finish>=8&&waiting==0)//如果完成数超过8并且没有人等待
{
cout<<"已经为"<<finish<<"个顾客理发了,是否停止营业?"<<endl;//提示是否关门
cin>>close_door;
return close_door;
}
}
if (close_door=='y')
{
cout<<"暂停营业!欢迎下次光临!"<<endl;
return?0;
}
return?0;
}
#include"windows.h"
#include"iostream.h"
#include"math.h"
int long waiting(0);//等待理发的顾客人数
int chairs;//店中椅子的总数目
char open_door; //开门
charclose_door;//关门
int count(0);//顾客的序号
int finish(0);//已经理完发的顾客人数
DWORD a;
(无符号整数32位)
HANDLE Mutex=::CreateMutex(NULL,FALSE,"Mutex");//用来实现进程的互斥
HANDLE barbers=::CreateSemaphore(NULL,1,1,"barbers");//定义信号量来进行线程间的同步
HANDLE customers=::CreateSemaphore(NULL,0,3,"customers");//定义信号量来进行线程间的同步
int random()//
定义随机函数来产生顾客,并使两个顾客间的时间少于15秒 {
return(rand()*15000)/RAND_MAX;
}
void cuthair()//理发师的理发函数,用时15秒
{
::Sleep (15000);
cout<<"理发结束!"<<endl;
}
void?gethaircut()?//?顾客被理发的函数
{
::Sleep?(15001);//顾客被理发的函数,为了和理发师之间有所区别,比理
发师理发时间长1毫秒
cout<<"第"<<finish<<"个顾客理发完毕,离开"<<endl;
}
DWORD?WINAPI?customer(LPVOID?pParm2)
//顾客线程
{
::WaitForSingleObject(Mutex?,INFINITE);//P(mutex)来进行互斥操作
count++;//来的是第几个顾客
cout<<"顾客敲门!第"<<count<<"?个顾客到来"<<endl;
if(waiting<chairs)//如果有空椅子
{
if(waiting!=0)
{
cout<<"现在有"<<waiting?<<"?个人在等待理发"<<endl;
}
else
cout<<"无人在等待理发"<<endl;//输出有多少人在等待
waiting++;
cout<<"
剩余"<<chairs-waiting+1<<"个座位"<<endl;
cout<<"有空位,顾客已坐下"<<endl;
::ReleaseSemaphore(customers,1,NULL);//V(customer)
::ResumeThread(customers);//唤醒理发师进程
::ReleaseMutex(Mutex);//释放互斥量,以便其他线程使用
::WaitForSingleObject(barbers,INFINITE);//等待理发
gethaircut();//理发并离开
}
else
{
cout<<"没有空椅子,第"<<count<<"个顾客离开理发店"<<endl;//没有椅子,顾
客直接离开
::ReleaseMutex(Mutex);
}
return 0;
}
DWORD?WINAPI?barber(LPVOID?pParm1)?//理发师线程
{
while(true)?//外循环
{
::WaitForSingleObject(customers,INFINITE);//p(customers),等待顾客
::WaitForSingleObject(Mutex,INFINITE);//等待互斥量
waiting--;//等待的人数减1
::ReleaseSemaphore(barbers,1,NULL);//释放信号量
::ResumeThread(barbers);//唤醒顾客进程
::ReleaseMutex(Mutex);
?//v(mutex);
cuthair();//理发
finish++;//理发完毕的顾客数目加1
}
return?0;
}
int?main(int?argc,?char*?argv[])//实现线程的操作
{
cout<<"输入理发店中的椅子个数:";
cin>>chairs;
cout<<"店中有"<<chairs<<"把椅子"<<endl;//设置椅子数目
cout<<"是否开始接待顾客?Y/N"<<endl;//是否开门营业
cin>>open_door;
while?(open_door!='Y')
{
cout<<endl<<"尚未营业!"<<endl;
cout<<"是否开始接待顾客?Y/N"<<endl;
cin>>open_door;
}
HANDLE hThread1;
HADLE?hThread2;
hThread2=::CreateThread?(NULL,0,barber,NULL,0,NULL);//产生一个理发师进程
while(open_door='y') {
while(close_door!='y')
{
::Sleep(random());//函数实现顾客随机到来
hThread1=::CreateThread(NULL,0,customer,NULL,a,NULL);
cout<<endl<<"正在营业,请进!"<<endl;
if(finish>=8&&waiting==0)//如果完成数超过8并且没有人等待
{
cout<<"已经为"<<finish<<"个顾客理发了,是否停止营业?"<<endl;//提示是否关门
cin>>close_door;
return close_door;
}
}
if (close_door=='y')
{
cout<<"暂停营业!欢迎下次光临!"<<endl;
return?0;
}
return?0;
}