key_t
key is a 32-bit identifier associated with an #Interprocess Communication (IPC) object on a #unix system. It can be obtained from the function ftok()
imported from sys/types.h
and sys/ipc.h
that maps a file’s pathname and an 8-bit integer into a key. The creation of key is shown as below:
// pathname
key_t key = ftok("/tmp", 'S');
// 8-bit integer
If the flag passed to the IPC object System Call (#Message Queue, semaphore, and share memory) are IPC_CREAT
and IPC_EXCL
(exclusive), there is an additional need to check against the error number EEXIST
which indicates that the IPC object has already existed.
If duplication is undesired, we could use IPC_PRIVATE
as the IPC key instead so that it guarantees to create a unique IPC object.