The actual system call

We have seen several APIs that perform the work of having the parent process wait until the child changes state (dies, or stops, or resumes after stop):

  • wait
  • waitpid
  • waitid
  • wait3
  • wait4

Interestingly, and similar to the situation with the exec family APIs, the Linux implementation is such that most of the preceding APIs are library (glibc) wrappers: The fact is that, on the Linux OS, of all the preceding APIs, wait4(2) is the actual system call API.

Performing an strace(1) on a program that uses one of the wait APIs proves the point (we strace our simpsh_v1 program, which calls wait):

$ strace -e trace=process -o strc.txt ./simpsh_v1 
>> ps
PID TTY TIME CMD
14874 pts/6 00:00:00 bash
27248 pts/6 00:00:00 strace
27250 pts/6 00:00:00 simpsh_v1
27251 pts/6 00:00:00 ps
>> quit
$

This is the output of strace:

execve("./simpsh_v1", ["./simpsh_v1"], 0x7fff79a424e0 /* 56 vars */) = 0 
arch_prctl(ARCH_SET_FS, 0x7f47641fa4c0) = 0
clone(child_stack=NULL,
flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x7f47641fa790) = 27251
wait4(-1, NULL, 0, NULL) = 27251
[...]

While discussing performing an strace, another interesting question does arise: if you strace an application that calls fork(2)after the fork API, will strace trace the execution path of the child process as well? By default, no, but just pass along the -f option, and it will!

The man page on strace(1) says this:

-f           Trace child processes as they are created by currently traced processes as a result of the fork(2), vfork(2) and clone(2) system calls. ...

In a similar vein, systems programmers are probably aware of the tremendously powerful GNU debugger—GDB. If one is debugging a multiprocess application with gdb(1), how can one request GDB which process's execution path to follow after encountering a fork in the instruction stream? The setting is called follow-fork-mode: in gdb; here, we show an example of setting the mode to child:

​(gdb) show follow-fork-mode 
Debugger response to a program call of fork or vfork is "parent".
(gdb) set follow-fork-mode child
(gdb)
With respect to GDB: Debugging multi-process applications with GDB: Using the GDB attach <PID> command is useful to attach to another process (say, the child). GDB also provides a powerful catch command; see help catch in GDB for more details.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset