Unix Processes - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Unix Processes

Description:

All operating systems must provide a means for creating new processes. ... The child process starts execution immediately after the fork() rather than ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 18
Provided by: alanb9
Category:

less

Transcript and Presenter's Notes

Title: Unix Processes


1
Unix Processes
  • Chapter 27

2
Processes
  • A process is an executing program.
  • All operating systems must provide a means for
    creating new processes.
  • Each time you run a program from a Unix prompt,
    the shell process creates a new child process to
    run the command.
  • Each process gets a unique identifier called the
    process id, or PID.

3
Background Processes
  • Since Unix is a multitasking operating system,
    its possible to run more than one job at a time.
    Generally when we run a program, the shell waits
    for the program to exit before giving you another
    prompt. To make the shell continue immediately
    after launching the program, you can place an
    as the last argument to the command. When you do
    this, the process you create is called a
    background process.

4
Creating a Process
  • fork() all Unix processes are created with the
    fork() system call. fork() is defined in
    unistd.h.
  • pid_t fork(void)
  • returns 0 to the child process and the PID of the
    child process to the parent process.
  • fork() creates an exact duplicate of the calling
    process, and returns the PID of the child process
    to the parent process. fork() is the only
    function that can be called by one process and
    return two processes.
  • The child process starts execution immediately
    after the fork() rather than starting back at the
    beginning of main().
  • The only way to tell the two processes apart is
    with the PID that is returned by the fork()
    function.

5
Why an exact duplicate?
  • Starting a new program from scratch has some
    limitations. The biggest limitation is that the
    its difficult to pass vital information from the
    parent to the child. Instead, if the child is a
    copy of the parent, it inherently has all the
    information it needs. The child can then modify
    this data as necessary before transforming into a
    new program.

6
Why would you need to modify the data first?
  • Weve talked before about being able to
    background a process with and redirect the
    output of a program with the gt operator, or
    redirect the input with the lt. These are
    universal in the Unix world, and expecting every
    program to individually implement them would mean
    a whole lot of extra code for each program.
    Instead, its the shells job to handle these for
    every program.
  • When the input/output redirectors are used, all
    the shell needs to do is change where stdin and
    stdout point and then transform into the new
    program. As far as the new program is concerned,
    stdin and stdout still work as usual, but in fact
    theyre redirected to a file instead.
  • When the background operator is used, the shell
    simply does not wait for the child to terminate
    before continuing execution.

7
Whats this about transforming into another
process?
  • Weve forked our process out, now what?
    Currently all weve got is a duplicate of our
    original process, which doesnt get us any closer
    to running another program. Once youve modified
    any necessary data, you can then call another
    program using any of the exec functions found in
    later slides. Calling an exec function replaces
    the current process with the program called in
    exec, utilizing the same PID effectively
    transforming the process into a new program.

8
A forking example
  • include ltstdio.hgt
  • include ltunistd.hgt
  • include ltsysexits.hgt
  • int main()
  • //variable declarations operations
  • if( fork()0 )
  • //statements to be executed in the child go
    here
  • else
  • //statements to be executed in the parent go
    here
  • return EX_OK
  • //end main

9
Transforming Processes exec functions
  • int execve( char path, char argv, char
    envp)
  • returns -1 on error, does not return if
    successful
  • execve replaces the currently running process
    with the program specified in the first argument.
  • path must be the full or relative path of the
    program to be run.
  • argv and envp must be formatted as specified
    in sections 16.6 and 16.7 of the book.
  • execve() is inconvenient in that it makes you
    determine the path of the program on your own,
    and it makes you pass the envp argument. If
    youre calling execve() from a function other
    than main() you would need to pass envp from
    main() to your other function before you could
    pass it to execve() which can be a hassle. There
    are much easier ways to do this.

10
Other exec functions
  • int execvp(char progname, char argv)
  • Returns -1 on error, nothing on success
  • Automatically searches the PATH environment
    variable so that you dont have to determine the
    path yourself.
  • Eliminates the need for the envp argument.

11
Other exec Functions
  • int execlp(char prog, char arg0, )
  • Returns -1 on error, nothing on success
  • Like execvp(), it searches the PATH variable
    automatically.
  • Builds the argv array for you
  • arg0 should always contain the program name
  • Last argument needs to be NULL
  • Works well if you need a hard coded program call
    in your code.
  • execlp(ls,ls,-al,/etc,NULL)

12
Waiting
  • If you want your parent to wait for the child to
    exit before continuing execution, use with wait()
    function.
  • int wait(int status)
  • The PID of the child that terminated is returned.
    The exit status of the child process is stored
    in status.
  • wait() is defined in sys/wait.h

13
Still Waiting
  • There are several macros predefined to find out
    how the child process exited.
  • WIFEXITED(status) is non-zero if the child exited
    voluntarily using exit() or return.
  • WIFSIGNALLED(status) is non-zero if the child
    process was killed by a signal.
  • WIFSTOPPED(status) is non-zero if the child
    process was stopped and may be continued.
  • WEXITSTATUS(status) extracts the exit status from
    the status variable
  • WTERMSIG(status) produces the signal value that
    caused termination
  • WCOREDUMP(status) is non-zero if a core file was
    produced
  • WSTOPSIG(status) will provide the signal status
    that stopped it.

14
Yep, still waiting .
  • If your parent process launches more than one
    child, the wait() will stop as soon as any of
    them returns. In this case, you may use the
    waitpid() or wait4() functions to wait for the
    child with the specified PID to exit. These are
    advanced functions. Its good to know they
    exist, but since we wont need them, I wont bore
    you with the details. Examine the man pages for
    usage if you ever need to use them.

15
Another forking example
  • include ltstdio.hgt
  • include ltunistd.hgt
  • include ltsysexits.hgt
  • include ltstring.hgt
  • int main()
  • char prog500, input500, argv500
  • int status, ex_pid
  • fgets(input, 500, stdin)// get a command from
    the user
  • //tokenize the command put the program name with
    path if necessary into prog and build argv
  • if( fork()0 )
  • execvp(prog,argv)
  • //only if execvp failed will the rest of these
    commands execute
  • perror(prog) // prints program name followed
    by a colon and error message
  • exit(errno)
  • else
  • ex_pid wait( status )

16
Redirection
  • The open() function which opens filestreams,
    always uses the lowest available file descriptor.
    Closing a descriptor makes it available for use
    in the next open() call. We will exploit that to
    redirect stdout, stdin, and stderr. open() is
    provided by fcntl.h.
  • close(0) //stdinopen(filename, O_RDONLY, 0)
    //redirect stdin to filename
  • close(1) //stdoutopen(filename,O_WRONLYO_TRUNC
    O_CREAT,0644) //redirect stdout to filename
  • close(2)//stderropen(filename,O_WRONLYO_TRUNCO
    _CREAT,0644) //redirect stderr to filename
  • Proper use would be to fork your process, make
    the necessary redirections in the child, then
    call an exec function to transform the child into
    the necessary program.

17
Manipulating the Environment
  • To see the environment variables in use from the
    command line, type the command env, it will
    display all of your current environment variables
    and their values.
  • getenv() read environment variables
  • char getenv(char name)
  • returns a string with the value of the
    environment variable specified with name, or NULL
    if name is not defined.
  • putenv() add a new environment variable
  • int putenv(char string)
  • Returns 0 on success, -1 on failure.
  • Adds string to the environment
  • setenv() edit the value of an environment
    variable
  • int setenv(char name, char value, int
    overwrite)
  • returns 0 on success, -1 on failure
  • Adds the string namevalue\0 to the environment.
    If the variable name is already defined, and
    overwrite is non-zero, then the value of name is
    replaced with the new value.
  • The shell uses this on the pwd variable when you
    issue a cd command.
Write a Comment
User Comments (0)
About PowerShow.com