06 Processes Sol

Embed Size (px)

Citation preview

  • 8/3/2019 06 Processes Sol

    1/22

    Copyright : Nahrstedt, Angrave, Abdelzaher 1

    Processes

    Tarek Abdelzaher

    Vikram AdveMarco Caccamo

  • 8/3/2019 06 Processes Sol

    2/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    2

    Users, Programs, Processes Users have accounts on the system

    Users launch programs Many users may launch same program

    One user may launch many instances ofthe same program

    Processes:

    an executing program (the unit of work ofa modern computer)

  • 8/3/2019 06 Processes Sol

    3/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    3

    Process States

    Possible process states (simplified view): Running (occupy CPU) Blocked

    Ready (does not occupy CPU)

    Question: in a single processor machine, how manyprocesses can be in running state?

  • 8/3/2019 06 Processes Sol

    4/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    4

    Linux: 5 State Process ModelAdd states for creating and deleting process

    Add transitions Timeout, Dispatch, Event

    Occurs

  • 8/3/2019 06 Processes Sol

    5/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    5

    How to List all Processes? On Windows: run Windows task

    manager Hit Control+ALT+delete Click on the processes tab

    On UNIX

    $ pse Try man ps to understand more about its

    usage.

  • 8/3/2019 06 Processes Sol

    6/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    6

    Process Execution and Context

    Switching

  • 8/3/2019 06 Processes Sol

    7/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    7

    Process Identification UNIX identifies processes via a unique Process ID

    Each process also knows its parent process ID since each process iscreated from a parent process.

    Root process is the init process

    getpid and getppid functions to return process ID (PID) andparent process ID (PPID)

    Example 1

    #include

    #include

    int main (void) {

    printf(I am process %ld\n, (long)getpid());

    printf(My parent id is %ld\n, (long)getppid());

    return 0;}

  • 8/3/2019 06 Processes Sol

    8/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    8

    Creating a Process fork() pid_t fork() creates a duplicate of the calling process:

    Both processes continue with return from fork()

    Child gets exact copy of code, stack, file descriptors, heap,globals, and program counter

    fork() returns -1 if fork fails

    0 in child process

    childs PID in parent process

  • 8/3/2019 06 Processes Sol

    9/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    9

    Creating a child Process fork()

    fork() creates a new process by duplicating the callingprocess. The new process, referred to as the child, is anexact duplicate of the calling process, referred to as the

    parent, except for the following points:

    The child has its own unique process ID

    The child does not inherit its parents memory locks (e.g., mlock(2)).

    Process resource utilizations and CPU time counters are reset to zero

    in the child. The childs set of pending signals is initially empty.

    The child does not inherit record locks from its parent (fcntl(2)).

    The child does not inherit timers from its parent.

    The termination signal of the child (sent to the parent) is always

    SIGCHLD.

  • 8/3/2019 06 Processes Sol

    10/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    10

    Creating a child Process fork()

    The entire virtual address space of the parent is replicated inthe child, including the states of mutexes, etc.

    The child inherits copies of the parents set of open filedescriptors. Each file descriptor in the child refers to thesame open file description (see open(2)) as the

    corresponding file descriptor in the parent. This means thatthe two descriptors share open file status flags, and currentfile offset.

  • 8/3/2019 06 Processes Sol

    11/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    11

    Creating a Process in UnixExample 2

    #include

    #include

    int main(void) {pid_t x;x = fork();if (x == 0)

    printf(In child: fork() returned %ld\n, (long) x);else

    printf(In parent: fork() returned %ld\n", (long) x);}

    What does this print?

  • 8/3/2019 06 Processes Sol

    12/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    12

    Chain and Fan

    pid_t childpid = 0;for (i=1;i

  • 8/3/2019 06 Processes Sol

    13/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    13

    Chain and Fan

    ChildChildParent

    Parent

    Child Child

    pid_t childpid = 0;for (i=1;i

  • 8/3/2019 06 Processes Sol

    14/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    14

    Process Operations: Creation A new process needs resources such as CPU,

    memory, file descriptors Child can get resources from OS or from parent

    Child address space is a duplicate of parent process

    Child process is given a subset of parent resources Prevents too many processes from overloading system

    Execution possibilities are Parent continues concurrently with child

    Parent waits until child has terminated

    C i ht N h t dt A Abd l h

  • 8/3/2019 06 Processes Sol

    15/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    15

    Process Termination Normal exit (voluntary)

    Returning zero from main()

    exit(0)

    Error exit (voluntary)

    exit(1)

    Fatal error (involuntary) Divide by 0, seg fault, exceeded resources

    Killed by another process (involuntary)

    Signal: kill(procID)

    C i ht N h t dt A Abd l h

  • 8/3/2019 06 Processes Sol

    16/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    16

    Process Operations:Termination

    When a child process terminates: Open files are flushed and closed

    tmp files are deleted Childs resources are de-allocated

    File descriptors, memory, semaphores, file locks,

    Parent process is notified via signal SIGCHLD

    Exit status is available to parent via wait()

    Cop ight Nah stedt Ang a e Abdel ahe

  • 8/3/2019 06 Processes Sol

    17/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    17

    Process Hierarchies

    Parent creates a child process, a childprocess can create its own child processes

    Forms a hierarchy UNIX calls this a "process group"

    Windows has no concept of process

    hierarchy all processes are created equal

    Copyright : Nahrstedt Angrave Abdelzaher

  • 8/3/2019 06 Processes Sol

    18/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    18

    wait(), waitpid() System Calls

    pid_t wait(int *status);

    wait()causes parentprocess to wait (block)until somechild finishes

    wait()returns childs pid

    and exit status to parent waitpid()waits for a

    specific child

    errno Cause

    ECHILD Caller has no

    unwaited-forchildren

    EINTR Function wasinterrupted bysignal

    EINVAL Options parameterof waitpid wasinvalid

    Copyright : Nahrstedt Angrave Abdelzaher

  • 8/3/2019 06 Processes Sol

    19/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    19

    pid_t wait(int *status);

    In the case of a terminated child, performing a wait allows thesystem to release the resources associated with the child; ifa wait is not performed, then the terminated child remains ina "zombie state.

    If a child has already terminated, then the call returnsimmediately. Otherwise it blocks until either a child terminatesor a signal handler interrupts the call. A child that hasterminated and which has not yet been waited upon by thissystem call (or waitpid) is termed waitable.

    wait(), waitpid() System Calls

    Copyright : Nahrstedt Angrave Abdelzaher

  • 8/3/2019 06 Processes Sol

    20/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    20

    If status is not NULL, wait() stores status information in theint to which it points. This integer can be inspected withspecific macros (see man pages):

    WIFEXITED(status) returns true if the child terminated normally, that is, by calling exit, or

    by returning from main().

    WEXITSTATUS(status) returns the exit status of the child. This consists of the least

    significant 8 bits of the status argument that the child specified in acall to exit or as the argument for a return statement in main().This macro should only be employed if WIFEXITED returned true.

    wait(), waitpid() System Calls

    Copyright : Nahrstedt Angrave Abdelzaher

  • 8/3/2019 06 Processes Sol

    21/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    21

    A child that terminates, but has not been waited for becomes a"zombie". The kernel maintains a minimal set of information about thezombie process (PID, termination status, resource usage information) inorder to allow the parent to later perform a wait to obtain information

    about the child.

    As long as a zombie is not removed from the system via a wait, it willconsume a slot in the kernel process table, and if this table fills, it will notbe possible to create further processes.

    If a parent process terminates, then its "zombie" children (if any) areadopted by init(8), which automatically performs a wait to remove thezombies.

    wait() & zombie

    Copyright : Nahrstedt Angrave Abdelzaher

  • 8/3/2019 06 Processes Sol

    22/22

    Copyright : Nahrstedt, Angrave, Abdelzaher

    22

    Waiting for a child to finish(Try man s 2 wait)

    #include #include

    pid_t childpid;

    childpid = wait(NULL);

    if (childpid != -1)printf(waited for child with pid %ld\n, childpid);