如何在 Linux 中为特定程序设置进程 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18122592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set process ID in Linux for a specific program
提问by Borja Tarraso
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.
我想知道是否有某种方法可以在运行某些应用程序之前强制将某些特定的进程 ID 用于 Linux。我需要提前知道进程 ID。
采纳答案by Ruslan Kuprieiev
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:
实际上,有一种方法可以做到这一点。由于内核 3.3 设置了 CONFIG_CHECKPOINT_RESTORE 设置(在大多数发行版中设置),因此 /proc/sys/kernel/ns_last_pid 包含内核生成的最后一个 pid。因此,如果要为分叉程序设置 PID,则需要执行以下操作:
- Open /proc/sys/kernel/ns_last_pid and get fd
- flock it with LOCK_EX
- write PID-1
- fork
- 打开 /proc/sys/kernel/ns_last_pid 并获取 fd
- 用 LOCK_EX 蜂拥而至
- 写PID-1
- 叉子
Voilà! Child will have PID that you wanted. Also, don't forget to unlock (flock with LOCK_UN) and close ns_last_pid.
瞧!孩子将拥有您想要的PID。另外,不要忘记解锁(使用 LOCK_UN 锁定)并关闭 ns_last_pid。
You can checkout C code at my blog here.
您可以在我的博客中查看C 代码。
回答by deagh
Every process on a linux system is generated by fork()so there should be no way to force a specific PID.
linux 系统上的每个进程都是由fork()生成的,所以应该没有办法强制特定的 PID。
回答by Elisiano Petrini
As many already suggested you cannot set directly a PID but usually shells have facilities to know which is the last forked process ID.
正如许多人已经建议的那样,您不能直接设置 PID,但通常 shell 可以知道哪个是最后一个分叉的进程 ID。
For example in bash you can lunch an executable in background (appending &
) and find its PID in the variable $!
.
Example:
例如,在 bash 中,您可以在后台运行一个可执行文件(追加&
)并在变量中找到它的 PID $!
。例子:
$ lsof >/dev/null &
[1] 15458
$ echo $!
15458
回答by Oscerd
There's no way to force to use specific PID for process. As Wikipediasays:
没有办法强制对进程使用特定的 PID。正如维基百科所说:
Process IDs are usually allocated on a sequential basis, beginning at 0 and rising to a maximum value which varies from system to system. Once this limit is reached, allocation restarts at 300 and again increases. In Mac OS X and HP-UX, allocation restarts at 100. However, for this and subsequent passes any PIDs still assigned to processes are skipped
进程 ID 通常按顺序分配,从 0 开始并上升到最大值,该值因系统而异。一旦达到此限制,分配将从 300 重新开始并再次增加。在 Mac OS X 和 HP-UX 中,分配从 100 重新开始。但是,对于本次和后续传递,任何仍分配给进程的 PID 都将被跳过
回答by oliver
You could just repeatedly call fork()
to create new child processes until you get a child with the desired PID. Remember to call wait()
often, or you will hit the per-user process limit quickly.
您可以重复调用fork()
以创建新的子进程,直到获得具有所需 PID 的子进程。请记住wait()
经常调用,否则您将很快达到每个用户的进程限制。
This method assumes that the OS assigns new PIDs sequentially, which appears to be the case eg. on Linux 3.3.
这种方法假设操作系统按顺序分配新的 PID,这似乎就是这种情况,例如。在 Linux 3.3 上。
The advantage over the ns_last_pid
method is that it doesn't require root permissions.
相对于该ns_last_pid
方法的优点是它不需要 root 权限。
回答by Kentgrav
On CentOS7.2 you can simply do the following:
在 CentOS7.2 上,您可以简单地执行以下操作:
Let's say you want to execute the sleep command with a PID of 1894.
假设您要执行 PID 为 1894 的 sleep 命令。
sudo echo 1893 > /proc/sys/kernel/ns_last_pid; sleep 1000
(However, keep in mind that if by chance another process executes in the extremely brief amount of time between the echo and sleep command you could end up with a PID of 1895+. I've tested it hundreds of times and it has never happened to me. If you want to guarantee the PID you will need to lock the file after you write to it, execute sleep, then unlock the file as suggested in Ruslan's answer above.)
(但是,请记住,如果另一个进程偶然在 echo 和 sleep 命令之间的极短时间内执行,您最终可能会得到 1895+ 的 PID。我已经对其进行了数百次测试,但从未发生过对我来说。如果你想保证 PID,你需要在写入文件后锁定文件,执行睡眠,然后按照上面 Ruslan 的回答中的建议解锁文件。)