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
-
I think you have to call it again. This sounds like an XY problem, what is the overall goal?Barmar– Barmar2021年01月18日 18:12:33 +00:00Commented Jan 18, 2021 at 18:12
-
2When it returns error with EXCL and CREAT, just call it again without EXCL and CREAT to get an id.RKou– RKou2021年01月18日 18:13:13 +00:00Commented Jan 18, 2021 at 18:13
1 Answer 1
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 );
Comments
Explore related questions
See similar questions with these tags.