System Processes and Memory Management Objectives Upon completion of this module, you should be able to: • Identify processes on your system using the ps command • Find a process using the pgrep command • Control processes using the jobs command • Terminate unwanted processes using the kill and pkill commands Discussion – At times an application that you are running will freeze or cause your system to become inaccessible. How would you regain control of your workstation? System Process Overview Each task you perform in the Linux environment starts a process. An example of a process is using vi to edit a letter, or sending a file to the printer. Each process is assigned a unique process identification number (PID), which is used by the system to identify the process. The following pages define useful commands on how to locate a process and terminate processes. Process Hierarchy There are five types of processes on a Linux system: • Daemon • Parent • Child • Orphan • Zombie or defunct
Daemon processes are processes that are started by the kernel and exist for a specific purpose. For instance, the lpd daemon exists for the sole purpose of handling print jobs. When no printing is taking place on the system, the lpd daemon is running but inactive. When a print job is submitted, this daemon becomes active until the job is finished. The login daemon provides the CDE login screen at the beginning of a user’s session and again after the user exits CDE. Following system boot-up, a process called init is invoked. This process is at the top of the process hierarchy and is responsible for spawning many system processes. The login daemon is spawned by init and init is, therefore, referred to as the parent process of the login daemon. When a user is working in a terminal window in CDE, that terminal’s PID is the parent process ID (PPID) of any commands issued in the terminal. These commands are child processes of the terminal process. The parent process receives and displays the output from the child process and then “kills” the process. If a command is issued in a terminal window and the window is closed before the command returns output, that process becomes an orphan. The system passes the orphan process to init which then becomes the parent process and terminates the child process. Occasionally a child process does not return to the parent process with its output. This process becomes “lost” in the system. The only resource this process uses is a slot in the process table; it cannot be stopped in a conventional manner. This type of process is called a zombie or defunct process. The only way to kill a defunct process is to reboot the system. Processes and PIDs Use the ps command to list the processes currently running on the system. The output of this command will display the PID number and the command associated with it. Many times a PID number is needed for use with the kill command . Command Format ps [-options] Options -e Print information about every process on the system. -f Generate a full listing. (Refer to the man pages for a description of the headings displayed.) Because of the number of processes usually running on a system, it is useful to pipe the ps -ef command to more so that the output can be read a page at a time, as in the example on the next page. Displaying a Full Listing of All Processes $ ps -ef | more UID PID PPID C STIME TTY TIME CMD root 0 0 80 16:46:41 ? 0:01 sched root 1 0 80 16:46:44 ? 0:40 /etc/init - root 2 0 27 16:46:44 ? 0:00 pageout root 3 0 80 16:46:44 ? 4:33 fsflush
root 236 1 80 16:48:08 ? 0:01 /usr/lib/saf/sac root 844 1 54 12:12:10 ? 0:00 /usr/lib/lpsched aster 1292 1 80 06:48:51 console 0:01 -ksh root 241 236 69 16:48:14 ? 0:01 /usr/lib/saf/ttymon rose 1400 321 80 20:03:11 ? 0:00 /usr/openwin/bin/clock --More— Table 16-1 describes the column headings. Table 16-1 Column Headings Value Description PID The process identification number of the process PPID The parent process identification number of the process TTY The controlling terminal for the process a TIME The amount of CPU time used by the process CMD The command name b a The controlling terminal for system daemons appears as a question mark (?). b <defunct> will appear in the the CMD column if a process is a zombie or defunct process. Also, the CPU time may be a bit higher for a defunct process than for other processes. Searching for a Specific Process A quicker way of determining the correct PID is to pipe the output of the ps command through grep, searching for the specific process you want to terminate. $ ps -e | grep lp 225 ? 0:01 lpNet 217 ? 0:0 lpsched 260 ? 0:01 lpNet $ In Linux, you can use the pgrep command to search for a specific process. Using the -l option will display the names of the processes associated with the PID found. $ pgrep -l lp 225 lpNet 217 lpsched 260 lpNet $ Managing Jobs The shell gives you the ability to execute several jobs simultaneously. Print requests or an application that has been executed are examples of jobs. Every job is assigned a job ID. The coordination of multiple jobs within the shell is called job control. When a job has been executed in the window environment, it runs in the foreground and ties up that window until the job is done. Jobs executing in the background do not tie up your window, so you can start other jobs without waiting for the background job to finish. The commands used to control a job based on the job ID can only be used in the window in which the job was started. Use the following commands to control jobs: Table 16-2 Job Management Commands Command Value jobs Display which jobs are currently running. fg %n Place a job in the foreground. bg %n Place a job in the background. kill %n Abort the specified background job. The job ID must be specified. Control-c Abort the foreground job. Control-z Suspend the foreground job. To run a job in the background, type the command followed by an ampersand (&). The number returned in brackets is the job ID number. An example of a time-consuming job that could be run in the background is searching a large directory stucture with the find command. For example: $ find / -name core > trash 2> /dev/null &  3923 In this example, each argument has the following meaning: Table 16-3 Command Arguments Argument Meaning > Redirect standard output (1) to filename trash The file used to capture standard output 2> Redirect standard error (2) to filename /dev/null Data written to this file is discarded & Process job in the background The responses displayed by the system have the following meaning: Table 16-4 Responses Response Meaning  The job ID number 3923 The process ID number If you are still working in the shell, the next time you press Return you will see a message indicating that the background process has completed. [job-id] + Done job description . . . • Use the jobs command to list your active jobs. $ jobs  + Running find / -name core> trash 2> /dev/null & • If you bring the background job back to the foreground, it will tie up your shell until the job is completed or placed back in the background. $ fg %1 find / -name core > trash 2> /dev/null • To put the same job in the background, suspend it first. find / -name core > trash 2> /dev/null ^Z  + Stopped(SIGTSTP) find / -name core> trash \ 2> /dev/null & $ jobs  + Stopped(SIGTSTP) find / -name core > trash \ 2> /dev/null & $ bg %1  find / -name core > trash 2> /dev/null & $ Placing a stopped job into either the foreground or the background restarts the job. If you log off before the background job is completed, use the nohup command to enable a background job to complete, otherwise, the background job will be terminated when you log off. Note – Notification is sent when there are stopped jobs at logout.