5.0 The Process


5.1 Viewing Processes

The the ps command without any options will gives information about your processes that are currently running in the terminal. However, there are many other proceses on the system. To get a full listing of all the processes use the -e (all processes) option. Another usefull option to ps is the -f (full) option. Lets use both of these to get all the processes on our machine.

% ps -ef 
    UID   PID  PPID  C    STIME TTY      TIME COMMAND
    root     0     0  0  Dec 31  ?        0:56 swapper
    root     1     0  0  Mar  9  ?        0:02 init
    root     2     0  0  Mar  9  ?        4:15 vhand
Most of the process are system processes or daemons. They are processes that act without user request. To search for a particular string in the process list I could pipe the output of ps to the grep command.
% ps -ef | grep scot
    UID   PID  PPID   C  STIME   TTY      TIME COMMAND
    scot  4948  4944  0  Mar 17  ttyp2    1:59 emacs chapter2.html
    scot  4934  4928  0  Mar 17  ?        2:08 vuewm
    scot  4941  4928  0  Mar 17  ?        0:08 hpterm
    scot  4944  4940  0  Mar 17  ttyp2    0:04 tcsh
    scot  4942  4928  0  Mar 17  ?        0:00 xload -name vueload -nolabel 
This will give a listing of all proceses that the user scot is running.

5.2 Background Processes

So far we have had to wait for our commands to complete before we were able to perform other tasks. These processes are foreground processes since they are at the foreground of our attention. When you run a foreground process you must wait until the process completes before you can get the system prompt back. It is possible to get the system prompt back immediately by running the command in the background. To run a command in the background you end the command with the ampersand character(&). Lets run a process in the background.

% sleep 400 &
[2] 8416
% 
The sleep command is used as a time delay. It does nothing more than wait until its time runs out. After you ran the process a number appeared on the screen. This number is the process identification number(PID). We will use that number in the next section to kill the process.

If you have created a background process and you log off, your process will be killed since it is associated with your shell. The nohup (no hangup) command allows you to keep processes running after you have logged off. The nohup command, is usefull if you have a process that needs to run all night or a few weeks.

% nohup sleep 400 &
Now when we exit from the shell the sleep process will still be running.

All user processes have the same priority. Each is given the the same treatment by the CPU. However, if you leave a process running with the nohup command you should use the nice command to reduce its priority.

% nohup nice sleep 400 &
This will reduce the priority of sleep by a factor of ten.

5.3 Killing Processes

The kill command provides a way to stop commands that you not longer want. Occasionally you will need to stop a process. First you must determine the process identification number (PID). If you know the command name you can use grep to find the process. Lets find the sleep process that we created in the previous section and then kill it.

% ps -ef | grep sleep
    scot  8419  4944  3 20:17:42 ttyp2    0:00 grep sleep
    scot  8416  4944  0 20:17:32 ttyp2    0:00 sleep 400
% kill 8416
[2]    Terminated             sleep 400
%
A process is stopped when a kill signal is sent to it. Signal number range from 1 to 31. When you kill a process the default signal is 15. Some processes will ignore this signal. A sure way to kill these processes is with signal number 9.

One process that will not die with kill signal 15 is the shell. Lets start a C-shell process and then try to kill it with signal 15. We will then kill the process with signal 9.

% csh
% ps
   PID TTY      TIME COMMAND
  4944 ttyp2    0:04 tcsh
  8429 ttyp2    0:00 csh
  8430 ttyp2    0:00 ps
% kill 8429
% ps
   PID TTY      TIME COMMAND
  4944 ttyp2    0:04 tcsh
  8429 ttyp2    0:00 csh
  8431 ttyp2    0:00 ps
% kill -9 8429
Killed
%


Last Modified: 4/4/94 by Scott Miller