4

I'm writing to message queue

if (msgsnd(q, &msg, sizeof(message), slaves_list[to]) == -1)

and reading

if (msgrcv(q, &msg, sizeof(message), id, 0) == -1)

but what if this queue is empty? How to check that? If there is nothing I want execute next instruction in the loop

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
OnTheFly
  • 2,059
  • 5
  • 26
  • 61

3 Answers3

4

Use IPC_NOWAIT. From the documentation:

If (msgflg & IPC_NOWAIT) is non-zero, the calling thread will return immediately with a return value of -1 and errno set to [ENOMSG].

NPE
  • 486,780
  • 108
  • 951
  • 1,012
4

Use IPC_NOWAIT for msgflg parameter: http://man7.org/linux/man-pages/man2/msgsnd.2.html

IPC_NOWAIT Return immediately if no message of the requested type is in the queue. The system call fails with errno set to ENOMSG.

Alex F
  • 42,307
  • 41
  • 144
  • 212
0

You can check if queue is empty using the

ipcs

command in linux terminal.

It will show you the queues that you have created.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30
paras
  • 1
  • Thanks for the feedback, but please note that the OP is asking about `C` programming, so that shelling out to an external utility is likely not an option. – Felix Frank Aug 13 '14 at 11:45