我正在嘗試創建一個信號量,但我的代碼沒有通過第一次檢查并列印:“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);
}
uj5u.com熱心網友回復:
根據這個手冊頁,
該
oflag
引數指定控制呼叫操作的標志。(標志值的定義可以通過包含來獲得<fcntl.h>
。)如果O_CREAT
在 中指定oflag
,則如果信號量不存在,則創建信號量。
也就是說O_CREAT
,不是IPC_CREAT
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464194.html
上一篇:如何制作使用openmp并使用intel的C編譯器進行編譯的可移植二進制檔案?
下一篇:在C中使用PCWSTR