您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 操作系统课程设计银行系统源代码
##includewindows.h#includectime#includeiostreamusingnamespacestd;#defineSeat10#defineTime500//顾客来的最大间隔时间intnumber=0;//当前服务的顾客总数intPrivateNum=0,PublicNum=0,FinancialNum=0;//取号数intseat_num=10;HANDLEseat,SemaphorePrviate,SemaphorePublic,SemaphoreFinancial;//窗口信号量HANDLEcSemaphorePrviate,cSemaphorePublic,cSemaphoreFinancial;//顾客信号量HANDLEmutex;CRITICAL_SECTIONc_seat;//临界区,用来限制同一时刻只能有一个线程来改变座位的数量CRITICAL_SECTIONprint;//临界区,用来限制同一时刻只能有一个线程来访问资源,防止输出重叠//对私叫号DWORDWINAPIPrivateServiceThread(PVOIDs1pv){while(true){srand((unsigned)time(NULL));WaitForSingleObject(cSemaphorePrviate,INFINITE);Sleep(1500);EnterCriticalSection(&print);cout对私窗口叫号!endl;LeaveCriticalSection(&print);ReleaseSemaphore(SemaphorePrviate,1,NULL);EnterCriticalSection(&c_seat);seat_num++;LeaveCriticalSection(&c_seat);}return0;}//对公叫号DWORDWINAPIPublicServiceThread(PVOIDs2pv){while(true){srand((unsigned)time(NULL));WaitForSingleObject(cSemaphorePublic,INFINITE);//p(&cSemaphorePublic)Sleep(1500);EnterCriticalSection(&print);cout对公窗口叫号!endl;LeaveCriticalSection(&print);ReleaseSemaphore(SemaphorePublic,1,NULL);//v(&SemaphorePublic)EnterCriticalSection(&c_seat);seat_num++;LeaveCriticalSection(&c_seat);}return0;}//理财叫号DWORDWINAPIFinancialServiceThread(PVOIDSemaphoreFinancialpv){while(true){srand((unsigned)time(NULL));WaitForSingleObject(cSemaphoreFinancial,INFINITE);//p(&cSemaphoreFinancial)Sleep(1500);EnterCriticalSection(&print);cout理财窗口叫号!endl;LeaveCriticalSection(&print);ReleaseSemaphore(SemaphoreFinancial,1,NULL);//v(&SemaphoreFinancial)EnterCriticalSection(&c_seat);seat_num++;LeaveCriticalSection(&c_seat);}return0;}//办理对私服务顾客DWORDWINAPIPrivateCustomer(PVOIDc1pv){srand((unsigned)time(NULL));intn=rand()%2;if(seat_num=0&&n==1){EnterCriticalSection(&print);cout等待室座位已满,该顾客选择离开!endl;LeaveCriticalSection(&print);}elseif(seat_num=0){seat_num--;EnterCriticalSection(&print);cout该顾客选择了继续等待,已有-seat_num顾客在门外等待endl;LeaveCriticalSection(&print);}EnterCriticalSection(&c_seat);seat_num--;LeaveCriticalSection(&c_seat);WaitForSingleObject(seat,INFINITE);//p(&seat)WaitForSingleObject(mutex,INFINITE);//p(&mutex)number++;PrivateNum++;intt=PrivateNum;EnterCriticalSection(&print);cout第number个顾客已取号,号码:Atendl;LeaveCriticalSection(&print);ReleaseMutex(mutex);//v(&mutex)ReleaseSemaphore(cSemaphorePrviate,1,NULL);//v(&cSemaphorePrviate)EnterCriticalSection(&print);cout顾客At等待对私服务!endl;LeaveCriticalSection(&print);intServeTime=rand()%4000+500;Sleep(ServeTime);WaitForSingleObject(SemaphorePrviate,INFINITE);//p(&SemaphorePrviate)EnterCriticalSection(&print);cout顾客At正在进入服务!endl;LeaveCriticalSection(&print);Sleep(ServeTime);ReleaseSemaphore(seat,1,NULL);//v(&seat)seat_num++;if(seat_num=0){EnterCriticalSection(&print);cout门外还剩-seat_num人在等待。endl;LeaveCriticalSection(&print);}EnterCriticalSection(&print);cout对顾客At服务结束,服务时间:ServeTimeendl;LeaveCriticalSection(&print);return0;}//办理对公服务顾客DWORDWINAPIPublicCustomer(PVOIDc2pv){srand((unsigned)time(0));intn=rand()%2;if(seat_num=0&&n==1){EnterCriticalSection(&print);cout等待室座位已满,该顾客选择离开!endl;LeaveCriticalSection(&print);}elseif(seat_num=0){seat_num--;EnterCriticalSection(&print);cout该顾客选择继续等待,已有-seat_num顾客在门外等待endl;LeaveCriticalSection(&print);}EnterCriticalSection(&c_seat);seat_num--;LeaveCriticalSection(&c_seat);WaitForSingleObject(seat,INFINITE);//p(&seat)WaitForSingleObject(mutex,INFINITE);//p(&mutex)number++;PublicNum++;intt=PublicNum;EnterCriticalSection(&print);cout第number个顾客已取号,号码:Btendl;LeaveCriticalSection(&print);ReleaseMutex(mutex);//v(&mutex)ReleaseSemaphore(cSemaphorePublic,1,NULL);//v(&cSemaphorePublic)EnterCriticalSection(&print);cout顾客Bt等待对公服务!endl;LeaveCriticalSection(&print);intServeTime=rand()%4000+500;Sleep(ServeTime);WaitForSingleObject(SemaphorePublic,INFINITE);//p(&SemaphorePublic)EnterCriticalSection(&print);cout顾客Bt正在进入服务!endl;LeaveCriticalSection(&print);Sleep(ServeTime);ReleaseSemaphore(seat,1,NULL);//v(&seat)seat_num++;if(seat_num=0){EnterCriticalSection(&print);cout门外还剩-seat_num人在等待。endl;LeaveCriticalSection(&print);}EnterCriticalSection(&print);cout对顾客BPublicNum服务结束,服务时间:ServeTimeendl;LeaveCriticalSection(&print);return0;}//办理理财业务的顾客DWORDWINAPIFinancialCustomer(PVOIDc3pv){srand((unsigned)time(0));intn=rand()%2;if(seat_num=0&&n==1){EnterCriticalSection(&print);cout等待室座位已满,该顾客选择离开!endl;LeaveCriticalSection(&print);}elseif(seat_num=0){seat_num--;EnterCriticalSection(&print);cout该顾客选择继续等待,已有-seat_num顾客在门外等待endl;LeaveCriticalSection(&print);}EnterCriticalSection(&c_seat);seat_num--;LeaveCriticalSection(&c_seat);WaitForSingleObject(seat,INFINITE);//p(&seat)WaitForSingleObject(mutex,INFINITE);//p(&muteprint)number++;FinancialNum++;intt=FinancialNum;EnterCriticalSection(&print);cout第number个顾客已取号,号码:Ctendl;LeaveCriticalSection(&print);ReleaseMutex(mutex);//v(&mutex)ReleaseSemaphore(cSemaphoreFinancial,1,NULL);//v(&cSemaphorePublic)EnterCriticalSection(&print);cout
本文标题:操作系统课程设计银行系统源代码
链接地址:https://www.777doc.com/doc-2381497 .html