Top Banner

of 23

Process Management Compatibility Mode

Mar 03, 2018

Download

Documents

Manoj Kathuria
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/26/2019 Process Management Compatibility Mode

    1/23

    Process & Process Management

    Prachi Pandey

    [email protected]

    Hybrid Computing Group (SSDH)CDAC, Bangalore

    mailto:[email protected]:[email protected]
  • 7/26/2019 Process Management Compatibility Mode

    2/23

    Outline

    Process concept

    Process address space

    PCB Process Creation and Management

    Fork, exec and other system calls

    Process life cycle

  • 7/26/2019 Process Management Compatibility Mode

    3/23

    Process

    The process is the OSs abstraction for

    execution

    A Program in ExecutionAn active instance of a program

    Process is often called a job, or task

  • 7/26/2019 Process Management Compatibility Mode

    4/23

    Program and Process

    Program

    Hello.c Compiler a.out

    Resides in secondary memory

    Process

    is created by OS to execute a program

    OS puts it in the main memory

    Creates a data structure

  • 7/26/2019 Process Management Compatibility Mode

    5/23

    Process Address Space

  • 7/26/2019 Process Management Compatibility Mode

    6/23

    Process Control Block (PCB)

    Information associated with each process

    Process Id

    Process state

    Program counter

    CPU registers

    Priority

    Memory-management info List of open files

    Protection

  • 7/26/2019 Process Management Compatibility Mode

    7/23

    Process Management

    Complete activity required for managing a

    process throughout its lifecycle

    Allocate resources to processesEnable processes to share and exchange

    information

    Protect the resources of each process from other

    processesEnable synchronization among processes.

    Maintain a data structure for each process

    Execution and Control of processes

  • 7/26/2019 Process Management Compatibility Mode

    8/23

    Process Management System Calls

    Fork()

    Execv()

    Wait() Getpid()

  • 7/26/2019 Process Management Compatibility Mode

    9/23

    How To Create New Processes?

    Underlying mechanism

    A process runs fork to create a

    child process

    Child process is a duplicate of theparent process

    Both parent and child execute next

    line

    Parent and children executeconcurrently

    parent

    child

    fork()

  • 7/26/2019 Process Management Compatibility Mode

    10/23

    Fork

    Child and parent are identical

    child returns a 0

    parent returns nonzero (child PID) PID and PPID differs

    Synopsis

    pid_t fork(void);

  • 7/26/2019 Process Management Compatibility Mode

    11/23

    Fork()

    Splits into . Process The child process

    inherits from parent

    Identical copy of

    memoryCPU registers

    all files that have beenopened by the parent

    PCB of child is copy ofparents context attime of call

    Text

    Data

    Stack

    Text

    Data

    Stack

    ret = xxx

    ret = 0

    fork()

  • 7/26/2019 Process Management Compatibility Mode

    12/23

    Example: Process Creation in Unix

    int pid;

    pid = fork()if (pid > 0){

    /* parent */..

    }else if (pid == 0)

    {/* child */..exit(status);

    }

    The fork syscall returns twice: itreturns a zero to the child and thechild process ID (pid) to the

    parent.

  • 7/26/2019 Process Management Compatibility Mode

    13/23

    Fork Example 1: What Output?

    int main()

    {

    pid_t pid;

    int x = 1;

    pid = fork();

    if (pid != 0) {

    printf(parent: x = %d\n, --x);

    exit(0);

    } else {printf(child: x = %d\n, ++x);

    exit(0);

    }

    }

  • 7/26/2019 Process Management Compatibility Mode

    14/23

    Fork Example 2

    Both parent and child can continue forking

    void fork2(){

    printf("L0\n");

    fork();

    printf("L1\n");

    fork();

    printf("Bye\n");

    }

    L0 L1

    L1

    Bye

    Bye

    Bye

    Bye

  • 7/26/2019 Process Management Compatibility Mode

    15/23

    exit()

    Closes all open files, connections and

    deallocates memory

    Checks if parent is alive If parent is alive, holds the result value until the

    parent requests it (with wait); in this case, the

    child process does not really die, but it enters a

    zombie/defunct state

  • 7/26/2019 Process Management Compatibility Mode

    16/23

    Waiting for the Child to Finish

    Waiting for some child to terminate: wait()

    Blocks until some child terminates

    Returns the process ID of the child process

    Or returns -1 if no children exist (i.e., alreadyexited)

    Waiting for a specific child to terminate:waitpid()

    Blocks till a child with particular process IDterminates

    #include

    #include

    pid_t wait(int *status);

    pid_t waitpid(pid_t pid, int *status, int options);

  • 7/26/2019 Process Management Compatibility Mode

    17/23

    Unixs execv

    The system call execv executes a file, transforming thecalling process into a new process.

    After a successful execv, there is no return to the calling

    process.

    execv(const char * path, char * const argv[])

    - path is the full path for the file to be executed

    - argv is the array of arguments for the program to

    execute

    - each argument is a null-terminated string

    - the first argument is the name of the program (argv[0])

    - the last entry in argv is NULL

  • 7/26/2019 Process Management Compatibility Mode

    18/23

    Example: A Simple Shell

    Shell is the parentprocess

    E.g., bash

    Creates a new process

    fork

    Child executes a newprocess (ls)

    execv

    Parent waits for Childprocess

    Wait

    fork

    ls

    waitexecvp

    bash

  • 7/26/2019 Process Management Compatibility Mode

    19/23

    execv Example#include

    #include

    char * argv[] = {/bin/ls, -l, 0};

    int main()

    {

    int pid, status;

    if ( (pid = fork() ) < 0 ){

    printf(Fork error \n);

    exit(1);

    }

    if(pid == 0) { /* Child executes here */

    execv(argv[0], argv);

    printf(Exec error \n);exit(1);

    } else /* Parent executes here */

    wait(&status);

    printf("Hello there! \n);

    return 0;

    }

    Note the NULL string

    at the end

  • 7/26/2019 Process Management Compatibility Mode

    20/23

    Variations of execv()

    execvProgram arguments passed as an array of strings

    execvp

    Searches for the program name in the PATHenvironment

    execl

    Program arguments passed directly as a list

    execlpSearches for the program name in the PATH

    environment

  • 7/26/2019 Process Management Compatibility Mode

    21/23

    Other useful system calls: getpid, getppid

    getpid returns the identifier of the calling

    process. Example call (pid is an integer):

    pid = getpid();

    getppid returns the identifier of the parent.

    ppid = getppid();

  • 7/26/2019 Process Management Compatibility Mode

    22/23

    Process State

    As a process executes, it changes state

    new: The process is being created

    ready: The process is waiting to be assigned to aprocessor

    running: Instructions are being executed

    waiting: The process is waiting for some event to

    occurterminated: The process has finished execution

  • 7/26/2019 Process Management Compatibility Mode

    23/23

    Diagram of Process State