r/C_Programming • • Sep 01 '26

Question How process ids are generated

i know fork creates processes but how process ids are generated

6 Upvotes

22 comments sorted by

27

u/dfx_dj Sep 01 '26

They're assigned by the OS

5

u/userlivedhere Sep 02 '26

but how

12

u/Mortomes Sep 02 '26

That is up to the OS to decide.

6

u/_yrlf Sep 02 '26

The OS can assign them however it wants.

One possible design is that the OS has a process id counter, and every time a new process is forked / created, it increments that counter to get a new pid. Once that counter overflows, it also has to check that there is no process that already has that pid before giving it to you.

On Linux, it does basically that. Try calling fork() many times in a row, and usually you will get mostly consecutive pid numbers.

3

u/Key_River7180 Sep 03 '26

The os keeps track of the highest process ID assigned, when you make a process It just gets you the highest id + 1 and increases the counter

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

-10

u/[deleted] Sep 01 '26

Security through obscurity :)

14

u/Axman6 Sep 01 '26

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. 

-15

u/[deleted] Sep 01 '26

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

7

u/Axman6 Sep 01 '26

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. 

-15

u/[deleted] Sep 01 '26

Process ids are generated sequentially 

10

u/Axman6 Sep 01 '26

Not on all OS’s.

5

u/Paul_Pedant Sep 01 '26

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

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.