I have seen that question on SO already but it wasn't clear to me the following case:
A shm has been created. So if I call in my case:
int shmid = shmget(key, sizeof(struct messageQueue), IPC_CREAT | S_IRWXU | IPC_EXCL);
shmid turns -1 if the shm already exists. But can i somewhere get it's ID? Or do I need to call shmget(...) without EXCL flag again in order to get the ID?
Thanks in advance
Normally, IPC_CREAT | IPC_EXCL
is used if you want to create and initialize a new memory block. E.g.:
int shmid = shmget(key, sizeof(struct messageQueue), IPC_CREAT | S_IRWXU | IPC_EXCL);
if( shmid != -1 )
{
/* initialization code */
}
/* if it already exists, open it: */
if( shmid == -1 && errno == EEXIST )
shmid = shmget(key, sizeof(struct messageQueue), S_IRWXU );
if( shmid == -1 ) {
perror("shmget");
}
If you don't need to initialize it, you can skip the IPC_EXCL
:
int shmid = shmget(key, sizeof(struct messageQueue), IPC_CREAT | S_IRWXU );
and if you don't need to create it, you can skip the IPC_CREAT
:
int shmid = shmget(key, sizeof(struct messageQueue), S_IRWXU );