Controlling Processes

A process is a running program which uses system resources such as CPU, I/O and memory. commands
kill
ps
nice
renice
top
fuser
echo

Components of a process

Process Environments

This table summarizes the typical process environment consisting of variables and parameters required to run.
 
Terminal (TTY) Terminal ID from which the process was launched, provies linkage for 3 file descriptors: stdin, stdout, stderr
Open files Files the process is using
Current directory The directory from which the process was invoked
User ID (UID) ID of the user who invoked the process
Effective User ID (EUID) Determines what resources and files a process has permission to access at any given moment. There is a distinction between identity and permission. Think of setuid programs like sudo. (Next chapter)
Group ID (GID) ID of the group that the user belongs to
Process ID (PID) A unique number that the kernel randomly assigned to the process
Parent process ID (PPID) The parent process ID, which launched the subject process
Priority The processes scheduling priority or niceness. Determines how much CPU the process receives

Checking the Shell's Process ID (PID)

$ echo $$
3173
Parent-Child Relationships

All processes exist in parent-child hierarchies.

Take a look at the jobs currently running on your machine:

$ ps -laxfw | less
(excerpt)

UID   PID  PPID PRI   STAT  TTY    TIME COMMAND

500  5269  5095  15    S    pts/6   0:01  \_ /bin/bash
500 18006  5269  17    R    pts/6   0:00      \_ ps -laxfw
500 18007  5269  16    S    pts/6   0:00      \_ less

The echo command is built into the shell. It doesn't require that a subshell be created: ps options:
          u            select by effective user ID (supports names)
          a            Select all processes on a terminal,  including those of other users
          x            Select processes without controlling ttys
          l              Long format
          f             ASCII-art process heirarchy (forest)
         w             wide output

Process Signals

When a process receives a signal one of the following things happens: Most commonly used signals:
 
Numeric bash name Description
1 HUP Hang-up signal 
1. reset request for processes to reread config files (ie, syslog, init)
2. signal also generated by modem connection when dropped.
2 INT Interrupt signal sent when <Ctrl-C> is typed at keyboard, terminate the current operation. Most programs simply allow themselves to be killed. Some processes may wait for more text input from keyboard.
3 QUIT Similar to TERM, produces a core dump if not caught by process.
9 KILL Unconditional kill -- not graceful, no cleanup.
11 SEGV Segmentation fault caused by improper access of memory.
15 TERM Termination signal which instructs process to terminate, graceful termination!

Terminating Processes

Why would you want to terminate one of you beloved processes:

Termination Methods

   Foreground processes

   Background processes The Kill Command Syntax:
kill PID1 PID2 PID3
kill [-signal] PID
killall [-signal] process_name
Note that the process IDs are separated by a space. You can have as many as you want listed after the kill command and optional signal.

Example -- to create output and load:

[pattyo@ponto $ bash
[pattyo@ponto]$ yes > /dev/null &
[1] 6023

What the heck is the yes command anyway?

$ man yes
NAME
   yes - output a string repeatedly until killed
[pattyo@ponto]$ ps f
  PID TTY      STAT   TIME COMMAND
  763 pts/0    S      0:00 bash
 6013 pts/0    S      0:00 bash
 6023 pts/0    R      0:03  \_ yes
 6024 pts/0    R      0:00  \_ ps f

[pattyo@ponto]$ kill 6023
[pattyo@ponto]$

No error message is displayed. Check process exit status.
[pattyo@ponto]$ echo $?
0
[1]+  Terminated   yes >/dev/null

A return code of 0 is good news,  anything else for an exit status is bad.

Using the killall command NOTE: If you entire session is hung, you can log in to a different virtual terminal and use the kill command from there. <Ctrl><Alt>F3. You can get back to your original session with <Ctrl><Alt>F7 (depending how your system is configured).

kill Command Signals

To get a complete listing of the kill signals type:

$ kill -l
 1) SIGHUP     2) SIGINT   3) SIGQUIT   4) SIGILL
 5) SIGTRAP    6) SIGIOT   7) SIGBUS    8) SIGFPE
 9) SIGKILL   10) SIGUSR1 11) SIGSEGV  12) SIGUSR2
13) SIGPIPE   14) SIGALRM 15) SIGTERM  17) SIGCHLD
18) SIGCONT   19) SIGSTOP 20) SIGTSTP  21) SIGTTIN
22) SIGTTOU   23) SIGURG  24) SIGXCPU  25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO
30) SIGPWR    31) SIGSYS

Process Monitoring

A process is a single program or task running in its own virtual address space.

The ps (process status) command is used to

This command illustrates the parent child relationships of process. The ps command can be used without any options.
$ ps
  PID TTY          TIME CMD
  889 pts/3    00:00:00 bash
 5708 pts/3    00:00:00 ps
Output of ps Command above

PID Process identification number assigned by the kernel
TTY Terminal where the process originated
TIME Cumulative execution time  (min:sec)
COMMAND Name of the process being executed

ps options (documented in man pages)

u select by  user name
a Select all processes on a terminal-- including those of other users
Select processes without controlling ttys
f ASCII-art process heirarchy (forest)
e Environment of the process
Long listing (along with basic flags)

The top command
  Provides a way to view the most CPU intensive processes at any given moment

  8:43pm  up 17 min,  7 users,  load average: 0.54, 0.61, 0.38
89 processes: 86 sleeping, 2 running, 1 zombie, 0 stopped
CPU states:  7.3% user,  2.5% system,  0.0% nice, 90.0% idle
Mem:   158928K av,  139676K used,   19252K free,     740K shrd,    4252K buff
Swap:  337280K av,       0K used,  337280K free                   78256K cached

  PID USER     PRI  NI  SIZE  RSS SHARE STAT %CPU %MEM   TIME COMMAND
 1960 root      16   0 15760  11M  2536 R     4.5  7.2   0:09 X
 2154 pattyo    11   0 15396  14M  8964 S     2.3  9.5   0:05 netscape-commun
 2189 pattyo    14   0  1064 1064   836 R     0.9  0.6   0:00 top
 1995 pattyo    12   0  3728 3728  2048 S     0.7  2.3   0:03 sawfish
 2019 pattyo     9   0  5384 5384  3980 S     0.3  3.3   0:01 panel
    2 root       9   0     0    0     0 SW    0.1  0.0   0:00 keventd
 1977 pattyo     9   0  2044 2044  1704 S     0.1  1.2   0:00 gnome-smproxy
 2021 pattyo     9   0  4328 4328  3480 S     0.1  2.7   0:00 gnome-terminal
 2122 pattyo     9   0  3876 3876  3240 S     0.1  2.4   0:00 deskguide_apple
    1 root       8   0   520  520   452 S     0.0  0.3   0:04 init
    3 root       9   0     0    0     0 SW    0.0  0.0   0:00 kapm-idled
    4 root      19  19     0    0     0 SWN   0.0  0.0   0:00 ksoftirqd_CPU0
    5 root       9   0     0    0     0 SW    0.0  0.0   0:00 kswapd
    6 root       9   0     0    0     0 SW    0.0  0.0   0:00 kreclaimd
    7 root       9   0     0    0     0 SW    0.0  0.0   0:00 bdflush
    8 root       9   0     0    0     0 SW    0.0  0.0   0:00 kupdated
    9 root      -1 -20     0    0     0 SW<   0.0  0.0   0:00 mdrecoveryd

GUI tools provided on your Linux system

gtop

kpm


Memory Usage

Locating a number of processes using ps and killing them all from a single command
$ sudo kill `ps -lax | grep nfsd | \
    grep -v grep | awk '{print $3}'`

Renicing a process Lab: renicing a process

Use ps -lax to view fields such as the parent process IDs and nice values of your processes. 

  F   UID  PID PPID PRI NI VSZ  RSS  WCHAN STAT TTY    TIME COMMAND

000  500 8529 8527 15  0  2556 1236 wait4 S    pts/1  0:01 /bin/bash

If you have a runaway process that is driving the load way up on your system create a shell with a higher priority than the default using the nice command. Nice takes command line arguments:
$ sudo nice --adjustment=-10 bash
$ ps -lax | tail
This command creates a shell with a nice value of 10 less than the default.
When would this come in handy?

Exercise: Runaway Processes

Create a script file to create load on your system.
*Don't forget to make it executable* OR from the command line (from bash shell): Renice the process to use less CPU: The following example is from page 60 in your text.
What happens when a user executes this program?
How do you identify a process that is consuming huge amounts of disk space? Process States

State Meaning
Runnable The process can be exectued whenever the cpu is ready
Sleeping The process is waiting for some resource, or keyboard input
Zombie The process is finished but has not reported its status back to its parent
Stopped The process is suspended, received a STOP signal

Zombie Processes (nothing to do with Halloween)

Sometimes a child process is killed (with a -9 signal) but the parent doesn't acknowledge the termination.

Daemons: Never-Ending System Processes Daemons typically have process names that end in 'd' Invoking Foreground and Background Processes Foreground Process The syntax for a foreground process:
command options arguments
Background Process The syntax for a background process:
command options arguments > backfile &
Here you are saving the output to the file backfile. Note the ampersand at the end of the command. This is how a background process is invoked.

Job Control in the bash and tcsh Shells

jobs

Lab: Background Processes
$ nohup yes >> junk 2>&1 &
[1] 6292
$ jobs
[1]+  Running     nohup yes >>junk 2>&1 &
$ ps
  PID TTY          TIME CMD
  776 pts/1    00:00:00 bash
 6292 pts/1    00:00:11 yes
 6295 pts/1    00:00:00 ps
$ kill -15 6292
$ jobs
[1]+  Terminated    nohup yes >>junk 2>&1

Note:  kill -15  is using the TERM signal for graceful termination.

Suspending and Resuming a Foreground Task Foreground Task Control

To suspend a foreground task:

$ yes > /dev/null
<Ctrl-z>
[1]+  Stopped
To resume a suspended task:
[pattyo@ponto fall2001]$ fg
yes >/dev/null
To suspend a background task you will first need to bring it to the foreground using the fg command. Then suspend it using <Ctrl>-z>.

If you have have more than one process running in the background you will need to provide a jobnumber to the fg command.

Example:

$ yes > /dev/null
<Ctrl>z
[1]+  Stopped                 yes >/dev/null

$ yes > /dev/null &
[2] 6440

$ jobs
[1]+  Stopped                 yes >/dev/null
[2]-  Running                 yes >/dev/null &

Now we have two jobs, one stopped, one running

$ fg %2
yes >/dev/null
<Ctrl>z
[2]+  Stopped                 yes >/dev/null

$ jobs
[1]-  Stopped                 yes >/dev/null
[2]+  Stopped                 yes >/dev/null