Mutex in shared memory when one user crashes? Mutex in shared memory when one user crashes? linux linux

Mutex in shared memory when one user crashes?


It seems that the exact answer has been provided in the form of robust mutexes.

According to POSIX, pthread mutexes can be initialised "robust" using pthread_mutexattr_setrobust(). If a process holding the mutex then dies, the next thread to acquire it will receive EOWNERDEAD (but still acquire the mutex successfully) so that it knows to perform any cleanup. It then needs to notify that the acquired mutex is again consistent using pthread_mutex_consistent().

Obviously you need both kernel and libc support for this to work. On Linux the kernel support behind this is called "robust futexes", and I've found references to userspace updates being applied to glibc HEAD.

In practice, support for this doesn't seem to have filtered down yet, in the Linux world at least. If these functions aren't available, you might find pthread_mutexattr_setrobust_np() there instead, which as far as I can gather appears to be a non-POSIX predecessor providing the same semantics. I've found references to pthread_mutexattr_setrobust_np() both in Solaris documentation and in /usr/include/pthread.h on Debian.

The POSIX spec can be found here: http://www.opengroup.org/onlinepubs/9699919799/functions/pthread_mutexattr_setrobust.html


If you're working in Linux or something similar, consider using named semaphores instead of (what I assume are) pthreads mutexes. I don't think there is a way to determine the locking PID of a pthreads mutex, short of building your own registration table and also putting it in shared memory.


How about file-based locking (using flock(2))? These are automatically released when the process holding it dies.

Demo program:

#include <stdio.h>#include <time.h>#include <sys/file.h>void main() {  FILE * f = fopen("testfile", "w+");  printf("pid=%u time=%u Getting lock\n", getpid(), time(NULL));  flock(fileno(f), LOCK_EX);  printf("pid=%u time=%u Got lock\n", getpid(), time(NULL));  sleep(5);  printf("pid=%u time=%u Crashing\n", getpid(), time(NULL));  *(int *)NULL = 1;}

Output (I've truncated the PIDs and times a bit for clarity):

$ ./a.out & sleep 2 ; ./a.out [1] 15pid=15 time=137 Getting lockpid=15 time=137 Got lockpid=17 time=139 Getting lockpid=15 time=142 Crashingpid=17 time=142 Got lockpid=17 time=147 Crashing[1]+  Segmentation fault      ./a.outSegmentation fault

What happens is that the first program acquires the lock and starts to sleep for 5 seconds. After 2 seconds, a second instance of the program is started which blocks while trying to acquire the lock. 3 seconds later, the first program segfaults (bash doesn't tell you this until later though) and immediately, the second program gets the lock and continues.