Professional Documents
Culture Documents
6-1
Chapter Objectives
Define processes as used by the Linux kernel View and control running processes using command-line and graphical utilities Understand and manage how memory is used by Linux processes Schedule delayed execution of processes
Creating Processes
Each process has a unique process identifier (PID). The init process is the first process started by the Linux kernel and has a PID of 1. All processes are descended from the init process by forking, or creating new child processes. When a process ends, it exits, and all of the resources it used are returned to the kernel for use by other processes.
6-2
Process States
Processes may be in one of four states: running, sleeping, stopped, or zombie. A running process is currently executing or waiting for the kernel to assign it a time slice. A sleeping process is waiting for something to happen (but not for the kernel to give it a time slice). Processes commonly wait for input and output channels to become available. A stopped process has been stopped before exiting normally. A zombie process is no longer active, but the kernel is still holding on to resources allocated to the process. The kernel regularly cleans up zombie processes.
Swap Space
When the memory requirements of processes are greater than the available memory, the kernel uses the virtual memory or swap space to temporarily hold programs and data. The swap space is typically a separate hard disk partition, called the swap partition. A process that has been placed in the swap space has been swapped out. It cannot start running again until it is swapped back in to physical memory. When the kernel spends too much time moving processes to and from the swap space, the system is said to be thrashing.
6-3
Another place to obtain information about processes is the /proc file system. In order to view this information, the PID is required. A subdirectory of /proc is created for each process with the name /proc/PID (for example, /proc/1066). The files in this subdirectory contain information about the process. Many of the files are difficult to interpret, and Chapter 15 contains more details on the /proc file system. Note that the /proc file system does not contain actual files, but is accessed like any Linux directory. The top command is used to view information about CPU utilization by process. The list of processes returned by this command is arranged in descending order, with the process that consumes the most CPU time at the top. The information is updated regularly. The top command cannot be run in the background, since it continuously prints information to the screen.
Controlling Processes
The kill command is used to send a signal to a process. There are about 30 different signals, though many of them are infrequently used. The kill l command can be used to view a list of signals. Two important signals for ending processes are SIGTERM and SIGKILL. SIGTERM is sent to the process and asks the process to terminate. This gives the process a chance to save data, release resources, etc., before exiting. The SIGKILL signal is executed by the kernel and kills the process immediately. Since the process does not have an opportunity to save data or clean up, it is best to try SIGTERM first. The killall command can be used to kill all processes descended from a single process. A complete list of signals can be found at: http://www.comptechdoc.org/os/linux/howlinuxworks/linux_hlprocess.html. A process priority determines how much CPU time is allocated to the process. Another name for priority is the nice level. A higher nice level means that the process will yield CPU time to other processes. Administrators may change the priority of any process, while regular users may change the priority of processes they create. The standard nice level is 0. The highest level is 20. The nice command is used to start a process with an altered nice level. By default, the command starts the process with a nice level of 10. Options to the command are used to change this default. The renice command is used to change the nice level for a process that is already started. Pages 250 and 251 contain examples of using nice and renice. Administrators can alter process priority while using the top command. Pages 251 to 252 outline the procedure for altering process priority in a top session. Table 6-4 lists interactive commands in top.
6-4
A list of commands associated with process management can be found at: http://www.comptechdoc.org/os/linux/commands/linux_crprocman.html.
Quick Quiz
1. 2. 3. 4. What are the four states for Linux processes? ANSWER: Running, sleeping, stopped, and zombie _____ occurs when the kernel spends so much time moving processes to and from the swap space that the kernel and the processes bog down and work inefficiently. ANSWER: Thrashing The _____ command lists the processes that are currently running on your Linux system. ANSWER: ps True or False: The SIGKILL signal is handled by the process to which it is sent. ANSWER: False
6-5
Scheduling Processes
Linux provides the ability to schedule processes to start at a later time. Processes can be scheduled to execute once using the at command or regularly using the crontab command. Both commands rely on background processes to check for and execute scheduled tasks. Another name for a scheduled task is a job, and the terms at job and cron job are commonly used.
6-6
Students should create scheduled tasks using crontab and at, then view the /var/spool subdirectories to find the files corresponding to their commands.
Quick Quiz
1. 2. 3. _____ linked applications assume that any needed library files are available on the Linux system. ANSWER: Dynamically True or False: The free command displays information about RAM and virtual memory. ANSWER: True A scheduled task is often called a(n) _____. ANSWER: job
Discussion Questions
1. 2. 3. 4. 5. What are the benefits of the hierarchical system of creating processes where one process descends from another? What type of processes normally run in the background? Would you prefer to use graphical or command-line tools to monitor system load? What steps can you take to ensure that memory is used most efficiently? What type of tasks would you perform using at, batch, and crontab?