4.9. Managing Processes
4.9.1. How Do I Do That?
Fedora provides multiple tools to monitor process activity and resource usage, modify process priority, and terminate 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.
188.8.131.52. Monitoring process information graphically in GNOME
The menu item ApplicationsSystem ToolsSystem Monitor will run gnome-system-monitor and present the display shown in Figure 4-13.
Figure 4-13. GNOME System Monitor window
This display has two tabs:
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:
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).
184.108.40.206. Monitoring process information graphically in KDE
If you're using KDE in Fedora, the menu item SystemKSysGuard will start ksysguard and display the window shown in Figure 4-14.
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:
To terminate a process, right-click on the process and select Send SignalSIGTERM. 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).
220.127.116.11. Monitoring process information on a character display
The output from top is shown in Figure 4-15.
Like the graphical process monitors, top updates its display regularlyevery three seconds by default. You can customize the display using the controls shown in Table 4-15.
Figure 4-15. Output from top
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.
18.104.22.168. Displaying process information from the shell prompt
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:
Other options are used to control the output format:
22.214.171.124. 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.
$ 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.
$ kill 48292
$ kill -KILL 48292
4.9.2. 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.
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.
$ 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.
4.9.3. What About...
126.96.36.199. ...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 AppletKSysGuard. You can configure the display by right-clicking on it and selecting Preferences or Properties.
188.8.131.52. ...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
184.108.40.206. ...changing the priority of an existing process?
$ xboard &  27365 $ renice 5 27365 27365: old priority 0, new priority 5 $ renice 2 27365 renice: 27365: setpriority: Permission denied
$ 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
220.127.116.11. ...starting and managing background processes?
$ xboard &  $ mc &  $
$ jobs - Running xboard & + 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.
$ fg 2
$ fg 1 xboard ...User presses Ctrl-Z... + Stopped xboard $ jobs + Stopped xboard - Stopped . /usr/share/mc/bin/mc-wrapper.sh $ bg 1 + xboard & $
$ kill %1 $ - Exit 15 xboard
4.9.4. Where Can I Learn More?