O'Reilly logo

Fedora Linux by Chris Tyler

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Managing Processes

A process is a running instance of a program. If you run a program twice, two processes are created. In order to manage a Fedora system effectively, you must be able to monitor and control processes.

How Do I Do That?

Fedora provides multiple tools to monitor process activity and resource usage, modify process priority, and terminate processes.

Processes are identified by a Process ID (PID) number, which is sequentially assigned. There is a small set of information associated with each process, including:

nice

A value used to alter a process’s scheduling priority, which determines how much CPU time the process receives. The actual priority assigned to a process is calculated based upon this factor, as well as how much CPU time the process has recently received and how many input/output (I/O) operations it has recently performed. This value is inherited by child processes.

parent process ID

The PID of the process that started the process. If the parent process disappears, this is replaced by 1 (the init process).

real user ID and effective user ID

The numeric user ID of the user actually running the program and the effective user running the program. These can be different only when the suid mechanism is active (see Lab 4.8, “Control Access to Files”), although an effective user ID remains in effect when a suid program calls a non-suid program.

real group ID and effective group ID

The numeric group ID of the group actually running the program and the effective group running the program. These are similar to the real and effective user IDs in that they will be different only when the sgid mechanism is active.

umask

The permission mask received from the parent process.

tty

The terminal associated with the program (if applicable). This permits all programs on that terminal to receive a hangup signal (HUP) when the terminal connection is lost, which is the case when a telephone modem call is hung up, a terminal window is closed, or a remote access Telnet/SSH session is terminated. This value is inherited by child processes.

It’s important to realize that at any particular point in time, most processes are sleeping while they wait for some resource to become available. That resource might be a mouse click, a keystroke, a network packet, some data from disk, or a particular time of day.

Monitoring process information graphically in GNOME

The menu item Applications→System Tools→System Monitor will run gnome-system-monitor and present the display shown in Figure 4-13.

GNOME System Monitor window

Figure 4-13. GNOME System Monitor window

This display has two tabs:

Processes

Displays a table of current processes with information about each.

Resources

Displays scrolling graphs displaying CPU, memory, and swap usage.

By default, the Processes tab displays the name of the program executing, process status (Sleeping or Running), Virtual Memory (VM) size, percentage of CPU time, the SELinux Security Context, and the arguments used on the command line that started the process (including the command name).

The default display shows the most useful information about each process, but to configure the display to your liking, you can:

Add and remove fields

Select Edit→Preferences to view a list of available fields (columns) with a checkbox for each. Check or uncheck items to add them to or remove them from the list. Close this window when you are done editing the field list.

Reorder and resize columns

Drag column headings to rearrange the order in which they are displayed. To change a column width, click between it and an adjacent column, and then drag to the desired width.

Sort a column

Click on a column heading to select that column for the sort sequence. An arrow will appear in the header (as shown on the VM Size column in Figure 4-13); click on the heading again to toggle between ascending and descending sort order.

Filter by process type

The Show menu lets you select your own processes, all processes on the system, or just the active (running, not sleeping) processes.

To terminate a process, highlight it by clicking on it and then click the End Process button, type Alt-P, or right-click on the process and select End Process. If that doesn’t cause the process to terminate within a few seconds, right-click on the process and select Kill Process (or highlight the process and type Ctrl-K).

Warning

You won’t be able to terminate processes owned by other users (including system processes) this way because you have insufficient permission. It is possible to run this program as root, which will let you terminate any process:

# gnome-system-monitor

Terminating the wrong process(es) can leave your system in a partially functioning or unusable state, and it may be necessary to reboot the system to recover. Be careful!

Monitoring process information graphically in KDE

If you’re using KDE in Fedora, the menu item System→KSysGuard will start ksysguard and display the window shown in Figure 4-14.

KSysGuard window

Figure 4-14. KSysGuard window

This tool is very customizable, but the basic display is similar to the GNOME System Monitor, except that the CPU usage is broken down into User% and System%, and the memory size is broken down into VmSize (total process size) and VmRSS (Resident Set Size, the portion of the VmSize currently in memory instead of swap). Use the Process Table tab to monitor and control running processes.

To customize the display, you can:

Show and hide columns

To remove a column from the display, right-click somewhere within that column (not on the heading) and select Hide Column. To add a column, right-click in an existing column (again, not on the heading), and select Show Column and then the column name you wish to add.

Reorder and resize columns

Drag column headings to rearrange the order in which they are displayed. To change a column width, click between it and an adjacent column, and then drag to the desired width.

Sort a column

Click on a column heading to select that column for the sort sequence. Click on the heading again to toggle between ascending and descending sort order.

Filter by process type

The pull-down menu at the bottom of the display enables you to choose whether to display all processes, system processes (such as servers), user processes for all users, or just your own processes.

To terminate a process, right-click on the process and select Send Signal→SIGTERM. If that doesn’t cause the process to terminate within a few seconds, highlight the process and then click the Kill button in the lower-right corner of the window (right-click on the process and select Send Signal→ SIGKILL).

Tip

Just like the GNOME System Monitor, the KSysGuard program can’t terminate processes owned by other users (including system processes) when run by a normal user. To run the program as root:

# ksysguard

KSysGuard can monitor many aspects of system status in addition to the process table; it’s also capable of monitoring remote systems. See the KSysGuard Manual for details (press F1 in the KSysGuard window).

Monitoring process information on a character display

A similar tool is available for character-mode displays, named top:

$ top

The output from top is shown in Figure 4-15.

Like the graphical process monitors, top updates its display regularly—every three seconds by default. You can customize the display using the controls shown in Table 4-15.

Output from top

Figure 4-15. Output from top

Table 4-15. Top customization options

KeyDescription
?Display help.
uRestrict the display to processes owned by one user.
MSort by memory usage.
PSort by current CPU usage.
TSort by time (cumulative CPU usage).
mToggle memory summary on/off.
fField-list customization display. You will see a menu of possible fields; press the letter of the field you wish to toggle on/off, then Enter to exit from this display.
oField-order customization display. You will see a list of displayed fields; type the uppercase letter for a field to shift the field left on the display, or type the lowercase letter to shift it right. Press Enter to exit this display.

To end a process, type k (for kill). Type in the process ID and press Enter; top will prompt you for the signal to be used. Press Enter to accept the default (15). If the process does not terminate within a few seconds, repeat the procedure with the signal 9.

Displaying process information from the shell prompt

Instead of using top to continuously monitor information, you can use the ps (process status) command to display a snapshot of the process table at a particular point in time.

By default, ps shows only processes executed by you on the current terminal:

$ ps
  PID TTY          TIME CMD
14797 pts/1    00:00:00 bash
22962 pts/1    00:00:00 ps

This shows the process ID, terminal device (pts/1 means /dev/pts/1), total amount of CPU time consumed (less than one second in this example), and the command executed. This information alone is rarely useful, so ps is almost always used with some arguments.

ps uses options to select the processes to be displayed. The most useful ones are:

-A-e

All (or everyone’s) processes

-u user

Processes owned by user (which can be a username or numeric user ID)

Other options are used to control the output format:

-f

Displays full information, including the UID, PID, PPID, start time (STIME), terminal (TTY), total CPU time used (TIME), and command (CMD).

-F

Displays extra-full information: everything included in -f, plus the processor number of the CPU the program is running on (PSR) and the approximate kilobytes of RAM used (RSS).

Tip

Like ls, the ps command has dozens of options. The Fedora version of ps can use Unix System V syntax or BSD syntax, so many option letters have two meanings; the one that is used depends on whether the option is specified with or without a hyphen!

To see the full documentation for ps, view the manpage—but be prepared to take some time; it’s over 16 pages long!

Terminating processes from the shell prompt

You can terminate processes by command name or by PID. When you terminate a process by name, you save yourself the hassle of looking up the PID, but if there is more than one process of the same name running, you will kill them all.

To kill by command name:

$ killall 
                  
                     xclock
                  

If the process doesn’t terminate within a few seconds, add the -KILL argument:

$ killall 
                  -KILL 
                  
                     xclock
                  

Note that this will kill only processes of that name that are owned by you; you don’t have permission to kill other users’ processes unless you are root. You will see an error message if other users have a process of the same name running, but this will not affect the killing of the processes that you own.

To kill PID 48292:

$ kill 
                  
                     48292
                  

Again, if that doesn’t work within a reasonable period of time, add the -KILL argument:

$ kill -KILL 
                  
                     48292
                  

How Does It Work?

The Linux kernel has only two basic functions for starting processes: fork() and exec().

fork() makes an exact copy of the current process and starts it running. exec() replaces the currently running program with a new program, running in the same process. So to get a new program running, the shell uses fork() to create a child process (a copy of the shell) and then uses exec() to change the program running in the child process.

When a child process is created, a number of variables are inherited from the parent process, including the real and effective user IDs, the real and effective group IDs, the umask, the terminal, the current working directory, and the environment variables.

Processes are generally permitted to run on a CPU until their timeslice—the amount of time allocated to them by the scheduling algorithm—is over, at which point another process is scheduled to be run.

However, processes frequently give up the CPU early because they reach a point when they need a resource to continue; this is called blocking. This is often due to slow input/output operations; no matter how fast your disk drive is, the CPU is still faster, so when one process is waiting for disk data, another process can be executing.

The difference between your typing speed and your CPU speed is even greater; most people type six characters per second or less, so on a 3 GHz PC, the CPU will average at least 500 million operations between keystrokes.

Since processes are usually waiting for data, it’s not uncommon for programs to run for only a few seconds a day. I’ve been using my X display server heavily all day, and it’s accumulated less than 30 minutes of CPU time; my POP3 mail server, which is accessed 600 times and transfers several hundred megabytes of data each day, accumulates less than 20 seconds of CPU time a day.

The 2.6 kernels now used in Fedora do fully preemptive scheduling, which means that when data does arrive for a sleeping process, and that sleeping process has a higher priority than the process currently running, the kernel will preempt the running process and immediately schedule the new process for execution (instead of waiting for the currently executing process to reach the end of its timeslice).

The kernel dynamically changes the priority of a process based on the amount of time since it last executed, the amount of time it has executed recently, the amount of I/O it is performing, and the nice value.

To terminate a running process, a numeric signal is sent to that process. To see all of the available signals, use the -l argument to kill, which shows the signal names and numbers:

$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      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      34) SIGRTMIN    35) SIGRTMIN+1
36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4  39) SIGRTMIN+5
40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8  43) SIGRTMIN+9
44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13
52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9
56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6  59) SIGRTMAX-5
60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2  63) SIGRTMAX-1
64) SIGRTMAX

Each of these signals has a specific meaning, which can usually be determined from the signal name; for example, SIGHUP is the hangup signal, SIGINT is the interrupt signal (sent when you use Ctrl-C to try to interrupt a program running on a terminal or character-mode VT), SIGFPE is the signal for floating-point exceptions (such as division by zero), and SIGPWR is the signal for a power failure. Most of these signals are generated automatically by the kernel and basic server processes.

In most cases, a process can arrange to catch a particular signal and do something; for example, a text editor might save the current file when SIGHUP is received (the connection to the terminal is lost). If the process has not arranged to catch a signal, a default action is taken. In most cases, the default action is to terminate the process, but the default for some signals (such as SIGPWR) is simply to ignore the signal and keep on running.

SIGTERM is used to request that a program terminate itself. Most programs catch this signal and clean up before they terminate, deleting any temporary files, saving data if necessary, informing network peers that they are terminating (where appropriate), and so forth. This is the default signal sent by top and kill, and the signal sent by the GNOME System Monitor when you specify End Program.

SIGKILL is uncatchable. It always terminates a program. This is useful when you wish to definitely terminate a program, but it doesn’t give the program an opportunity to shut down gracefully, so files and network communications may be left in half-finished states, which may cause future problems. For this reason, it should be used as a last resort. SIGKILL is the signal sent by the Kill Program option in System Monitor and the Kill button in KSysGuard.

What About...

...monitoring CPU load continuously?

Both KDE and GNOME provide panel applets that display a continuous graph of the current CPU load, memory usage, and more. To add this applet to your panel bar, right-click on an empty area on the bar and select “Add to Panel.” For GNOME, select System Monitor; for KDE, select Applet→KSysGuard. You can configure the display by right-clicking on it and selecting Preferences or Properties.

...starting a process with a lower (or higher) priority than normal?

The nice command starts a process with a lower-than-normal priority. The priority can be reduced by any value from 1 to 19 using the -n argument; without -n, the priority is reduced by a value of 10. The command to be run is the only other argument required (any additional arguments are used as arguments to that command):

$ nice 
                  -n 
                  
                     15 xboard
                  

To raise the priority of a process, you must be root; supply a negative priority adjustment between –1 (slight boost in priority over normal) to –20 (highest priority):

# nice -n 
                  
                     -12 xboard
                  

...changing the priority of an existing process?

renice is the tool for this:

$ xboard &
[3] 27365
$ renice 
                  
                     5 27365
                  
27365: old priority 0, new priority 5
$ renice
                  
                      2 27365
                  
renice: 27365: setpriority: Permission denied

Tip

Note that the value used with the nice command is the opposite of what you may usually associate with a priority. Put another way, a nice level of –20 results in a process that isn’t very nice to its fellow processes, since it’s running at a high priority and hogs the CPU.

Notice that renice does not permit the user to increase the priority of a process, even if the user lowered it in the first place. However, root can set any priority she chooses:

$ renice -5
                  
                      27365
                  
renice: 27365: setpriority: Permission denied
# renice 
                  
                     2 27365
                  
27365: old priority 5, new priority 2
# renice 
                  
                     -5 27365
                  
27365: old priority 2, new priority -5

You can also adjust the priority of processes in System Monitor and KSysGuard using the options on the context menu (right-click on the process you wish to adjust).

...starting and managing background processes?

When using the shell, you can start a process in the background by placing an ampersand after the command:

$ xboard & 
[21771]
$ mc &
[21783]
$

The shell will display the PID of the background process, then immediately present a new prompt, permitting you to enter additional commands before the background command has finished executing.

You can display background processes using the jobs command:

$ jobs
[1]-  Running                 xboard &
[2]+  Stopped                 . /usr/share/mc/bin/mc-wrapper.sh

Any program that attempts to communicate through the character interface, such as Midnight Commander (mc) in this example, will be stopped. Programs that communicate through the graphical user interface, such as xboard, are free to do so while running in the background.

To put a stopped command in the foreground so that you can interact with it, use the fg command:

$ fg 
                  
                     2
                  

The argument is the job number as reported by the jobs command. You can stop the current foreground process by pressing Ctrl-Z.

To run a stopped process in the background, use the bg command:

$ fg 
                  
                     1
                  
xboard
...User presses Ctrl-Z...
[1]+  Stopped                 xboard
$ jobs
[1]+  Stopped                 xboard
[2]-  Stopped                 . /usr/share/mc/bin/mc-wrapper.sh
$ bg 
                  
                     1
                  
[1]+ xboard &
$

You can use a percent sign and a job number instead of a PID when killing processes:

$ kill 
                  
                     %1
                  
$
[3]-  Exit 15                 xboard

Where Can I Learn More?

  • Descriptions of each signal: the manpage for signal(7)

  • The manpages for bash (for job control, including jobs, fg, bg, and the version of kill that is built into bash), top, ps, and kill

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required