无法等待共享信号量

Can not wait on shared semaphore

我必须编程 Readers-Writers Problem in C without using threads. I have used this 来初始化共享信号量:

/* initialize semaphores for shared processes */
resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/rSem");
mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
sem_unlink ("/mSem");

但是 运行 子进程

sem_wait(resource);

终止程序。

这是我的完整代码:

/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <semaphore.h>  /* Semaphore */
#include <sys/mman.h>
#include <fcntl.h>          /* O_CREAT, O_EXEC          */


/* prototype for thread routine */
void writer ( int , int *);
void reader ( int , int *, int *);
void multipleFork ( int );

/* global variables */
sem_t  *mutex;
sem_t  *resource;

int main(int argc, char **argv)
{
    int * Buffer = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);
    int * readcount = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_SHARED|MAP_ANON, -1, 0);

    /* initialize semaphores for shared processes */
    resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);
    sem_unlink ("/rSem");
    mutex = sem_open ("/mSem", 0644, O_CREAT | O_EXCL, 1);
    sem_unlink ("/mSem");


    pid_t pid = fork();
    if (pid == 0)
        writer(1, Buffer);
    else
        reader(3, Buffer, readcount);

    sem_destroy(resource);
    sem_destroy(mutex);

    exit(0);
} /* main() */

void writer ( int n , int * Buffer)
{
    multipleFork(n);
    sleep(1);

    while(1){
        usleep(rand()%3000);
        sem_wait(resource);

        //<CRITICAL Section>
        printf("The writer (%d) ‫‪acquires‬‬ ‫‪the‬‬ ‫‪lock.\n‬‬", getpid());
        (*Buffer)++;
        printf("The writer (%d) writes ‫‪the‬‬ ‫‪value %d.\n‬‬", getpid(), *Buffer);
        sleep(1);
        printf("The writer (%d) releases ‫‪the‬‬ ‫‪lock.‬‬\n", getpid());
        //<CRITICAL Section>
        sem_post(resource);
        sleep(1);
    }

}

void reader( int n , int * Buffer, int * readcount) {
    multipleFork(n);

    while(1){
        usleep(rand()%3000);
        sem_wait(mutex);
        usleep(rand()%3000);

        if ((*readcount) == 0)
        {
            sem_wait(resource);
            printf("The first reader (%d) acquires the lock.%d\n", getpid(), *readcount);
            (*readcount)++;
        }
        else if ((*readcount) > 0)
        {
            printf("The reader (%d) acquires the lock.%d\n", getpid(), *readcount);
            (*readcount)++;
        }
        sem_post(mutex);

        //<CRITICAL Section>
        printf("Reader (%d) reads the value %d.%d\n", getpid(), *Buffer, *readcount);
        sleep(1);
        //<CRITICAL Section>

        sem_wait(mutex);
        (*readcount)--;
        if ((*readcount) == 0)
        {
            printf("The last reader (%d) releases the lock.\n", getpid());
            sem_post(resource);
        }
        else
            printf("The reader (%d) releases the lock.\n", getpid());
        sem_post(mutex);
        sleep(1);
    }
}

void multipleFork (int n)
{
    while(n-1 > 0){
        pid_t pid = fork();
        if (pid == 0){
            return;
        }
        else
            n--;
    }
    return;
}

我用过

resource = sem_open ("/rSem", 0644, O_CREAT | O_EXCL, 1);

而不是

resource = sem_open ("/rSem", O_CREAT | O_EXCL, 0644, 1);