To manage a desktop and control its resources, it is very important to grasp the concepts behind processes running in the computer. You can think of a process simply as an independent program entity executing and using computer resources such as CPU time and memory. Any application that is executed is started as an independent process. An application may start its own child processes ”indeed many applications, such as Web servers and database servers, have multiple processes to tend to the needs of different clients at the same time.
Typically, a process is started by a parent process . The newly launched process can in turn launch child processes if it wants to. When you are working at the desktop, any new program is executed as a child process of the program that is receiving and interpreting your commands. For example, when you are at the terminal prompt, you are interacting with the shell interpreter program (by default, this is bash). The shell itself is running as a process in the computer. Any program that you execute at the bash prompt is launched as a child process of the bash program.
In fact, every executing program on the computer is a process that has been launched by some other parent process; so all the running processes in the computer can be represented in the form of a hierarchical tree. There is only one exception ”the first process of the computer. This is the init program, which is launched by the operating system itself just after booting. Thereafter init forks off child processes, which, in turn, may fork off other child processes of their own.
Because of the underlying way new processes are launched, launching a child process in the UNIX environment is also frequently referred to as forking. When launching a child process, the parent process makes a copy of itself by forking, and then executes the child program in the memory space of the new copy.
The System Monitor program discussed earlier in the chapter also allows you to see the processes running in the machine. You can see this list of processes in Figure 2-46.
You can view the processes sorted by any of the columns on display by clicking the respective column header. Here, you can see that we ve sorted the processes according to their memory usage.
At the terminal, you can use the ps program to view the processes running in the machine in a non-dynamic way. You can invoke this program in a variety of ways, which are listed on its man page. However, we will mention some commonly used arguments of this program.
To view all the processes running within your current logging session, you can use the ps program without any arguments. To view all the processes running in the machine, you can use the command ps ax , as demonstrated in Figure 2-47.
The pstree program allows you to view all the processes in the system in a tree format, and gives a good idea of the hierarchical nature of processes in the computer. Figure 2-48 shows just a small part of the output of this program, beginning with the init process.
As you saw earlier in the chapter, the terminal also provides the t op program, which allows you to view the process list in an interactive manner. It shows the process list just as you would see it on the GUI system monitor, and allows you to sort the results by pressing various keystrokes. To get more help on the keys to use for sorting the display, press the help (?) key.
Sometimes you need to exercise some control on a process to keep your memory or CPU resources in check. A process might take up all the available memory in the system and make the system virtually unusable. In such a case, terminating the offending process to regain control of the system might be appropriate. The act of terminating a process is also referred to as killing it.
You can use the System Monitor (see the section Viewing Running Processes) to kill runaway processes. You can select the target process in the list and select the End Process button at the bottom right of the dialog box. Of course, killing a process can result in loss of unsaved data (depending on how you kill it, as explained shortly). For this reason, a confirmation dialog box allows you to confirm your decision to kill the process.
You can also use the kill command from a terminal window to kill a running process. The kill “9 < process id > command terminates the process abruptly. The < process id > corresponds to the actual process id of the process you are attempting to kill. The “9 indicates that the SIGKILL signal is sent to the process causing it to terminate immediately. Other signals may be delivered to running processes from the command line using the kill command. For more information on specific signals, run the man 7 signal command.
Note that you can kill a process only if you have permission to ”that is, if it s running under your user account or if you have permission to take control of another user s processes. This is a security feature that ensures that a user s processes are safe from being terminated by other users of the machine. The only exception is the root (super user).
The root user can kill any of the machine s processes; therefore, it is necessary to take precautions to ensure that (as the root user) you don t kill a process by mistake. To prevent this, it s a good idea not to log in as the root user to the machine. Instead, log in using a normal user account. Then, whenever you need to perform a task as the root user, you can assume super user permissions using the following command, as explained earlier in the chapter:
After finishing your immediate job as the root user, log out and return to the login shell of the previous user.
You can also use the t op program to kill processes. You can sort the display of the top utility by resource usage (in the same way as you can sort the processes using the System Monitor), which is useful when a process is taking up too much CPU or memory resource. Sorting the processes in this way brings the offending program to the top of the list and makes it easy to identify. You can then use the process s process ID to kill it using the k command of the top program.
Notice that the top program asks for a signal number to send to the process. A kill signal is a special message sent to the process by the current process. If you just accept the default option of 15, a signal called SIGTERM is sent to the target process asking it to shut down on its own. This allows the target process to shut down cleanly after saving all its unsaved information. However, if the program seems to be ignoring this signal, you can repeat the process sending the signal number of 9 instead; this simply terminates the process without giving it a chance to shut down on its own.