Reading from the message queue (no lock if empty)

I am writing to the 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 it? If I want nothing to execute the following statement in a loop

+4
source share
3 answers

Use IPC_NOWAIT . From the doc:

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

+4
source

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

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

+3
source

You can check if empty is empty using

 ipcs 

in linux terminal.

It will show you the queues that you created.

0
source

All Articles