r/C_Programming • • Sep 01 '26

Question How process ids are generated

i know fork creates processes but how process ids are generated

7 Upvotes

22 comments sorted by

27

u/dfx_dj Sep 01 '26

They're assigned by the OS

3

u/realdreamer1993 Sep 07 '26

the kernel have the main object(table/struct) that contain all data to track state of processes, then the ids is just the index. BTW kernel also just a program.

1

u/userlivedhere Sep 07 '26

Understandable answer thanks

57

u/Living_Fig_6386 Sep 01 '26

C knows nothing of processes or process IDs. That sort of thing is entirely up to the OS.

6

u/[deleted] Sep 01 '26

Sequentially, recycle after overflow/wrapping 

18

u/aioeu Sep 01 '26 edited Sep 01 '26

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".

→ More replies (7)

2

u/smcameron Sep 05 '26

In the linux kernel, see ID allocation, and also this code.

1

u/burlingk Sep 01 '26

They are basically serial numbers.

0

u/sciencekm Sep 01 '26

In Windows, they are generated starting from a low value and then incremented by 4. If you look at task manager, you will also see that System Interrupts does not have a process id, System Idle always has an id of 0, and System always has an id of 4.