无法使用 sem_open 创建信号量

Unable to create semaphore with sem_open

我正在尝试创建一个信号量,但我的代码没有通过第一次检查并打印:“sem_open/producer:没有这样的文件或目录”。

请注意 SEM_CONSUMER_FNAME 和 SEM_PRODUCER_FNAME 是在 shared_memory.h 中定义的。

有人可以帮我吗?

#include <stdio.h>
#include <stdlib.h>
#include <string.h> 
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include "map.h"
#include <errno.h>
#include "shared_memory.h"
#include <semaphore.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <fcntl.h> 
#include <sys/stat.h> 


int main(){     
        
    //Remove semaphores with same name;
    sem_unlink(SEM_CONSUMER_FNAME);
    sem_unlink(SEM_PRODUCER_FNAME);  
    
    //Setup some semaphores
    sem_t *sem_prod = sem_open(SEM_PRODUCER_FNAME, IPC_CREAT, 0660, 0);
    if (sem_prod == SEM_FAILED){
        perror("sem_open/producer");
        exit(EXIT_FAILURE);
    }   
    
    sem_t *sem_cons = sem_open(SEM_CONSUMER_FNAME, IPC_CREAT, 0660, 1);
    if (sem_cons == SEM_FAILED) {
        perror("sem_open/consumer");
        exit(EXIT_FAILURE);
    }

根据this man page

The oflag argument specifies flags that control the operation of the call. (Definitions of the flags values can be obtained by including <fcntl.h>.) If O_CREAT is specified in oflag, then the semaphore is created if it does not already exist.

也就是O_CREAT,不是IPC_CREAT