Normal termination occurs in five ways:
exit
_exit
or _Exit
pthread_exit
from the last threadAbnormal termination occurs in three ways:
abort
#include <stdlib.h>
void exit(int status);
void _Exit(int status);
#include <unistd.h>
void _exit(int status);
_Exit
and _exit
return to kernel immediatelyexit
performs certain cleanup processing and then return to kernel (call fclose
to all open stream for example)exit(0)
is the same as return 0
in the main
function.
When exit status undefined:
main
does a return without a return valuemain
function is not declared to return an integerIf the return type of main
is an integer, both explicit return 0 or implicit return ("falls off the end") will make the exit status 0
ISO C 1999 implies that the return type of main defaults to int
.
main() {} // Return value is 0 in c99, undefined before c99
extern char **environ;
This section simply describe the memory layout of a C program. Refer to CSAPP for more information.
Although sbrk
can expand or contract the memory of a process, most versions of malloc
and free never decrease their memory size. The space that we free is available for a later allocation, but the freed space is not usually returned to the kernel; instead, that space is kept in the malloc
pool.
This may explain why the memory consumed by a process rarely decreases.
Warning: don't use the putenv
function. putenv
is free to place the *name=value* string passed to it directly into the environment, without copy it. So it would be an error to pass a string allocated on the stack, since the memory would be reused after we return from the current function.
setjmp
and longjmp
FunctionsRefer to the code files setjmp_variables.c
.
When we return to the code as a result of the longjmp
, do the variables have values corresponding to those when the setjmp
was previous called (i.e. their values rolled back), or are their values left alone?
volatile
attribute.longjmp
is executed.Q: 7.4 Some UNIX system implementations purposely arrange that, when a program is executed, location 0 in the data segment is not accessible. why?
A: This provides a way to terminate the process when it tries to dereference a null pointer, a common C programming error.