• C

msg.h vs mqueue.h

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bganoushAuthor Commented:

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
bganoushAuthor Commented:

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
bganoushAuthor Commented:

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
        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,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.