C 无法等待共享信号量

C 无法等待共享信号量,c,multiprocessing,semaphore,shared-memory,terminate,C,Multiprocessing,Semaphore,Shared Memory,Terminate,我必须在不使用线程的情况下用C编程。我曾经初始化过共享信号量: /* 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"); 但在运行子进程时 se

我必须在不使用线程的情况下用C编程。我曾经初始化过共享信号量:

/* 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;
}
/*包括*/
#包含/*符号常量*/
#包含/*基本系统数据类型*/
#包括/*错误*/
#包括/*输入/输出*/
#包括/*一般公用设施*/
#包括/*POSIX线程*/
#include/*字符串处理*/
#include/*信号量*/
#包括
#包括/*O_创建,O_执行*/
/*线程例程原型*/
空写器(int,int*);
无效读卡器(int,int*,int*);
无效倍数(int);
/*全局变量*/
sem_t*互斥体;
sem_t*资源;
int main(int argc,字符**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|u WRITE,MAP|SHARED | MAP|ANON,-1,0);
/*初始化共享进程的信号量*/
resource=sem_open(“/rSem”,0644,O_CREAT | O_EXCL,1);
sem_unlink(“/rSem”);
互斥量=sem_open(“/mSem”,0644,O_CREAT | O_EXCL,1);
sem_unlink(“/mSem”);
pid_t pid=fork();
如果(pid==0)
写入器(1,缓冲器);
其他的
读卡器(3,缓冲区,读取计数);
sem_销毁(资源);
sem_销毁(互斥);
出口(0);
}/*main()*/
无效写入程序(int n,int*缓冲区)
{
多重分形(n);
睡眠(1);
而(1){
usleep(兰特()%3000);
sem_wait(资源);
//
printf(“写入程序(%d)‫‪获得‬‬ ‫‪这个‬‬ ‫‪锁定。\n‬‬", getpid());
(*缓冲区)+;
printf(“写入程序(%d)写入‫‪这个‬‬ ‫‪值%d。\n‬‬,getpid(),*缓冲区);
睡眠(1);
printf(“写入程序(%d)释放‫‪这个‬‬ ‫‪锁‬‬\n“,getpid());
//
行政主任职位(资源);
睡眠(1);
}
}
无效读取器(int n,int*缓冲区,int*读取计数){
多重分形(n);
而(1){
usleep(兰特()%3000);
sem_wait(互斥);
usleep(兰特()%3000);
如果((*readcount)==0)
{
sem_wait(资源);
printf(“第一个读取器(%d)获取锁。%d\n”,getpid(),*readcount);
(*读取计数)+;
}
如果(*readcount)>0,则为else
{
printf(“读取器(%d)获取锁。%d\n”,getpid(),*readcount);
(*读取计数)+;
}
sem_post(互斥);
//
printf(“读取器(%d)读取值%d.%d\n”,getpid(),*Buffer,*readcount);
睡眠(1);
//
sem_wait(互斥);
(*读取计数)--;
如果((*readcount)==0)
{
printf(“最后一个读取器(%d)释放锁。\n”,getpid());
行政主任职位(资源);
}
其他的
printf(“读取器(%d)释放锁。\n”,getpid();
sem_post(互斥);
睡眠(1);
}
}
无效的多重标记(整数n)
{
而(n-1>0){
pid_t pid=fork();
如果(pid==0){
返回;
}
其他的
n--;
}
返回;
}
我用过

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

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

父进程之前似乎已退出。请检查您的程序。