Process Management!

The wheels are in motion

Introduction

Linux in general is a fairly stable system. Occasionally, things do go wrong however and sometimes we also wish to tweak the running of the system to better suit our needs. In this section we will take a brief look at how we may manage programs, or processes on a Linux system.

So what are they?

A program is a series of instructions that tell the computer what to do. When we run a program, those instructions are copied into memory and space is allocated for variables and other stuff required to manage its execution. This running instance of a program is called a process and it's processes which we manage.

What is Currently Running?

Linux, like most modern OS's is a multitasking operating system. This means that many processes can be running at the same time. As well as the processes we are running, there may be other users on the system also running stuff and the OS itself will usually also be running various processes which it uses to manage everything in general. If we would like to get a snapshot of what is currently happening on the system we may use a program called top.

top

Below is a simplified version of what you should see when you run this program.

  1. top
  2. Tasks: 174 total, 3 running, 171 sleeping, 0 stopped
  3. KiB Mem: 4050604 total, 3114428 used, 936176 free
  4. Kib Swap: 2104476 total, 18132 used, 2086344 free
  5.  
  6.  PID USER %CPU %MEM COMMAND
  7. 6978 ryan 3.0  21.2 firefox
  8.   11 root 0.3   0.0 rcu_preempt
  9. 6601 ryan 2.0   2.4 kwin
  10. ...

Let's break it down:

  • Line 2 Tasks is just another name for processes. It's typical to have quite a few processes running on your system at any given time. Most of them will be system processes. Many of them will typically be sleeping. This is ok. It just means they are waiting until a particular event occurs, which they will then act upon.
  • Line 3 This is a breakdown of working memory (RAM). Don't worry if a large amount of your memory is used. Linux keeps recently used programs in memory to speed up performance if they are run again. If another process needs that memory, they can easily be cleared to accommodate this.
  • Line 4 This is a breakdown of Virtual memory on your system. If a large amount of this is in use, you may want to consider increasing it's size. For most people with most modern systems having gigabytes of RAM you shouldn't experience any issues here.
  • Lines 6 - 10 Finally is a listing of the most resource intensive processes on the system (in order of resource usage). This list will update in real time and so is interesting to watch to get an idea of what is happening on your system. The two important columns to consider are memory and CPU usage. If either of these is high for a particular process over a period of time, it may be worth looking into why this is so.The USER column shows who owns the process and the PID column identifies a process's Process ID which is a unique identifier for that process.

Top will give you a realtime view of the system and only show the number of processes which will fit on the screen. Another program to look at processes is called ps which stands for processes. In it's normal usage it will show you just the processes running in your current terminal (which is usually not very much). If we add the argument aux then it will show a complete system view which is a bit more helpful.

ps [aux]

It does give quite a bit of output so people usually pipe the output to grep to filter out just the data they are after. We will see in the next bit an example of this.

Killing a Crashed Process

It doesn't happen often, but when a program crashes, it can be quite annoying. Let's say we've got our browser running and all of a sudden it locks up. You try and close the window but nothing happens, it has become completely unresponsive. No worries, we can easily kill Firefox and then reopen it. To start off we need to identify the process id.

  1. ps aux | grep 'firefox'
  2. ryan 6978 8.8 23.5 2344096 945452 ? Sl 08:03 49:53 /usr/lib64/firefox/firefox

It is the number next to the owner of the process that is the PID (Process ID). We will use this to identify which process to kill. To do so we use a program which is appropriately called kill.

kill [signal] <PID>

  1. kill 6978
  2. ps aux | grep 'firefox'
  3. ryan 6978 8.8 23.5 2344096 945452 ? Sl 08:03 49:53 /usr/lib64/firefox/firefox

Sometimes you are lucky and just running kill normally will get the process to stop and exit. When you do this kill sends the default signal ( 1 ) to the process which effectively asks the process nicely to quit. We always try this option first as a clean quit is the best option. Sometimes this does not work however. In the example above we ran ps again and saw that the process was still running. No worries, we can run kill again but this time supply a signal of 9 which effectively means, go in with a sledge hammer and make sure the process is well and truly gone.

  1. kill -9 6978
  2. ps aux | grep 'firefox'

Normal users may only kill processes which they are the owner for. The root user on the system may kill anyones processes.

My Desktop has locked up

On rare occassions, when a process crashes and locks up, it can lock up the entire desktop. If this happens there is still hope.

Linux actually runs several virtual consoles. Most of the time we only see console 7 which is the GUI but we can easily get to the others. If the GUI has locked up, and we are in luck, we can get to another console and kill the offending process from there. To switch between consoles you use the keyboard sequence CTRL + ALT + F<Console>. So CTRL + ALT F2 will get you to a console (if all goes well) where you can run the commands as above to identify process ids and kill them. Then CTRL + ALT F7 will get you back to the GUI to see if it has been fixed. The general approach is to keep killing processes until the lock up is fixed. Normally you can look for tell tale signs such as high CPU or Memory usage and start with those processes first. Sometimes this approach works, sometimes it doesn't and you need to restart the computer. Just depends how lucky you are.

Foreground and Background Jobs

You probably won't need to do too much with foreground and background jobs but it's worth knowing about them just for those rare occassions. When we run a program normally (like we have been doing so far) they are run in the foreground. Most of them run to completion in a fraction of a second as well. Maybe we wish to start a process that will take a bit of time and will happily do it's thing without intervention from us (processing a very large text file or compiling a program for instance). What we can do is run the program in the background and then we can continue working. We'll demonstrate this with a program called sleep. All sleep does is wait a given number of seconds and then quit. We can also use a program called jobs which lists currently running background jobs for us.

jobs

  1. sleep 5

If you run the above example yourself, you will notice that the terminal waits 5 seconds before presenting you with a prompt again. Now if we run the same command but instead put an ampersand ( & ) at the end of the command then we are telling the terminal to run this process in the background.

  1. sleep 5 &
  2. [1] 21634
  3. [1]+ Done sleep 5

This time you will notice that it assigns the process a job number, and tells us what that number is, and gives us the prompt back straight away. We can continue working while the process runs in the background. If you wait 5 seconds or so and then hit ENTER you will see a message come up telling you the job has completed.

We can move jobs between the foreground and background as well. If you press CTRL + z then the currently running foreground process will be paused and moved into the background. We can then use a program called fg which stands for foreground to bring background processes into the foreground.

fg <job number>

  1. sleep 15 &
  2. [1] 21637
  3. sleep 10
  4. (you press CTRL + z, notice the prompt comes back.)
  5. jobs
  6. [1]- Running sleep 15 &
  7. [2]+ Stopped sleep 10
  8. fg 2
  9. [1] Done sleep 15

CTRL + z is used in Windows but for the purpose of running the undo command. It is not uncommon for people coming from the Windows world to accidentally hit the key combo (especially in the editor VI for instance) and wonder why their program just dissappeared and the prompt returned. If you do this, don't worry, you can use jobs to identify which job it has been assigned to and then fg to bring it back and continue working.

Summary

top
View real-time data about processes running on the system.
ps
Get a listing of processes running on the system.
kill
End the running of a process.
jobs
Display a list of current jobs running in the background.
fg
Move a background process into the foreground.
ctrl + z
Pause the current foreground process and move it into the background.
Control
We have quite a bit of control over the running of our programs.

Activities

Time for some fun:

  • First off, start a few programs in your desktop. Then use ps to identify their PID and kill them.
  • Now see if you can do the same, but switch to another virtual console first.
  • Finally, play about with the command sleep and moving processes between the foreground and background.