程序代码:
// BankHandle.cpp : 定义控制台应用程序的入口点。
//
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include <queue>
using namespace std;
#define CHAIR_NUM 5 //椅子数
#define ARRIVE_TIME 5000 //
#define CUT_TIME 5000 //办理业务时长
int num_empty_chair=CHAIR_NUM; //等待区椅子数
HANDLE hFull=CreateSemaphore(NULL, 0,CHAIR_NUM,NULL); //信号量
HANDLE hEmpty=CreateSemaphore(NULL, CHAIR_NUM,CHAIR_NUM,NULL); //信号量
HANDLE hDoor= CreateEvent(NULL, TRUE,FALSE, NULL); //开门事件,人工重置事件
//HANDLE hBaberReady= CreateEvent(NULL, FALSE,TRUE, NULL);
HANDLE hCutOver= CreateEvent(NULL, FALSE,FALSE, NULL); //办理业务结束事件,自动重置事件
int in=-1,out=0;
int CCount=0; //银行中的所有顾客数,包括正在办理业务的
HANDLE hChairs[CHAIR_NUM]; //事件数组,每把椅子对应一个事件
CRITICAL_SECTION cs; //临界区
typedef struct SParam
{
int Custid;
string Type;
}cust,*sParam;
DWORD WINAPI SeriverWindows(LPVOID p)
{
printf("\n广播:各位顾客久等了,现在开始为各位办理业务,请到取票台拿号!!");
sParam sparam;
sparam = (sParam)p;
SetEvent(hDoor); //开门
while(true)
{
if(WaitForSingleObject(hFull,1)==WAIT_TIMEOUT) //检查等候室有没有顾客
{
printf("\n营业员:没有顾客了,整理一下资料吧......");
if(WaitForSingleObject(hFull,10000)==WAIT_TIMEOUT)
{
printf("\n营业员:这么长时间都没有顾客,关门了!!");
ResetEvent(hDoor);
return 0;
}
}
else
{
printf("\n\n\t\t\t\t\t广播:请%d号顾客到XX号窗口办理....\n",sparam->Custid);
SetEvent(hChairs[out]);
out=(out+1)%CHAIR_NUM;
}
Sleep(CUT_TIME); //办理业务计时
printf("\n营业员:办理完了,您看看怎么样?");
SetEvent(hCutOver);
WaitForSingleObject(hCutOver,INFINITE);
printf("\n营业员:再见,欢迎再来!!");
}
}
DWORD WINAPI Customer(LPVOID p)
{
sParam sparam;
sparam = (sParam)p;
int c_num;//存储要占用的椅子号
if(WaitForSingleObject(hDoor,1)==WAIT_TIMEOUT)
{
printf("\n顾客%d:银行还没有开门啊,等一会吧......",sparam->Custid);
if(WaitForSingleObject(hDoor,10000)==WAIT_TIMEOUT)
{
printf("\n顾客%d::这么长时间都还不开门,明天再来吧!!", sparam->Custid);
return 0;
}
}
EnterCriticalSection(&cs);
CCount++; //顾客数增加
if(CCount==1)
{
printf("\n顾客%d :您好,我要办理业务......", sparam->Custid);
ReleaseSemaphore(hFull,1,NULL);
LeaveCriticalSection(&cs);
}
else
{
if(WaitForSingleObject(hEmpty,1)==WAIT_TIMEOUT)
{
printf("\n顾客%d :好多人啊,连座位都没有,下次再来吧......",sparam->Custid);
LeaveCriticalSection(&cs);
return 0;
}
printf("\n顾客%d :幸好还有%d个座位,等一会吧......", sparam->Custid,CHAIR_NUM+2-CCount);
in=(in+1)%CHAIR_NUM;
c_num=in;
LeaveCriticalSection(&cs);
ReleaseSemaphore(hFull,1,NULL);
WaitForSingleObject(hChairs[c_num],INFINITE); //坐在椅子上等待
printf("\n顾客%d :终于轮到我了!", sparam->Custid);
ReleaseSemaphore(hEmpty,1,NULL);
// SetEvent(hCutOver);
}
printf("\n顾客%d :开始办理业务......", sparam->Custid);
WaitForSingleObject(hCutOver,INFINITE);
printf("\n顾客%d:很好,谢谢,再见!", sparam->Custid);
SetEvent(hCutOver);
EnterCriticalSection(&cs);
CCount--;
LeaveCriticalSection(&cs);
}
void main()
{
SParam sparam;
SParam *p;
p = &sparam;
InitializeCriticalSection(&cs); //进入临界区
for (int i = 0; i < CHAIR_NUM ; i++)
hChairs[i] =CreateEvent(NULL,FALSE,FALSE, NULL); //自动重置事件
HANDLE t[11];
t[0]=CreateThread(NULL, 0, SeriverWindows,NULL, 0, NULL ); //服务线程线程
Sleep(2000);
for(int i=1;i<=10;i++)
{
sparam.Custid = i;
sparam.Type = "普通";
t[i]=CreateThread(NULL, 0, Customer,(LPVOID)p,0, NULL ); //顾客来
Sleep(2000);
}
WaitForMultipleObjects(11, t, TRUE, INFINITE);
DeleteCriticalSection(&cs);
}
//
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <string>
#include <queue>
using namespace std;
#define CHAIR_NUM 5 //椅子数
#define ARRIVE_TIME 5000 //
#define CUT_TIME 5000 //办理业务时长
int num_empty_chair=CHAIR_NUM; //等待区椅子数
HANDLE hFull=CreateSemaphore(NULL, 0,CHAIR_NUM,NULL); //信号量
HANDLE hEmpty=CreateSemaphore(NULL, CHAIR_NUM,CHAIR_NUM,NULL); //信号量
HANDLE hDoor= CreateEvent(NULL, TRUE,FALSE, NULL); //开门事件,人工重置事件
//HANDLE hBaberReady= CreateEvent(NULL, FALSE,TRUE, NULL);
HANDLE hCutOver= CreateEvent(NULL, FALSE,FALSE, NULL); //办理业务结束事件,自动重置事件
int in=-1,out=0;
int CCount=0; //银行中的所有顾客数,包括正在办理业务的
HANDLE hChairs[CHAIR_NUM]; //事件数组,每把椅子对应一个事件
CRITICAL_SECTION cs; //临界区
typedef struct SParam
{
int Custid;
string Type;
}cust,*sParam;
DWORD WINAPI SeriverWindows(LPVOID p)
{
printf("\n广播:各位顾客久等了,现在开始为各位办理业务,请到取票台拿号!!");
sParam sparam;
sparam = (sParam)p;
SetEvent(hDoor); //开门
while(true)
{
if(WaitForSingleObject(hFull,1)==WAIT_TIMEOUT) //检查等候室有没有顾客
{
printf("\n营业员:没有顾客了,整理一下资料吧......");
if(WaitForSingleObject(hFull,10000)==WAIT_TIMEOUT)
{
printf("\n营业员:这么长时间都没有顾客,关门了!!");
ResetEvent(hDoor);
return 0;
}
}
else
{
printf("\n\n\t\t\t\t\t广播:请%d号顾客到XX号窗口办理....\n",sparam->Custid);
SetEvent(hChairs[out]);
out=(out+1)%CHAIR_NUM;
}
Sleep(CUT_TIME); //办理业务计时
printf("\n营业员:办理完了,您看看怎么样?");
SetEvent(hCutOver);
WaitForSingleObject(hCutOver,INFINITE);
printf("\n营业员:再见,欢迎再来!!");
}
}
DWORD WINAPI Customer(LPVOID p)
{
sParam sparam;
sparam = (sParam)p;
int c_num;//存储要占用的椅子号
if(WaitForSingleObject(hDoor,1)==WAIT_TIMEOUT)
{
printf("\n顾客%d:银行还没有开门啊,等一会吧......",sparam->Custid);
if(WaitForSingleObject(hDoor,10000)==WAIT_TIMEOUT)
{
printf("\n顾客%d::这么长时间都还不开门,明天再来吧!!", sparam->Custid);
return 0;
}
}
EnterCriticalSection(&cs);
CCount++; //顾客数增加
if(CCount==1)
{
printf("\n顾客%d :您好,我要办理业务......", sparam->Custid);
ReleaseSemaphore(hFull,1,NULL);
LeaveCriticalSection(&cs);
}
else
{
if(WaitForSingleObject(hEmpty,1)==WAIT_TIMEOUT)
{
printf("\n顾客%d :好多人啊,连座位都没有,下次再来吧......",sparam->Custid);
LeaveCriticalSection(&cs);
return 0;
}
printf("\n顾客%d :幸好还有%d个座位,等一会吧......", sparam->Custid,CHAIR_NUM+2-CCount);
in=(in+1)%CHAIR_NUM;
c_num=in;
LeaveCriticalSection(&cs);
ReleaseSemaphore(hFull,1,NULL);
WaitForSingleObject(hChairs[c_num],INFINITE); //坐在椅子上等待
printf("\n顾客%d :终于轮到我了!", sparam->Custid);
ReleaseSemaphore(hEmpty,1,NULL);
// SetEvent(hCutOver);
}
printf("\n顾客%d :开始办理业务......", sparam->Custid);
WaitForSingleObject(hCutOver,INFINITE);
printf("\n顾客%d:很好,谢谢,再见!", sparam->Custid);
SetEvent(hCutOver);
EnterCriticalSection(&cs);
CCount--;
LeaveCriticalSection(&cs);
}
void main()
{
SParam sparam;
SParam *p;
p = &sparam;
InitializeCriticalSection(&cs); //进入临界区
for (int i = 0; i < CHAIR_NUM ; i++)
hChairs[i] =CreateEvent(NULL,FALSE,FALSE, NULL); //自动重置事件
HANDLE t[11];
t[0]=CreateThread(NULL, 0, SeriverWindows,NULL, 0, NULL ); //服务线程线程
Sleep(2000);
for(int i=1;i<=10;i++)
{
sparam.Custid = i;
sparam.Type = "普通";
t[i]=CreateThread(NULL, 0, Customer,(LPVOID)p,0, NULL ); //顾客来
Sleep(2000);
}
WaitForMultipleObjects(11, t, TRUE, INFINITE);
DeleteCriticalSection(&cs);
}