How do I know my Q-KEY is unique ?

Gil Pilz@Eng@Banyan gil at banyan.UUCP
Wed Sep 27 08:08:14 AEST 1989


In article <1747 at draken.nada.kth.se> d88-jwa at nada.kth.se (Jon W{tte) writes:
>I am currently considering using message queues for a game I'm writing.
>The problem is: when I create a message queue, I pass a (hopefully)
>unique 32-bit key. How do I make SURE this key is unique ? (I need to
>compile the key into a server as well as a client...)

The usual method for "solving" this is to use the ftok() library call
(documented in stdipc(3C)).  It takes a pathname and an "id" and spits
out a "unique" key for that pair.  Your client and server then just
have to agree upon an existing, readable file to work off of and a
common "id" (0 usually) and they'll be using the same key.

>Is there anywhere where you register your 32-bit key to ensure that it 
>is unique and noone else uses it ? Or do I use one (presently I'm using 
>8273460...) and hope noone else uses the same ?

As stdipc(3C) documents, there is no way to "secure" a key value such
that another process can't use it. However when you do use ftok() you
end up with these large key values that other, innocent programs are
unlikely to stumble upon (like the one you picked). Furthermore, if
everyone on your system wore white hats and always used ftok() ;-) you
could be sure that nobody would ever step on anybody elses IPC object
unless they had access to and picked the same file which brings the
whole thing back to the realm of UNIX file names which is where the
IPC objects should have been placed in the FIRST place but there you
are . . .

Also, since they're often overlooked, I'd like to point out the
existence of the ipcrm(1) and ipcs(1) utilities. They come in handy
when you're trying to debug programs using the IPC stuff.

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-
        Gilbert W. Pilz Jr.       gil at banyan.com
        Banyan Systems Inc.       (617) 898-1196
        115 Flanders Road
        Westboro, MA 01581
-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-



More information about the Comp.unix.wizards mailing list