Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use mutex in multiprocessing case on 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

like image 786
user1002288 Avatar asked Feb 22 '12 05:02

user1002288


People also ask

Can processes share mutex?

PTHREAD_PROCESS_SHARED. Permits a mutex to be operated upon by any thread that has access to the memory where the mutex is allocated, even if the mutex is allocated in memory that is shared by multiple processes.

How does mutex work in Linux?

The idea behind mutexes is to only allow one thread access to a section of memory at any one time. If one thread locks the mutex, any other lock attempts will block until the first one unlocks.

In what scenario do you need to use mutex semaphores and why?

The correct use of a semaphore is for signaling from one task to another. A mutex is meant to be taken and released, always in that order, by each task that uses the shared resource it protects. By contrast, tasks that use semaphores either signal or wait—not both.

Can multiple threads be used simultaneously in mutex?

Only one thread can hold the mutex at a time, and the other thread can do no useful work.


2 Answers

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().

like image 56
laksbv Avatar answered Sep 22 '22 17:09

laksbv


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.

like image 27
Maxim Egorushkin Avatar answered Sep 20 '22 17:09

Maxim Egorushkin