180 likes | 209 Views
Learn about how the main function is executed, passing command-line arguments, process termination, memory layout, and more in C programming. Understand exit functions, environment variables, and memory allocation.
E N D
Chapter 7Process Environment Chien-Chung Shen CIS/UD cshen@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 • The start-up routine is also written so that if main() returns, exit() is called • if the start-up routine were coded in C (it is often coded in assembly language) the call to main look like exit(main(argc, argv));
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 fclose() for all open streams causing all buffered output data to be flushed (written to files) • status: exit status
Exit Status • If • any exist functionis called without a status • main() does a return without a return value • 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) • Fig. 7.1 APUE/environ/hello1.c • $ echo $? % print exit status • add exit() to hello1.c • gcc -std=c99 hello1.c (change 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()) • Fig. 7.3 (APUE/environ/doatexit.c) • An exit handler is called once for each time it is registered
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 • Fig. 7.4 (APUE/environ/echoarg.c) • argv[argc]is a null pointer for (i = 0; argv[i] != NULL; i++) for (i = 0; I < argc; i++)
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 arrayof pointers is contained in the global variable environ extern char **environ; // pointer to pointers • int main(intargc, char *argv[], char *envp[]); • Each environment string is a name=value string • getenv()/putenv()
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 pointer (environ) • environment list (the array of pointers) • environment strings (strings pointed to)
Memory Layout of a C Program • Text segment: machine instructions (read only and can be shared) • (Initialized) data segment: initialized global variables • Uninitialized data (BSS) segment: [Block Started by Symbol] uninitialized global variables • Stack: automatic variables and information of function calls (e.g., return address) • Heap: dynamic memory allocation $ size /usr/bin/gcc a.out
Shared Library • Remove common library routines from executable files, but maintain a single copy of library routine in memory that all processes reference • pros and cons ??? • reduces the size of each executable file but may add some runtime overhead • library functions can be replaced with new versions without having to relink edit every program that uses the library (assuming that the number and type of arguments haven’t changed) • gcc–static hello1.c // no SL gcc hello1.c // default using SL size a.out
Environment Variables • Unix kernel never looks at these strings • Their interpretation is up to the various applications • The shells, for example, use numerous environment variables • some, such as HOME and USER, are set automatically at login • others are left for us to set • normally environment variables are set in a shell start-up file to control the shell’s actions • getenv() returns a pointer to the value of a name=value string • Always use getenv() to fetch a specific value from the environment, instead of accessing environ directly • Use putenv()/setenv() to change the value of an existing variable or add a new variable to the environment