1 / 14

Chapter 7 Process Environment

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

wayne
Download Presentation

Chapter 7 Process Environment

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7Process 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(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

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

  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

More Related