[comp.unix.questions] System V message queues?

robert@PVAB.SE (Robert Claeson) (02/21/89)

I want to create a message queue that will serve as the communications
channel for two co-operating processes. The type field of the message
will be used to indicate what process the message is destined to.
One process will typically fork() and exec() the second process. And I
decided to use message queues (rather than pipes) since the messages
are delimited (with pipes, I only get a raw data stream, no matter if
the data was written byte-for-byte or in some other way).

Now, the first process creates a message queue with a parameter of
IPC_PRIVATE to msgget() before it starts the second process. But how
do I pass the queue id to the new process? I could probably use sema-
phores or shared memory for this, but how do I then pass the semaphore
id to the new process? How are the System V IPC facilities intended to
be used, by the way?

Enlighten me, please.
-- 
Robert Claeson, ERBE DATA AB, P.O. Box 77, S-175 22 Jarfalla, Sweden

Tel:   +46 758 202 50              EUnet:    rclaeson@ERBE.SE
Fax:   +46 758 197 20              Internet: rclaeson@ERBE.SE

bes@holin.ATT.COM (Bradley Smith) (02/22/89)

> Now, the first process creates a message queue with a parameter of
> IPC_PRIVATE to msgget() before it starts the second process. But how
> do I pass the queue id to the new process? I could probably use sema-
> phores or shared memory for this, but how do I then pass the semaphore
> id to the new process? How are the System V IPC facilities intended to
> be used, by the way?
I am not sure you want to use PRIVATE, but what you could do is
to use the same key. ie before the fork use getpid and save it,
after the fork then use that number as the key.  If you have 2 different
programgs, then send the number on the command line.
-- 
Bradley Smith
Computer Systems Offer Integration Laboratory
AT&T Bell Labs, Holmdel, NJ 
201-949-0090 att!holin!bes or bes@holin.ATT.COM

crossgl@ingr.com (Gordon Cross) (02/23/89)

In article <561@pvab.PVAB.SE> robert@PVAB.SE (Robert Claeson) writes:
>
>Now, the first process creates a message queue with a parameter of
>IPC_PRIVATE to msgget() before it starts the second process. But how
>do I pass the queue id to the new process?

IPC_PRIVATE causes the creation of a message queue that is meant to be used
exclusively by the process that created it.  The correct way to use message
queues for interprocess communication is to create it using a value for
'key' that is known (or can be determined) by the other processes that wish
to use the queue.  There is a function 'ftok' available that can be used to
generate such a key.  It requires the path name of an existing file and an
unique id (the manual suggests that this id should uniquely identify a
project).  My personal opinion of 'ftok' is UGHHHH!  I usually just pick
some random number and use that as the key then we register this number so
no other project will use the same one.  Another method I've seen used before
is to use the PID of the master (controlling) process as the key but this
method presents problems in situations where (a) there isn't any real "master"
process or its PID is not known to all and (b) PIDs roll-over often.  Hope
this helps!!
-- 

Gordon Cross             UUCP:      uunet!ingr!crossgl     "all opinions are
111 Westminister Way     INTERNET:  crossgl@ingr.com        mine and not those
Madison, AL 35758        MA BELL:   (205) 772-7842          of my employer."

blum@drutx.ATT.COM (Mark Blumhardt) (02/27/89)

In article <561@pvab.PVAB.SE>, robert@PVAB.SE (Robert Claeson) writes:
> But how
> do I pass the queue id to the new process? I could probably use sema-
> phores or shared memory for this, but how do I then pass the semaphore
> id to the new process? 

Since my applications usually run on dedicated machines, I can choose
the keys that I use.  I just use a #define in a global header file.
Each process can use that key and access that message queue.

Mark Blumhardt

lars@myab.se (Lars Pensj|) (03/02/89)

In article <4043@ingr.com> crossgl@ingr.UUCP (Gordon Cross) writes:
>The correct way to use message
>queues for interprocess communication is to create it using a value for
>'key' that is known (or can be determined) by the other processes that wish
>to use the queue.  There is a function 'ftok' available that can be used to
>generate such a key.  It requires the path name of an existing file and an
>unique id (the manual suggests that this id should uniquely identify a
>project).  My personal opinion of 'ftok' is UGHHHH!  I usually just pick
>some random number and use that as the key then we register this number so
>no other project will use the same one.



My opinion of ftok() is that it should have been mandatory. If you use
ftok() on a local file, you will get a unique key. If you want more than
one unique key, use an id value of 0, 1 and so on for the same file.

Never use a random number, it can collide with other programs !
-- 
    Lars Pensj|
    lars@myab.se