ftok(3)

โœจ ftok(3) โ€” the key generator bestie โœจ

okay so this is ftok from section 3 of the manual (yes, the library functions girlies), and she's here to turn a file path + a project id into a System V IPC key. iconic behaviour honestly.


๐Ÿ’– NAME ๐Ÿ’–

ftok โ€” convert a pathname and a project identifier to a System V IPC key

๐ŸŽ€ LIBRARY ๐ŸŽ€

Standard C library (libc, -lc)

๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <sys/ipc.h>

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

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so what does she actually do? ftok() takes the identity of the file named by path โ€” and btw that file has to actually exist and be accessible, no ghost files allowed โ€” and smooshes it together with the least significant 8 bits of proj_id (which must be nonzero, more on that drama in NOTES). out comes a key_t type System V IPC key, ready to be handed to msgget(2), semget(2), or shmget(2). slay.

the resulting value is the same for all pathnames that name the same file, as long as you're using the same value of proj_id. and the value returned should be different when the (simultaneously existing) files or the project IDs differ. like, that's the whole point bestie ๐Ÿ’…

๐Ÿ”ฅ RETURN VALUE ๐Ÿ”ฅ

  • on success: the generated key_t value is returned. ate.
  • on failure: -1 is returned, and errno tells you what went wrong, same as it does for the stat(2) system call. ๐Ÿ˜ญ

๐Ÿ’… ATTRIBUTES ๐Ÿ’…

for an explanation of the terms used in this section, see attributes(7).

  • Interface: ftok()
  • Attribute: Thread safety
  • Value: MT-Safe

so yeah, she's MT-Safe, thread-safe queen, no cap ๐Ÿ‘‘

โญ STANDARDS โญ

POSIX.1-2008.

๐ŸŒˆ HISTORY ๐ŸŒˆ

POSIX.1-2001. she's been around a minute, fr fr.

๐Ÿ‘‘ NOTES ๐Ÿ‘‘

okay bestie this is the part where we get into the lore. on some ancient systems, the prototype was:

key_t ftok(char *path, char proj_id);

today, proj_id is an int, but still only 8 bits are used. typical usage has an ASCII character as proj_id, which is why the behavior is said to be undefined when proj_id is zero. so likeโ€ฆ don't do that. ๐Ÿซถ

and of course, no guarantee can be given that the resulting key_t is unique. typically, a best-effort attempt combines the given proj_id byte, the lower 16 bits of the inode number, and the lower 8 bits of the device number into a 32-bit result. collisions may easily happen โ€” for example, between files on /dev/hda1 and files on /dev/sda1. it's giving โœจchaosโœจ ngl.

not me warning you about collisions twice: seriously, two different files can absolutely produce the same key. vibe check your keys before you commit, ok?

๐Ÿ“ EXAMPLES ๐Ÿ“

See semget(2).

๐Ÿ’œ SEE ALSO ๐Ÿ’œ

msgget(2), semget(2), shmget(2), stat(2), sysvipc(7)


okk that's the whole page, mwah ๐Ÿ’‹ this one came from Linux man-pages 6.20, dated 2026-02-08, and it's your girl ftok(3) signing off โ€” go make some IPC keys, but responsibly ๐ŸŽ€โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms