银行服务窗口并发实现
客户可以看作一个有序的队列,先来后到排序。
服务窗口,并发调用队列的客户,每个客户的服务时间不同,每一次调用队列有最先结束服务的窗口进行分配。
#include
#include
#include
using namespace std;
typedef struct window //创建银行服务窗口
{
int time; //服务时间
bool visit;
}WINDOW;
#define N 50
queue Link; //客户队列
WINDOW Wind[N];
int n, m;
void create_Link() //创建服务队列客户所需的服务时间
{
cout << "Please input how long in the link: ";
cin >> n;
cout << "\nPlase inout how many server window: ";
cin >> m;
for (int i = 0; i < n; i++)//输入每个客户需要的服务时间
{
cout << "Plsae input the " << i + 1 << " customer server time: ";
int time;
cin >> time;
Link.push(time);
cout << endl;
}
}
void Server()//为队列分配服务窗口
{
int num = 1;
for (int i = 0; i < m&&i<n;i++)
{
if (!Wind[i].visit)
{
Wind[i].time = Link.front();
Wind[i].visit = true;
cout << num << “customer to " << i + 1 << " server window!\n”;
num++;
Link.pop();
}
}
while (!Link.empty())
{
int temp = Link.front();
int Min = 99999, pex = -1;
for (int j = 0; j < m; j++)//寻找最早结束服务的窗口号
{
if (Wind[j].time < Min)
{
Min = Wind[j].time;
pex = j;
}
}
Wind[pex].time += temp;//记录窗口的服务时间
cout << num << " customer to " << pex + 1 << " window\n";//提示排到服务的客户到所服务的窗口
num++;
Link.pop();//客户出队列
}
}
int main()
{
create_Link();//创建客户队列
Server();//分配服务窗口
for (int i = 0; i < m; i++)//每个窗口的服务时间
{
cout << i + 1 << " window server time is : " << Wind[i].time << endl;
}
return 0;
}