Process Termination
- This is chapter 25, you should read it.
- exit, _exit, return, abort and exit handlers.
- exit
- cstdlib, or stdlib.h
- This is the normal exit function for C
- void exit(int status);
- Two standard return values EXIT_SUCCESS, EXIT_FAILURE,
- Use of numbers larger than 127 may cause issues with the shell.
- Exit handlers are called.
- This should not return
- For the c-99 standard, programs not calling exit, exit with a 0.
- Before that it was the value of the last statement executed.
- _exit
- unistd.h
- void _exit(int status);
- exit immediately, do not call exit handlers.
- List of actions when exit is called
- Open file descriptors are closed.
- Directory streams and other types of streams are closed as well.
- File locks are released
- Shared memory is freed.
- Sighup is sent to any processes in a process group, if this is the process leader.
- Other things we have yet to discuss.
- abort
- cstdlib
- void abort(void);
- If sigabort is blocked, it unblocks it.
- Then it raises the abort signal for the calling process.
- This is an abnormal termination of a process.
- Exit handlers
- These are mechanisms to clean up memory, libraries, ...
- Only called from exit, not _exit.
- Not called during abnormal termination.
- There are two different mechanisms.
- But they can be freely mixed.
- Exit handlers are called in reverse order from when they were registered (stack)
- And even added during an exit handler call.
- These additions are pushed on the top of the "stack".
- If an exit handler fails to return, other exit handlers are not called
- The exit handler called _exit
- The exit handler caused a signal to be raised, abort or some signal causing error
- Another signal was received which caused termination
- It is undefined for an exit handler to call exit.
- atexit
- cstdlib.
- int atexit(void (*function)(void));
- In this case, we are passing a function pointer.
- The function must be a void function that takes no parameters.
- Child process inhered registered exit functions.
- on_exit()
- cstdlib.
- This is mostly a linux function.
- int on_exit(void (*function)(int , void *), void *arg);
- the function receives the exit value
- And a pointer to an argument.