操作系统中互斥的问题:用C写程序,模拟飞机上两个乘客互斥使用洗手间的问题。
操作系统中互斥的问题:用C写程序,模拟飞机上两个乘客互斥使用洗手间的问题。参考WINDOWS下的同步与互斥机制...在线等......
操作系统中互斥的问题:用C写程序,模拟飞机上两个乘客互斥使用洗手间的问题。
参考WINDOWS下的同步与互斥机制...
在线等... 展开
参考WINDOWS下的同步与互斥机制...
在线等... 展开
展开全部
void Tolie(void*p){
DWORD m_delay;
DWORD m_persist;
int m_serial;
DWORD wait_for_mutex3;
HANDLE h_Mutex3;
h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3"); //打开用于保护计数器和控制台输出的互斥体
m_serial = ((threadinfo*)(p))->serial;
m_delay = (DWORD)(((threadinfo*)(p))->delay*INTE_PER_SEC);
m_persist = (DWORD)(((threadinfo*)(p))->persist*INTE_PER_SEC);
Sleep(m_delay); //延迟
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
//请求使用控制台和人数计数器修改权限
printf("第%d人准备进入厕所,剩余%d间厕所\n",m_serial,(Tolie_Num-writecount));
ReleaseMutex(h_Mutex3); //信号释放
EnterCriticalSection(&CS_Write);//进入通道排队
Sleep(m_persist);
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
//进入以后,请求使用控制台,并拥有计数器修改权
writecount++;
if(writecount!=Tolie_Num) LeaveCriticalSection(&CS_Write);
//若人数未达到上限则出临界区执行线程剩余!
printf("第%d个人正在使用厕所,剩余%d间厕所.\n",m_serial,(Tolie_Num-writecount));
ReleaseMutex(h_Mutex3); //信号释放
Sleep(m_persist); //使用ing。。。。
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
writecount--;
printf("第%d个人出厕所,剩余%d间厕所.\n", m_serial,(Tolie_Num-writecount));
if(writecount!=Tolie_Num)LeaveCriticalSection(&CS_Write);
//当还有个空位时出临界区
ReleaseMutex(h_Mutex3); //信号释放
}
DWORD m_delay;
DWORD m_persist;
int m_serial;
DWORD wait_for_mutex3;
HANDLE h_Mutex3;
h_Mutex3 = OpenMutex(MUTEX_ALL_ACCESS,FALSE,"mutex3"); //打开用于保护计数器和控制台输出的互斥体
m_serial = ((threadinfo*)(p))->serial;
m_delay = (DWORD)(((threadinfo*)(p))->delay*INTE_PER_SEC);
m_persist = (DWORD)(((threadinfo*)(p))->persist*INTE_PER_SEC);
Sleep(m_delay); //延迟
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
//请求使用控制台和人数计数器修改权限
printf("第%d人准备进入厕所,剩余%d间厕所\n",m_serial,(Tolie_Num-writecount));
ReleaseMutex(h_Mutex3); //信号释放
EnterCriticalSection(&CS_Write);//进入通道排队
Sleep(m_persist);
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
//进入以后,请求使用控制台,并拥有计数器修改权
writecount++;
if(writecount!=Tolie_Num) LeaveCriticalSection(&CS_Write);
//若人数未达到上限则出临界区执行线程剩余!
printf("第%d个人正在使用厕所,剩余%d间厕所.\n",m_serial,(Tolie_Num-writecount));
ReleaseMutex(h_Mutex3); //信号释放
Sleep(m_persist); //使用ing。。。。
wait_for_mutex3 = WaitForSingleObject(h_Mutex3,-1);
writecount--;
printf("第%d个人出厕所,剩余%d间厕所.\n", m_serial,(Tolie_Num-writecount));
if(writecount!=Tolie_Num)LeaveCriticalSection(&CS_Write);
//当还有个空位时出临界区
ReleaseMutex(h_Mutex3); //信号释放
}
ZESTRON
2024-09-04 广告
2024-09-04 广告
在Dr. O.K. Wack Chemie GmbH,我们高度重视ZESTRON的表界面分析技术。该技术通过深入研究材料表面与界面的性质,为提升产品质量与可靠性提供了有力支持。ZESTRON的表界面分析不仅涵盖了相变化、化学反应、吸附与解吸...
点击进入详情页
本回答由ZESTRON提供
展开全部
像这样吗?使用两个线程和一个mutex。
#include <windows.h>
HANDLE wcMutex;
DWORD WINAPI Passenger(PVOID pvParam)
{
int count = 0;
DWORD passenger = (DWORD)pvParam;
DWORD dwWaitResult;
DWORD dwWait;
while (count < 10)
{
printf("Passenger %d tries to use WC...\n", passenger);
dwWaitResult = WaitForSingleObject(wcMutex, INFINITE);
if (dwWaitResult == WAIT_OBJECT_0)
{
printf("Passenger %d starts using WC...\n", passenger);
rand_s(&dwWait);
dwWait = (dwWait+1000) % 2000;
Sleep(dwWait);
printf("Passenger %d finished!\n", passenger);
ReleaseMutex(wcMutex);
}
count++;
rand_s(&dwWait);
dwWait = (dwWait+1000) % 2000;
Sleep(dwWait);
}
return 0;
}
void main()
{
HANDLE hPassenger[2];
DWORD i;
printf("\tSTART\n");
wcMutex = CreateMutex(NULL, FALSE, NULL);
for (i = 1; i <=2; i++)
{
hPassenger[i-1] = CreateThread(NULL, 0, Passenger, (PVOID)i, CREATE_SUSPENDED, NULL);
ResumeThread(hPassenger[i-1]);
}
WaitForMultipleObjects(2, hPassenger, TRUE, INFINITE);
for(i = 0; i < 2; i++)
CloseHandle(hPassenger[i]);
CloseHandle(wcMutex);
printf("\tEND\n");
}
#include <windows.h>
HANDLE wcMutex;
DWORD WINAPI Passenger(PVOID pvParam)
{
int count = 0;
DWORD passenger = (DWORD)pvParam;
DWORD dwWaitResult;
DWORD dwWait;
while (count < 10)
{
printf("Passenger %d tries to use WC...\n", passenger);
dwWaitResult = WaitForSingleObject(wcMutex, INFINITE);
if (dwWaitResult == WAIT_OBJECT_0)
{
printf("Passenger %d starts using WC...\n", passenger);
rand_s(&dwWait);
dwWait = (dwWait+1000) % 2000;
Sleep(dwWait);
printf("Passenger %d finished!\n", passenger);
ReleaseMutex(wcMutex);
}
count++;
rand_s(&dwWait);
dwWait = (dwWait+1000) % 2000;
Sleep(dwWait);
}
return 0;
}
void main()
{
HANDLE hPassenger[2];
DWORD i;
printf("\tSTART\n");
wcMutex = CreateMutex(NULL, FALSE, NULL);
for (i = 1; i <=2; i++)
{
hPassenger[i-1] = CreateThread(NULL, 0, Passenger, (PVOID)i, CREATE_SUSPENDED, NULL);
ResumeThread(hPassenger[i-1]);
}
WaitForMultipleObjects(2, hPassenger, TRUE, INFINITE);
for(i = 0; i < 2; i++)
CloseHandle(hPassenger[i]);
CloseHandle(wcMutex);
printf("\tEND\n");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呵呵用java我知道应该是个什么思路,应该用线程的同步来处理,就像操作系统中的生产者消费者问题一样
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询