I was reading Linux’s keys.txt and realised that this provided a place to store process or even thread local data that didn’t depend on libc.

Detecting if the current process has been forked is important for PRNGs (Psuedo random number generators). Normally fork detection has been done by checking getpid() and/or the installation of an pthread_atfork handler, but this can be fooled due to pid wrapping (see here).

I wrote up this example to show how you might accomplish pid detection on Linux utilising the kernel’s thread specific keyring.

(Source: gist.github.com)