working with processes linux

Upload: ruli2010

Post on 14-Apr-2018

222 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Working With Processes Linux

    1/20

    Working with Processes

  • 7/30/2019 Working With Processes Linux

    2/20

    Objectives

    After completing this unit, you should be able to:Define a Linux process

    Describe the relationship between parent and child

    processes

    Explain the purpose of a shell

    Start foreground and background processesExplain the concept of signals and use them to terminateprocesses

    Explain the concept of priorities and manage them

  • 7/30/2019 Working With Processes Linux

    3/20

    What is a Process?

    A program is an executable fileA process is a program which is being executed

    Each process has its own environment:

    To see the PID of your current shell process type:$ echo $$

    Program name User and Group ID

    Internal data Process ID (PID)Open Files Parent PID (PPID)

    Current Directory Program variables

    additional parameters

    process environment

  • 7/30/2019 Working With Processes Linux

    4/20

    Starting and Stopping a Process

    All processes are started by other processesParent/Child relationship

    $ ls -l

    bash fork()

    exec() ls -l exit()

    A process can be terminated because of tworeasons:

    The process terminates itself when done

    The process is terminated by a signal from anotherprocess

  • 7/30/2019 Working With Processes Linux

    5/20

    Environment

    Login:

    Linux System

    Login Process Environment

    login: /sbin/mingetty

    password: ...forks /bin/bash

    $ -bash (login shell)

    Program -bashUID 503 (john)

    GID 100 (users)

    open files /dev/tty1

    PID 201

  • 7/30/2019 Working With Processes Linux

    6/20

    Parents and Children

    -bash

    bash

    date

    PID PPID

    561 320

    675 561777 675

    $ echo $$

    561

    $bash

    $ echo $$

    675

    $ date

    Thu Mar 25 22:28:21 CET 1999

    $

    $ echo $$

    561

  • 7/30/2019 Working With Processes Linux

    7/20

    Monitoring Processes

    The ps command displays process status information

    $ ps aux

    USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

    root 1 0.0 0.0 1336 436 ? S Mar16 0:05 init

    root 2 0.0 0.0 0 0 ? SW Mar16 0:00 [keventd]

    root 3 0.0 0.0 0 0 ? SW Mar16 0:05 [kapmd]

    root 4 0.0 0.0 0 0 ? SW Mar16 0:05 [kswapd]

    ...

    root 10248 0.0 0.1 2852 884 pts/2 R 13:47 0:00 ps aux

    ps supports a large number of options - you typically use ps aux:

    a all processes attached to a terminal

    x all other processes

    u provides more columns

    options are typed without a leading "-" !

  • 7/30/2019 Working With Processes Linux

    8/20

    Viewing Process Hierarchy

    pstree shows process hierarchy

    $ pstree

    init-+-apmd

    |-atd|-crond

    |-gpm

    |-httpd---10*[httpd]

    |-inetd

    |-kattraction.kss|-kdm-+-X

    | `-kdm---kwm-+-kbgndwm

    | |-kfm

    | |-kpanel| |-krootwm

    | |-kvt---bash---man---sh-+-gunzip

    | | `-less

    | `-startkde---autorun

    |-kflushd

  • 7/30/2019 Working With Processes Linux

    9/20

    Controlling Processes

    Processes can be controlled in two ways:From the shell that started it, using itsjob numberFrom anywhere else on the system, using itsPID

    Actions that can be performed on a running process:TerminateKillStop/Continue

    These actions are performed by sending signals

  • 7/30/2019 Working With Processes Linux

    10/20

    Starting Processes

    Foreground Processes

    Foreground processes are invoked by simply typing acommand at the command line.

    Background processes are invoked by putting an "&" atthe end of the command line

    bash wait()

    bash

    $ find / -name README

    $ find / -name README &

    Background Processes

    J b C t l i th B h Sh ll

  • 7/30/2019 Working With Processes Linux

    11/20

    Job Control in the Bash Shell

    suspends foreground task

    jobs lists background or suspended jobs

    fg resume suspended task in the

    foreground

    bg resume suspended task in thebackground

    Specify a job number forbg, fg and kill using %job

    J b C t l E l

  • 7/30/2019 Working With Processes Linux

    12/20

    Job Control Example

    $ find / -name README &

    [1] 2863

    $ jobs[1]+ running find / -name README &

    $ fg %1

    /usr/share/doc/packages/grub/README

    ....

    [1]+ stopped find / -name README

    $ bg 2863

    $ jobs[1]+ running find / -name README &

    $ kill %find

    Kill Si l

  • 7/30/2019 Working With Processes Linux

    13/20

    Signal Keyboard Meaning Default action

    01 hangup end process

    02 Ctrl-C interrupt end process03 Ctrl-\ quit end process & core dump09 kill end process - cannot be

    redefined - handled by kernel15 terminate end process

    Kill Signals

    Several signals can be sent to a processUsing keyboard interrupts (if foreground process)Using the kill commandSynopsis: kill -signal PID

    Using the killallcommand to kill all named appsSynopsis: killall -signal application

    Most important signals:

    R i L P

  • 7/30/2019 Working With Processes Linux

    14/20

    Running Long Processes

    The nohupcommand will stop a process from being killed ifyou log off the system before it completes, by intercepting andignoring the SIGHUP and SIGQUIT (hangup and quit) signals

    $ nohup find / -name README &

    nohup: appending output to `nohup.out

    $ logout

    Managing Process Priorities

  • 7/30/2019 Working With Processes Linux

    15/20

    Managing Process Priorities

    Processes are scheduled according to priority

    Base priority (0)

    user defined priority

    (negative values areallowed only to root!)

    +

    -

    + 19

    - 20

    dynamic part of priority

    (calculated by scheduler)

    reserved for kernel

    threads... -60

    ... +39

    The nice Command

  • 7/30/2019 Working With Processes Linux

    16/20

    The nice Command

    The nicecommand is used to start a process with a userdefined priority

    nice [-n ]

    $ nice -n 10 my_program &

    [1] 4862

    $ ps l

    F UID PID PRI NI VSZ ... COMMAND

    0 500 4372 9 0 4860 ... -bash

    0 500 4862 15 10 3612 ... my_program

    0 500 4863 21 0 1556 ... ps l

    The renice Command

  • 7/30/2019 Working With Processes Linux

    17/20

    The renice Command

    The renicecommand is used to change the priority of acurrently running process

    renice

    $ renice 15 4862

    4862: old priority 10, new priority 15

    $ ps lF UID PID PRI NI VSZ ... COMMAND

    0 500 4372 9 0 4860 ... -bash

    0 500 4862 22 15 3612 ... my_program

    0 500 4868 21 0 1556 ... ps l

    Integrated Process Management

  • 7/30/2019 Working With Processes Linux

    18/20

    Integrated Process Management

    Various integrated tools exist for process managementtop, gpm, kpm

    Availability depends on distribution

    Daemons

  • 7/30/2019 Working With Processes Linux

    19/20

    Daemons

    The word "Daemon" refers to a never-ending process, usually asystem process that controls a system resource such as theprinter queue or performs a network service

    lpd

    /dev/lp0

    print job 1

    print job 2

    print job 3

    Print Queue

    Unit Summary

  • 7/30/2019 Working With Processes Linux

    20/20

    Unit Summary

    All processes are started by a "parent" process (exceptfor init, which is started by the kernel)

    Every process is identified with a Process Identifier (PID)

    A special process is the shell, which can interpret usercommands

    Processes can terminate by themselves, or upon

    reception of a signal

    Signals can be sent by the shell, using a keyboardsequence, or by the kill and killall commands

    Processes are started with equal priority, but this can bechanged using the nice and renice commands

    A daemon is a background process that typically controlsa system resource or offers a network service