简体   繁体   English

GCC Red Hat 4.8.5-39 上的 SEM_FAILED - 信号量

[英]SEM_FAILED on GCC Red Hat 4.8.5-39 - Semaphores

This is simple shared memory program that I tried writing:这是我尝试编写的简单共享内存程序:

#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
    }
}

All semaphores are open correctly and the program compiles with:所有信号量都正确打开并且程序编译为:

gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0

But when I try it on:但是当我试穿时:

gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39) gcc (GCC) 4.8.5 20150623(红帽 4.8.5-39)

I get SEM_FAILED on sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);我在sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0);上得到SEM_FAILED sem_t *sem_read = sem_open(SEM_1, O_CREAT | O_EXCL, 0600, 0); and errno is set to Permission denied .并且errno设置为Permission denied

Here is my Makefile:这是我的 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

Can somebody explain me what I did wrong?有人可以解释我做错了什么吗?

errno is set to Permission denied . errno设置为Permission denied

From shm_open() documentation :shm_open() 文档

[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. [EACCES] 共享内存对象存在并且oflag指定的权限被拒绝,或者共享内存对象不存在并且创建共享内存对象的权限被拒绝,或者指定了O_TRUNC并且写权限被拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM