• 大小: 85KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-22
  • 语言: C/C++
  • 标签:

资源简介

操作系统的理发师问题解决文件打包下载(可执行文件,文档,代码齐全),C++实现,简单易学易用。

资源截图

代码片段和文件信息

// babe.cpp : Defines the entry point for the console application.
//

#include “stdafx.h“
#include “windows.h“
#include “iostream.h“
#include “math.h“

int long waiting(0);          //等待理发的顾客人数
int      chairs;              //店中椅子的总数目
char     open_door;           //开门
char     close_door;          //关门
int      count(0);            //顾客的序号
int      finish(0);           //已经理完发的顾客人数
DWORD a;
HANDLE Mutex    =::CreateMutex(NULL FALSE “Mutex“);  //用来实现进程的互斥  
HANDLE barbers   =::CreateSemaphore(NULL 11 “barbers“); //定义信号量来进行线程间的同步
HANDLE customers =::CreateSemaphore(NULL03“customers“); //定义信号量来进行线程间的同步

int random()
//定义随机函数来产生顾客,并使两个顾客间的时间少于15秒
{
    return (rand()*15000)/RAND_MAX;
}
void cuthair()
//理发师的理发函数,用时15秒

::Sleep (15000);
cout<<“理发结束 !“<}

void gethaircut()
// 顾客被理发的函数
{
::Sleep (15001);          //顾客被理发的函数,为了和理发师之间有所区别,比理发师理发时间长1毫秒
cout<<“第“<}
DWORD WINAPI customer(LPVOID pParm2)
// 顾客线程
{
::WaitForSingleobject(Mutex INFINITE);      //P(mutex)来进行互斥操作
count++;                                     //来的是第几个顾客
cout<<“顾客敲门!第 “< if (waiting {
if (waiting!=0)
{
cout<<“现在有“< }
else
cout<<“无人在等待理发“< waiting++;
cout<<“剩余“< cout<<“有空位,顾客已坐下“< ::ReleaseSemaphore(customers1NULL); //V(customer)
::ResumeThread(customers); //唤醒理发师进程
::ReleaseMutex(Mutex); //释放互斥量,以便其他线程使用
::WaitForSingleobject(barbersINFINITE);//等待理发
gethaircut(); //理发并离开
}
else
{
cout<<“没有空椅子,第“< ::ReleaseMutex(Mutex);
}
return 0;
}
DWORD WINAPI barber(LPVOID pParm1)
//理发师线程
{
   while(true)                         
   //外循环
   {
::WaitForSingleobject(customersINFINITE);//p(customers),等待顾客
::WaitForSingleobject(MutexINFINITE);   //等待互斥量
        waiting--;                               //等待的人数减1
::ReleaseSemaphore(barbers1NULL);      //释放信号量
::ResumeThread(barbers);                 //唤醒顾客进程
::ReleaseMutex(Mutex);                   //v(mutex);   
cuthair();                               //理发
finish++;                                //理发完毕的顾客数目加1
   }
   return 0;
}

int main(int argc char* argv[])
//实现线程的操作
{
    cout<<“输入理发店中的椅子个数:“;
cin>>chairs;
cout<<“店中有“< cout<<“是否开始接待顾客?Y/N“< cin>>open_door;
while (open_door!=‘y‘)
{
cout< cout<<“是否开始接待顾客?Y/N“< cin>>open_door;
}
HANDLE hThread1;
HANDLE hThread2;
hThread2=::CreateThread (NULL0barberNULL0NULL); //产生一个理发师进程
while(close_door!=‘y‘)

::Sleep(random());//函数实现顾客随机到来
hThread1=::CreateThread(NULL0customerNULLaNULL);
cout< if (finish>=8 && waiting==0)
//如果完成数超过8并且没有人等待

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     192603  2009-12-18 19:19  理发师\babe.exe

     文件       3708  2009-12-18 12:57  理发师\babe.cpp

     文件        667  2009-12-17 21:54  理发师\StdAfx.h

     文件        291  2009-12-17 21:54  理发师\StdAfx.cpp

     文件     112128  2010-04-17 09:01  理发师\操作系统文档.doc

     目录          0  2009-12-18 19:20  理发师

----------- ---------  ---------- -----  ----

               309397                    6


评论

共有 条评论

相关资源