I've a somewhat silly question, if i have a series of processes that are created ... these are not necessarily some sort of inheritance, the pid of the processes have to be numbers continuous or random pids ?
-
in linux, had no idea that they also depended on the operating system.. – franvergara66 May 05 '09 at 00:47
7 Answers
This is determined by the Operating System.
In general, they are usually sequentially assigned by the OS. However, this behavior is not something to base decisions upon, since most operating system specifications state that the process ID allocation is not deterministic (ie: it could be random, and could change in a future version, etc).

- 554,122
- 78
- 1,158
- 1,373
On Linux, pids are generally allocated sequentially on a system-wide basis, but they will wrap around periodically, and there may be 'gaps' caused by other unrelated processes. In an extreme case, you might have a 'gap' wide enough to cause this wrap-around. So don't assume any particular ordering - instead explicitly keep track of your parent or child process' PIDs.

- 224,562
- 31
- 268
- 324
-
1I'm doing a program in C using semaphore that simulates the creation and release process, then I don't know whether to assign a PID random process or whether each assigning consecutive numbers from a value, I want the simulation to be as similar as possible to the clearance processes in linux – franvergara66 May 05 '09 at 00:51
-
1If you're making a simulation, the best approach from the fuzzing viewpoint would be to have multiple methods (random, sequential, almost-sequential) and try whatever you're testing with each. – bdonlan May 05 '09 at 00:54
Here's how to test what your system does:
for i in $(seq 20); do ps; done | grep ps
The PIDs of the "ps" commands are consecutive processes, or as close to consecutive as any other caller could reasonably expect to be able to spawn.
My cygwin terminal on Windows allocates them randomly-ish, my web host allocates them sequentially (with occasional gaps which presumably are for processes run by other users or servers).
Some consider sequential PID allocation to be a slight possible security concern, since it may leak information between users of a multi-user system.

- 273,490
- 39
- 460
- 699
-
3A better approach would be for i in `seq 20`; do sh -c 'echo $$'; done Grepping for 'ps' in 'ps's output may get some noise in the result (eg, other users running ps at the same time...) – bdonlan May 05 '09 at 01:58
-
Sure, I was just thinking about a quick developer test from the command line. If you wrote a script or something that relied on the output it's a different matter. – Steve Jessop May 05 '09 at 02:05
On AIX, you will often see bigger (e.g. 7-digit) PIDs and they are not necessarily allocated semi-sequentially (though I seemed to be cycling in increments of 2 when I tested; there was another user on the machine, so it may not mean much).
Fresh login on an AIX 5.3 machine:
$ ps
PID TTY TIME CMD
1060910 pts/27 0:00 -ksh
1155224 pts/27 0:00 ps
$

- 730,956
- 141
- 904
- 1,278
Depends on your platform, but you shouldn't be dependent on any specific order to your pid's.
On Windows, pid's are usually allocated in increasing numbers, but as processes exit the pid's can be recycled and you will see cases where a newer process has a lower pid than an older process. Also, pid's come out of the same namespace as tid's, so you won't see pid's increasing by 4 as you launch new processes.

- 54,279
- 5
- 125
- 144
From your perspective they will be random. The system manages those numbers and assigns them as processes are created. A quick look at the PID's currently on my system shows that they are all divisible by 4...

- 87,343
- 27
- 171
- 245
-
I'm doing a program in C using semaphore that simulates the creation and release process, then I don't know whether to assign a PID random process or whether each assigning consecutive numbers from a value, I want the simulation to be as similar as possible to the clearance processes in linux – franvergara66 May 05 '09 at 00:50
If you're creating those childs you'll know the pid, the pid depends on the OS scheduler, you don't care about this stuff.

- 1,088
- 1
- 11
- 20