理解strace为什么在getpid()中显示返回的父PID



我对运行strace时的一些strace捕获感到困惑。以下是相关线路:

[pid 170039] 03:21:51 clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f89ee4b8210) = 171280
...
[pid 171280] 03:21:51 getppid( <unfinished ...>
[pid 171280] 03:21:51 <... getppid resumed>) = 170039
...
[pid 171280] 03:22:12 clone(child_stack=0x7faa47ffaf30, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tid=[171977], tls=0x7faa47ffb700, child_tidptr=0x7faa47ffb9d0) = 171977
...
[pid 171977] 03:22:12 getpid()          = 171280
...
[pid 171977] 03:22:12 getppid()         = 170039
[pid 171977] 03:22:12 getpid()          = 171280
[pid 171977] 03:22:12 getpid()          = 171280

由此我推断170039是171280的母体,而这一个又是171977的母体。最后几行的差异如何?strace在括号之间显示的pid与调用的返回值不同?

我看到第二个克隆有CLONE_THREAD标志:

CLONE_THREAD (since Linux 2.4.0)
If CLONE_THREAD is set, the child is placed in the same
thread group as the calling process.
(...)
Internally, this shared PID is the so-called
thread group identifier (TGID) for the thread group.
Since Linux 2.4, calls to getpid(2) return the TGID of the
caller.

我想这可能解释了为什么PID保持不变,但strace显示为";PID";(与克隆的返回代码一致(,即本例中的171977?

我看到第二个克隆具有CLONE_THREAD标志

我想这可能解释了PID保持相同的原因

正确。

但是strace显示为";PID";(与克隆的返回代码一致(,即本例中的171977?

线程ID(TID(。要查看此信息,请尝试向正在跟踪的程序添加一些gettid()调用。

最新更新