Read from a Message Queue - Unix

This is a discussion on Read from a Message Queue - Unix ; 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: // ...

+ Reply to Thread
Results 1 to 2 of 2

Thread: Read from a Message Queue

  1. Read from a Message Queue

    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

  2. Re: Read from a Message Queue

    Ramon 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.

+ Reply to Thread