是否可以在 Linux/UNIX 上的多处理情况下使用互斥锁?
这是一道面试题。
是否可以在 Linux/UNIX 上的多处理情况下使用互斥锁?
我的想法: 不,不同的进程有独立的内存空间。
互斥量仅用于多线程。
信号量用于多处理进行同步。
正确的 ?
欢迎任何评论。
谢谢
This is an interview question.
Is it possible to use mutex in multiprocessing case on Linux/UNIX ?
My idea:
No, different processes have separate memory space.
mutex is only used for multithreading.
semaphore is used for multiprocessing to do synchronization.
right ?
Any comments are welcome.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
互斥锁(互斥锁)可防止多个线程
同时执行代码的关键部分
访问共享数据(即,互斥体用于序列化
线程的执行)。所有互斥体必须是全局的。一个
通过 mutex_lock() 成功调用互斥锁
将导致另一个线程也尝试锁定
相同的互斥锁会阻塞,直到所有者线程通过方式解锁它
mutex_unlock() 的。同一进程内的线程或
其他进程内可以共享互斥体。
互斥体可以同步同一进程内的线程或
在其他进程中。互斥体可用于同步
如果互斥体分配在进程之间的线程
可写内存并在协作进程之间共享
(请参阅 mmap(2)),并且已为此任务进行了初始化。
初始化
互斥体可以是进程内的,也可以是进程间的,具体取决于
根据隐式或显式传递给的参数
该互斥体的初始化。静态分配的互斥体
不需要显式初始化;默认情况下,一个
静态分配的互斥体初始化为全零
其范围设置为调用进程内。
对于进程间同步,需要分配互斥体
存储在这些进程之间共享的内存中。自从
这种互斥体的内存必须动态分配,
互斥锁需要使用 mutex_init() 显式初始化。
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.
Initialization
Mutexes are either intra-process or inter-process, depending
upon the argument passed implicitly or explicitly to the
initialization of that mutex. A statically allocated mutex
does not need to be explicitly initialized; by default, a
statically allocated mutex is initialized with all zeros
and its scope is set to be within the calling process.
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().
很有可能使用进程共享互斥体。
事实上,现代应用程序更喜欢使用进程共享互斥体以及进程共享条件变量而不是信号量,因为后者灵活性较差。
我记得在 2004 年使用 Red Hat Linux,当时它支持进程共享互斥体和条件变量。
It is quite possible to use a process-shared mutex.
In fact, modern applications prefer using a process shared mutex along with process shared condition variable over a semaphore because the latter is less flexible.
I remember using Red Hat Linux in 2004 and at that time it supported both process shared mutexes and condition variables.
不完全是。 POSIX 线程有一个进程共享属性的概念,它可以用于创建可由多个进程操作的互斥体。
您可以将这样的互斥锁放入共享内存中,以便多个进程都可以访问它。
LINUX是否实现了这个,我不确定,我从来没有需要使用它,因为它看起来不必要的复杂。
有关属性的有用概要,请参阅我对此问题的回答。
Not quite. POSIX threads has a concept of a process-shared attribute which can be used to create mutexes that can be operated on by multiple processes.
You can put such a mutex in shared memory so that multiple processes can all get at it.
Whether LINUX implements this., I'm not sure, I've never had a need to use it since it seems unnecessarily complex.
For a useful precis of attributes, see my answer to this question.
我正在寻找一个命名互斥体,以便我可以确保进程生命周期内的互斥(确保每个属性集仅运行一个进程)。我没有找到(看起来我可能还不够努力),所以我通过使用抽象的 UNIX 域套接字在 Linux 中实现了我自己的伪命名互斥体。只有对该套接字的单个 bind() 才会成功。另一个好处是,如果进程终止,操作系统将清理抽象 UNIX 域套接字,因此不会清理套接字本身。不幸的是,我不确定有什么方法可以让您“等待”这个伪互斥锁变得可用。
抽象 UNIX 域套接字是名称以空字节开头的 UNIX 域套接字。但请注意,我相信整个缓冲区都用作名称,因此您要确保不只是 memcpy 或 strcpy 部分字符串到其中,或者如果您确实确保首先用某些字符填充整个缓冲区。
除了第一个bind()之外,所有的bind()都会失败,错误号为EADDRINUSE。
谢谢,
缺口
I was looking for a named mutex so that I could ensure mutual exclusion for the lifetime of a process (making sure only one process running per some set of properties). I didn't find one (looks like I might have not looked hard enough) and so I implemented my own pseudo named mutex in linux by using an abstract UNIX domain socket. Only a single bind() to that socket will succeed. The other nice thing is that the OS will cleanup the abstract UNIX domain socket if the process dies and thus doesn't cleanup the socket itself. Unfortunately I'm not sure of any way for you to "wait" on this pseudo mutex to become available.
An abstract UNIX domain socket is a UNIX domain socket whose name begins with a null byte. Beware though, I believe that the entire buffer is used as the name and thus you want to ensure that you don't just memcpy or strcpy a partial string into it, or if you do make sure you first fill the entire buffer with some character.
All but the first bind() will fail with an errno of EADDRINUSE.
Thanks,
Nick
是的,一般来说,在 Linux 中我们只有未命名的互斥体,因此它们无法在进程之间进行操作。我们需要一个信号量来克服这个问题。
在 Windows 中,它们有一个命名互斥体的概念,它允许我们跨进程使用互斥体。
Yes, in general in Linux we have only unnamed mutexes due to which they cannot operate between processes. We need a semaphore to get over that.
In windows, they have a concept of named mutexes which lets us use mutexes across processes.