解释STRACE输出-管道和叉子 [英] Interpreting STRACE output - pipes and forks

查看:267
本文介绍了解释STRACE输出-管道和叉子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下用C语言编写的代码,摘录自 https://beej.us/guide/bgipc/html/multi/pipes.html :

I have the following code written in C, taken from https://beej.us/guide/bgipc/html/multi/pipes.html:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{ 
    int pfds[2];

    pipe(pfds);

    if (!fork()) {
        close(1);       /* close normal stdout */
        dup(pfds[1]);   /* make stdout same as pfds[1] */
        close(pfds[0]); /* we don't need this */
        execlp("/bin/ls", "ls", NULL);
    } else {
        close(0);       /* close normal stdin */
        dup(pfds[0]);   /* make stdin same as pfds[0] */
        close(pfds[1]); /* we don't need this */
        execlp("/usr/bin/wc", "wc", "-l", NULL);
    }

return 0;
}

使用strace在终端中编译并运行此代码时,我得到以下输出:

When compiling and running this code in the terminal using strace I get the following output:

execve("./forks", ["./forks"], [/* 55 vars */]) = 0
arch_prctl(ARCH_SET_FS, 0x7f2b0e498700) = 0
pipe([3, 4])                            = 0
clone(Process 7304 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f2b0e4989d0) = 7304
[pid  7303] execve("/usr/bin/wc", ["wc", "-l"], [/* 55 vars */] <unfinished ...>
[pid  7304] execve("/bin/ls", ["ls"], [/* 55 vars */] <unfinished ...>
[pid  7303] <... execve resumed> )      = 0
[pid  7304] <... execve resumed> )      = 0
[pid  7303] arch_prctl(ARCH_SET_FS, 0x7f558acde700) = 0
[pid  7304] arch_prctl(ARCH_SET_FS, 0x7f4bef4f67c0) = 0
[pid  7304] exit_group(0)               = ?
Process 7304 detached
--- SIGCHLD (Child exited) @ 0 (0) ---
21
exit_group(0) 

有人可以逐行解释strace输出中发生了什么吗?我试图研究如何解释strace输出,但是没有任何运气.

Can anybody explain, line by line, what is going on in the strace output? I've attempted to research how to interpret strace outputs but haven't had any luck.

提前谢谢.

推荐答案

execve("./forks", ["./forks"], [/* 55 vars */]) = 0

shell使用可执行文件调用execve,将./forks作为argv[0]. /* 55 vars */是从shell继承的环境变量.

The shell calls execve with your executable and ./forks as argv[0]. The /* 55 vars */ are the environment variables inherited from the shell.

arch_prctl(ARCH_SET_FS, 0x7f2b0e498700) = 0

可能为新启动的进程设置了线程本地存储.

Probably sets up thread local storage for the newly launched process.

pipe([3, 4])

pipe系统调用返回一对描述符,34.之所以使用数字,是因为到目前为止尚未为该进程分配除0(stdin),1(stdout)和2(stderr)之外的描述符.

The pipe system call returns a pair of descriptors, 3 and 4. The numbers are such because no descriptors other than 0 (stdin), 1 (stdout) and 2 (stderr) have been allocated so far to the process.

clone(Process 7304 attached child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f2b0e4989d0) = 7304

clone系统调用对应于对fork的调用,从而产生一个新进程.即使fork通常被称为系统调用,尤其是在Linux上,它还是包装了对clone(2)的调用.

The clone system call corresponds to the call to fork, spawning a new process. Even though fork is commonly referred to as a system call, particularly on Linux it wraps a call to clone(2).

[pid  7303] execve("/usr/bin/wc", ["wc", "-l"], [/* 55 vars */] <unfinished ...>
[pid  7304] execve("/bin/ls", ["ls"], [/* 55 vars */] <unfinished ...>
[pid  7303] <... execve resumed> )      = 0
[pid  7304] <... execve resumed> )      = 0
[pid  7303] arch_prctl(ARCH_SET_FS, 0x7f558acde700) = 0
[pid  7304] arch_prctl(ARCH_SET_FS, 0x7f4bef4f67c0) = 0
[pid  7304] exit_group(0)

在父级和子级中,将启动两个新的可执行文件.子进程以[pid 7304] exit_group(0)退出,父进程立即收到SIGCHLD信号,表明子进程已更改其状态.

Within the parent and the child, two new executables are started. The child exits with [pid 7304] exit_group(0) and the parent immediately receives a SIGCHLD signal that a child process has changed its state.

这篇关于解释STRACE输出-管道和叉子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆