0

I got the following piece of C code which executes successfully:

...
fd = shm_open(memory_package_name, O_CREAT | O_RDWR | O_EXCL , S_IRUSR | S_IWUSR);

if (fd == -1) { //will fail if file already exists because of flag O_EXCL
    printf("\n shm_open() failed with error [%s]\n",strerror(errno));
    exit(EXIT_FAILURE);
} else {
    printf("shm_open success\n");
}
...

Using ls -l /dev/shm I see the file with name memory_package_name correctly created, but when I use ipcs it doesn't show any shared memory segment(s):

------ Shared Memory Segments --------
key        shmid      owner      perms      bytes      nattch     status      

------ Semaphore Arrays --------
key        semid      owner      perms      nsems     

------ Message Queues --------
key        msqid      owner      perms      used-bytes   messages

Why?

Marvin Effing
  • 2,693
  • 3
  • 20
  • 35

1 Answers1

0

Have you tried with shmget() and shmat()?? Anyway if you need to share the memory between two unrelated processes, you will need shm_open() indeed, but I think the missing point in your case is the call to attach to that memory region with shmat().

Fernando
  • 71
  • 5
  • It was my understanding that `shmget()` and `shmat()` were deprecated. The goal of my program is that no two same processes should be allowed at the same time due to licensing. I.e. process #1 executes, uses `shm_open()`, importantly with the oflag `O_EXCL`. Then if the another instance of the same program executes as process #2 it should fail, because #1 is already running. As a side note, I'm looking to make the shm non-persistent which I need to call `ipcs` to remove the shm after program execution, because otherwise it will fail every next call. – Marvin Effing Aug 09 '18 at 10:47
  • I didn't know that they are deprecated, do you have some references, please?? Assuming that they are not, I *think* that you will see your shared memory segment in `ipcs -m` after you execute `shmat()`. – Fernando Aug 09 '18 at 10:54
  • My sources: [here](https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c) (ctrl + f deprecrated) and [here](https://stackoverflow.com/questions/34471969/can-not-increase-size-of-shared-memory) (also ctrl+f deprecated). I will try to use `shmat` as you suggested and will come back to you. – Marvin Effing Aug 09 '18 at 11:43
  • I added the following lines to the code (after `shm_open()`): ```` int *shm_ptr; shm_ptr = shmat(fd, NULL, 0); if ((int) shm_ptr == -1) { printf("shmat() failed with error[%s]\n", strerror(errno)); exit(EXIT_FAILURE); } ```` The result is output: ````shmat() failed with error[Invalid argument]````` Btw, am i correct in assuming that my `fd` here is the returned shmid from `shm_open()`? – Marvin Effing Aug 09 '18 at 12:03
  • Yes, I am trying as well and it's not working. Seems like I confused some concepts here, `shm_open()` and `shmget()`. Sorry mate. I am going to try with `mmap()`, using the file descriptor `fd`, let me a moment. – Fernando Aug 09 '18 at 12:06
  • Ok, so with mmap you can map the memory segment, but it doesn't appear in `ipcs`. You will have to think about some mechanism to `shm_unlink()` the memory segment before the program that holds it dies – Fernando Aug 09 '18 at 12:11
  • I planed to use `shmctl` with flag `IPC_RMID` to clean up after program execution that holds the process, but that also gives me back an `Invalid argument` error message: `shmctl(fd, IPC_RMID, (struct shmid_ds *) NULL)`. `mmap` I already tried as examplified (here)[http://pubs.opengroup.org/onlinepubs/009604599/functions/shm_open.html] – Marvin Effing Aug 09 '18 at 12:17
  • I think you will need to call to `shm_unlink()` from the process that issued the `shm_open()` successfully. As you are limiting the processes to hold it exclusively (O_EXCL) just the process holding the file can do it. Another "hack" (not recommended but may work) is directly deleting the file from `/dev/shm` ... but you have to be sure that the other process died; anyway this is not elegant at all. – Fernando Aug 09 '18 at 12:20
  • Yes, that will be a possible solution. However I'm looking for a non-persistent solution, as in the shm will remove itself after execution. This is because the program I'm working on is used as a plugin for another software suite where I don't know at what point the process is finished with no clear point-of-exit. Therefore simply `shm_unlink()`-ing is not valid. Which is why I was looking into `shmctl()` with that specific flag. – Marvin Effing Aug 09 '18 at 12:43
  • Entries under ipcs will be generated only for shmget which invokes/implements systemV shared memories. shm_open is posix way of doing it. – saai63 Aug 29 '20 at 17:48