How does Linux determine the next PID?

前端 未结 4 1404
囚心锁ツ
囚心锁ツ 2020-11-30 03:22

How does Linux determine the next PID it will use for a process? The purpose of this question is to better understand the Linux kernel. Don\'t be afraid to post kernel sou

4条回答
  •  鱼传尺愫
    2020-11-30 03:42

    I would rather assume the behavior you watch stems from another source:

    Good web servers usually have several process instances to balance the load of the requests. These processes are managed in a pool and assigned to a certain request each time a request comes in. To optimize performance Apache probably assigns the same process to a bunch of sequential requests from the same client. After a certain amount of requests that process is terminated and a new one is created.

    I don't believe that more than one processes in sequence are assigned the same PID by linux.

    As you say that the new PID is gonna be close to the last one, I guess Linux simply assigns each process the last PID + 1. But there are processes popping up and being terminated all the time in background by applications and system programs, thus you cannot predict the exact number of the apache process being started next.

    Apart from this, you should not use any assumption about PID assignment as a base for something you implement. (See also sanmai's comment.)

提交回复
热议问题