Linux 使用 pthread 进行进程间互斥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6477525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Interprocess mutex with pthreads
提问by MetallicPriest
I want to use a mutex which will be used to synchronize access to some variables residing in the memory shared b/w two different processes. How can I achieve that. Code sample to perform that will be very appreciated.
我想使用一个互斥锁,它将用于同步对驻留在共享 b/w 两个不同进程的内存中的一些变量的访问。我怎样才能做到这一点。要执行的代码示例将不胜感激。
采纳答案by cnicutar
Use a POSIX semaphoreinitialized to (See below) Use 1
instead.sem_init
for unnamed semaphores or sem_open
for named ones.
使用初始化为的POSIX 信号量(见下文)1
。sem_init
用于未命名的信号量或sem_open
命名的信号量。
sem_t sem;
/* initialize using sem_init or sem_open */
sem_wait(&sem);
/* critical region */
sem_post(&sem);
Many years after initially posting this answer, it has to be updated.
在最初发布此答案多年后,必须对其进行更新。
Mutexes should actually be used instead of semaphores. R and kuga's comments (copied verbatim below) explain why. In particular I find kuga's mention that mutexes can only be post
ed by their locking thread most compelling.
实际上应该使用互斥体而不是信号量。R 和 kuga 的评论(在下面逐字复制)解释了原因。特别是我发现 kuga 提到的互斥锁只能post
通过它们的锁定线程进行编辑,这点最引人注目。
R
电阻
sem_init requires a nonzero pshared argument to be shared, just like a mutex would require the pshared attribute. There's no reason to prefer semaphores over mutexes for this, and in fact mutexes would be better because you could use a robust mutex which allows you to handle the (very real!) case where one process dies while holding the lock.
sem_init 需要共享一个非零的 pshared 参数,就像互斥锁需要 pshared 属性一样。没有理由为此更喜欢信号量而不是互斥锁,实际上互斥锁会更好,因为您可以使用强大的互斥锁,它允许您处理(非常真实!)一个进程在持有锁时死亡的情况。
kuga
库加
Additionally to R..`s post, a mutex can only be posted by the thread that locks it. This is often required and a semaphore does not provide this feature. So this is not the correct answer, Jeff′s answer should be flagged as the correct answer.
除了 R.. 的帖子外,互斥锁只能由锁定它的线程发布。这通常是必需的,而信号量不提供此功能。所以这不是正确答案,杰夫的答案应该被标记为正确答案。
回答by Jeff
The following example demonstrates the creation, use and destruction of a Pthread interprocess mutex. Generalizing the example for multiple processes is left as an exercise for the reader.
以下示例演示了 Pthread 进程间互斥锁的创建、使用和销毁。将多个进程的示例概括为读者的练习。
#include <pthread.h>
pthread_mutex_t shm_mutex;
int main(void)
{
int err;
pthread_mutexattr_t attr;
err = pthread_mutexattr_init(&attr); if (err) return err;
err = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); if (err) return err;
err = pthread_mutex_init(&shm_mutex, &attr); if (err) return err;
err = pthread_mutexattr_destroy(&attr); if (err) return err;
err = pthread_mutex_lock(&shm_mutex); if (err) return err;
err = pthread_mutex_unlock(&shm_mutex); if (err) return err;
err = pthread_mutex_destroy(&shm_mutex); if (err) return err;
return 0;
}