160 likes | 276 Views
Chapter 7 Process Environment. Chien -Chung Shen CIS, UD cshen@cis.udel.edu. Introduction. “Environment” of a process How the main() function is called when the corresponding program is executed How command-line arguments are passed to a program Process termination
E N D
Chapter 7Process Environment Chien-Chung Shen CIS, UD cshen@cis.udel.edu
Introduction • “Environment” of a process • How the main() function is called when the corresponding program is executed • How command-line arguments are passed to a program • Process termination • Memory layout of a process • Memory allocation • Environment variables
main()Function • A C program starts execution from its main() function • Prototype of main() int main(intargc, char *argv[]); • argc: # of command-line arguments • argv: an array of pointers to the arguments • When a C program is executed by the kernel via exec(), a special start-up routine is called beforemain()is called
main()Function • The executable file (e.g., a.out) of the program specifies this start-up routine as the starting address for the program • This is set up by link editor invoked by C compiler • The start-up routine takes values from the kernel (command-line arguments and environment variables), sets things up, and calls main()
Process Termination • Normal termination • Return from main() • Call exit() • Call _exit() or _Exit() • Return of the last thread from its start routine • Call pthread_exit()from the last thread • Abnormal termination • Call abort() • Receive a signal • Response of the last thread to a cancellation request
Exit Functions • #include <stdlib.h> // ISO C void exit(intstatus); void _Exit(intstatus); • #include <unistd.h> // POSIX.1 void _exit(intstatus); • exit() performs clean shutdown of standard I/0 library: call flcose() for all open streams causing all buffered output data to be flushed (written to files)
Exit Status • If (a) exit function is called without a status, (b) main() does a return without a return value, or (c) main() is not declared to return an integer, exit status of the process is undefined • If return type of main() is integer, and main() “falls off the end” (implicit return), exit status is 0 • exit(0)== return(0) • $ echo $? % print exit status
atexit() Function • A process can “register” up to 32 functions (termed exit handlers) that are automatically called (in reverse order of registration) by exit() • exit()calls exit handlers and then closes all open streams (via fclose()) • Figure 7.3
How C Program Starts and Terminates • The only way a C program is executed by the kernel is when exec() is called • The only way a process voluntarily terminates is when _exit() or _Exit() is called, either explicitly or implicitly (by calling exit()) • A process can be involuntarily terminated by signals
Command-Line Arguments • When a program is executed, the process that does the exec() can pass command-line arguments to the new program • Code in Figure 7.4 • argv[argc]is a null pointer for (i = 0; argv[i] != NULL; i++) for (i = 0; I < argc; i++)
Environment List • Each program is passed an environment list • extern char **environ; • an array of character pointers, containing • the address of a null-terminated C string
Environment List • Each program is also passed an environment list (an array of character pointers, with each pointer containing the address of a null-terminated C string) • The address of the array of pointers is contained in the global variable environ extern char **environ; • Environment pointer (environ), environment list, environment strings • Each environment string is a name=value string • getenv()/putenv()