use*_*288 24 multithreading synchronization mutex semaphore multiprocessing
这是一个面试问题.
是否可以在Linux/UNIX上的多处理案例中使用互斥?
我的想法:不,不同的进程有独立的内存空间.
mutex仅用于多线程.
信号量用于多处理以进行同步.
对 ?
欢迎任何评论.
谢谢
小智 28
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.
Run Code Online (Sandbox Code Playgroud)
初始化互斥锁是进程内或进程间,具体取决于隐式或显式传递给该互斥锁初始化的参数.静态分配的互斥锁不需要显式初始化; 默认情况下,静态分配的互斥锁初始化为全零,其范围设置为在调用进程内.
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().
Run Code Online (Sandbox Code Playgroud)