进程互斥
2016-06-01 19:33
197 查看
代码及运行结果:
#include "windows.h"
#include "process.h"
#include<iostream>
#define N 5 //顺时针排序
#define R(x) (x)
#define L(x) ((x+1)%N)
using namespace std;
HANDLE hMutex
;
HANDLE Mutex;
void pick_up(int me)
{
if(me==0)
{
WaitForSingleObject(hMutex[L(me)],INFINITE);//等左边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up left No."<<L(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
WaitForSingleObject(hMutex[R(me)],INFINITE);//等右边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up rightNo."<<R(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
else
{
WaitForSingleObject(hMutex[R(me)],INFINITE);//等右边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up rightNo."<<R(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
WaitForSingleObject(hMutex[L(me)],INFINITE);//等左边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up left No."<<L(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
}
void put_down(int me)
{
ReleaseMutex(hMutex[R(me)]);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher put down right No."<<R(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
ReleaseMutex(hMutex[L(me)]);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher put down left No."<<L(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
}
DWORD WINAPI PhilosopherThread(LPVOIDlpParameter)
{
int*me=(int *)lpParameter;
while(1)
{
pick_up(*me);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<*me<<"Philosopher is eating..."<<endl;
ReleaseMutex(Mutex);
Sleep(500);
put_down(*me);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<*me<<"Philosopher is thinking..."<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
return 1;
}
int main(int argc, char* argv[])
{
intThrdNo[5];
inti;
Mutex=CreateMutex(NULL,FALSE,NULL);
for(i=0;i<N;i++)hMutex[i]=CreateMutex(NULL,FALSE,NULL);
for(i=0;i<N;i++)
{
ThrdNo[i]=i;
CreateThread(NULL,0,PhilosopherThread,&ThrdNo[i],NULL,NULL);
}
Sleep(10000);
return0;
}
#include "windows.h"
#include "process.h"
#include<iostream>
#define N 5 //顺时针排序
#define R(x) (x)
#define L(x) ((x+1)%N)
using namespace std;
HANDLE hMutex
;
HANDLE Mutex;
void pick_up(int me)
{
if(me==0)
{
WaitForSingleObject(hMutex[L(me)],INFINITE);//等左边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up left No."<<L(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
WaitForSingleObject(hMutex[R(me)],INFINITE);//等右边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up rightNo."<<R(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
else
{
WaitForSingleObject(hMutex[R(me)],INFINITE);//等右边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up rightNo."<<R(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
WaitForSingleObject(hMutex[L(me)],INFINITE);//等左边筷子
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher pick up left No."<<L(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
}
void put_down(int me)
{
ReleaseMutex(hMutex[R(me)]);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher put down right No."<<R(me)<<"chopstick"<<endl;
ReleaseMutex(Mutex);
ReleaseMutex(hMutex[L(me)]);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<me<<"Philosopher put down left No."<<L(me)<<" chopstick"<<endl;
ReleaseMutex(Mutex);
}
DWORD WINAPI PhilosopherThread(LPVOIDlpParameter)
{
int*me=(int *)lpParameter;
while(1)
{
pick_up(*me);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<*me<<"Philosopher is eating..."<<endl;
ReleaseMutex(Mutex);
Sleep(500);
put_down(*me);
WaitForSingleObject(Mutex,INFINITE);
cout<<"No."<<*me<<"Philosopher is thinking..."<<endl;
ReleaseMutex(Mutex);
Sleep(500);
}
return 1;
}
int main(int argc, char* argv[])
{
intThrdNo[5];
inti;
Mutex=CreateMutex(NULL,FALSE,NULL);
for(i=0;i<N;i++)hMutex[i]=CreateMutex(NULL,FALSE,NULL);
for(i=0;i<N;i++)
{
ThrdNo[i]=i;
CreateThread(NULL,0,PhilosopherThread,&ThrdNo[i],NULL,NULL);
}
Sleep(10000);
return0;
}
相关文章推荐
- 微型泵输出流量脉动性研究
- An introduction to the credit scheduler in Xen
- Leetcode 43. Multiply Strings
- 微型气泵最大启动压力试验报告
- js函数(类)的继承机制的设计与实现(四)
- 第十一周项目训练9 存储班长信息的学生类
- JZOJ.3400【GDOI2014模拟】旅行 解题报告
- 微型真空水泵WAJ280降低工作电压测试报告
- Web前端学习第十六天·fighting_JavaScript(DOM编程艺术5-6章)
- READING NOTE: Inside-Outside Net: Detecting Objects in Context with Skip Pooling and Recurrent Neura
- 输入流转为字符串
- 研究微型真空水泵有背压工况下的寿命
- IntelliJ IDEA 2016.1.2开发servlet
- HTTP头Content-Type
- HDU1102(最小生成树Kruskal算法)
- 微型调速气泵KAC050.3样品扼要技术说明
- POJ 3249 Test for Job【SPFA】
- NSString 常用方法汇总
- 一些不错的文章链接记录
- iOS导航控制器的注意事项