cpointersposixsemaphorealternating

Issue with alternating between parent and child process using POSIX semaphore functions


I'm attempting to create a C program where the counter is incremented by alternating between the parent and child using the POSIX semaphore functions. So far I'm having trouble using it considering this is the first time I am using semaphore functions. If there is anything wrong with my program some pointers would be great.

So far it seems that the child is not incrementing at all and I'm not certain why it's happening. The program also acts strange near the beginning by staying in the child before alternating to the parent afterward.

The results of the program so far:

GOT EM - parent: expected 0, got 0
GOT EM - child: expected 1, got 1
    child: expected 3, got 1
    child: expected 5, got 1
    child: expected 7, got 1
    child: expected 9, got 1
    child: expected 11, got 1
    child: expected 13, got 1
    child: expected 15, got 1
    child: expected 17, got 1
    child: expected 19, got 1
    child: expected 21, got 1
    child: expected 23, got 1
    child: expected 25, got 1
    child: expected 27, got 2
    child: expected 29, got 2
    child: expected 31, got 2
    child: expected 33, got 2
    child: expected 35, got 2
    child: expected 37, got 2
GOT EM - parent: expected 2, got 2
    child: expected 39, got 2
    child: expected 41, got 3
GOT EM - parent: expected 4, got 4
    child: expected 43, got 4
GOT EM - parent: expected 6, got 6
    child: expected 45, got 6
GOT EM - parent: expected 8, got 8
    child: expected 47, got 8
GOT EM - parent: expected 10, got 10
    child: expected 49, got 10

My program:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <semaphore.h>

#define NLOOPS  1000
#define SIZE    sizeof(long)    /* size of shared memory area */

struct shmbuf{
    sem_t child;
    sem_t parent;
};

static int update(long *ptr)
{
     return((*ptr)++);      /* return value before increment */
}

int main(void)
{
    int     fd, i, counter;
    pid_t   pid;
    struct shmbuf   *shmp;


    if ((fd = open("/dev/zero", O_RDWR)) < 0)
        perror("open error");

    ftruncate(fd, sizeof(struct shmbuf));

    if ((shmp = mmap(0, sizeof(struct shmbuf), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
        perror("mmap error");

    close(fd);



    sem_init(&shmp->child, 1, 0);       
    sem_init(&shmp->parent, 1, 1);       /*  Parent first */


    if ((pid = fork()) < 0) {
        perror("fork error");
    } else if (pid > 0) {           /* parent */
        for (i = 0; i < NLOOPS; i += 2) {

            sem_wait(&shmp->parent);

            if ((counter = update((long *)shmp)) != i)
                printf("    parent: expected %d, got %d\n", i, counter);
            else
                printf("GOT EM - parent: expected %d, got %d\n", i, counter);

            sem_post(&shmp->child);     
        }
    } else {                        /* child */
        for (i = 1; i < NLOOPS + 1; i += 2) {

            sem_wait(&shmp->child);

            if ((counter = update((long *)shmp)) != i)
                printf("    child: expected %d, got %d\n", i, counter);
            else
                printf("GOT EM - child: expected %d, got %d\n", i, counter);

            sem_post(&shmp->parent);    
        }   
    }

    exit(0);
}


Solution

  • update((long *)shmp)
    

    That cast is wrong, makes no sense whatsoever, and is the source of your problems. You can't just use the memory that's part of one of the semaphores as your own counter. You need to add a counter field of your own to your struct and pass a pointer to that field instead.