The Environment of Unix Process - PowerPoint PPT Presentation

About This Presentation
Title:

The Environment of Unix Process

Description:

How the main function is called. How command-line arguments are passed to the new program. What the typical memory layout looks like. How to allocate additional memory ... – PowerPoint PPT presentation

Number of Views:557
Avg rating:3.0/5.0
Slides: 39
Provided by: A267
Category:

less

Transcript and Presenter's Notes

Title: The Environment of Unix Process


1
The Environment of Unix Process
  • The environment of a single process
  • How the main function is called
  • How command-line arguments are passed to the new
    program
  • What the typical memory layout looks like
  • How to allocate additional memory
  • How the process can use environment variables
  • Different ways of process to terminate

2
The Environment of Unix Process
  • main function
  • int main(int argc, char argv)
  • argc -- the number of command-line arguments
  • argv -- an array of pointers to the arguments
  • When a C program is started by the kernel
  • a special start-up routine is called before the
    main function is called
  • the start-up routine takes values from the
    kernel
  • the command-line arguments
  • the environment
  • the start-up routine sets things up (and also
    clear things up)

3
The Environment of Unix ProcessProcess
Termination
  • Process Termination
  • Normal Termination
  • return from main
  • calling exit()
  • calling _exit()
  • Abnormal Termination
  • calling abort()
  • terminated by a signal
  • The start-up routine calls the exit() function
    when the main function returns (clear things up)

4
The Environment of Unix Process Process
Termination
  • Process Termination exit() and _exit()
    functions
  • include ltstdlib.hgt
  • void exit(int status)
  • include ltunistd.hgt
  • void _exit(int status)
  • _exit() returns to the kernel immediately
  • exit() performs certain cleanup processing and
    then returns to the kernel
  • fclose() is called for all open streams
  • caused all buffered output data to be flushed

5
The Environment of Unix Process Process
Termination
  • atexit Function
  • A process can register up to 32 functions that
    are automatically called by exit()
  • These are called exit handlers and are registered
    by calling the atexit() function
  • include ltstdlib.hgt
  • int atexit(void ( func)(void)
  • Returns 0 if ok, nonzero on error
  • The exit() function calls these functions in
    reverse order of their registration
  • Each function is called as many times as it was
    registered

6
The Environment of Unix Process Process
Termination
user process
user function
exit handler
_exit
exit
return
return
call
call
main function
exit handler
return
exit

_exit
call
call
exit function
return
call
call
standard I/O cleanup
return
C start-up routine
exit
_exit
exec
kernel
7
The Environment of Unix Process
  • Note
  • The only way a program is executed by the kernel
    is when one of the exec functions is called

8
  • Example of
  • exit handlers
  • a.out
  • main is done
  • first exit handler
  • first exit handler
  • second exit handler

9
The Environment of Unix Process
  • Command-Line Arguments
  • When a program is executed, the process that does
    the exec can pass command-line arguments to the
    new program
  • TO DO Write your own program to demonstrate how
    to pass command-line arguments.
  • And display how many arguments are passed each
    time
  • When is it useful?

10
The Environment of Unix Process
  • Environment List
  • Each program is also passed an environment list
    which is an array of char pointers,
  • Each pointer containing the address of a
    null-terminated string
  • The address of the array of pointers is contained
    in the global variable environ
  • extern char environ

11
The Environment of Unix Process
  • Environment List

Environment pointer
environ
HOME/home/xx\0
PATH/bin/sbin\0
extern char environ
12
The Environment of Unix Process
  • Environment Variables
  • include ltstdlib.hgt
  • char getenv(char name)
  • Returns pointer to value associated with name,
  • NULL if not found
  • int putenv(char str)
  • int setenv(char name, char value, int rewrite)
  • Returns 0 if OK, nonzero on error

13
The Environment of Unix Process
  • Memory Layout of a C Program
  • Historically a C program has been composed of
  • Text segment
  • Initialised data segment
  • Uninitialised data segment
  • Stack
  • Heap

14
The Environment of Unix Process Memory Layout
  • Text segment
  • machine instructions, executed by the CPU
  • sharable, only a single copy needs to be in the
    memory for frequently executed program
  • read-only, to prevent a program from being
    accidently modifying its instructions
  • Initialised data segment (or data segment)
  • contains variables that are specifically
    initialised in the program
  • e.g.
  • int max 99
  • appearing outside any function -- stored in the
    initialised data segment

15
The Environment of Unix Process Memory Layout
  • Unintialised data segment
  • often called bss segment -- block started by
    symbol
  • data is initialised by the kernel to be 0 before
    the program starts executing
  • e.g.
  • long sum100
  • appear outside any function gt stored in
    uninitialised data segment
  • Stack
  • stores automatic variables information saved
    each time a function is called
  • returned address
  • callers environment information

16
The Environment of Unix Process Memory Layout
  • Heap
  • Dynamic memory allocation usually takes place on
    the heap
  • Normally located between the uniintialised data
    segment and the stack
  • Write a Program with all types of variables to be
    stored in each of these segments

17
The Environment of Unix Process Memory Layout
Typical Memory Management
high address
command-line arguments and environment variables
stack
heap
initialised to zero by exec
uninitialised data
uninitialised data
read from prog file by exec
text
low address
18
The Environment of Unix Process Memory Layout
  • The size(1) command reports size of the text
    data, bss segments, e.g
  • size /bin/cc /bin/sh
  • text data bss dec hex
  • 81920 16384 664 98968 18298 /bin/cc
  • 90112 16384 0 106496 1a000 /bin/sh

19
The Environment of Unix Process Shared Library
  • A single copy of library routine is used by many
    processes
  • Reduce the size of each executable file
  • The library function can be replaced with new
    versions without having to re-link every program
    that uses the library
  • Disadvantage may add some run-time overhead,
    either
  • when the program is first execed, or
  • when the library function is called

20
The Environment of Unix Process Shared Library
  • TO DO
  • Write a classic hello.c program, and compile it
  • normally (cc hello.c)
  • using a shared library option
  • What are the differences, in term or size
    measures
  • Explain why?

21
The Environment of Unix Process Memory
Allcoation
  • Three functions for memory allocation
  • malloc()
  • calloc()
  • realloc()
  • Self-study !!!!

22
The Environment of Unix Process setjmp and
longjmp Functions
  • goto statement
  • Scope?
  • setjmp() and longjmp()
  • useful for handling error conditions that occur
    in a deeply nested function call
  • include ltsetjmp.hgt
  • int setjmp(jmp_buf env)
  • void longjmp(jmp_buf env, int val)

More
23
Process Control
  • Process Control Provided by UNIX
  • Creation of new processes

24
Process Control
  • Process Identifiers -- Process ID
  • Unique nonnegative integer
  • Some special processes, such as
  • swapper -- process ID 0
  • part of the kernel known as a system process
  • init -- process ID 1
  • a normal user process, runs with superuser
    privileges
  • bringing up a Unix system
  • read the system-dependent initialisation files
  • never dies
  • pagedaemon -- process ID 2
  • supporting the paging of the virtual memory
    system

25
Process Control
  • Process Identifiers -- Other IDs
  • parent process ID getppid()
  • real user ID getuid()
  • effective user ID geteuid()
  • real group ID getgid()
  • effective group ID getegid)

26
Process Control
  • fork Function pid_t fork(void)
  • creates a new process -- child process
  • called once, but returns twice
  • the return value in the child is 0
  • the return value in the parent is the process ID
    of the the child
  • WHY the return values are 0 and the child PID ?
    (given all functions related to processes are as
    from the previous page)
  • both the child and parent continue executing with
    the instruction that follos the call to fork
  • the child is a copy of the parent
  • gets a copy of the parents data space, heap,
    and stack

27
Process Control
  • Write a program to demonstrate the fork
    function. when executed, the result should be as
    follows
  • a.out
  • a write to standout out --- gt by write()
  • before fork --- gt by printf()
  • pid XXX, global variable XX, var XX
  • pid XXX, global variable XX, var XX
  • When global gt external variable in initialised
    data
  • var gt automatic variable on the stack

28
Process Control
  • Run the program several times
  • What is the order of the execution -- from the
    parent or child process first?
  • Why? -- reasons to support your answer
  • Then run the program as
  • a.out gt temp.out
  • Study the result
  • Explain your result

29
Process Control
  • File Sharing -- for parent child processes
  • All descriptors that are opened in the parent are
    duplicated in the child
  • Consider a process that has three different files
    opened for standard input, standard output and
    standard error. Write a diagram of file
    descriptors arrangement (process table, file
    table, etc.) on return from fork AND explain
    your diagram in words.
  • 30 mins.

30
Process Control
  • File offset sharing and Synchronisation issue
  • It is important that the child and parent share
    the same file offset
  • E.g. --- assume that the parent and child write
    to stdout
  • a parent forks a child, then wait for a child to
    complete
  • if the parent has its stdout redirected (by a
    shell), it is essential that the parents file
    offset must be updated by the child
  • the child can write to stdout while the parent is
    waiting
  • the parent continue writing on the completing of
    the child -- its output must be appended to
    whaever the child wrote

31
Process Control
  • Two normal cases for handling the descriptors
    after a fork
  • The parent waits for the child to complete
  • parent does not need to do anything with its
    descriptors
  • when the child terminated, file offsets are
    updated accordingly
  • The parent and child each go their own way
  • after the fork, the parent closes the descriptors
    that does not need
  • the child does the same thing -- gt not
    interferes with the others open fd
  • often the case with network servers

32
Process Control
  • Study the process characteristics/properties and
    answer the following questions
  • What are the properties of the parent that that
    are inherited by the child
  • What are the differences between the parent and
    the child
  • Study vfork() function, what is the different
    between fork() and vfork()

33
Process Control
  • Process Termination
  • By
  • return()
  • exit(), atexit(), _exit()
  • abort()
  • when a process receives certain signals, such as
    devided by zero..

34
Process Control
  • Process Termination
  • The same code in the kernel is executed
  • closes all open filederscriptors
  • release memory occupied by the process
  • Notify the parent process, by
  • for nomal termination passing an exit status
    (if exit and _exit is used) to the parent
  • for abnormal termination the kernel generate a
    termination status to indicate the reason for the
    abnormal termiation

35
Process Control
  • Process Termination
  • The parent process can obtain this status from
  • wait() or waitpid()
  • If the child terminates (before the parent) and
    does not send a status to the parent
  • the kernel keeps information about all
    terminating processes
  • this information is available for the parent
    process (when wait() or waitpid() is called)

36
Process Control
  • wait() and waitpid() Functions
  • When a process is terminated
  • the kernel sends SIGCHLD signal to the parent
  • it is a synchronous notification signal
  • the parent can choose to
  • ignore, or
  • provide a signal handler function (is called when
    a signal occurs)

37
Process Control
  • wait() and waitpid() Functions
  • If a process calls wait() or waitpid(), it can
  • block (if all of its child are still running), or
  • return immediately with the termination status of
    a child (if a child has terminated and is waiting
    for its status to be fetched), or
  • return immediately with an error (if it doesnt
    have any child processes)

38
Process Control
  • wait() and waitpid() Functions
  • include ltsys/types.hgt
  • include ltsys/wait.hgt
  • pid_t wait(int statloc)
  • pid_t waitpid(pid_t pid, int statloc, int
    options)
  • wait() can block the caller until a child process
    terminates
  • waitpid() has an option that prevents it from
    blocking
Write a Comment
User Comments (0)
About PowerShow.com