Pass the flag IPC_EXCLto msgget(), and if it doesn't work as errnowell EEXIST, then the queue exists.
int msgid = -1;
key_t key = -1;
if (-1 == (key = ftok(".", 'a')))
{
perror("ftok() failed");
}
else if (-1 == (msgid = msgget(key, IPC_EXCL | IPC_CREAT | 0666)))
{
if (EEXIST == errno)
{
fprintf(stderr, "The queue exists.\n");
}
else
{
perror("msgget() failed");
}
}
source
share