What does WEXITSTATUS(status) return?

WEXITSTATUS(stat_val) is a macro (so in fact it does not “return” something, but “evaluates” to something). For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use. The implementation of this macro might differ from one C-implementation to the …

Read more

Why fork() works the way it does

This is due to historical reasons. As explained at https://www.bell-labs.com/usr/dmr/www/hist.html, very early Unix did have neither fork() nor exec*(), and the way the shell executed commands was: Do the necessary initialization (opening stdin/stdout). Read a command line. Open the command, load some bootstrap code and jump to it. The bootstrap code read the opened command, …

Read more

How to kill a child process by the parent process?

Send a SIGTERM or a SIGKILL to it: http://en.wikipedia.org/wiki/SIGKILL http://en.wikipedia.org/wiki/SIGTERM SIGTERM is polite and lets the process clean up before it goes, whereas, SIGKILL is for when it won’t listen >:) Example from the shell (man page: http://unixhelp.ed.ac.uk/CGI/man-cgi?kill ) kill -9 pid In C, you can do the same thing using the kill syscall: kill(pid, …

Read more