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_tvalue is returned. ate. - on failure:
-1is returned, anderrnotells you what went wrong, same as it does for thestat(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
- Copyright 1993, Giorgio Ciucci <giorgio@crcc.it>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms