Named mutexes
POSIX mutexes work simply in multi-threaded programs; we demonstrated this in Chapter 16, Thread Synchronization. This would not be the case with regard to multiple process environments, however. To have a mutex work among a number of processes, it would need to be defined within a place that is accessible to all of them.
The best choice for a shared place such as this is a shared memory region. Therefore, to have a mutex that works in a multi-process environment, it should be distributed in a shared memory region.
The first example
The following example, example 18.2, is a clone of example 18.1, but it solves the potential race condition using named mutexes instead of named semaphores. It also shows how to make a shared memory region and use it to store a shared mutex.
Since each shared memory object has a global name, a mutex stored in a shared memory region can be considered named and can be accessed by other processes throughout the system.
The following...