Ch 7 Process Environment

Ch 7 Process Environment

Startup routine:

exit(main(argc, argv))

Process Termination Methods

Normal termination:

  1. Return from main
  2. Call exit, _exit, _Exit
  3. Last thread returns from its start routine
  4. Call pthread_exit from the last thread

Abnormal termination:

  1. Call abort
  2. Receive a signal (e.g., division by zero)
  3. Last thread responds to cancellation request

exit

exit() first calls termination handlers, then closes all open streams

atexit() can register up to 32 functions, though actual implementations may support more The only way the kernel makes a program execute is by calling exec functions, the only way a process voluntarily terminates is by explicitly or implicitly calling exit, and it can also be involuntarily terminated by a signal

Environment Variables

Environment variables can be found by searching the environment table environ is an environment pointer pointing to a NULL-terminated string where each KV pair is separated by \0, represented as K=V \0 K=V

getenv and putenv can also be used for access Setting environment variables only affects the current process and any child processes it generates and invokes unsetenv | clearenv Due to the arrangement of the environment table within the process, expansion upward or downward cannot occur when adding new variables. To add new variables, malloc must be used to allocate a pointer table, and the original environment table must be copied to the newly allocated space. If not the first addition, realloc is used to allocate space for one additional pointer beyond the original size, then stored

Program Memory Layout

High
Command line arguments and environment variables Stack Heap Uninitialized data (bss) Initialized data — loaded by exec from program file Program text — loaded by exec from program file Low

Storage Allocation

malloc allocates a specified number of bytes of storage area, initial value is undefined calloc allocates storage space for a specified number of objects of specified length, initializing every bit to 0 realloc increases or decreases the length of an already allocated region, may append or trigger reallocation and copying

free releases the storage space pointed to by the pointer, the released space is typically returned to the available storage pool

These functions are usually implemented using sbrk(), expanding and contracting the heap Most implementations allocate slightly more storage space than requested, with extra space used to record management information, allocation block length, and pointers to other allocation blocks

Memory leak: if malloc is called but forget to call free function, the storage space occupied by the process will continuously increase until there is no more free space, paging overhead will cause performance degradation

Functions setjmp and longjmp

goto jumps cannot cross function boundaries setjmp and longjmp can handle error conditions occurring in deeply nested function calls Call setjmp at the location where you want to return to, using a global variable to help record and restore all stack state information Since one setjmp can correspond to multiple longjmp calls, different return values are used to determine which longjmp returned. The essence is that when jumping back, nested stack frames are discarded and the stack is rewritten