SEM_FAILED GCC Red Hat 4.8.5-39 - 信号量
SEM_FAILED on GCC Red Hat 4.8.5-39 - Semaphores
这是我尝试编写的简单共享内存程序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/mman.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <semaphore.h>
#include <fcntl.h>
#define SHM_NAME "/my_shm"
#define SEM_1 "/sem_1"
#define SEM_2 "/sem_2"
#define SEM_3 "/sem_3"
#define BUFFER_SIZE 10
typedef struct {
int pred[8];
int succ[8];
} fb_set;
struct sharedMemory{
int rPos;
int wPos;
fb_set storage[BUFFER_SIZE];
int done;
};
static struct sharedMemory *shared;
int main(int argc, char *argv[])
{
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, 0600);
if (shmfd == -1){
// exit
}
if (ftruncate(shmfd, sizeof(struct sharedMemory)) == -1){
// exit
}
shared = mmap(NULL, sizeof(*shared), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if (shared == MAP_FAILED){
// close resources
}
sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);
sem_t *sem_write = sem_open(SEM_2, O_CREAT | O_EXCL, 0600, BUFFER_SIZE);
sem_t *sem_mutex = sem_open(SEM_3, O_CREAT | O_EXCL, 0600, 1);
if (sem_read == SEM_FAILED){
// close resources
}
if (sem_write == SEM_FAILED){
// close resources
}
if(sem_mutex == SEM_FAILED){
// close resources
}
}
所有信号量都正确打开并且程序编译为:
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
但是当我试穿时:
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
我在 sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);
上得到 SEM_FAILED
并且 errno
设置为 Permission denied
。
这是我的 Makefile:
CC = gcc
DEFS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_SOURCE=200809L
CFLAGS = -std=c99 -pedantic -Wall -g $(DEFS)
supervisor: supervisor.o
$(CC) $(CFLAGS) -o $@ $^ -lrt -pthread
supervisor.o : supervisor.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -rf *.o supervisor
有人可以解释一下我做错了什么吗?
errno
is set to Permission denied
.
来自 shm_open()
documentation:
[EACCES]
The shared memory object exists and the permissions specified by oflag are denied, or the shared memory object does not exist and permission to create the shared memory object is denied, or O_TRUNC is specified and write permission is denied.
这是我尝试编写的简单共享内存程序:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/mman.h>
#include <getopt.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <semaphore.h>
#include <fcntl.h>
#define SHM_NAME "/my_shm"
#define SEM_1 "/sem_1"
#define SEM_2 "/sem_2"
#define SEM_3 "/sem_3"
#define BUFFER_SIZE 10
typedef struct {
int pred[8];
int succ[8];
} fb_set;
struct sharedMemory{
int rPos;
int wPos;
fb_set storage[BUFFER_SIZE];
int done;
};
static struct sharedMemory *shared;
int main(int argc, char *argv[])
{
int shmfd = shm_open(SHM_NAME, O_RDWR | O_CREAT, 0600);
if (shmfd == -1){
// exit
}
if (ftruncate(shmfd, sizeof(struct sharedMemory)) == -1){
// exit
}
shared = mmap(NULL, sizeof(*shared), PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0);
if (shared == MAP_FAILED){
// close resources
}
sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);
sem_t *sem_write = sem_open(SEM_2, O_CREAT | O_EXCL, 0600, BUFFER_SIZE);
sem_t *sem_mutex = sem_open(SEM_3, O_CREAT | O_EXCL, 0600, 1);
if (sem_read == SEM_FAILED){
// close resources
}
if (sem_write == SEM_FAILED){
// close resources
}
if(sem_mutex == SEM_FAILED){
// close resources
}
}
所有信号量都正确打开并且程序编译为:
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
但是当我试穿时:
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39)
我在 sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);
上得到 SEM_FAILED
并且 errno
设置为 Permission denied
。
这是我的 Makefile:
CC = gcc
DEFS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_SOURCE=200809L
CFLAGS = -std=c99 -pedantic -Wall -g $(DEFS)
supervisor: supervisor.o
$(CC) $(CFLAGS) -o $@ $^ -lrt -pthread
supervisor.o : supervisor.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -rf *.o supervisor
有人可以解释一下我做错了什么吗?
errno
is set toPermission denied
.
来自 shm_open()
documentation:
[EACCES] The shared memory object exists and the permissions specified by oflag are denied, or the shared memory object does not exist and permission to create the shared memory object is denied, or O_TRUNC is specified and write permission is denied.