Linux:subshell(子shell)和childprocess(子进程)

日晨难再 2024-06-29 09:07:04 阅读 100

相关阅读

Linux

icon-default.png?t=N7T8

https://blog.csdn.net/weixin_45791458/category_12234591.html?spm=1001.2014.3001.5482

一、预备知识

        在进行这个话题之前,首先要了解Linux系统中一个进程是如何创建的,当一个命令被执行时,首先会在当前进程创建一个子进程,这个进程除了少数地方(例如进程PID和PPID),几乎和当前进程完全一样,使用的系统调用是fork()或者clone()。

        在创建了子进程后,通常会调用exec()系列的系统函数在子进程中进行替换,从而运行一个新的程序,该程序能继承父进程中处于导出状态的变量(环境变量)和函数。且子进程中的变量赋值和其他设置不会影响父进程。

        对于后面的内容,我们把“创建子进程”当做是上面的第一步,把“创建****子进程”当做是创建进程和替换进程,即完整的两步。

        下面举例说明了上述过程,首先创建了一个shell脚本,其中只有一个sleep命令,随后使用strace命令追踪使用bash解释执行这个脚本过程中的系统调用。

# 文件test.sh

sleep 20

$ strace -f bash test.sh # 需要加上-f选项,这样才能追踪子进程

execve("/usr/bin/bash", ["bash", "test.sh"], 0x7ffe5b2df940 /* 55 vars */) = 0

brk(NULL) = 0x55cfde987000

arch_prctl(0x3001 /* ARCH_??? */, 0x7fff3394b3b0) = -1 EINVAL (Invalid argument)

mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa2e2cac000

access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa2e2c94000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=200136, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 203040, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa2e2c62000

mmap(0x7fa2e2c70000, 69632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe000) = 0x7fa2e2c70000

mmap(0x7fa2e2c81000, 57344, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7fa2e2c81000

mmap(0x7fa2e2c8f000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2c000) = 0x7fa2e2c8f000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa2e2a00000

mprotect(0x7fa2e2a28000, 2023424, PROT_NONE) = 0

mmap(0x7fa2e2a28000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7fa2e2a28000

mmap(0x7fa2e2bbd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7fa2e2bbd000

mmap(0x7fa2e2c16000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7fa2e2c16000

mmap(0x7fa2e2c1c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa2e2c1c000

close(3) = 0

mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa2e2c5f000

arch_prctl(ARCH_SET_FS, 0x7fa2e2c5f740) = 0

set_tid_address(0x7fa2e2c5fa10) = 4064924

set_robust_list(0x7fa2e2c5fa20, 24) = 0

rseq(0x7fa2e2c600e0, 0x20, 0, 0x53053053) = 0

mprotect(0x7fa2e2c16000, 16384, PROT_READ) = 0

mprotect(0x7fa2e2c8f000, 16384, PROT_READ) = 0

mprotect(0x55cfdd194000, 16384, PROT_READ) = 0

mprotect(0x7fa2e2ce6000, 8192, PROT_READ) = 0

prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

munmap(0x7fa2e2c94000, 95179) = 0

openat(AT_FDCWD, "/dev/tty", O_RDWR|O_NONBLOCK) = 3

close(3) = 0

getrandom("\x34\xa4\x2c\xab\x9e\xab\xb9\x29", 8, GRND_NONBLOCK) = 8

brk(NULL) = 0x55cfde987000

brk(0x55cfde9a8000) = 0x55cfde9a8000

openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa2e2000000

close(3) = 0

openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=27002, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 27002, PROT_READ, MAP_SHARED, 3, 0) = 0x7fa2e2ca5000

close(3) = 0

futex(0x7fa2e2c1ba6c, FUTEX_WAKE_PRIVATE, 2147483647) = 0

getuid() = 1006

getgid() = 1002

geteuid() = 1006

getegid() = 1002

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

ioctl(-1, TIOCGPGRP, 0x7fff3394b2a4) = -1 EBADF (Bad file descriptor)

sysinfo({uptime=7178153, loads=[65344, 50976, 45152], totalram=135030112256, freeram=18004209664, sharedram=3393691648, bufferram=2800984064, totalswap=31999389696, freeswap=16647667712, procs=4198, totalhigh=0, freehigh=0, mem_unit=1}) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

uname({sysname="Linux", nodename="liubo-SYS-7048GR-TR", ...}) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

getpid() = 4064924

getppid() = 4064921

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/bin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/sbin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/bin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/sbin/bash", 0x7fff3394aee0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

getpid() = 4064924

getppid() = 4064921

getpid() = 4064924

getppid() = 4064921

getpgrp() = 4064921

ioctl(2, TIOCGPGRP, [4064921]) = 0

rt_sigaction(SIGCHLD, {sa_handler=0x55cfdd0a9d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, 8) = 0

prlimit64(0, RLIMIT_NPROC, NULL, {rlim_cur=514857, rlim_max=514857}) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

openat(AT_FDCWD, "test.sh", O_RDONLY) = 3

newfstatat(AT_FDCWD, "test.sh", {st_mode=S_IFREG|0644, st_size=9, ...}, 0) = 0

ioctl(3, TCGETS, 0x7fff3394b240) = -1 ENOTTY (Inappropriate ioctl for device)

lseek(3, 0, SEEK_CUR) = 0

read(3, "sleep 20\n", 80) = 9

lseek(3, 0, SEEK_SET) = 0

prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0

fcntl(255, F_GETFD) = -1 EBADF (Bad file descriptor)

dup2(3, 255) = 255

close(3) = 0

fcntl(255, F_SETFD, FD_CLOEXEC) = 0

fcntl(255, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)

newfstatat(255, "", {st_mode=S_IFREG|0644, st_size=9, ...}, AT_EMPTY_PATH) = 0

lseek(255, 0, SEEK_CUR) = 0

read(255, "sleep 20\n", 9) = 9

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/bin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/sbin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/bin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/sbin/sleep", 0x7fff3394ad30, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/sleep", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/sleep", R_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/sleep", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/sleep", R_OK) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0

clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fa2e2c5fa10) = 4064925

strace: Process 4064925 attached

[pid 4064924] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 4064925] set_robust_list(0x7fa2e2c5fa20, 24) = 0

[pid 4064924] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 4064925] getpid( <unfinished ...>

[pid 4064924] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 4064925] <... getpid resumed>) = 4064925

[pid 4064924] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 4064924] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 4064924] rt_sigaction(SIGINT, {sa_handler=0x55cfdd0abdc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, <unfinished ...>

[pid 4064925] close(255 <unfinished ...>

[pid 4064924] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] <... close resumed>) = 0

[pid 4064924] wait4(-1, <unfinished ...>

[pid 4064925] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 4064925] rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, {sa_handler=0x55cfdd0a9d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fa2e2a42520}, 8) = 0

[pid 4064925] execve("/usr/bin/sleep", ["sleep", "20"], 0x55cfde998b60 /* 54 vars */) = 0

[pid 4064925] brk(NULL) = 0x56531e557000

[pid 4064925] arch_prctl(0x3001 /* ARCH_??? */, 0x7fff4b839270) = -1 EINVAL (Invalid argument)

[pid 4064925] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1758aa0000

[pid 4064925] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

[pid 4064925] openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

[pid 4064925] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

[pid 4064925] mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1758a88000

[pid 4064925] close(3) = 0

[pid 4064925] openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

[pid 4064925] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

[pid 4064925] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 4064925] pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

[pid 4064925] pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

[pid 4064925] newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

[pid 4064925] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 4064925] mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1758800000

[pid 4064925] mprotect(0x7f1758828000, 2023424, PROT_NONE) = 0

[pid 4064925] mmap(0x7f1758828000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f1758828000

[pid 4064925] mmap(0x7f17589bd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7f17589bd000

[pid 4064925] mmap(0x7f1758a16000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7f1758a16000

[pid 4064925] mmap(0x7f1758a1c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1758a1c000

[pid 4064925] close(3) = 0

[pid 4064925] mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1758a85000

[pid 4064925] arch_prctl(ARCH_SET_FS, 0x7f1758a85740) = 0

[pid 4064925] set_tid_address(0x7f1758a85a10) = 4064925

[pid 4064925] set_robust_list(0x7f1758a85a20, 24) = 0

[pid 4064925] rseq(0x7f1758a860e0, 0x20, 0, 0x53053053) = 0

[pid 4064925] mprotect(0x7f1758a16000, 16384, PROT_READ) = 0

[pid 4064925] mprotect(0x56531d334000, 4096, PROT_READ) = 0

[pid 4064925] mprotect(0x7f1758ada000, 8192, PROT_READ) = 0

[pid 4064925] prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

[pid 4064925] munmap(0x7f1758a88000, 95179) = 0

[pid 4064925] getrandom("\xf0\x4d\x93\x81\xc9\x8a\x71\x74", 8, GRND_NONBLOCK) = 8

[pid 4064925] brk(NULL) = 0x56531e557000

[pid 4064925] brk(0x56531e578000) = 0x56531e578000

[pid 4064925] openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

[pid 4064925] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

[pid 4064925] mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1757e00000

[pid 4064925] close(3) = 0

[pid 4064925] clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=20, tv_nsec=0}, 0x7fff4b8392c0) = 0

[pid 4064925] close(1) = 0

[pid 4064925] close(2) = 0

[pid 4064925] exit_group(0) = ?

[pid 4064925] +++ exited with 0 +++

<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 4064925

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, {sa_handler=0x55cfdd0abdc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fa2e2a42520}, 8) = 0

ioctl(2, TIOCGWINSZ, {ws_row=32, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

newfstatat(AT_FDCWD, "/home/zhangchen/.terminfo", 0x55cfde99f160, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/etc/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/lib/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/share/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

access("/etc/terminfo/x/xterm-256color", R_OK) = -1 ENOENT (No such file or directory)

access("/lib/terminfo/x/xterm-256color", R_OK) = 0

openat(AT_FDCWD, "/lib/terminfo/x/xterm-256color", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=3852, ...}, AT_EMPTY_PATH) = 0

read(3, "\36\2%\0&\0\17\0\235\1Z\6xterm-256color|xterm"..., 32768) = 3852

read(3, "", 28672) = 0

close(3) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TIOCGWINSZ, {ws_row=32, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(0, TIOCGWINSZ, {ws_row=32, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

brk(0x55cfde9c9000) = 0x55cfde9c9000

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=4064925, si_uid=1006, si_status=0, si_utime=0, si_stime=0} ---

wait4(-1, 0x7fff3394a810, WNOHANG, NULL) = -1 ECHILD (No child processes)

rt_sigreturn({mask=[]}) = 0

read(255, "", 9) = 0

rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

exit_group(0) = ?

+++ exited with 0 +++

        可以看出,strace的输出在最开始没有显示从终端内的bash创建strace子进程和strace进程创建子进程的过程(因为这一般不是我们关心的),而是从strace进程创建子进程后调用execve进行替换开始(替换为bash)。从其中的getpid()=4064924可以看出,该bash进程的PID为4064924。从getppid()=4064921可以看出,该bash进程父进程的PID为4064921,这是strace进程的PID,而strace进程的父进程是终端内运行的bash。

        其中的clone(****)=4064925,展现了bash进程在读取到了sleep命令后,创建了一个子进程,并且子进程的PID为4064925。后面的[pid 4064925] execve("/usr/bin/sleep", ["sleep", "20"], 0x556bf6f3bb60 /* 54 vars */)=0展示了该子进程进行替换的过程。上面的两步,导致了bash进程创建了一个sleep子进程。

        其中的[pid 4064925] exit_group(0)=?代表了sleep进程退出,而最后的exit_group(0)=?代表了bash进程退出。

        在运行strace命令时,我们可以在另一个终端利用ps命令观察到进程的创建情况,如下所示。

UID PID PPID C STIME TTY TIME CMD

zhangch+ 4064921 4064501 0 14:19 pts/20 00:00:00 strace -f bash test.sh

zhangch+ 4064924 4064921 0 14:19 pts/20 00:00:00 bash test.sh

zhangch+ 4064925 4064924 0 14:19 pts/20 00:00:00 sleep 20

二、subshell(子shell)

        什么是子shell,似乎从字面意思,就是利用bash命令创建了一个bash子进程,就像是上面的starce命令所做的那样,它创建了bash子进程并在bash子进程中又创建了一个sleep子进程。

        但其实子shell不是如此,它特指在某些情况下,bash解释器会创建子进程,但不进行替换的过程,这与利用bash命令创建了一个bash子进程是不同的,后者存在替换的过程。

        下面的这些情况,会导致创建子shell。

1、小括号():使用小括号包围的命令,会在子shell中进行执行(意思就是先创建子shell(没有替换过程),随后在子shell中再根据命令创建子进程(完整的两步))。

2、后台控制符&:使用&结尾的命令,会在子shell中进行执行。

3、管道控制符| :使用|连接的命令,会分别各自创建子shell并进行执行。

4、命令替换$():命令替换中的命令,是在子shell中进行执行。

4、coproc命令:这个命令会导致开启一个子shell,并执行后面的命令。

        需要注意的是,我们本文所说的所有命令都是外部命令,而不是shell内建命令,对于内建命令,不会用clone、execve创建进程,而是直接执行。可以使用type来检测一个命令是否为内建命令,如下所示。

$ type cd

cd is a shell builtin

         我们以小括号举例,将shell脚本修改为如下所示。注意到我们在小括号中添加了两条命令,这么做的原因后面会进行说明。

​# 文件test.sh

(sleep 20;sleep 10)

        此时使用strace追踪的结果如下所示。 

execve("/usr/bin/bash", ["bash", "test.sh"], 0x7ffcd0c34cb0 /* 55 vars */) = 0

brk(NULL) = 0x5577b9e0f000

arch_prctl(0x3001 /* ARCH_??? */, 0x7fffb1f38ed0) = -1 EINVAL (Invalid argument)

mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f584c4fd000

access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f584c4e5000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=200136, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 203040, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f584c4b3000

mmap(0x7f584c4c1000, 69632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe000) = 0x7f584c4c1000

mmap(0x7f584c4d2000, 57344, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7f584c4d2000

mmap(0x7f584c4e0000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2c000) = 0x7f584c4e0000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f584c200000

mprotect(0x7f584c228000, 2023424, PROT_NONE) = 0

mmap(0x7f584c228000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f584c228000

mmap(0x7f584c3bd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7f584c3bd000

mmap(0x7f584c416000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7f584c416000

mmap(0x7f584c41c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f584c41c000

close(3) = 0

mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f584c4b0000

arch_prctl(ARCH_SET_FS, 0x7f584c4b0740) = 0

set_tid_address(0x7f584c4b0a10) = 649416

set_robust_list(0x7f584c4b0a20, 24) = 0

rseq(0x7f584c4b10e0, 0x20, 0, 0x53053053) = 0

mprotect(0x7f584c416000, 16384, PROT_READ) = 0

mprotect(0x7f584c4e0000, 16384, PROT_READ) = 0

mprotect(0x5577b9661000, 16384, PROT_READ) = 0

mprotect(0x7f584c537000, 8192, PROT_READ) = 0

prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

munmap(0x7f584c4e5000, 95179) = 0

openat(AT_FDCWD, "/dev/tty", O_RDWR|O_NONBLOCK) = 3

close(3) = 0

getrandom("\xaf\x8b\x4d\x62\x2c\xae\x8f\x65", 8, GRND_NONBLOCK) = 8

brk(NULL) = 0x5577b9e0f000

brk(0x5577b9e30000) = 0x5577b9e30000

openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f584b800000

close(3) = 0

openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=27002, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 27002, PROT_READ, MAP_SHARED, 3, 0) = 0x7f584c4f6000

close(3) = 0

futex(0x7f584c41ba6c, FUTEX_WAKE_PRIVATE, 2147483647) = 0

getuid() = 1006

getgid() = 1002

geteuid() = 1006

getegid() = 1002

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

ioctl(-1, TIOCGPGRP, 0x7fffb1f38dc4) = -1 EBADF (Bad file descriptor)

sysinfo({uptime=7261723, loads=[40352, 39168, 38336], totalram=135030112256, freeram=18767388672, sharedram=3393511424, bufferram=2823208960, totalswap=31999389696, freeswap=16653172736, procs=4076, totalhigh=0, freehigh=0, mem_unit=1}) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

uname({sysname="Linux", nodename="liubo-SYS-7048GR-TR", ...}) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

getpid() = 649416

getppid() = 649413

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/bin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/sbin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/bin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/sbin/bash", 0x7fffb1f38a00, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

getpid() = 649416

getppid() = 649413

getpid() = 649416

getppid() = 649413

getpgrp() = 649413

ioctl(2, TIOCGPGRP, [649413]) = 0

rt_sigaction(SIGCHLD, {sa_handler=0x5577b9576d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

prlimit64(0, RLIMIT_NPROC, NULL, {rlim_cur=514857, rlim_max=514857}) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

openat(AT_FDCWD, "test.sh", O_RDONLY) = 3

newfstatat(AT_FDCWD, "test.sh", {st_mode=S_IFREG|0777, st_size=20, ...}, 0) = 0

ioctl(3, TCGETS, 0x7fffb1f38d60) = -1 ENOTTY (Inappropriate ioctl for device)

lseek(3, 0, SEEK_CUR) = 0

read(3, "(sleep 20;sleep 10)\n", 80) = 20

lseek(3, 0, SEEK_SET) = 0

prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0

fcntl(255, F_GETFD) = -1 EBADF (Bad file descriptor)

dup2(3, 255) = 255

close(3) = 0

fcntl(255, F_SETFD, FD_CLOEXEC) = 0

fcntl(255, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)

newfstatat(255, "", {st_mode=S_IFREG|0777, st_size=20, ...}, AT_EMPTY_PATH) = 0

lseek(255, 0, SEEK_CUR) = 0

read(255, "(sleep 20;sleep 10)\n", 20) = 20

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0

clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f584c4b0a10) = 649417

strace: Process 649417 attached

[pid 649416] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649417] set_robust_list(0x7f584c4b0a20, 24 <unfinished ...>

[pid 649416] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649417] <... set_robust_list resumed>) = 0

[pid 649416] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 649416] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649417] getpid( <unfinished ...>

[pid 649416] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649417] <... getpid resumed>) = 649417

[pid 649416] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 649416] rt_sigaction(SIGINT, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649417] close(255 <unfinished ...>

[pid 649416] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] <... close resumed>) = 0

[pid 649416] wait4(-1, <unfinished ...>

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 649417] rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9576d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGINT, {sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGCHLD, {sa_handler=0x5577b9576d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 649417] newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

[pid 649417] newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/opt/miniconda3/bin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/usr/local/sbin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/usr/local/bin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/usr/sbin/sleep", 0x7fffb1f385a0, 0) = -1 ENOENT (No such file or directory)

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] geteuid() = 1006

[pid 649417] getegid() = 1002

[pid 649417] getuid() = 1006

[pid 649417] getgid() = 1002

[pid 649417] access("/usr/bin/sleep", X_OK) = 0

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] geteuid() = 1006

[pid 649417] getegid() = 1002

[pid 649417] getuid() = 1006

[pid 649417] getgid() = 1002

[pid 649417] access("/usr/bin/sleep", R_OK) = 0

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] geteuid() = 1006

[pid 649417] getegid() = 1002

[pid 649417] getuid() = 1006

[pid 649417] getgid() = 1002

[pid 649417] access("/usr/bin/sleep", X_OK) = 0

[pid 649417] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 649417] geteuid() = 1006

[pid 649417] getegid() = 1002

[pid 649417] getuid() = 1006

[pid 649417] getgid() = 1002

[pid 649417] access("/usr/bin/sleep", R_OK) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0

[pid 649417] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f584c4b0a10) = 649418

strace: Process 649418 attached

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649418] set_robust_list(0x7f584c4b0a20, 24 <unfinished ...>

[pid 649417] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649418] <... set_robust_list resumed>) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [CHLD], <unfinished ...>

[pid 649418] getpid( <unfinished ...>

[pid 649417] <... rt_sigprocmask resumed>[], 8) = 0

[pid 649418] <... getpid resumed>) = 649418

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649418] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649417] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649418] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [CHLD], <unfinished ...>

[pid 649418] rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649417] <... rt_sigprocmask resumed>[], 8) = 0

[pid 649418] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigaction(SIGINT, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649418] rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649417] <... rt_sigaction resumed>{sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649418] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] wait4(-1, <unfinished ...>

[pid 649418] rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649418] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649418] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649418] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9576d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649418] execve("/usr/bin/sleep", ["sleep", "20"], 0x5577b9e20b60 /* 54 vars */) = 0

[pid 649418] brk(NULL) = 0x5561fa911000

[pid 649418] arch_prctl(0x3001 /* ARCH_??? */, 0x7ffc69f99ce0) = -1 EINVAL (Invalid argument)

[pid 649418] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f554a9ee000

[pid 649418] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

[pid 649418] openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

[pid 649418] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

[pid 649418] mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f554a9d6000

[pid 649418] close(3) = 0

[pid 649418] openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

[pid 649418] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

[pid 649418] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 649418] pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

[pid 649418] pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

[pid 649418] newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

[pid 649418] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 649418] mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f554a600000

[pid 649418] mprotect(0x7f554a628000, 2023424, PROT_NONE) = 0

[pid 649418] mmap(0x7f554a628000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f554a628000

[pid 649418] mmap(0x7f554a7bd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7f554a7bd000

[pid 649418] mmap(0x7f554a816000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7f554a816000

[pid 649418] mmap(0x7f554a81c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f554a81c000

[pid 649418] close(3) = 0

[pid 649418] mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f554a9d3000

[pid 649418] arch_prctl(ARCH_SET_FS, 0x7f554a9d3740) = 0

[pid 649418] set_tid_address(0x7f554a9d3a10) = 649418

[pid 649418] set_robust_list(0x7f554a9d3a20, 24) = 0

[pid 649418] rseq(0x7f554a9d40e0, 0x20, 0, 0x53053053) = 0

[pid 649418] mprotect(0x7f554a816000, 16384, PROT_READ) = 0

[pid 649418] mprotect(0x5561f8e6f000, 4096, PROT_READ) = 0

[pid 649418] mprotect(0x7f554aa28000, 8192, PROT_READ) = 0

[pid 649418] prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

[pid 649418] munmap(0x7f554a9d6000, 95179) = 0

[pid 649418] getrandom("\xe2\xdf\xc1\x0c\x9a\xb3\xb1\x8c", 8, GRND_NONBLOCK) = 8

[pid 649418] brk(NULL) = 0x5561fa911000

[pid 649418] brk(0x5561fa932000) = 0x5561fa932000

[pid 649418] openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

[pid 649418] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

[pid 649418] mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5549c00000

[pid 649418] close(3) = 0

[pid 649418] clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=20, tv_nsec=0}, 0x7ffc69f99d30) = 0

[pid 649418] close(1) = 0

[pid 649418] close(2) = 0

[pid 649418] exit_group(0) = ?

[pid 649418] +++ exited with 0 +++

[pid 649417] <... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 649418

[pid 649417] rt_sigaction(SIGINT, {sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 649417] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=649418, si_uid=1006, si_status=0, si_utime=0, si_stime=0} ---

[pid 649417] wait4(-1, 0x7fffb1f38050, WNOHANG, NULL) = -1 ECHILD (No child processes)

[pid 649417] rt_sigreturn({mask=[]}) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0

[pid 649417] clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f584c4b0a10) = 649682

strace: Process 649682 attached

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 649682] set_robust_list(0x7f584c4b0a20, 24) = 0

[pid 649417] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 649682] getpid( <unfinished ...>

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649682] <... getpid resumed>) = 649682

[pid 649417] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649682] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 649417] rt_sigprocmask(SIG_BLOCK, [CHLD], <unfinished ...>

[pid 649682] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 649417] <... rt_sigprocmask resumed>[], 8) = 0

[pid 649682] rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649417] rt_sigaction(SIGINT, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649682] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] <... rt_sigaction resumed>{sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, <unfinished ...>

[pid 649417] wait4(-1, <unfinished ...>

[pid 649682] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9576d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649682] execve("/usr/bin/sleep", ["sleep", "10"], 0x5577b9e20b60 /* 54 vars */) = 0

[pid 649682] brk(NULL) = 0x5591174fc000

[pid 649682] arch_prctl(0x3001 /* ARCH_??? */, 0x7fffce0decd0) = -1 EINVAL (Invalid argument)

[pid 649682] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6113dd6000

[pid 649682] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

[pid 649682] openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

[pid 649682] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

[pid 649682] mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6113dbe000

[pid 649682] close(3) = 0

[pid 649682] openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

[pid 649682] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

[pid 649682] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 649682] pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

[pid 649682] pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

[pid 649682] newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

[pid 649682] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 649682] mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f6113a00000

[pid 649682] mprotect(0x7f6113a28000, 2023424, PROT_NONE) = 0

[pid 649682] mmap(0x7f6113a28000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f6113a28000

[pid 649682] mmap(0x7f6113bbd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7f6113bbd000

[pid 649682] mmap(0x7f6113c16000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7f6113c16000

[pid 649682] mmap(0x7f6113c1c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f6113c1c000

[pid 649682] close(3) = 0

[pid 649682] mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f6113dbb000

[pid 649682] arch_prctl(ARCH_SET_FS, 0x7f6113dbb740) = 0

[pid 649682] set_tid_address(0x7f6113dbba10) = 649682

[pid 649682] set_robust_list(0x7f6113dbba20, 24) = 0

[pid 649682] rseq(0x7f6113dbc0e0, 0x20, 0, 0x53053053) = 0

[pid 649682] mprotect(0x7f6113c16000, 16384, PROT_READ) = 0

[pid 649682] mprotect(0x5591162b5000, 4096, PROT_READ) = 0

[pid 649682] mprotect(0x7f6113e10000, 8192, PROT_READ) = 0

[pid 649682] prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

[pid 649682] munmap(0x7f6113dbe000, 95179) = 0

[pid 649682] getrandom("\x76\xed\x66\x62\x5c\xcf\x79\x1b", 8, GRND_NONBLOCK) = 8

[pid 649682] brk(NULL) = 0x5591174fc000

[pid 649682] brk(0x55911751d000) = 0x55911751d000

[pid 649682] openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

[pid 649682] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

[pid 649682] mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f6113000000

[pid 649682] close(3) = 0

[pid 649682] clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=10, tv_nsec=0}, 0x7fffce0ded20) = 0

[pid 649682] close(1) = 0

[pid 649682] close(2) = 0

[pid 649682] exit_group(0) = ?

[pid 649682] +++ exited with 0 +++

[pid 649417] <... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 649682

[pid 649417] rt_sigaction(SIGINT, {sa_handler=0x5577b95a3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

[pid 649417] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 649417] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=649682, si_uid=1006, si_status=0, si_utime=0, si_stime=0} ---

[pid 649417] wait4(-1, 0x7fffb1f38090, WNOHANG, NULL) = -1 ECHILD (No child processes)

[pid 649417] rt_sigreturn({mask=[]}) = 0

[pid 649417] exit_group(0) = ?

[pid 649417] +++ exited with 0 +++

<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 649417

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, {sa_handler=0x5577b9578dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7f584c242520}, 8) = 0

ioctl(2, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

newfstatat(AT_FDCWD, "/home/zhangchen/.terminfo", 0x5577b9e26460, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/etc/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/lib/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/share/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

access("/etc/terminfo/x/xterm-256color", R_OK) = -1 ENOENT (No such file or directory)

access("/lib/terminfo/x/xterm-256color", R_OK) = 0

openat(AT_FDCWD, "/lib/terminfo/x/xterm-256color", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=3852, ...}, AT_EMPTY_PATH) = 0

read(3, "\36\2%\0&\0\17\0\235\1Z\6xterm-256color|xterm"..., 32768) = 3852

read(3, "", 28672) = 0

close(3) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(0, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

brk(0x5577b9e51000) = 0x5577b9e51000

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=649417, si_uid=1006, si_status=0, si_utime=0, si_stime=0} ---

wait4(-1, 0x7fffb1f38310, WNOHANG, NULL) = -1 ECHILD (No child processes)

rt_sigreturn({mask=[]}) = 0

read(255, "", 20) = 0

rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

exit_group(0) = ?

+++ exited with 0 +++

       像之前一样,首先strace进程创建子进程后调用execve进行替换(替换为bash)。从其中的getpid()=649416可以看出,该bash进程的PID为649416。从getppid()=649413可以看出,该bash进程父进程的PID为649413,这是strace进程的PID,而strace进程的父进程是终端内运行的bash。

        其中共存在三个clone,其中的第一个clone(****)=649417,展现了bash进程在读取到了小括号后,创建了一个子shell,并且子shell的PID为649417。但是在这之后并没有执行execve进行替换(如果是执行了脚本中的bash命令,则会首先clone并使用execve进行替换,注意这两者的区别)。

        第二个出现的clone是[pid 649417] clone(****)=649418,这表示子shell又创建了一个子进程,并且子进程的PID为649418。后面的[pid 649418] execve("/usr/bin/sleep", ["sleep", "20"], 0x5577b9e20b60 /* 54 vars */)=0展示了该子进程进行替换的过程。上面的两步,导致子shell进程创建了一个sleep子进程。

        第三个出现的clone是[pid 649417] clone(****)=649682,这表示子shell创建了第二个子进程,并且子进程的PID为649682。后面的[pid 649682] execve("/usr/bin/sleep", ["sleep", "10"], 0x5577b9e20b60 /* 54 vars */)=0展示了该子进程进行替换的过程。上面的两步,导致子shell进程创建了第二个sleep子进程。

        在运行strace命令时,我们可以在另一个终端利用ps命令观察到进程的创建情况,如下所示。注意其中的,sleep 10进程需要在sleep 20进程结束后才能观察到。

UID PID PPID C STIME TTY TIME CMD

zhangch+ 649413 645887 0 13:31 pts/15 00:00:00 strace -f bash test.sh

zhangch+ 649416 649413 0 13:31 pts/15 00:00:00 bash test.sh

zhangch+ 649417 649416 0 13:31 pts/15 00:00:00 bash test.sh

zhangch+ 649418 649417 0 13:31 pts/15 00:00:00 sleep 20

UID PID PPID C STIME TTY TIME CMD

zhangch+ 649413 645887 0 13:31 pts/15 00:00:00 strace -f bash test.sh

zhangch+ 649416 649413 0 13:31 pts/15 00:00:00 bash test.sh

zhangch+ 649417 649416 0 13:31 pts/15 00:00:00 bash test.sh

zhangch+ 649682 649417 0 13:32 pts/15 00:00:00 sleep 10

        可以从图中看出PID为649417的进程就是创建的子shell,这印证了我们的说法。

        下面我们尝试在小括号中只放一条命令,并观察结果。

​# 文件test.sh

(sleep 20)

        此时使用strace追踪的结果如下所示。

execve("/usr/bin/bash", ["bash", "test.sh"], 0x7fff4735b310 /* 55 vars */) = 0

brk(NULL) = 0x55611d9ff000

arch_prctl(0x3001 /* ARCH_??? */, 0x7fff55dc2db0) = -1 EINVAL (Invalid argument)

mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd7fcc46000

access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd7fcc2e000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libtinfo.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\0\0\0\0\0\0\0\0"..., 832) = 832

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=200136, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 203040, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd7fcbfc000

mmap(0x7fd7fcc0a000, 69632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe000) = 0x7fd7fcc0a000

mmap(0x7fd7fcc1b000, 57344, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1f000) = 0x7fd7fcc1b000

mmap(0x7fd7fcc29000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x2c000) = 0x7fd7fcc29000

close(3) = 0

openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fd7fc800000

mprotect(0x7fd7fc828000, 2023424, PROT_NONE) = 0

mmap(0x7fd7fc828000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7fd7fc828000

mmap(0x7fd7fc9bd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7fd7fc9bd000

mmap(0x7fd7fca16000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7fd7fca16000

mmap(0x7fd7fca1c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fd7fca1c000

close(3) = 0

mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd7fcbf9000

arch_prctl(ARCH_SET_FS, 0x7fd7fcbf9740) = 0

set_tid_address(0x7fd7fcbf9a10) = 661035

set_robust_list(0x7fd7fcbf9a20, 24) = 0

rseq(0x7fd7fcbfa0e0, 0x20, 0, 0x53053053) = 0

mprotect(0x7fd7fca16000, 16384, PROT_READ) = 0

mprotect(0x7fd7fcc29000, 16384, PROT_READ) = 0

mprotect(0x55611c4a1000, 16384, PROT_READ) = 0

mprotect(0x7fd7fcc80000, 8192, PROT_READ) = 0

prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

munmap(0x7fd7fcc2e000, 95179) = 0

openat(AT_FDCWD, "/dev/tty", O_RDWR|O_NONBLOCK) = 3

close(3) = 0

getrandom("\x1b\x51\x8d\x2a\xb7\x02\xc5\xd7", 8, GRND_NONBLOCK) = 8

brk(NULL) = 0x55611d9ff000

brk(0x55611da20000) = 0x55611da20000

openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fd7fbe00000

close(3) = 0

openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=27002, ...}, AT_EMPTY_PATH) = 0

mmap(NULL, 27002, PROT_READ, MAP_SHARED, 3, 0) = 0x7fd7fcc3f000

close(3) = 0

futex(0x7fd7fca1ba6c, FUTEX_WAKE_PRIVATE, 2147483647) = 0

getuid() = 1006

getgid() = 1002

geteuid() = 1006

getegid() = 1002

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

ioctl(-1, TIOCGPGRP, 0x7fff55dc2ca4) = -1 EBADF (Bad file descriptor)

sysinfo({uptime=7262941, loads=[34464, 38112, 38752], totalram=135030112256, freeram=18682093568, sharedram=3393511424, bufferram=2823491584, totalswap=31999389696, freeswap=16653172736, procs=4084, totalhigh=0, freehigh=0, mem_unit=1}) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=0}, 8) = 0

rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigaction(SIGQUIT, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

uname({sysname="Linux", nodename="liubo-SYS-7048GR-TR", ...}) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

getpid() = 661035

getppid() = 661032

newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/opt/miniconda3/bin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/sbin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/local/bin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/sbin/bash", 0x7fff55dc28e0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", X_OK) = 0

newfstatat(AT_FDCWD, "/usr/bin/bash", {st_mode=S_IFREG|0755, st_size=1396520, ...}, 0) = 0

geteuid() = 1006

getegid() = 1002

getuid() = 1006

getgid() = 1002

access("/usr/bin/bash", R_OK) = 0

getpid() = 661035

getppid() = 661032

getpid() = 661035

getppid() = 661032

getpgrp() = 661032

ioctl(2, TIOCGPGRP, [661032]) = 0

rt_sigaction(SIGCHLD, {sa_handler=0x55611c3b6d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, 8) = 0

prlimit64(0, RLIMIT_NPROC, NULL, {rlim_cur=514857, rlim_max=514857}) = 0

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

openat(AT_FDCWD, "test.sh", O_RDONLY) = 3

newfstatat(AT_FDCWD, "test.sh", {st_mode=S_IFREG|0777, st_size=11, ...}, 0) = 0

ioctl(3, TCGETS, 0x7fff55dc2c40) = -1 ENOTTY (Inappropriate ioctl for device)

lseek(3, 0, SEEK_CUR) = 0

read(3, "(sleep 20)\n", 80) = 11

lseek(3, 0, SEEK_SET) = 0

prlimit64(0, RLIMIT_NOFILE, NULL, {rlim_cur=1024*1024, rlim_max=1024*1024}) = 0

fcntl(255, F_GETFD) = -1 EBADF (Bad file descriptor)

dup2(3, 255) = 255

close(3) = 0

fcntl(255, F_SETFD, FD_CLOEXEC) = 0

fcntl(255, F_GETFL) = 0x8000 (flags O_RDONLY|O_LARGEFILE)

newfstatat(255, "", {st_mode=S_IFREG|0777, st_size=11, ...}, AT_EMPTY_PATH) = 0

lseek(255, 0, SEEK_CUR) = 0

read(255, "(sleep 20)\n", 11) = 11

rt_sigprocmask(SIG_BLOCK, NULL, [], 8) = 0

rt_sigprocmask(SIG_BLOCK, [INT TERM CHLD], [], 8) = 0

clone(child_stack=NULL, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7fd7fcbf9a10) = 661036

strace: Process 661036 attached

[pid 661035] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 661035] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 661036] set_robust_list(0x7fd7fcbf9a20, 24 <unfinished ...>

[pid 661035] rt_sigprocmask(SIG_SETMASK, [], <unfinished ...>

[pid 661036] <... set_robust_list resumed>) = 0

[pid 661035] <... rt_sigprocmask resumed>NULL, 8) = 0

[pid 661035] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 661036] getpid( <unfinished ...>

[pid 661035] rt_sigaction(SIGINT, {sa_handler=0x55611c3b8dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, <unfinished ...>

[pid 661036] <... getpid resumed>) = 661036

[pid 661035] <... rt_sigaction resumed>{sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661035] wait4(-1, <unfinished ...>

[pid 661036] close(255) = 0

[pid 661036] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 661036] rt_sigaction(SIGTSTP, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGTTIN, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGTTOU, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_IGN, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=0x55611c3b6d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGINT, {sa_handler=0x55611c3e3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGCHLD, {sa_handler=0x55611c3b6d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

[pid 661036] rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

[pid 661036] newfstatat(AT_FDCWD, ".", {st_mode=S_IFDIR|0750, st_size=4096, ...}, 0) = 0

[pid 661036] newfstatat(AT_FDCWD, "/home/zhangchen/.vscode-server/cli/servers/Stable-dc96b837cf6bb4af9cd736aa3af08cf8279f7685/server/bin/remote-cli/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/home/zhangchen/.conda/envs/DL/bin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/opt/miniconda3/condabin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/opt/miniconda3/bin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/usr/local/sbin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/usr/local/bin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/usr/sbin/sleep", 0x7fff55dc25c0, 0) = -1 ENOENT (No such file or directory)

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] geteuid() = 1006

[pid 661036] getegid() = 1002

[pid 661036] getuid() = 1006

[pid 661036] getgid() = 1002

[pid 661036] access("/usr/bin/sleep", X_OK) = 0

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] geteuid() = 1006

[pid 661036] getegid() = 1002

[pid 661036] getuid() = 1006

[pid 661036] getgid() = 1002

[pid 661036] access("/usr/bin/sleep", R_OK) = 0

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] geteuid() = 1006

[pid 661036] getegid() = 1002

[pid 661036] getuid() = 1006

[pid 661036] getgid() = 1002

[pid 661036] access("/usr/bin/sleep", X_OK) = 0

[pid 661036] newfstatat(AT_FDCWD, "/usr/bin/sleep", {st_mode=S_IFREG|0755, st_size=35328, ...}, 0) = 0

[pid 661036] geteuid() = 1006

[pid 661036] getegid() = 1002

[pid 661036] getuid() = 1006

[pid 661036] getgid() = 1002

[pid 661036] access("/usr/bin/sleep", R_OK) = 0

[pid 661036] rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=0x55611c3e3410, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGQUIT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] rt_sigaction(SIGCHLD, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, {sa_handler=0x55611c3b6d10, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART, sa_restorer=0x7fd7fc842520}, 8) = 0

[pid 661036] execve("/usr/bin/sleep", ["sleep", "20"], 0x55611da10b60 /* 54 vars */) = 0

[pid 661036] brk(NULL) = 0x55fbbd90b000

[pid 661036] arch_prctl(0x3001 /* ARCH_??? */, 0x7fff5bb3c790) = -1 EINVAL (Invalid argument)

[pid 661036] mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1c1af61000

[pid 661036] access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)

[pid 661036] openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3

[pid 661036] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=95179, ...}, AT_EMPTY_PATH) = 0

[pid 661036] mmap(NULL, 95179, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1c1af49000

[pid 661036] close(3) = 0

[pid 661036] openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3

[pid 661036] read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0P\237\2\0\0\0\0\0"..., 832) = 832

[pid 661036] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 661036] pread64(3, "\4\0\0\0 \0\0\0\5\0\0\0GNU\0\2\0\0\300\4\0\0\0\3\0\0\0\0\0\0\0"..., 48, 848) = 48

[pid 661036] pread64(3, "\4\0\0\0\24\0\0\0\3\0\0\0GNU\0\302\211\332Pq\2439\235\350\223\322\257\201\326\243\f"..., 68, 896) = 68

[pid 661036] newfstatat(3, "", {st_mode=S_IFREG|0755, st_size=2220400, ...}, AT_EMPTY_PATH) = 0

[pid 661036] pread64(3, "\6\0\0\0\4\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0@\0\0\0\0\0\0\0"..., 784, 64) = 784

[pid 661036] mmap(NULL, 2264656, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f1c1ac00000

[pid 661036] mprotect(0x7f1c1ac28000, 2023424, PROT_NONE) = 0

[pid 661036] mmap(0x7f1c1ac28000, 1658880, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x28000) = 0x7f1c1ac28000

[pid 661036] mmap(0x7f1c1adbd000, 360448, PROT_READ, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1bd000) = 0x7f1c1adbd000

[pid 661036] mmap(0x7f1c1ae16000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x215000) = 0x7f1c1ae16000

[pid 661036] mmap(0x7f1c1ae1c000, 52816, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f1c1ae1c000

[pid 661036] close(3) = 0

[pid 661036] mmap(NULL, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f1c1af46000

[pid 661036] arch_prctl(ARCH_SET_FS, 0x7f1c1af46740) = 0

[pid 661036] set_tid_address(0x7f1c1af46a10) = 661036

[pid 661036] set_robust_list(0x7f1c1af46a20, 24) = 0

[pid 661036] rseq(0x7f1c1af470e0, 0x20, 0, 0x53053053) = 0

[pid 661036] mprotect(0x7f1c1ae16000, 16384, PROT_READ) = 0

[pid 661036] mprotect(0x55fbbc349000, 4096, PROT_READ) = 0

[pid 661036] mprotect(0x7f1c1af9b000, 8192, PROT_READ) = 0

[pid 661036] prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0

[pid 661036] munmap(0x7f1c1af49000, 95179) = 0

[pid 661036] getrandom("\x91\x51\x73\xc6\x31\xd4\xef\x2e", 8, GRND_NONBLOCK) = 8

[pid 661036] brk(NULL) = 0x55fbbd90b000

[pid 661036] brk(0x55fbbd92c000) = 0x55fbbd92c000

[pid 661036] openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3

[pid 661036] newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=8872448, ...}, AT_EMPTY_PATH) = 0

[pid 661036] mmap(NULL, 8872448, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f1c1a200000

[pid 661036] close(3) = 0

[pid 661036] clock_nanosleep(CLOCK_REALTIME, 0, {tv_sec=20, tv_nsec=0}, 0x7fff5bb3c7e0) = 0

[pid 661036] close(1) = 0

[pid 661036] close(2) = 0

[pid 661036] exit_group(0) = ?

[pid 661036] +++ exited with 0 +++

<... wait4 resumed>[{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 661036

rt_sigaction(SIGINT, {sa_handler=SIG_DFL, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, {sa_handler=0x55611c3b8dc0, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x7fd7fc842520}, 8) = 0

ioctl(2, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

getuid() = 1006

geteuid() = 1006

getgid() = 1002

getegid() = 1002

newfstatat(AT_FDCWD, "/home/zhangchen/.terminfo", 0x55611da162f0, 0) = -1 ENOENT (No such file or directory)

newfstatat(AT_FDCWD, "/etc/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/lib/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

newfstatat(AT_FDCWD, "/usr/share/terminfo", {st_mode=S_IFDIR|0755, st_size=4096, ...}, 0) = 0

access("/etc/terminfo/x/xterm-256color", R_OK) = -1 ENOENT (No such file or directory)

access("/lib/terminfo/x/xterm-256color", R_OK) = 0

openat(AT_FDCWD, "/lib/terminfo/x/xterm-256color", O_RDONLY) = 3

newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=3852, ...}, AT_EMPTY_PATH) = 0

read(3, "\36\2%\0&\0\17\0\235\1Z\6xterm-256color|xterm"..., 32768) = 3852

read(3, "", 28672) = 0

close(3) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0

ioctl(1, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

ioctl(0, TIOCGWINSZ, {ws_row=21, ws_col=200, ws_xpixel=0, ws_ypixel=0}) = 0

brk(0x55611da41000) = 0x55611da41000

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=661036, si_uid=1006, si_status=0, si_utime=0, si_stime=0} ---

wait4(-1, 0x7fff55dc2210, WNOHANG, NULL) = -1 ECHILD (No child processes)

rt_sigreturn({mask=[]}) = 0

read(255, "", 11) = 0

rt_sigprocmask(SIG_BLOCK, [CHLD], [], 8) = 0

rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0

exit_group(0) = ?

+++ exited with 0 +++

        令人疑惑的是,其中只有一个clone调用,即clone()=661036,这是为了创建子shell,而在这之后,并没有像上面那样再次为了sleep命令首先clone后调用execve替换的操作,而是直接进行了execve替换,即[pid 661036] execve("/usr/bin/sleep", ["sleep", "20"], 0x55611da10b60 /* 54 vars */)=0,这是bash进行了优化,即当其发现子shell中只需执行一条命令时,其直接调用execve替换,省略了一个clone创建子进程的过程。这么做的结果会导致我们无法使用ps观察到子shell,因为其已经被替换为sleep子进程,如下所示。

UID PID PPID C STIME TTY TIME CMD

zhangch+ 661032 660255 0 13:52 pts/20 00:00:00 strace -f bash test.sh

zhangch+ 661035 661032 0 13:52 pts/20 00:00:00 bash test.sh

zhangch+ 661036 661035 0 13:52 pts/20 00:00:00 sleep 20

        子shell不是一般的子进程,它能继承父进程的所有变量,无论其是否导出,因为只执行了clone的调用,下面是一个例子。

​# 文件test.sh

#############################

a=1

(echo $a)

#############################

$ bash test.sh

输出:1

        可能有人会认为这里是$a在父进程就进行了变量展开,这是错误的,一个命令只有在执行前才会进行变量展开,而不是解析时,详细信息可以参考下面的这篇文章。Linux:执行命令的命令eval与Bash解析命令的方式_linux bash解析-CSDN博客

        我们可以换一个思路,以便绕开上面的困境,Bash提供了三个特殊的变量,分别是$BASHPID、$BASH_SUBSHELL和$SHLVL。

        $BASHPID是对于每个shell都是唯一的(其实就是进程号),这包括使用bash命令创建的shell和上面谈到的子shell,例如下面的脚本。

​# 文件test.sh

#############################

echo $BASHPID

(echo $BASHPID)

#############################

$ bash test.sh

输出:

681607

681608

        $BASH_SUBSHELL是专门为子shell设计的变量,对于普通的shell,其值为0,对于一层子shell,其值为1...以此类推,例如下面的脚本。

​# 文件test.sh

#############################

echo $BASH_SUBSHELL

(echo $BASH_SUBSHELL;(echo $BASH_SUBSHELL))

#############################

$ bash test.sh

输出:

0

1

2

         $SHLVL是专门为普通递归创建的shell设计的环境变量,当进入了一个login shell,其值会被设定为1,每当创建了一个普通的nonlogin shell进程时,继承后的$SHLVL变量值会加1,如下所示。

$ su -l *****

Password:

$ echo $SHLVL

1

$ bash

$ echo $SHLVL

2

​# 文件test.sh

#############################

echo $SHLVL

(echo $SHLVL)

#############################

$ bash test.sh

输出:

2

2



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。