So how exactly does Linux determine the following PID it'll use for any process? The objective of this would be to better comprehend the Linux kernel. You shouldn't be afraid to publish kernel source code. If PIDs are allotted sequentially so how exactly does Linux complete the gaps? What goes on if this hits the finish?
For instance basically operate a PHP script from Apache that does a
<?php print(getmypid())?> exactly the same PID is going to be printed out for any couple of minutes while hit refresh. This time period is really a purpose of the number of demands apache gets. Even when there's just one client the PID will ultimately change.
Once the PID changes, it will likely be a detailed number. however , how close? The amount doesn't seem to be entirely consecutive. Basically perform a
ps aux grep apache I recieve a reasonable quantity of processes:
So how exactly does Linux choose this next number? The prior couple of PID's continue to be running, along with the newest PID which was printed. So how exactly does apache decide to reuse these PIDs?
I'd rather assume the behaviour you watch comes from another source:
Good web servers will often have several process instances to balance the burden from the demands. These processes are handled inside a pool and designated to some certain request every time a request is available in. To optimize performance Apache most likely assigns exactly the same process to a lot of consecutive demands in the same client. After some demands that process is ended and a replacement is produced.
I do not believe which more than one processes in sequence are designated exactly the same PID by linux.
While you state that the brand new PID is gonna bond with the final one, I suppose Linux simply assigns each process the final PID + 1. But you will find processes appearing and being ended constantly in background by programs and system programs, thus you can't predict the precise quantity of the apache process being began next.
Aside from this, you need to not use any assumption about PID assignment like a base for something you implement. (See also sanmai's comment.)
The kernel allocates PIDs in the plethora of (RESERVED_PIDS, PID_MAX_DEFAULT). It will so sequentially in each namespace (tasks in various namespaces can have a similar IDs). Just in case the number is exhausted, pid assignment systems around.
Some relevant code:
for (i = ns->level i >= i--)
static int alloc_pidmap(struct pid_namespace *pid_ns)
Do observe that PIDs poor the kernel are not only
int identifiers the appropriate structure are available in
/include/linux/pid.h. Aside from the id, it consists of a listing of tasks with this id, a reference counter along with a hashed list node for immediate access.
The reason behind PIDs not showing up consecutive in user space happens because kernel arranging might fork a procedure among your process'
fork() calls. It is extremely common, actually.
PIDs are consecutive. You can observe that by beginning several processes on your own on idle machine.
PIDs could be allotted at random. There's various ways to complete that.