ftok() — Generate an interprocess communication (IPC) key


Standards / Extensions C or C++ Dependencies
Single UNIX Specification, Version 3


#include <sys/ipc.h>

key_t ftok(const char *path, int id);

General description

The ftok() function returns a key based on path and id that is usable in subsequent calls to msgget(), semget(), and shmget(). The path argument must be the path name of an existing file that the process is able to stat().

The ftok() function returns the same key value for all paths that name the same file, when called with the same id value. If a different id value is given, or a different file is given, a different key is returned. Only the low-order 8-bits of id are significant, and must be nonzero.

Note: The ftok() function does not guarantee unique key generation. The key is created by combining the given id byte, the lower 16 bits of the inode number, and the lower 8 bits of the device number into a 32-bit result. The occurrence of key duplication is very rare.

Returned value

If successful, ftok() returns a key.

If unsuccessful, ftok() returns -1 and sets errno to one of the following values:
Error Code
Search permission is denied for a component of the path prefix.
The low-order 8-bits of id are zero.
Too many symbolic links were encountered in resolving path.
One of the following error conditions exists:
  • The length of the path argument exceeds PATH_MAX or a path name component is longer than NAME_MAX.
  • The path name resolution of a symbolic link produced an intermediate result whose length exceeds PATH_MAX.
A component of path does not name an existing file or path is an empty string.
A component of the path prefix is not a directory.

Related information