Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shm_open() function is not creating the shared memory

Tags:

c

posix

I'm trying to open a shared memory, Its giving me No such file or directory error. But i have a file as well as a directory in the name region.

 fd_sh = shm_open("/home/angus/c_tutorials/interview/linux_sys_pgm/mmap/region", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
 if(fd_sh == -1){
  perror("fd_sh:ERROR");
  return -1;
 }
like image 369
Angus Avatar asked Mar 26 '13 18:03

Angus


1 Answers

In Linux (I assume that's your OS, given your code), the name should start with a slash but not have any others afterwards, e.g. "/myshm" -- not a regular filename.

From the man page:

   The operation of shm_open() is analogous  to  that  of  open(2).   name
   specifies the shared memory object to be created or opened.  For porta‐
   ble use, a shared memory object should be identified by a name  of  the
   form  /somename;  that  is,  a null-terminated string of up to NAME_MAX
   (i.e., 255) characters consisting of an initial slash, followed by  one
   or more characters, none of which are slashes.

Doing it this way will work fine.

What in fact happens is that the name given is created as a file in /dev/shm, so you would need to create a directory structure to use a path; this is not a good idea since this directory is only in memory.

like image 108
teppic Avatar answered Sep 28 '22 06:09

teppic