next up previous contents
Next: The ipcs Command Up: 6.4.1 Fundamental Concepts Previous: IPC Identifiers

IPC Keys

To obtain a unique ID, a key must be used. The key must be mutually agreed upon by both client and server processes. This represents the first step in constructing a client/server framework for an application.

When you use a telephone to call someone, you must know their number. In addition, the phone company must know how to relay your outgoing call to its final destination. Once the other party responds by answering the telephone call, the connection is made.

In the case of System V IPC facilities, the ``telephone'' correllates directly with the type of object being used. The ``phone company'', or routing method, can be directly associated with an IPC key.

The key can be the same value every time, by hardcoding a key value into an application. This has the disadvantage of the key possibly being in use already. Often, the ftok() function is used to generate key values for both the client and the server.


  LIBRARY FUNCTION: ftok();

  PROTOTYPE: key_t ftok ( char *pathname, char proj );
    RETURNS: new IPC key value if successful
             -1 if unsuccessful, errno set to return of stat() call

The returned key value from ftok() is generated by combining the inode number and minor device number from the file in argument one, with the one character project indentifier in the second argument. This doesn't guarantee uniqueness, but an application can check for collisions and retry the key generation.


        key_t   mykey;
        mykey = ftok("/tmp/myapp", 'a');

In the above snippet, the directory /tmp/myapp is combined with the one letter identifier of 'a'. Another common example is to use the current directory:


        key_t   mykey;
        mykey = ftok(".", 'a');

The key generation algorithm used is completely up to the discretion of the application programmer. As long as measures are in place to prevent race conditions, deadlocks, etc, any method is viable. For our demonstration purposes, we will use the ftok() approach. If we assume that each client process will be running from a unique ``home'' directory, the keys generated should suffice for our needs.

The key value, however it is obtained, is used in subsequent IPC system calls to create or gain access to IPC objects.


next up previous contents
Next: The ipcs Command Up: 6.4.1 Fundamental Concepts Previous: IPC Identifiers

Converted on:
Fri Mar 29 14:43:04 EST 1996

mirror server hosted at Truenetwork, Russian Federation.