Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using pthread mutex shared between processes correctly

There are many questions on stackoverflow about if a pthread mutex can be shared between processes, but I found no questions/answers regarding initialization of the shared mutex.

As far as I understand, the common way of using a process-shared mutex is the following: allocate a block of shared memory, initialize a pthread mutex on the shared memory block, use it.

In case of shared memory creation, it is handled by OS if multiple processes try to allocate a shared memory block with the same key ID. OK, but what I don't understand is how can I initialize a mutex on the shared memory block safely?

Am I right that the pthread_mutex_init doesn't provide any safe approach to initialize the pthread_mutex_t simultaneously from different processes? If yes, how can I provide exclusive access for processes to initialize a shared "mutual exclusion"? And how can I make sure if another process initialized the mutex successfully or not?

The second question relates to a case when a process blocking a mutex crashes. OK, there is a robust mutex which handles such cases and returns a corresponding error code. What about the shared memory block? It seems like a process should take care about if it is the last process which uses the shared memory to destroy it.

like image 888
Rom098 Avatar asked Mar 06 '17 15:03

Rom098


People also ask

What is the process shared attribute in mutex?

The process shared attribute indicates whether the mutex that is created using the mutex attributes object can be shared between threads in separate processes ( PTHREAD_PROCESS_SHARED) or shared between threads within the same process ( PTHREAD_PROCESS_PRIVATE ).

How to set pthread_process_shared to the mutex variable?

As title, the key is to set an attribute(PTHREAD_PROCESS_SHARED) to the mutex/condition variable using pthread_mutexattr_setpshared()or pthread_condattr_setpshared(). Without these function calls, the parent in the following code will not get signaled forever.

What does int pthread_mutex_destroy do?

int pthread_mutex_destroy (pthread_mutex_t *mutex) : Deletes a mutex object, which identifies a mutex. Mutexes are used to protect shared resources. mutex is set to an invalid value, but can be reinitialized using pthread_mutex_init ().

What happens if pthread_mutex_init () fails?

If unsuccessful, pthread_mutex_init () returns -1. int pthread_mutex_lock (pthread_mutex_t *mutex) : Locks a mutex object, which identifies a mutex. If the mutex is already locked by another thread, the thread waits for the mutex to become available.


1 Answers

Am I right that the pthread_mutex_init doesn't provide any safe approach to initialize the pthread_mutex_t simultaneously from different processes?

Correct. It is up to you to ensure that only one process calls pthread_mutex_init() on the mutex, and that no process tries to operate on the mutex until that call has successfully returned.

For example, with POSIX shm_open() shared memory regions, you can have the processes try to open the region with the O_CREAT and O_EXCL flags, so that exactly one process will succeed in creating it. This process is then responsible for resizing the shared memory region and initialising the mutex with pthread_mutex_init(). The other processes must then wait for some kind of notification from the initialising process before opening the shared memory region - eg you could have the processes block opening a FIFO O_RDONLY, and have the initialising process notify them by opening the FIFO O_WRONLY (which will cause the open to succeed).

Usually, a shared memory segment will not be the only communication channel between the processes. Typically you would bootstrap the communication through a UNIX domain socket and negotiate the setup of the shared memory region over it, probably even passing the shared memory region file descriptor through the socket with a SCM_RIGHTS message. The shared memory region would then be used to accelerate the performance-sensitive IPC.

like image 124
caf Avatar answered Oct 12 '22 23:10

caf