I'm trying to create a semaphore, but my code doesn't pass the first check and prints: "sem_open/producer: No such file or directory".
Note that SEM_CONSUMER_FNAME and SEM_PRODUCER_FNAME were defined in shared_memory.h.
Can someone lend me a hand, please?
#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);
}
CodePudding user response:
According to 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>
.) IfO_CREAT
is specified inoflag
, then the semaphore is created if it does not already exist.
That is O_CREAT
, not IPC_CREAT
.