How to set process ID in Linux for a specific program

linux, process, set

Solution

Actually, there is a way to do this. Since kernel 3.3 with CONFIG_CHECKPOINT_RESTORE set(which is set in most distros), there is /proc/sys/kernel/ns_last_pid which contains last pid generated by kernel. So, if you want to set PID for forked program, you need to perform these actions:

- Open /proc/sys/kernel/ns_last_pid and get fd

- flock it with LOCK_EX

- write PID-1

- fork

Voilà! Child will have PID that you wanted. Also, don't forget to unlock (flock with LOCK_UN) and close ns_last_pid.

You can checkout C code at my blog here.

Problem

I was wondering if there is some way to force to use some specific process ID to Linux to some application before running it. I need to know in advance the process ID.

Original source