Main index

Introducing UNIX and Linux


Processes and devices

Overview
Processes
      Process status
      Foreground and background
      Process control
      Signals
Environment
      Environment variables
      Global and local variables
      Executable scripts
Program control
      Job control
      Command history list
      Running a job at a specific time
      Running programs periodically
      Big programs
      Timing a program
      Running programs in order
Quotes and escapes
Devices
Backquotes
Summary
Exercises

Process status

Within a UNIX system there are one or more processing elements (which we will refer to as processors) that can only run a single process at any one time. At any instant, that process can be described completely by a sequence of bytes - some representing the memory contained within the processor, some describing the precise current state of the computation. It is possible to copy these bytes to memory, and replace them by a sequence of bytes from elsewhere in memory. Thus a partially completed process can be temporarily suspended (or stopped), and completed at a later time, and in the meantime another process can be run on that processor.

The kernel manages a large 'pool' of processes, most of which are not running at any specific instant, and moves them to and from the processor (or processors) as required. Much of this movement is automatic and hidden from users - for instance, when there are several users on the machine each with a program which is running, the relevant processes are moved in and out of the processor so that each is allocated a fair percentage of the time (this is known as time-sharing). However, users do have a certain degree of control over their own processes.

A process can be in a number of states, normally either running (currently being worked on by a processor), or stopped (not being processed, but available to continue evaluation when instructed). When a process completes its execution; it is removed from the system entirely and killed. Not all processes will have been invoked by a user - some are so-called system processes, and are constantly running while the system is operational.

When you initially log in, you invoke a program that is a copy of the shell, and known as your login shell. This program forms a process which must be present during the whole time you are logged in. Any subsequent process that is created by you is controlled from the terminal (or window) in which you were typing the command that created it.

If you type in a command to a shell, it can be of two possible types. Either it is a 'built-in' shell command, which is interpreted by the shell process directly, or a new process is created for it. In the latter case the binary code for it is copied from storage, and forms the process to execute that command. The process that is the current shell is suspended and the process for the new command is run. After that process has finished running, it is destroyed, and the shell process is resumed.

You can find out which processes you have, either running or stopped, by means of the command ps. This displays a list of your processes, by default in a format similar to the following:

ps
  PID   TTY      TIME COMMAND
10312 p7   00:02:23 sh
14277 p7   00:00:00 ps

Note that all time when a process has been suspended, or has otherwise been moved out of any processor, is excluded from the figures.

Each process is given a unique identification number, its process-id or PID, which is indicated in the first column of the output from ps. The second column displays the name of the terminal from which that process is being controlled (normally your own terminal or one of your windows). The column headed TIME is the amount of processor time the process has so far consumed while actually running, a measure of how much work the computer has so far done for that process. In the final column, the name of the command that process is running is given. In the above example, the user has two processes running, the login shell sh, and the ps command that is being used to display the list of processes. The login shell (PID 10312) has already consumed 2 minutes and 23 seconds of processor time, whereas ps (PID 14277) has used less than 1 second (rounded down to 0 in the output from ps). The PID number is used internally by the kernel, and is not normally needed by the user.

Another very useful utility is top (not POSIX), which will display in order the processes (including other users') which are taking up the most system resources. This can be useful if your machine appears to be slowing down, and you need to diagnose the problem. It is an interactive utility (press q to terminate it) which combines most of the functionality of ps and kill (see below). In the following sample output, the main system user is Sam, with a program gen taking up 249M of RAM, which has been running for over 18 hours. Chris also has two small programs running which have been active for several days. For the moment you do not need to be concerned with the details of the output of top. The output of top differs significantly between different UNIX systems, and you should always refer to the manual page.

  5:21pm  up 1 day, 23:02,  2 users, load av: 1.00, 1.00, 1.00
150 processes: 146 sleeping, 1 running, 3 zombie, 0 stopped
CPU states:  0.1% user,  0.7% system,  0.0% nice, 99.0% idle
Mem:  128808K av, 120728K used,   8080K free,  37432K buff
Swap: 152248K av,   9812K used, 142436K free  618980K cached

  PID USER    PRI  NI  SIZE  RSS STAT %CPU %MEM   TIME COMMAND
10545 sam      16   0  249M 106M R    24.2 17.1  18.5H gen
14250 chris     9   0    92   80 S    23.4 11.0  74.3H S5Ba
14271 chris     9   0     0    0 SW   23.5 11.0  74.4H S5Bb
28095 sam       9   0     0    0 SW    0.7  0.1   0:00 wget
25716 root     19  19     0    0 SWN   0.0  0.0   0:00 init
   11 root      9   0     0    0 SW    0.0  0.0   0:00 khubd
  372 root      9   0   676  640 S     0.0  0.0   0:00 dhcpcd
  631 root      9   0   892  852 S     0.0  0.0   0:00 sshd
  646 root      9   0   692  688 S     0.0  0.0   0:00 syslogd
  649 root      9   0  1056 1048 S     0.0  0.1   0:00 klogd

Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck