c中进程间同步中互斥量的使用
Usage of mutexes in interporcess sync in c
我已经搜索了一段时间,但由于找不到正确答案,所以决定问一下。
其实我有两个进程
进程 1:
#include <windows.h>
#include <stdio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
TRUE, // mutex owned
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n");
if(WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
printf("Error while waiting for the mutex.\n");
else
printf("Mutex openned by second process.\n");
CloseHandle(hMutex);
return 0;
}
进程 2:
#include <windows.h>
#include <stdio.h>
// This process opens a handle to a mutex created by another process.
int main(void)
{
HANDLE hMutex;
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %d\n", GetLastError() );
else printf("OpenMutex successfully opened the mutex.\n");
if(!ReleaseMutex(hMutex)){
printf("Error while releasing the mutex.\n")
}
CloseHandle(hMutex);
return 0;
}
因此,当我 运行 第一个进程时,它不会等待第二个进程释放互斥锁;由于互斥锁是创建的,没有信号,它不应该等到一些 process/thread 释放它然后打印消息?
您似乎混淆了两种类型的同步对象:Mutexes
和 Events
。
Mutexes
通常用于保护对共享资源的访问。在临界区的入口处,应该调用 Mutex
上的 WaitForSingleObject
。如果Mutex
没有被另一个执行单元(线程或进程)获取,则本执行单元获取并继续运行,否则一直锁定,直到其他进程释放它。在关键部分的末尾, Mutex
应该被释放。参见 Using Mutexes。这就是为什么你的进程没有暂停。
Events
另一方面用于同步执行单元或通知某些事情。当一个人创建一个事件时,它指定了它的信号状态。当 WaitForSingleObject
被调用并且事件处于非信号状态时,执行单元将被挂起,直到有东西发出事件信号。参见 Using Events
在你的情况下你应该使用 Event
我已经搜索了一段时间,但由于找不到正确答案,所以决定问一下。
其实我有两个进程
进程 1:
#include <windows.h>
#include <stdio.h>
// This process creates the mutex object.
int main(void)
{
HANDLE hMutex;
hMutex = CreateMutex(
NULL, // default security descriptor
TRUE, // mutex owned
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("CreateMutex error: %d\n", GetLastError() );
else
if ( GetLastError() == ERROR_ALREADY_EXISTS )
printf("CreateMutex opened an existing mutex\n");
else printf("CreateMutex created a new mutex.\n");
if(WaitForSingleObject(hMutex, INFINITE) == WAIT_FAILED)
printf("Error while waiting for the mutex.\n");
else
printf("Mutex openned by second process.\n");
CloseHandle(hMutex);
return 0;
}
进程 2:
#include <windows.h>
#include <stdio.h>
// This process opens a handle to a mutex created by another process.
int main(void)
{
HANDLE hMutex;
hMutex = OpenMutex(
MUTEX_ALL_ACCESS, // request full access
FALSE, // handle not inheritable
TEXT("AnotherMutex")); // object name
if (hMutex == NULL)
printf("OpenMutex error: %d\n", GetLastError() );
else printf("OpenMutex successfully opened the mutex.\n");
if(!ReleaseMutex(hMutex)){
printf("Error while releasing the mutex.\n")
}
CloseHandle(hMutex);
return 0;
}
因此,当我 运行 第一个进程时,它不会等待第二个进程释放互斥锁;由于互斥锁是创建的,没有信号,它不应该等到一些 process/thread 释放它然后打印消息?
您似乎混淆了两种类型的同步对象:Mutexes
和 Events
。
Mutexes
通常用于保护对共享资源的访问。在临界区的入口处,应该调用 Mutex
上的 WaitForSingleObject
。如果Mutex
没有被另一个执行单元(线程或进程)获取,则本执行单元获取并继续运行,否则一直锁定,直到其他进程释放它。在关键部分的末尾, Mutex
应该被释放。参见 Using Mutexes。这就是为什么你的进程没有暂停。
Events
另一方面用于同步执行单元或通知某些事情。当一个人创建一个事件时,它指定了它的信号状态。当 WaitForSingleObject
被调用并且事件处于非信号状态时,执行单元将被挂起,直到有东西发出事件信号。参见 Using Events
在你的情况下你应该使用 Event