Named POSIX semaphores
As you saw in Chapter 16, Thread Synchronization, semaphores are the main tool to synchronize a number of concurrent tasks. We saw them in multi-threaded programs and saw how they help to overcome the concurrency issues.
In this section, we are going to show how they can be used among some processes. Example 18.1 shows how to use a POSIX semaphore to solve the data races we encountered in examples 17.6 and 17.7 given in the previous chapter, Process Execution. The example is remarkably similar to example 17.6, and it again uses a shared memory region for storing the shared counter variable. But it uses named semaphores to synchronize the access to the shared counter.
The following code boxes show the way that we use a named semaphore to synchronize two processes while accessing a shared variable. The following code box shows the global declarations of example 18.1:
#include <stdio.h> ... #include <semaphore.h> // For using semaphores #define...