假设我有一个会话 ID 为 10030 的进程,有什么方法可以从此会话中删除当前进程并将其分配给另一个进程?或者至少从此会话中删除?我读过以下内容:
The session's ID is the same as the pid of the process that created the session through the setsid() system call. That process is known as the session leader for that session group. All of that process's descendants are then members of that session unless they specifically remove themselves from it.
但不幸的是,我找不到如何they specifically remove themselves from it
实现“”。希望您能帮忙。谢谢。
答案1
如果这是一次性的事情,这里有一个用于gdb
附加到正在运行的进程的技术:
启动示例程序。我们将更改sleep
的 sid。
spectras@etherbee:~$ (echo;sleep 86400)&
[1] 23340
我们的流程如下sleep
:
spectras@etherbee:~$ ps -efj -q 23342
UID PID PPID PGID SID C STIME TTY TIME CMD
spectras 23342 23340 23340 22112 0 14:50 pts/5 00:00:00 sleep 86400
让我们使用 附加到它gdb
。请注意,这将暂停该过程。
spectras@etherbee:~$ gdb -p 23342
[lots of irrelevant text]
Attaching to process 23342
[more irrelevant text]
(gdb) p setsid()
$1 = 23342
(gdb) q
A debugging session is active.
Inferior 1 [process 23342] will be detached.
Quit anyway? (y or n) y
Detaching from program: /bin/sleep, process 23342
我们来看看结果:
spectras@etherbee:~$ ps -efj -q 23342
UID PID PPID PGID SID C STIME TTY TIME CMD
spectras 23342 23340 23342 23342 0 14:50 ? 00:00:00 sleep 86400
我们sleep
现在是其自己会议的流程组组长。