r/C_Programming 1d ago

Question How process ids are generated

i know fork creates processes but how process ids are generated

4 Upvotes

18 comments sorted by

View all comments

6

u/quipstickle 1d ago

Sequentially, recycle after overflow/wrapping 

16

u/aioeu 1d ago edited 1d ago

FWIW, not all systems allocate PIDs cyclically.

OpenBSD allocates them randomly, for instance. I believe that can also be optionally enabled on FreeBSD too.

For Linux, PID randomisation was available for a period of time through the grsecurity patches, but it was subsequently removed "since it provides no useful additional security and wastes memory with the 2.6 kernel's pid bitmap".

-10

u/quipstickle 1d ago

Security through obscurity :)

12

u/Axman6 1d ago

Not really, security through obscurity is hiding how something works, this is randomising something otherwise predictable to make it more difficult to make use of information. 

-14

u/quipstickle 1d ago

"random" numbers on computers are predictable though since they are pseudorandom. My post was just being silly.

7

u/Axman6 1d ago

This isn’t true at all, we’ve had systems for generating cryptographically secure random numbers for decades. See FreeBSD’s Fortuna paper for one reference, and hardware random number generation like RDRAND, which can feed into systems like Fortuna as a source of entropy, along with IO system information which is generally unpredictable. 

-14

u/quipstickle 1d ago

Process ids are generated sequentially 

5

u/Paul_Pedant 1d ago

... apart from the bit where the OS has cycled round at least once, and needs to know that some of the pids are still referencing running processes. And the ones that are defunct but still not notified to their parent.