msg.h vs mqueue.h

Posted on 2004-11-09
Last Modified: 2008-01-09

What's the better api to use? (<-- a loaded question!)

Is there a difference between using mqueue or using msg calls? I need to write an application on AIX where one process is asked to do some work and then a second one would receive a message from the first to try and pre-empt what the first process will ask for next...

So basically, process one is asked by the user to get info on a client from a client database... The information is retrieved and sent to the user. At the same time, this process will send a message to another process to ask it to get more in-depth information about the same client and fill up a temporary database with all this information. When the user then asks the first process to get the in-depth information, the data will already be available and packaged accordingly.

So which messaging scheme seems better adapted to my specific problem?

-- Bubba
Question by:bganoush
    LVL 5

    Accepted Solution

    there are two points to consider:

    1) the advantage sysV message queues(msg calls) have over
         posix message queues(mqueue.h) is that  you can read the
          messages from a sysv msg queue in any order you want.
         This can be done by setting priority value for each message.
        posix message queues return the oldest message  on every read.

    2) the advantage posix message queues have is that  they allow
         generation of signals when a message a placed in an empty queue.

    How are you communicating the indepth info about a particullar client
    from process2 to process1 ? will you make  process2 write this extra info
    on a client in the message queue itself or will it write this info in a file?


    ...//some code
         for(;;) {
                    read(STDIN_FILENO, ... read client id you want to get info on
                    retreive the record for this client id.
                    place a message requesting  more info for this client id in a message queue
                   go on to process the record.
          //suppose you read info for a number of clients from the data base , now the user might want
         // the indepth info on say client id x.
             retreive the message with priority x from the message queue..

          .. //some code
                        read message from queue, find the client id.
                       retreive extra info for that client id
                       bundle up the info and mark it with priority = client id. put this info in the message queue

    as you can see from above, it will be easier for the process1 to select which client id it wants
    the extra info on, and selectively read it from the queue, IF you use sysV msg queues. this wont
    bepossible with mqueue.

    However if you are writing the extra info in a file, i think  both the queues will work
    equally fine.

    hope this helps,

    Author Comment


    Actually, I shouldn't have closed this...

    I was hoping to send the information via the queue in a message. Why isn't it possible for mqueue to do this?

    -- Bubba

    Author Comment


    The procedure would look like this:

    1) Process 1 sends the client ID to process 2
    2) Process 1 returns to the user, not expecting any data from process 2

    3) Process 2 retrieves the client ID from the queue
    4) Process 2 gets information from various databases and sets up a summary table in a local database.

    5) Process 1 sends the client ID to process 2 with a directive to return all data
    6) Process 2 picks up the data from the summary table and returns it to Process 1

    By what I got from your response, it looks like the problem is that step 6 is not possible with mqueue?  By the way, this communication is pretty synchornous... Process 1 will never ask for more than one client at a time in step 5, and will just sit there until process 2 returns the data in step 6.

    -- Bubba

    Author Comment


    One last thing....

    Even if the user has asked for info on 20 clients, only one "in depth" package will returned in the queue at any given time, the one the user is currently working with.

    -- Bubba
    LVL 5

    Expert Comment

            It seems like that the problem at hand is simpler
    than what i thought.

    >> I was hoping to send the information via the queue in a message. Why isn't it possible for mqueue to do this?
         Sorry if i implied that somehow. It is very much possible with mqueue to do so.
    The point i was making can be explained better with the following figure:

    case I:              (oldest message)  ..........................................................................(latest message)                      
    [msg-sysV]---->[msg1, p1]----->[msg2, p2]------>[msg3, p3]------>[msg4, p4]----->[msg5, p5]

    caseII:                 (oldest message)..........................................................................(latest message)

         In sysV msg queues, you have a priority value associated with each message(p1, p2 ..), while there is no priority
    value associated with mqueue messages.  to read a message from caseI, you can specify a priority value to
    the msgget() function and the relevant message will be obtained from the queue. however when u call mq_read()
    (I am not very sure if this is the name of the function to read message from posix queues), only the oldest message
    from the mqueue, in our case msg1, is returned(you dont have the control over how u read from the queue).

    >>By what I got from your response, it looks like the problem is that step 6 is not possible with mqueue?
        I would suggest  you to use 2 message queues.
    queue1: dedicated for communicating client ids from process1 to process2.
    queue2: dedicated for communicating extra client info from process2 to process1.

    so basically
    1) process1 writes client-id as a message in the queue1
    2) process2 is blocked reading on queue1. when a message is put, it returns with the message. this message will
        contain the client id. then process2 will do its bit with the databases and summary table retreiving all the relevant
        info on the clint-id.
    3)  process2 will bundle up all the info in a message and mark it with priority = client-id. then it will place this
         message in queue2.
    4)  process1 will read from queue2. knowing that it will contain information on various client-ids, proces1 will
          specify a priority = client-id, to retreive the extra info on a particullar client-id. (this is where sysV are better).

    This was the model i was suggesting earlier. However, your problem is pretty synchronous and i gather things
    happen like this

    user gives client-id--->process1 gets some data on client id, prints it to user, puts the client id in a queue --> process2
    retreives the message from queue-->gets all the info and stores it in summary table--> process2 blocks reading on the
    queue once again--->process1 writes a client id in the queue-> process2 checks if it has the info on the client id in
    the summary table, if it is there it bundles up all the info in a message and puts the message in the queue for process1
    to read.

    in that case one single message queue will suffice, and it can be just  either of the two , msg or mqueue.

    hope it helps,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    Video by: Grant
    The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now