Chapter 7 process environment
This presentation is the property of its rightful owner.
Sponsored Links
1 / 14

Chapter 7 Process Environment PowerPoint PPT Presentation


  • 46 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 7 Process Environment. Chien -Chung Shen CIS, UD [email protected] 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

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 7 process environment

Chapter 7Process Environment

Chien-Chung Shen

CIS, UD

[email protected]


Introduction

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

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 function1

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

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

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

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


A texit function

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

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


How c program starts and terminates1

How C Program Starts and Terminates


Command line arguments

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

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 list1

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


Memory layout of a c program

Memory Layout of a C Program


  • Login