Skip to main content Skip to navigation

1.10 Processes

When Linux is running, many activities are happening at the same time. These activities are called processes. Usually, the kernel has many processes running, and each user logged in will also have several processes running. For example, you might wish to run a C program that takes a long time to finish. It's possible in Linux to run your program in the background, while freeing up the prompt in your shell to get on with some other work (like analysing your data). So, in Linux, several programs can run at once, and each process can run at a different priority level. In this example, you might wish to give the C program a lower priority than your data analysis program.

1. Jobs: background and foreground

All shells under Linux have this capability, it is called job control, and it lets the user switch back and forth between processes, putting processes into the background, suspending processes, killing processes and changing the priority at which any of these processes are handled.

When ever you launch a new process by typing a command in your shell, you can automatically put this process into the background by appending your command with a & .

Of all jobs running at one time, at most one job is the foreground job. All other jobs will be suspended jobs or background jobs. The foreground job ties up the shell prompt - so if you want access to your shell in this instance, this foreground job must be put into the background. To do this you must first suspend the forground job, tying CtrlZ in your shell (you don't need to hit return).

You can always find out what jobs you have launched and are running in the current shell by typing the command jobs . Again, an example may help.


Let's say the user phillipa types jobs at the prompt. She is running 3 processes in this directory. One is called emacs (an editor), one is called xmakemol, and another is called xmgrace (she has suspended this job). The output of jobs is as follows;
 [1]  - 21682 Running                       emacs carts2hexfrac.f 
 [2]    24410 Running                       xmakemol -f
 [3]  + 25174 Suspended                     xmgrace 
The first entry in each line gives the job number. The second entry gives an indication of the most recently stopped (or started) job - in other words, the current job. The current job is signified by a + . Also indicated in this column is the previous job (the one that was current before the current job was started/stopped. The previous job is signified by a - .
The third column gives the status of each job. In this case you can see that two are running and one is suspended. A suspended job will stay in this state until it is restarted or killed.
The final entry on each line gives the actual process.  

There are several Linux commands regarding jobs, so you need a way to refer to each job. This can be done by referring to the job number, symbollically given as %n for job number n. You can also refer to the current job with just % .

We've already seen an example of moving the foreground job to the background. You can also move a background job to the foreground by typing fg %n (where n is a number). You can also cause any suspended job (of which there might be several) to execute in the background by typing bg %n (where n is a number).

You can kill any job (so it will cease to exist as a process) by typing kill %n (where n is a number). The kill command comes with plenty of options - you'll find out more later.

Putting it into Practice:

Part I Suppose you'd like to start a program called emacs from your shell. You'd also like to put this job into the background. You can then run this job from the prompt by typing
 emacs & 
After you hit return, a window should pop up, and you should have control of your shell prompt as normal.
Now, check what processes you have active at the moment by typing jobs . Note the job number of your emacs process.
You should now kill your emacs process by entering kill %n (where n is your job number). The emacs window should vanish.
Part II Try launching emacs from your shell again, this time without the ampersand (no & at the end ).
The emacs window will again pop up, but this time you will not have control of the shell prompt, since emacs is now running as a foreground job.
You can always kill a foreground process by typing Ctrl C. Or, you can background this process by the usual route of suspending (Ctrl Z followed by bg).

Back to top

2. Finding the process id

Linux assigns each process an identification number called a process id (PID) when the process is created. There are several ways for a user to determine a PID for a given process.

First, if you launch a background process by issuing a command to your shell, after you hit return, you should see two items displayed in the standard output of your xterm. The first one will be in square brackets (this is the job number), and the second is a number (this is the PID).


The user harry launches an emacs process by typing
 emacs & 
Immediately after hitting return, the following output is sent to his display
 [5] 18408
This tells harry that this job number is 5, and the PID for this emacs process is 18408

Using ps

Sometimes a process is not explcitly launched by the user (e.g. a process can be launched by a shell script executed by the user), and therefore the above method can not be used to determine the PID. Another way to find the PID is to use the command ps . This command reports the process status for ALL processes running on your machine. This may be a long list, since even if you are not running any jobs yourself, the kernel runs many background processes (known as daemons). ps (stands for Process Status) comes with many options, the most useful being:

-e produce information about every process
-f produce a full listing
-l produce a long listing
a helpful option is -ef which gives two of these options.


The user harry wants a status report of all processes on his machine. To do this he types
 ps -ef 
Immediately after hitting return, output is sent to his display, a sample of this is shown below:
  root     0     0  0   Sep 18 ?    0:17 sched
  root     1     0  0   Sep 18 ?    0:54 /etc/init -
daemon   156     1  0   Sep 18 ?    0:00 /usr/lib/nfs/statd

The headings on this table of output have the following meanings

UID the user who owns the process
PID the process id, a unique identifier assigned to each process
PPID the parent process id, the process that spawned the current process
C the percentage of CPU usage (per core) for the process
STIME the start time for the current process
TTY the controlling terminal for the current process
TIME the amount of CPU time accumulated by the current process
CMD the command used to invoke the process

Using top

Another way to find out PIDs is to use the command top. As output this command gives a continually updated listing of active processes in real time. It is run ONLY as a foreground process. The output can be sorted in terms of CPU usage (this is the default), memory usage and runtime. top has an interactive element - while it is running, you can type many options, a sample of these are given below:

space key immediately updates the display
n or # change the number of processes to show
M sort display by memory usage
k kill a job

The last option is important. To kill a job in top, after typing k, you will be prompted for the PID of the process you wish to kill. Type in the PID of your desired process and hit return. You will then be prompted for the signal with which to send this kill command (a signal notifies a process of any abnormal external event). To make sure the process really dies, use the signal "9" (type 9 when prompted). This is known as a "hard kill".

Putting it into Practice:

Start by typing top at your prompt and hit return. As output, you should see a list of processes under various headings (these heading should be self-explanatory). One of those headings, PID, gives the process id for each process.
While top is running, type k in the window.
You will be prompted for a process id. Find the PID of the top process itself (it should be up the top of the list), type in this PID and hit return.
You will now be prompted for the signal. Type 9 and hit return.
You top session should terminate immediately!

Process Priority

From running your top process in the previous example, most of the headings in the output display should make sense. One heading, PRI deserves a little more explanation. The number under PRI gives the priority of the job. For regular users, you can set this number between 1 (highest priority) and 19 (lowest priority) - more about this later. The default priority is 10. System administrators can set the priority to be a negative number (even higher priority), all the way to -20.

Back to top

3. Killing jobs by PID

Now that you know how to get a PID by using the ps command, you can kill jobs on the command line, even if these processes don't show up when you type jobs. You can simply kill a process by using the kill command in the following way:
kill -signal PID

usually, the signal number will be 9 (as explained above)

For example, if user harry wants to kill an emacs process, knowing that the PID of this process is 6743, at his shell prompt, he can type

 kill -9 6743 

Back to top