打开信号量返回0

问题描述

char* cath="/cath";
char* cathFlag="/cathf";
char* hyp="/hyp";
char* hypFlag="/hypf";

printf("SEMS OPEN\n");
sem_t *csem = sem_open(cath,O_CREAT,0777,0);
printf("CSEM: %d\n",*csem);
perror("ERROR: ");
sem_t *cfsem = sem_open(cathFlag,0);
sem_t *hsem = sem_open(hyp,0);
sem_t *hfsem = sem_open(hypFlag,0); 
printf("SEMS OPENED\n");

sem_open返回0,perror写入成功,信号灯没有打开。浏览sem_overview后,我发现问题可能出在名称开头,但没有出现斜线,添加没有帮助。当sem_post被称为shell中止进程且出现分段错误时,无法访问该信号量。帮助我了解问题所在。 编辑:如果我重新启动系统(并清除信号量?),perror将返回“ No such file or directory”,但返回“ Success”之后。

解决方法

这是我尝试过的方法,它有效:

#include <fcntl.h>           /* For O_* constants */
#include <sys/stat.h>        /* For mode constants */
#include <semaphore.h>
#include <stdio.h>


int main(void)
{

char* cath="/cath";
char* cathFlag="/cathf";
char* hyp="/hyp";
char* hypFlag="/hypf";

printf("SEMS OPEN\n");
sem_t *csem = sem_open(cath,O_CREAT,0777,0);
//perror("ERROR: ");
sem_t *cfsem = sem_open(cathFlag,0);
sem_t *hsem = sem_open(hyp,0);
sem_t *hfsem = sem_open(hypFlag,0); 
printf("SEMS OPENED\n");

}

我编译了它:

$ gcc tsem.c -l pthread

我运行它:

$ ./a.out
SEMS OPEN
SEMS OPENED

我看着/ dev / shm:

$ ls /dev/shm
sem.cath  sem.cathf  sem.hyp  sem.hypf