Unix Inter-Process Communications (IPC)

System V IPC and select and poll


Basic Properties


Permission


Message Queues


Message Structure - struct msgbuf

msgsnd


Reading A Message

Process Synchronization:Semaphores


Creating A Semaphore Set


Setting And Getting Semaphore Values.


Using Semaphores for Mutual Exclusion


Using Semaphores For Producer-Consumer Operations

  • Semaphores are more general than mutexes. The can also be used to keep track of availabe resources. For example, a producer-comsumer scenario:

    To control a printing system, we need the producers to maintain a count of the number of files waiting in the spool directory and incrementing it for every new file placed there. The consumers check this counter, and whenever it gets above zero, one of them grabs a file from the spool, and sends it to the printer. If there are no files in the spool (i.e. the counter value is zero), all consumer processes get blocked.

    
    /* this variable will contain the semaphore set. */
    int sem_set_id;
    
    /* semaphore value, for semctl().                */
    union semun sem_val;
    
    /* structure for semaphore operations.           */
    struct sembuf sem_op;
    
    /* first we create a semaphore set with a single semaphore, */
    /* whose counter is initialized to '0'.                     */
    sem_set_id = semget(IPC_PRIVATE, 1, 0600);
    if (sem_set_id == -1) {
        perror("semget");
        exit(1);
    }
    sem_val.val = 0;
    semctl(sem_set_id, 0, SETVAL, sem_val);
    
    /* we now do some producing function, and then signal the   */
    /* semaphore, increasing its counter by one.                */
    .
    .
    sem_op.sem_num = 0;
    sem_op.sem_op = 1;
    sem_op.sem_flg = 0;
    semop(sem_set_id, &sem_op, 1);
    .
    .
    .
    /* meanwhile, in a different process, we try to consume the      */
    /* resource protected (and counter) by the semaphore.            */
    /* we block on the semaphore, unless it's value is non-negative. */
    sem_op.sem_num = 0;
    sem_op.sem_op = -1;
    sem_op.sem_flg = 0;
    semop(sem_set_id, &sem_op, 1);
    
    /* when we get here, it means that the semaphore's value is '1'  */
    /* or more, so there's something to consume.                     */
    .
    .
    
  • Note that the "wait" and "signal" operations are the same as when using the semaphore as a mutex. The only difference is in who is doing the "wait" and the "signal". With a mutex, the same process did both the "wait" and the "signal" (in that order). In the producer-consumer example, one process is doing the "signal" operation, while the other is doing the "wait" operation.
  • The full source code for a simple program that implements a producer-consumer system with two processes, is found in the file sem-producer-consumer.c.
  • More Examples

    Semop Revised


    Behavior of semop depends on the value of semop.semop and the state of the semaphore.

    Semctl Revised



    Shared Memory


    Allocating A Shared Memory Segment


    Attaching And Detaching A Shared Memory Segment


    Placing Data In Shared Memory