Search code examples
c++pid

C++ specify process id when creating process


CreateProcess contains a PROCESS_INFORMATION structure that specifies what process id was chosen for the created process.

How can I specify which process id for the process to use when creating it?


Solution

  • You simply can't specify the process id. It is selected by the operating system.

    Imagine that you are allowed to specify a process id. The os must lock its internal process list structure, in order to find if the process id is already there. And if not, to prevent any another process creation with the same id. This of course increases the complexity of the kernel with no real gain for the programmer.

    Without specifying a process id, the os can just atomically increment a counter for determining the next process id, which is way easier and faster.