System Processes and Memory Management

12 316 0
  • Loading ...
1/12 trang

Thông tin tài liệu

Ngày đăng: 02/10/2013, 09:20

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 & [1] 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 [1] 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 [1] + 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 [1] + Stopped(SIGTSTP) find / -name core> trash \ 2> /dev/null & $ jobs [1] + Stopped(SIGTSTP) find / -name core > trash \ 2> /dev/null & $ bg %1 [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. Terminating Processes Signals There are currently 33 signals defined in the Linux operating system (see /usr/include/signum.h). Each signal is associated with a number and a name. Signals are used to terminate, suspend, and continue processes. Information on the different signals can be found by using the following command: $ man -S 7 signal A process that is not responding can sometimes be terminated by using Ctrl-c. This sends an interrupt (INT) signal to the process, terminating it and any child processes it might have spawned. The kill Command The kill command provides a direct way to terminate unwanted command processes. It is useful when you want to stop a command that takes a long time to run, or when you need to terminate a process that you cannot quit in the normal way. Command Format kill [-signal] job-id or process-id Terminating a Process You would: 1. Type ps to find out the PID(s) for the process(es). 2. Type kill followed by the PID(s). If you use the kill command without specifying a signal, signal 15 (SIGTERM) is sent to the process with the specified PID number. This usually causes the process to terminate. Finding and Terminating a Process by User The ps -u command can be used either with a login name or a UID number. A user can only terminate his or her own processes, but the superuser can terminate any process running on the system. If there are multiple windows open on a user’s desktop, the output of the ps command will show pts/# under the TTY heading for every window open. A pts (pseudoterminal) is the device name given to windows and remote login sessions. $ ps -u login-ID PID TTY TIME COMD 12892 console 0:01 ksh 12932 pts/0 0:01 find 12935 pts/1 0:00 ps <Output omitted> $ kill 12932 [1] + Terminated find / -name core -exec rm {} \; & $ If you need to forcibly terminate a process, you can append the -9 option to the kill command. This option is necessary for killing shells which will not respond to any other signal to terminate. $ kill -9 PID# $ Note – For processes other than shells, use the kill -9 command as a last resort because it is an abrupt method and does not allow for proper process termination. • To abort a background job, use the specific job number as the argument to the kill command. $ kill %1 [1] + Terminated find / -name core > trash \ 2> /dev/null & $ jobs $ The pkill command works exactly like the pgrep command, except that it terminates the matching process or processes with a kill signal. $ sleep 100& $ ps 472 pts/3 0:01 ksh 12418 pts/3 0:00 sleep $ pkill sleep $ ps 472 pts/3 0:01 ksh $ Note – The sleep command is frequently used in shell scripts to cause the machine to pause for a specified number of seconds before continuing on to the next command. It is used in the above example only for purposes of illustration. Memory Physical Memory (RAM) and Swap Space All computers have central memory, or system memory, which has a sequence of instructions (a program) and data related to the program. This memory is controlled directly by the processor in conjunction with the Memory Management Unit (MMU) and is called physical memory, or random access memory (RAM). Many processes are held simultaneously in RAM, so it is possible to use it up. If a new process needs to be placed in RAM, the pageout process selects pages of a process that are not currently in use and pages them out to swap space on the disk. The selection of pages is made based on a “not recently used algorithm.” Swap space is a raw slice or disk file set aside for this purpose. Processes remain in RAM while they are active. The swapping method uses swap space on the disk which is limited. RAM plus swap space constitutes virtual memory which is the maximum space that processes can use. Virtual memory has a much shorter access time than accessing something on the formatted areas of the disk. For obvious reasons, the kernel is not swapped. Figure 16-1 Swapping Paging A program on a Sun workstation can address up to 4 Gbytes of memory. A system is typically equipped with some number of Mbytes of RAM. During the execution, a process is placed in RAM (cut in pages) and is swapped page by page in and out of RAM. This process is called paging. Swapping rarely takes place on systems with very large amounts of RAM, whereas paging frequently occurs and is a normal part of system operation. Exercise: Manipulating System Processes Exercise objective – In this exercise, you will use the commands learned in this module to determine PID numbers, kill processes, and control jobs. Tasks Complete the following steps: 1. In a terminal window, issue the following command: $ cat -v /dev/zero Note – This command is being used to produce a continuously running command for demonstration purposes only. For information on the /dev/zero file, see man zero. 2. Open another terminal window (shell) and use the ps command to identify the process ID of the cat command. 3. From the current window, kill the cat command using the cat command’s process ID. 4. From the current window, determine the PID of the window in which the cat command was running and kill that window. 5. Issue the following command in the background: $ sleep 500 & 6. Using the jobs command, find the job number of the sleep command in step 5. Bring the job to the foreground and then put it back in the background. 7. Kill the job running the sleep command. Workshop Labs Use what you have learned so far in this course to work through the following: Getting rid of core files that are taking up space on your hard drive is a common task that is useful to run in the background. 1. Issue a find command that will look for and remove core files starting at the root directory, and send error messages to /dev/null. Have the process run in the background. Next, bring the job to the foreground to check on its progress, then place it back in the background again. 2. This type of command can use up quite a bit of system resources; at this point, the response time of your machine is more important to you than removing the core files. Stop the job; it will be resumed in a later exercise. Exercise Summary Discussion – Take a few minutes to discuss what experiences, issues, or discoveries you had during the lab exercises.  Manage the discussion here based on the time allowed for this module, which was given in the “About This Course” module. If you find you do not have time to spend on discussion, then just highlight the key concepts students should have learned from the lab exercise. • Experiences  Ask students what their overall experiences with this exercise have been. You might want to go over any trouble spots or especially confusing areas at this time. • Interpretations  Ask students to interpret what they observed during any aspects of this exercise. • Conclusions  Have students articulate any conclusions they reached as a result of this exercise experience. [...]... the following:     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 Think Beyond What do you currently do when you need to stop a process? How will using kill change that? What might be the advantage of having daemon processes? What might you expect... following command: $ cat -v /dev/zero 2 Open another terminal window (shell) and use the ps command to identify the process ID of the cat command $ ps -ef | grep cat 3 From the current window, kill the cat command using the cat command’s process ID $ kill PID where PIDis the process ID of the cat command 4 From the current window, determine the PID of the window in which the cat command was running and kill... at the PPID of the cat command: $ ps -ef | grep PPID then type $ kill -9 PID where PID is the PPID of cat 5 Issue the following command in the background: $ sleep 500 & 6 Using the jobs command, find the job number of the sleep command in step 5 Bring the job to the foreground and then put it back in the background $ fg %1 ^Z $ bg %1 7 Kill the job running the sleep command $ kill %1 Check Your Progress... when you need to stop a process? How will using kill change that? What might be the advantage of having daemon processes? What might you expect to see that would indicate you are running out of virtual memory? . System Processes and Memory Management Objectives Upon completion of this module, you should be able to: • Identify processes on your system using. command • Find a process using the pgrep command • Control processes using the jobs command • Terminate unwanted processes using the kill and pkill commands
- Xem thêm -

Xem thêm: System Processes and Memory Management, System Processes and Memory Management, System Processes and Memory Management

Gợi ý tài liệu liên quan cho bạn