0

I have just started using shared memory. I cannot attach file which I created using shm_open() (shmat()) returns permission denied.Later I will attatch the shm in another process which I will create using fork exec, but I cannot get shmat() to work. It works if I use sudo to execute the program. How can I set the permissions for sharedMemory file. I tried creating the memory in a local folder but shm_open() does not allow it.

int shm_fd;

shm_fd = shm_open("mySharedMemory", O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG);

if(shm_fd == -1)
{
    perror("p1");
    exit(1);
}

if( truncate("/run/shm/mySharedMemory", myFileSize) == -1 )
{
    perror("p1");
    exit(1);
}   

close(shm_fd);

key_t key;
int shmid;

key = ftok("/run/shm/mySharedMemory", 'A');
printf("\n%d\n",key);
shmid = shmget(key, 80, S_IWUSR | IPC_CREAT);

//char *ptr = shmat(shmid,NULL,0);
perror("p1");

exit(1);
alk
  • 69,737
  • 10
  • 105
  • 255

0 Answers0