Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Process Environment Chien-Chung Shen CIS, UD

Similar presentations


Presentation on theme: "Chapter 7 Process Environment Chien-Chung Shen CIS, UD"— Presentation transcript:

1 Chapter 7 Process Environment Chien-Chung Shen CIS, UD cshen@cis.udel.edu

2 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

3 main() Function A C program starts execution from its main() function Prototype of main() int main(int argc, 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 before main() is called

4 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()

5 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

6 Exit Functions #include // ISO C void exit(int status ); void _Exit(int status ); #include // POSIX.1 void _exit(int status ); 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)

7 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

8 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

9 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

10 How C Program Starts and Terminates

11 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++)

12 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

13 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()

14 Memory Layout of a C Program

15 Shared Library Remove common library routines from executable files – Maintain a single copy of the library routine in memory that all processes reference Reduce the size of each executable file, but add some runtime overhead gcc –static hello1.c // no SL gcc hello1.c // default with SL size a.out

16 Memory Allocation


Download ppt "Chapter 7 Process Environment Chien-Chung Shen CIS, UD"

Similar presentations


Ads by Google