I'd assume that you want to exit the loop based on a message that indicates to do so, e.g.
Main Topics
Browse All TopicsHi, I have a message queue from which I am getting messages in a loop. The problem is that I don't know how to exit the loop. msgrcv returns type size_t so it keeps going. What value should I compare the return value so I can exit the loop?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
msgrcv returns 20 always. so the size must be 20. so i i changed your code to (ssize_t)20 it still doesn't work bc even after all messages have been retrieved from the queue the value is still 20. is there a way to figure out how many messages are in the queue. i can't do it when i add them because it is children processes that add messages so the parent doesn't read that value. unless i make a shared memory segment, but that is more work than i need to
Business Accounts
Answer for Membership
by: evilrixPosted on 2009-11-05 at 11:10:41ID: 25752792
According to the docs...
nlinepubs/ 007908799/ xsh/ msgrcv .html
"Upon successful completion, msgrcv() returns a value equal to the number of bytes actually placed into the buffer mtext. Otherwise, no message will be received, msgrcv() will return (ssize_t)-1 and errno will be set to indicate the error."
http://www.opengroup.org/o
So, according to that you should test for (ssize_t)-1
hile(msgrcv(msqid, &msgreceived, sizeof(msgreceived), BUFFER_CHANGED, 0) != (ssize_t)-1)