For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2008 > Read from a Message Queue









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Read from a Message Queue
Ramon

2008-03-16, 5:30 am

Hi,

I'm currently writing a program that tries to read (from a message
queue) either a message of type 123 or a one of type 111.

I've tried the following code but it seems that it is not working:


// reads from the message queue
if ( msgrcv(qid, &mesg, sizeof(mesg), 123 || 111, 1) < 0 )
/** error occurs and halt **/

if (mesg.mtype == 123) {
....
}

else {
....
}


Is there a way to write a code that can read either a message from the
message queue of type A or a one of type B /without/ busy waiting??

Thankx
Ben Bacarisse

2008-03-16, 7:33 pm

Ramon <ramif_47@yahoo.co.uk> writes:

> I'm currently writing a program that tries to read (from a message
> queue) either a message of type 123 or a one of type 111.
>
> I've tried the following code but it seems that it is not working:
>
>
> // reads from the message queue
> if ( msgrcv(qid, &mesg, sizeof(mesg), 123 || 111, 1) < 0 )


That is not how the fourth parameter of msgrcv works. Also, the final
1 is rather bad style (and is probably wrong). You probably want
IPC_NOWAIT.

> Is there a way to write a code that can read either a message from the
> message queue of type A or a one of type B /without/ busy waiting??


Not in general. You need to read "man msgrcv". If A < B and there
are no other types < B then passing -B as the fourth parameter will do
what you want, but a general read either A or B is not possible.

Nit: Unix calls don't, in general, busy wait. You mean without
blocking or waiting.

--
Ben.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com