0

I'm trying to share some memory from a producer process to a consumer process in linux (Ubuntu16).

There's just a common header which defines this small structure

struct my_small_pod_structure {
    void *handle;
    int width;
    int height;
};

The producer process is doing:

// Create a semaphore to synchronize
psem = sem_open ("producerSem", O_CREAT | O_EXCL, 0644, 0);
sem_unlink ("producerSem"); // Avoid persisting after the process closed

shmkey = ftok("/dev/null", 5);

int shmid = shmget(shmkey, sizeof(my_small_pod_structure), 0644 | IPC_CREAT);
if (shmid < 0) {
    error("shmget failed");
    exit(1);
}


my_small_pod_structure *shared_ptr = (my_small_pod_structure*) shmat (shmid, NULL, 0);
// populate shared_ptr with the producer data here..
...

sem_post (psem); // data is ready

sleep(1000000); // very long value, assured that the producer isn't exiting before the consumer pulls it

While the consumer:

sem_t *psem = sem_open ("producerSem", O_CREAT | O_EXCL, 0644, 0);
sem_unlink ("producerSem");

key_t shmkey = ftok("/dev/null", 5);
int shmid = shmget(shmkey, sizeof(my_small_pod_structure), 0644 | IPC_CREAT);
if (shmid < 0) {
    int error = errno;
    printf("shmget failed %d\n", error);
    exit(1);
}

sem_wait (psem); // wait for data to be ready

// DATA IS READY!

my_small_pod_structure *shared_ptr = (my_small_pod_structure*) shmat (shmid, NULL, 0);

// use shared_ptr..
...

but even though the producer starts BEFORE the consumer (although I suppose it shouldn't matter because of the semaphores), I'm just getting from the consumer

shmget failed 13

The producer seems to work and reach the sleeping point waiting for the consumer to pick it up.

What am I doing wrong? Why the 13 - permission denied?

Dean
  • 6,610
  • 6
  • 40
  • 90
  • 1
    Check the value of `errno` after `shmget` fails. Should give you some clue. – G.M. Jul 11 '18 at 10:59
  • @G.M. edited the question – Dean Jul 11 '18 at 12:26
  • Did you check that `ftok` did not return `-1`? Did you try with other `ftok` parameters? Are producer and consumer launched by the same user? – Mathieu Jul 11 '18 at 12:49
  • 1
    Possible duplicate of [shmat returns segmentation falut with errno=13(EACCES)](https://stackoverflow.com/questions/19463957/shmat-returns-segmentation-falut-with-errno-13eacces) – Mathieu Jul 11 '18 at 12:51
  • 1
    I tried to replicate your programs on a Fedora Linux workstation. The shmget does not return an error, but your semaphore calls appear to be wrong. If you call `sem_unlink` on the producer before the consumer runs, the consumer won't be able to open the same semaphore by name. If you call `sem_open` with `O_EXCL`, the second program to run will fail to open the existing semaphore. – FBergo Jul 11 '18 at 12:52
  • @purplepsycho no they are not, I wonder if 0666 is the culprit here.. – Dean Jul 11 '18 at 12:54
  • @FBergo You're right for O_EXCL, but why calling sem_unlink would prevent the consumer from accessing the semaphore? I thought its purpose was to just destroy the semaphore as soon as all the processes close it – Dean Jul 11 '18 at 12:55
  • @Dean according to the sem_unlink man page, the semaphore name is removed immediately, so any calls trying to open that semaphore by name after that will either fail or create a distinct semaphore with same name (while the old one is still open somewhere, but without a name). – FBergo Jul 11 '18 at 12:57
  • @FBergo ugh you're right. If you post that as an answer I'll upvote it since it fixed another bug. It still doesn't explain the shmget though but I suppose those permissions might be the culprit, I'll test it out – Dean Jul 11 '18 at 12:58
  • Your shmget permission issue may be related to the selinux configuration (selinux can restrict access to the shm subsystem): https://stackoverflow.com/questions/7572974/configuring-selinux-permissions-on-svs-v-ipc-semaphores – FBergo Jul 11 '18 at 13:07

0 Answers0