Chapter 25 Process Termination
Objectives
We would like to :
- Understand the system calls related to process termination.
Notes
- This is chapter 25, read it.
- A bash note:
- the variable ? contains the exit code of the last command run
-
echo $?
will print that.
- See simpleExit.cpp
- In general
- A zero on exit indicates success
- Anything else indicates an error.
- Exit codes are only 8 bits. (0 to 255)
- But apparently shells only expect 7 bits (0 to 127)
- Bash at least adds 128 to a exit status when the process is killed by a signal.
- The exit code is delivered to the parent process.
- See the man page for make
- We will see more when we get to wait.
- There are an amazing number of ways to terminate a process
- normal termination is when the process requests an exit.
- When this occurs, exit handlers are called.
- abnormal termination is when this process is not followed.
- return, return int, exit(int),
- These are normal exit conditions.
- return with no arguments returns 0.
- This was not always the case, but it is now.
- All three call eventually call _exit();
-
void _exit(int status)
- Returns
satus & 0xFF
- Children are given a new parent (1), or a subreaper
- The parent is sent a SIGCHLD
- _exit does not call exit functions.
- This is sort of a bail, turn off the engine, quit now function
- Abort
-
void abort(void)
- Raises SIGABRT
- Which causes instant termination.
- This is an abnormal termination.
- This is sort of like crashing into a wall.
- Throwing an uncaught exception
- This is similar to abort.
- This is an abnormal termination.
- In fact, as far as I can tell, it ends in an abort.
- kill -l
- ON 533 he provides a list of things both forms of termination do
- Many involve things we have yet to discuss
- But closing open file descriptors is one.
- Exit handlers
- These are user functions that get called during the normal exit process.
- They allow for
- General clean up.
- Unloading libraries.
-
int atexit(void (*func)(void));
- Needs stdlib.h
- adds a function to the stack of exit functions.
- Functions are called in the reverse order in which they are registered.
- This makes sense, just like unwinding a stack.
- There can be at least 32 of these, but perhaps more.
- These are passed on to child functions
- But cleared on exec.
- DO NOT CALL EXIT in an exit function.
- This may result in infinite recursion.
- These are not called on abnormal termination.
- Look at the code in myExit.cpp