Pass the flag IPC_EXCL
to msgget()
and if it fails and errno
equals 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");
}
}