Shared Memory: Permission denied

I am currently working on IPC for one of the projects. In particular, working on shared memory. Over a past couple of days, I was facing problems with accessing the shared memory as a local user (not root). Though, the shared memory is created(shmget) by the local user, I was not able to attach to the shared memory (using shmat).

After some googling and from strace logs I realised the problem is due to EACCS (permisssion denied). Indeed, it was permission denied. While creating the shared memory, we normally give IPC_CREAT|IPC_EXCL. Now, if you are launching the process as the local user, you also have to give permssion to the user, group, and other.i.e IPC_CREAT|IPC_EXCL|0600. i.e

0600 -> permission only to user

0660 -> permission to user, group

0666 -> permission to user, group, others


Here's the call to shmget and shmat.

void* addr;
shmid = shmget (key, size, IPC_CREAT|IPC_EXCL|0666);
addr=shmat(shmid,NULL,0);

More information is available in the below URL:
www.expressnewsindia.com/site/indore/IPCShared%20Memory.htm