#include<iostream>
#include<Windows.h>
#include<ctime>
using namespace std;
#define Manual_ticket_window 2 //定义两个人工售票窗口
#define Manual_change_window 1 //定义一个改签、退票窗口
#define Automatic_Ticket_Machine 3 //定义三个自动售票机
#define Capacity_Max_num 200 //大厅最大等待总人数
//人工售票窗口
HANDLE Mantw_num1 = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数
HANDLE Mantw_num2 = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数
HANDLE ticket_seller_1 = CreateSemaphore(NULL, 0, 1, NULL);//正在等待顾客的售票员数(其值为0或1)
HANDLE ticket_seller_2 = CreateSemaphore(NULL, 0, 1, NULL);//正在等待顾客的售票员数(其值为0或1)
//改签、退票窗口
HANDLE Mancw_num = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数
HANDLE ticket_change = CreateSemaphore(NULL, 0, 1, NULL);//正在等待顾客的改签人员数(其值为0或1)
/*自动售卖机器
HANDLE Auttm_num1 = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数
HANDLE Auttm_num2 = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数
HANDLE Auttm_num3 = CreateSemaphore(NULL, 0, 200, NULL);//正在等待人数*/
HANDLE ATM_num1 = CreateSemaphore(NULL, 1, 1, NULL);//正在等待顾客的ATM机数
HANDLE ATM_num2 = CreateSemaphore(NULL, 1, 1, NULL);//正在等待顾客的ATM机数
HANDLE ATM_num3 = CreateSemaphore(NULL, 1, 1, NULL);//正在等待顾客的ATM机数
//入口、出口
CRITICAL_SECTION Entrance_channel_num; //互斥信号量
CRITICAL_SECTION Export_channel_num; //互斥信号量
//LPCRITICAL_SECTION LP;
//用于互斥信号量
//CRITICAL_SECTION mutex;
CRITICAL_SECTION mutex1;
CRITICAL_SECTION mutex2;
CRITICAL_SECTION mutex3;
CRITICAL_SECTION mutex4;
CRITICAL_SECTION mutex5;
CRITICAL_SECTION mutex6;
//实时人数
int Real_time_num = 0; //初始值为零
int wMantw_num1 = 0;
int wMantw_num2 = 0;
int wMancw_num = 0;
int autom1 = 0;
int autom2 = 0;
int autom3 = 0;
//人工改签、退票窗口线程
void change_ticket(int *p)
{
SYSTEMTIME time1;
GetLocalTime(&time1);
Sleep(1000);
srand(time(NULL));
int a = rand() % 2;
switch (a)
{
case 0:
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "已改票" << endl;
break;
case 1:
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "已退票" << endl;
break;
}
}
//人工窗口
void rengong(int *p)
{
SYSTEMTIME time1;
GetLocalTime(&time1);
if (wMantw_num1 >= wMantw_num2)
{
//EnterCriticalSection(&mutex2);//进入互斥临界区
ReleaseSemaphore(Mantw_num2, 1, NULL); //某区排队队列人数加一,这里与售票员进程写一致
wMantw_num2++;
//LeaveCriticalSection(&mutex2);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入窗口2队列" << ",等待人数:" << wMantw_num2 - 1 << endl;
WaitForSingleObject(ticket_seller_2, INFINITE);//排队等待服务
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "完成购票" << endl;
}
else
{
//EnterCriticalSection(&mutex1);//进入互斥临界区
ReleaseSemaphore(Mantw_num1, 1, NULL); //某区排队队列人数加一,这里与售票员进程写一致
wMantw_num1++;
//LeaveCriticalSection(&mutex1);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入窗口1队列" << ",等待人数:" << wMantw_num1 - 1 << endl;
WaitForSingleObject(ticket_seller_1, INFINITE);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "完成购票" << endl;
}
}
//ATM窗口
void autom(int *p)
{
SYSTEMTIME time1;
GetLocalTime(&time1);
if (autom1<autom2)
{
if (autom1<autom3)
{
EnterCriticalSection(&mutex4);
autom1++;
LeaveCriticalSection(&mutex4);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入自动售卖机1号队列" << ",等待人数:" << autom1 - 1 << endl;
WaitForSingleObject(ATM_num1, INFINITE);
Sleep(15000);
EnterCriticalSection(&mutex4);
autom1--;
LeaveCriticalSection(&mutex4);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "在自动售卖机1号买票" << endl;
ReleaseSemaphore(ATM_num1, 1, NULL);
}
else
{
EnterCriticalSection(&mutex6);
autom3++;
LeaveCriticalSection(&mutex6);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入自动售卖机3号队列" << ",等待人数:" << autom3 - 1 << endl;
WaitForSingleObject(ATM_num3, INFINITE);
Sleep(15000);
EnterCriticalSection(&mutex6);
autom3--;
LeaveCriticalSection(&mutex6);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "在自动售卖机3号买票" << endl;
ReleaseSemaphore(ATM_num3, 1, NULL);
}
}
else
{
if (autom2<autom3)
{
EnterCriticalSection(&mutex5);
autom2++;
LeaveCriticalSection(&mutex5);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入自动售卖机2号队列" << ",等待人数:" << autom2 - 1 << endl;
WaitForSingleObject(ATM_num2, INFINITE);
Sleep(15000);
EnterCriticalSection(&mutex5);
autom2--;
LeaveCriticalSection(&mutex5);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "在自动售卖机2号买票" << endl;
ReleaseSemaphore(ATM_num2, 1, NULL);
}
else
{
EnterCriticalSection(&mutex6);
autom3++;
LeaveCriticalSection(&mutex6);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入自动售卖机3号队列" << ",等待人数:" << autom3 - 1 << endl;
WaitForSingleObject(ATM_num3, INFINITE);
Sleep(15000);
EnterCriticalSection(&mutex6);
autom3--;
LeaveCriticalSection(&mutex6);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "在自动售卖机3号买票" << endl;
ReleaseSemaphore(ATM_num3, 1, NULL);
}
}
}
//判断去人工窗口还是去ATM机
void judge(int *p)
{
Sleep(1000);
srand(time(NULL));
int a = rand() % 2;
switch (a)
{
case 0:
rengong(p);
break;
case 1:
autom(p);
break;
}
}
//改签退票窗口
void qianpiao(int *p)
{
SYSTEMTIME time1;
GetLocalTime(&time1);
//EnterCriticalSection(&mutex3);//进入互斥临界区
ReleaseSemaphore(Mancw_num, 1, NULL); //某区排队队列人数加一,这里与售票员进程写一致
wMancw_num++;
//LeaveCriticalSection(&mutex3);
cout << time1.wHour << ":" << time1.wMinute << " 顾客" << *p << "进入改票、退票窗口队列" ",等待人数:" << wMancw_num - 1 << endl;
WaitForSingleObject(ticket_change, INFINITE); //如果-售票员-在等待,则唤醒售票员
change_ticket(p);
}
//
void lineup(int *p)
{
Sleep(1000);
srand(time(NULL));
int a = rand() % 5;
switch (a)
{
case 0:
judge(p);
break;
case 1:
judge(p);
break;
case 2:
judge(p);
break;
case 3:
judge(p);
break;
case 4:
qianpiao(p);
break;
}
}
//售票员一
DWORD WINAPI ticket_seller1(LPVOID lp)//售票员线程
{
SYSTEMTIME time1;
GetLocalTime(&time1);
if (time1.wHour >7 && time1.wHour< 20)//时间段允许
{
while (1)
{
WaitForSingleObject(Mantw_num1, INFINITE);//人工售票窗口:人如果此队列没有人,售票员就等待
EnterCriticalSection(&mutex1); //进入互斥临界区,同一个售票员不能为两个人服务
Sleep(20000);
wMantw_num1--; //等待人数-1
ReleaseSemaphore(ticket_seller_1, 1, NULL); //售票员准备服务
LeaveCriticalSection(&mutex1);
}
}
return 0;
}
//售票员二
DWORD WINAPI ticket_seller2(LPVOID lp)//售票员线程
{
SYSTEMTIME time1;
GetLocalTime(&time1);
if (time1.wHour >7 && time1.wHour< 20)//时间段允许
{
while (1)
{
WaitForSingleObject(Mantw_num2, INFINITE);//人工售票窗口:人如果此队列没有人,售票员就等待
EnterCriticalSection(&mutex2);//进入互斥临界区,同一个售票员不能为两个人服务
Sleep(20000);
wMantw_num2--; //等待人数-1
ReleaseSemaphore(ticket_seller_2, 1, NULL);//售票员准备服务
LeaveCriticalSection(&mutex2);
}
}
return 0;
}
//改票员
DWORD WINAPI ticket_change1(LPVOID lp)//改票员线程
{
SYSTEMTIME time1;
Get
Mr_lovone
- 粉丝: 17
- 资源: 7
会员权益专享
最新资源
- scrt-x86-bsafe.9.3.2.2978.exe
- scrt-x64-bsafe.9.3.2.2978.exe
- scrt-sfx-x86-bsafe.9.3.2.2978.exe
- scrt-sfx-x64-bsafe.9.3.2.2978.exe
- scrt-sfx-9.3.2-2978.macos-x64.dmg
- scrt-sfx-9.3.2-2978.macos-arm64.dmg
- scrt-9.3.2-2978.macos-x64.dmg
- scrt-9.3.2-2978.macos-arm64.dmg
- 文学系中国水墨画风格论文答辩PPT模板.pptx
- 文化气息浓厚的中国风公司介绍PPT模板.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈



评论0