cdac,hyderabad

Upload: srinu63

Post on 30-May-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 c dac,Hyderabad

    1/13

    C-DAC,HYDERABAD 1

    Processes

  • 8/14/2019 c dac,Hyderabad

    2/13

    C-DAC,HYDERABAD 2

    What Is a Process?

    In Linux a process as just a program that isrunning.

    A program or process that is running consists of

    program code, data, variables (occupyingsystem memory), open files (file descriptors),and an environment.

    Typically, a Linux system will share code andsystem libraries among processes so thattheres only one copy of the code in memory atany one time.

  • 8/14/2019 c dac,Hyderabad

    3/13

    C-DAC,HYDERABAD 3

    Process Structure

    Each process is allocated a unique number,called a process identifier or PID. This isusually a positive integer between 2 and

    32,768. When a process is started, the next unused

    number in sequence is chosen and thenumbers restart at 2 so that they wrap around.

    The number 1 is typically reserved for thespecial init process, which manages otherprocesses.

  • 8/14/2019 c dac,Hyderabad

    4/13

    C-DAC,HYDERABAD 4

    The Process Table

    The Linux process table is like a data structuredescribing all of the processes that arecurrently loaded with, for example, their PID,

    status, and command string, the sort ofinformation output by ps command .

    The ps command shows the processes yourerunning, the process another user is running,

    or all the processes on the system .

    Some flags which are most frequently usedwith ps are ax and ef.

  • 8/14/2019 c dac,Hyderabad

    5/13

    C-DAC,HYDERABAD 5

    Status of a process

  • 8/14/2019 c dac,Hyderabad

    6/13

    C-DAC,HYDERABAD 6

    The operating system manages processes

    using their PIDs, and they are used as an indexinto the process table.

    The table is of limited size, so the number of

    processes a system will support is limited. Early UNIX systems were limited to 256

    processes. More modern implementations haverelaxed this restriction considerably and may be

    limited only by the memory available toconstruct a process table entry.

  • 8/14/2019 c dac,Hyderabad

    7/13

    C-DAC,HYDERABAD 7

    Starting New Process

    You can cause a program to run from insideanother program and thereby create a newprocess by using the system library function.

    #include int system (const char *string);

  • 8/14/2019 c dac,Hyderabad

    8/13

    C-DAC,HYDERABAD 8

    Duplicating a Process Image

    You can create a new process by calling fork.

    This system call duplicates the currentprocess, creating a new entry in the process

    table with many of the same attributes as thecurrent process.

    The new process is almost identical to theoriginal, executing the same code but with itsown data space, environment,and filedescriptors.

  • 8/14/2019 c dac,Hyderabad

    9/13

    C-DAC,HYDERABAD 9

    #include

    #include pid_t fork(void);

  • 8/14/2019 c dac,Hyderabad

    10/13

    C-DAC,HYDERABAD 10

    Waiting for a Process

    You can arrange for the parent process towait until the child finishes before continuingby calling wait.

    #include #include

    pid_t wait(int *stat_loc);

  • 8/14/2019 c dac,Hyderabad

    11/13

    C-DAC,HYDERABAD 11

    Zombie Processes

    Using fork to create processes can be very useful, but youmust keep track of child processes.

    When a child process terminates, an association with itsparent survives until the parent in turn either terminates

    normally or calls wait. The child process entry in the process table is therefore not

    freed up immediately.

    Although no longer active, the child process is still in the

    system because its exit code needs to be stored in case theparent subsequently calls wait.

    It becomes what is known as defunct, or a zombie process.

  • 8/14/2019 c dac,Hyderabad

    12/13

    C-DAC,HYDERABAD 12

    Replacing a Process Image

    There is a whole family of related functionsgrouped under the exec heading.

    They differ in the way that they start

    processes and present program arguments. An exec function replaces the current process

    with a new process specified by the path orfile argument.

    You can use exec functions to hand offexecution of your program to another.

  • 8/14/2019 c dac,Hyderabad

    13/13

    C-DAC,HYDERABAD 13

    THANK YOU