tags:

views:

590

answers:

7

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 ?

A: 

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.

Michael
A: 

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

Chris Lively
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
Melkhiah66
+4  A: 

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

Reed Copsey
+2  A: 

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.

bdonlan
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
Melkhiah66
If 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
A: 

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.

ktulur
A: 

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.

Steve Jessop
A better approach would be for i in `seq 20`; do sh -c 'echo $$'; doneGrepping for 'ps' in 'ps's output may get some noise in the result (eg, other users running ps at the same time...)
bdonlan
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
A: 

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
$
Jonathan Leffler