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, O_CREAT, 0777, 0);
sem_t *hsem = sem_open(hyp, O_CREAT, 0777, 0);
sem_t *hfsem = sem_open(hypFlag, O_CREAT, 0777, 0); 
printf("SEMS OPENED\n");

sem_open返回 0,perror写入 Success,信号量不打开。看过之后sem_overview,我发现问题可能出在名称开头没有斜线,添加没有帮助。没有对信号量的访问,当sem_post被称为具有分段错误的 shell 中止进程起飞时。帮助我了解问题所在。编辑:如果我重新启动系统(并清理信号量?),perror 返回“没有这样的文件或目录”,但在返回“成功”之后。

4

1 回答 1

0

这是我尝试过的并且有效:

#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, O_CREAT, 0777, 0);
sem_t *hsem = sem_open(hyp, O_CREAT, 0777, 0);
sem_t *hfsem = sem_open(hypFlag, O_CREAT, 0777, 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
于 2020-10-17T13:37:44.257 回答