在Linux / UNIX上的多处理情况下可以使用互斥体吗?

这是一个面试问题。

在Linux / UNIX的多处理情况下可以使用互斥体吗?

我的想法:不,不同的进程有不同的内存空间。

互斥体仅用于multithreading。

信号量用于多处理进行同步。

对 ?

任何意见,欢迎。

谢谢

Mutual exclusion locks (mutexes) prevent multiple threads from simultaneously executing critical sections of code that access shared data (that is, mutexes are used to serialize the execution of threads). All mutexes must be global. A successful call for a mutex lock by way of mutex_lock() will cause another thread that is also trying to lock the same mutex to block until the owner thread unlocks it by way of mutex_unlock(). Threads within the same process or within other processes can share mutexes. Mutexes can synchronize threads within the **same process** or in ***other processes***. Mutexes can be used to synchronize threads between processes if the mutexes are allocated in writable memory and shared among the cooperating processes (see mmap(2)), and have been initialized for this task. 

初始化互斥锁可以是进程内进程,也可以是进程间进程,这取决于隐式或显式传递给该互斥体初始化的参数。 静态分配的互斥量不需要显式初始化; 默认情况下,一个静态分配的互斥量被全零初始化,其范围被设置在调用过程中。

  For inter-process synchronization, a mutex needs to be allo- cated in memory shared between these processes. Since the memory for such a mutex must be allocated dynamically, the mutex needs to be explicitly initialized using mutex_init(). 

使用共享进程的互斥体是完全可能的。

实际上,现代应用程序更喜欢使用进程共享互斥体和信号量的进程共享条件variables,因为后者不够灵活。

我记得2004年使用Red Hat Linux,当时它支持进程共享互斥和条件variables。

不完全的。 POSIX线程有一个进程共享属性的概念,可用于创build可以由多个进程操作的互斥体。

你可以把这样一个互斥体放在共享内存中,这样多个进程都可以得到它。

不pipeLINUX是否实现了这个,我不确定,我从来没有必要使用它,因为它似乎不必要的复杂。

有关属性的有用属性,请参阅我对此问题的回答 。

我正在寻找一个有名的互斥体,这样我就可以在一个进程的生命周期中确保相互排斥(确保每个进程只运行一个进程)。 我没有find一个(看起来像我可能看起来不够辛苦),所以我通过使用抽象的UNIX域套接字在Linux中实现了我自己的伪命名互斥体。 只有一个绑定()到该套接字将成功。 另一个好处是,如果进程死亡,操作系统将清理抽象的UNIX域套接字,从而不清除套接字本身。 不幸的是,我不确定是否有任何方法让你“等待”这个伪互斥体变得可用。

抽象的UNIX域套接字是一个UNIX域套接字,其名称以空字节开头。 但是要小心,我相信整个缓冲区被用作名称,因此你要确保你不只是memcpy或strcpy部分string,或者如果你确保你首先用一些字符填充整个缓冲区。

除了第一个bind()之外的所有东西都会失败,并带有EADDRINUSE错误。

 // Create an abstract socket to use as a mutex. int err; int mutex_sock = socket(AF_UNIX, SOCK_STREAM, 0); if (mutex_sock == -1) { err = errno; printf("main, failed creating mutex socket: %s\n", get_error_string(errno, error_string, sizeof(error_string))); log_event(LOG_LEVEL_ERROR, "main, failed creating mutex socket: " "%s", get_error_string(errno, error_string, sizeof(error_string))); errno = err; goto done; } // Bind to abstract socket. We use this as a sort of named mutex. struct sockaddr_un addr; memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; strncpy(addr.sun_path + 1, socket_name, sizeof(addr.sun_path) - 2); result = bind(mutex_sock, (struct sockaddr*) &addr, sizeof(addr)); if (result == -1) { err = errno; if (errno == EADDRINUSE) { printf("main, failed bind to mutex socket: %s. " "Another instance must be running.\n", get_error_string(errno, error_string, sizeof(error_string))); log_event(LOG_LEVEL_ERROR, "main, failed bind to mutex socket: " "%s. " "Another instance must be running.", get_error_string(errno, error_string, sizeof(error_string))); } else { printf("main, failed bind to mutex socket: %s\n", get_error_string(errno, error_string, sizeof(error_string))); log_event(LOG_LEVEL_ERROR, "main, failed bind to mutex socket: %s", get_error_string(errno, error_string, sizeof(error_string))); } errno = err; goto done; } 

谢谢,尼克

是的,一般在Linux中,我们只有未命名的互斥体,因此它们不能在两个进程之间运行。 我们需要一个信号来解决这个问题。

在Windows中,他们有一个名为mutexes的概念,它允许我们跨进程使用互斥锁。