Skip to main content
Clarifying as per comment.
Source Link
kenorb
  • 22.1k
  • 18
  • 149
  • 172

Basically I want to know how a command is run in background, in bash -c, and directly in bash shell in terms of execve() and clone().

Basically I want to know how a command is run in background, in bash -c, and directly in bash shell in terms of execve() and clone().

How to understand the output of trainingtracing running an external command in background via strace

deleted 733 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
    $ 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?

    $ sudo strace -f -e trace=process -p 6913
    Process 6913 attached
    clone(Process 9143 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 attached
    [pid  9143] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
    [pid  9143] arch_prctl(ARCH_SET_FS, 0x7f672c6f4740) = 0
    [pid  9144] clone(child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 9145
    Process 9145 attached
    [pid  9143] exit_group(0)               = ?
    [pid  9143] +++ exited with 0 +++
    [pid  6913] --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9143, si_status=0, si_utime=0, si_stime=0} ---
    [pid  6913] wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 9143
    [pid  9144] wait4(-1,  <unfinished ...>
    [pid  6913] wait4(-1, 0x7ffea6780f18, WNOHANG|WSTOPPED|WCONTINUED, NULL) = 0
    [pid  9145] execve("/bin/sed", ["sed", "s:\\([^/]\\)[^/]*/:\\1/:g"], [/* 66 vars */]) = 0
    [pid  9145] arch_prctl(ARCH_SET_FS, 0x7f6a38f9a840) = 0
    [pid  9145] exit_group(0)               = ?
    [pid  9145] +++ 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, WNOHANG, NULL) = -1 ECHILD (No child processes)
    [pid  9144] exit_group(0)               = ?
    [pid  9144] +++ exited with 0 +++
    --- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=9144, si_status=0, si_utime=0, si_stime=0} ---
    wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 9144
    wait4(-1, 0x7ffea6780f18, 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?

$ sudo strace -f -e trace=process -p 6913
Process 6913 attached
clone(Process 12931 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 12931
[pid 12931] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
[pid 12931] arch_prctl(ARCH_SET_FS, 0x7f530c5ee740) = 0
[pid 12931] exit_group(0)               = ?
[pid 12931] +++ exited with 0 +++
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12931, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WNOHANG|WSTOPPED|WCONTINUED, NULL) = 12931
wait4(-1, 0x7ffea6780718, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
 

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 -e trace=process -p 6913
[sudo] password for t: 
Process 6913 attached
clone(Process 12918 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD, child_tidptr=0x7f457c05ca10) = 12918
[pid  6913] wait4(-1,  <unfinished ...>
[pid 12918] execve("/bin/date", ["date"], [/* 66 vars */]) = 0
[pid 12918] arch_prctl(ARCH_SET_FS, 0x7ff00c632740) = 0
[pid 12918] exit_group(0)               = ?
[pid 12918] +++ exited with 0 +++
<... wait4 resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], WSTOPPED|WCONTINUED, NULL) = 12918
--- SIGCHLD {si_signo=SIGCHLD, si_code=CLD_EXITED, si_pid=12918, si_status=0, si_utime=0, si_stime=0} ---
wait4(-1, 0x7ffea6781518, WNOHANG|WSTOPPED|WCONTINUED, NULL) = -1 ECHILD (No child processes)
deleted 3413 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
edited title
Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
added 11 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
added 30 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
deleted 388 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
deleted 388 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
added 200 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
added 200 characters in body
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading
Source Link
Tim
  • 106.8k
  • 234
  • 651
  • 1.1k
Loading