$ sudo strace -f -e trace=process -p 6913
Process 6913 attached
clone(Process 914312931 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 9143
[pid 6913] clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 9144
Process 9144 attached12931
[pid 9143]12931] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
[pid 9143]12931] arch_prctl(ARCH_SET_FS, 0x7f672c6f47400x7f530c5ee740) = 0
[pid 9144] clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 9145
Process 9145 attached
[pid 9143]12931] exit_group(0) = ?
[pid 9143]12931] +++ exited with 0 +++
[pid 6913] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9143si_pid=12931, si_status=0, si_utime=0, si_stime=0} ---
[pid 6913] wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 914312931
wait4(-1, 0x7ffea6780718, WNOHANG|WSTOPPED|WCONTINUED, NULL) [pid= -1 9144]ECHILD wait4(-1,No child <unfinishedprocesses)
...>
The bash shell 6913 clone() to create subprocess 12931.
Then the subprocess 12931 execve() date and exits.
Questions:
It is said that running a command in background makes the command run in a subshell of the original shell.
Does that mean that the
subshell (here 12931) should run the command, in the same way as the
original shell 6913 runs the command directly (see below for its tracing output)?
If yes, why doesn't 12931
clone() itself, and then its clone execve() date? (actually 12931execve() date directly without clone() itself)
When running date instead of date &, the output of tracing the bash shell:
$ sudo strace -f [pid-e trace=process 6913]-p wait46913
[sudo] password for t:
Process 6913 attached
clone(-1,Process 0x7ffea6780f1812918 attached
child_stack=0, WNOHANG|WSTOPPED|WCONTINUEDflags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, NULLchild_tidptr=0x7f457c05ca10) = 012918
[pid 6913] wait4(-1, [pid <unfinished 9145]...>
[pid 12918] execve("/bin/sed", ["sed"date", "s:\\([^/]\\)[^/]*/:\\1/:g"]["date"], [/* 66 vars */]) = 0
[pid 9145]12918] arch_prctl(ARCH_SET_FS, 0x7f6a38f9a8400x7ff00c632740) = 0
[pid 9145]12918] exit_group(0) = ?
[pid 9145]12918] +++ exited with 0 +++
[pid 9144] <... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0, NULL) = 9145
[pid 9144] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9145, si_status=0, si_utime=0, si_stime=0} ---
[pid 9144] wait4(-1, 0x7ffea6780c58, WNOHANGWSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
[pid 9144] exit_group(0) = ?
[pid 9144] +++ exited with 0 +++12918
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9144si_pid=12918, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 9144
wait4(-1, 0x7ffea6780f180x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
The bash shell 6913 clone() twice to create subprocesses 9143 and 9144.
Then the subprocess 9143 execve() date and exits.
The subprocess 9144 clone() to creat subsubprocess 9145 before
9143 exits, and then subsubprocess 9145 execve() sed after 9143
exits.
Questions:
It is said that running a command in background makes the command run in a subshell of the original shell. Does that mean that the
subshell (here 9143) should run the command, in the same way as the
original shell 6913 runs the command directly (see below for its tracing output)? If yes, why doesn't 9143
clone() itself, and then its clone execve() date? (actually 9143 execve() date directly without clone() itself)
Why does 6913 make two clones 9143 and 9144? What does the other clone 9144 do?