使用互斥体调节两个进程之间的 IPC

发布于 2024-11-19 23:55:10 字数 1291 浏览 0 评论 0原文

我正在开发一个创建两个进程的项目,我想调节它们之间的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

盗心人 2024-11-26 23:55:10

如果您检查这些 Win32 API 调用的错误,那就很明显了。在编写此代码时,OpenMutex 调用一定会失败,因为还没有其他人创建具有该名称的互斥体。

来自 OpenMutex 文档

OpenMutex 函数允许多个进程打开
相同的互斥对象。 仅当某个进程有时该函数才会成功
已经使用 CreateMutex 函数创建了互斥体。

每个 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:

The OpenMutex function enables multiple processes to open handles of
the same mutex object. The function succeeds only if some process has
already created the mutex by using the CreateMutex function.

Every Win32 API can fail - you need to check for and handle those errors properly.

一梦浮鱼 2024-11-26 23:55:10

您使用 CreateMutex 创建匿名互斥体,然后尝试通过名称查找它

You create anonymous Mutex using CreateMutex, then try to find it by name

水染的天色ゝ 2024-11-26 23:55:10

您必须提及 CreateMutex 和 OpenMutex 中进程的 UNIQUE 互斥体名称。

You have to mention UNIQUE mutex name both process in CreateMutex and in OpenMutex .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文