使用互斥体调节两个进程之间的 IPC
我正在开发一个创建两个进程的项目,我想调节它们之间的 IPC。
这些进程是使用 createProces 函数创建的,我想使用互斥体来执行一些 IPC。
在 Linux 中,我使用信号量来完成此操作,但是我读到,对于 Windows 中的 IPC,我必须使用互斥体。
在 Windows 中我似乎无法让它工作。首先,我创建如下步骤:
CreateProcess(IpApplicationName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartInfo, &ProcessInfo);
CreateProcess(IpApplicationName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartInfo2, &ProcessInfo2);
进程正常启动,但是当我从一个进程中删除 releaseMutex
调用时,它不会在该进程中保持等待。这是过程一:
volatile HANDLE hMutex; // Global hMutex Object
int main()
{
hMutex=CreateMutex(NULL,FALSE,NULL);
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
printf("Thread writing to database...\n");
Sleep(2000);
ReleaseMutex(hMutex);
}
return 0;
}
在过程二中,我使用 open mutex 打开互斥体并注释 releaseMutex
(这样它就会卡在这里,用于测试。但是它会继续进行):
int main()
{
while(1)
{
HANDLE hMutex;
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,MUTEXNAME);
WaitForSingleObject(hMutex,INFINITE);
printf("Thread writing to database22...\n");
Sleep(2000);
//ReleaseMutex(hMutex);
}
return 0;
}
任何人都可以告诉我我做错了什么?
I am working on a project that creates two processes and I want to regulate the IPC between them.
The processes are created with the createProces
function, and I want to use a mutex to do some IPC.
In Linux I do this with semaphores, however I have read that for IPC in Windows I have to use a mutex.
In windows I can't seem to get it to work. First I create the treads like this:
CreateProcess(IpApplicationName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartInfo, &ProcessInfo);
CreateProcess(IpApplicationName, NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &StartInfo2, &ProcessInfo2);
The processes start up normal but when I remove the releaseMutex
call from one process it won't be kept waiting in that process. Here is process one:
volatile HANDLE hMutex; // Global hMutex Object
int main()
{
hMutex=CreateMutex(NULL,FALSE,NULL);
while(1)
{
WaitForSingleObject(hMutex,INFINITE);
printf("Thread writing to database...\n");
Sleep(2000);
ReleaseMutex(hMutex);
}
return 0;
}
In process two I open the mutex with open mutex and comment the releaseMutex
(so that it will be stuck here, for testing. However it will keep on going):
int main()
{
while(1)
{
HANDLE hMutex;
hMutex=OpenMutex(MUTEX_ALL_ACCESS,FALSE,MUTEXNAME);
WaitForSingleObject(hMutex,INFINITE);
printf("Thread writing to database22...\n");
Sleep(2000);
//ReleaseMutex(hMutex);
}
return 0;
}
Can anyone tell me what I am doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您检查这些 Win32 API 调用的错误,那就很明显了。在编写此代码时,
OpenMutex
调用一定会失败,因为还没有其他人创建具有该名称的互斥体。来自 OpenMutex 文档:
每个 Win32 API 都可能失败 - 您需要正确检查并处理这些错误。
If you were checking errors on these Win32 API calls it would be obvious. The
OpenMutex
call must be failing, as this code is written, since nobody else has yet created a mutex with that name.From the OpenMutex docs:
Every Win32 API can fail - you need to check for and handle those errors properly.
您使用 CreateMutex 创建匿名互斥体,然后尝试通过名称查找它
You create anonymous Mutex using CreateMutex, then try to find it by name
您必须提及 CreateMutex 和 OpenMutex 中进程的
UNIQUE
互斥体名称。You have to mention
UNIQUE
mutex name both process in CreateMutex and in OpenMutex .