Linux Process Management

Source: Internet
Author: User

Linux Process Management
Introduction when we run a program, Linux will create a special environment for the program, which contains all the resources required to run the program, to ensure that the program can run independently without interference from other programs. This special environment is called a process. Each Linux Command corresponds to a program in the system. input the command to create a new process in Linux. For example, when you use the ls command to traverse files in the directory, a process is created. In short, a process is a program instance. The system tracks the running status of a program using a five-digit number, which is called a pid or process ID. Each process has a unique pid. Theoretically, the five-digit number is limited. When the number is used up, the next pid will start again, so the pid will eventually repeat. However, two processes with the same pid cannot exist at the same time, because Linux uses the pid to track the running status of the program. There are two ways to create a process: foreground process and background process. By default, all processes created by the user are foreground processes. Foreground processes read data from the keyboard and output the processing results to the display. We can see the running process of the foreground process. For example, use the ls command to traverse the files under the current directory: $ ls ch * .docch01-1.doc ch010.doc ch02.doc ch03-2.docch04-1.doc ch040.doc ch05.doc this program runs on the front-end, it will directly output the results to the display. If the ls command requires data (not required), it will wait for the user to input data from the keyboard. When the program runs on the front-end, because the command prompt ($) has not yet appeared, you cannot enter other commands. Even if the program needs to run for a long time, you must wait until the program stops running before entering other commands. There is no inevitable relationship between background processes and the keyboard. Of course, background processes may also wait for keyboard input. The advantage of background processes is that you can enter other commands without waiting for the program to finish running. The simplest way to create a background process is to add & at the end of the command, for example, $ ls ch *. doc &ch01-1.doc ch010.doc ch02.doc ch03-2.docch04-1.doc ch040.doc ch05.doc ch06-2.docch01-2.doc ch02-1.doc if ls command needs to be input (actually not needed) then it will pause, it will not continue until the user transfers it to the foreground and enters data from the keyboard. To view a running process, run the ps command to view the running status of the process, including the background process, for example: $ psPID tty time limit 18358 ttyp3 00:00:00 sh18361 ttyp3 00:01:31 abiword18789 ttyp3 00:00:00 ps you can also use the-f option to view more information. f is the abbreviation of full. For example: $ ps-fUID pid ppid c stime tty time interval amrood 6738 3662 0 10:23:03 pts/6 10:22:54 first_oneamrood 6739 3662 3662 0 08:10:53 pts/6 00 second_oneamrood 3657 6892 0 pts/6-kshamrood 3662 4 10:51:50 pts/6 ps-f the meaning of each column is as follows: Column describes the ID of the user to which the UID process belongs, that is, the user who created the process. PID process ID. PPID parent process ID. The process that creates this process is called the parent process. C cpu usage. The time when the STIME process was created. TTY terminal type related to the process. The cpu time used by the TIME process. CMD command to create the process. The ps command has other options: Option description-a displays all processes of all users. -X: displays non-final processes. -U displays more information, similar to the-f option. -E: displays all processes. Terminate a process when the process is running on the foreground, you can use the kill command or Ctrl + C to end the process. If the process runs in the background, first obtain the process ID using the ps command, and then use the kill command to "kill" the process, for example: $ ps-fUID pid ppid c stime tty time interval amrood 6738 3662 0 10:23:03 pts/6 10:22:54 first_oneamrood 6739 3662 3662 0 08:10:53 pts/6 00 second_oneamrood 3657 6892 0 pts/6-kshamrood 3662 4 10:51:50 pts/6 ps-f $ kill 6738 Terminated as shown above, the kill command terminates the first_one process. If the process ignores the kill command, kill-9 can be used to end: $ kill-9 6738Terminated1. command Format: kill [parameter] [process number] 2. command function: Send a specified signal to the corresponding process. If no model is specified, SIGTERM (15) is sent to terminate the specified process. If the program cannot be terminated and the "-KILL" parameter is available and the signal sent is SIGKILL (9), the process is forcibly terminated. You can run the ps command or the jobs command to view the process number. Root users will affect user processes. Non-root users can only affect their processes. 3. command Parameter:-l signal. If the serial number parameter is not added, the "-l" parameter will be used to list all the signal names-a when processing the current process, do not restrict the correspondence between command names and process numbers-p specify the kill command to print only the process numbers of the relevant process, without sending any signal-s specify the sending signal-u specify the user's attention: 1. The kill command can contain the signal number option or not. If there is no signal number, the kill command will send a termination signal (15), which can be captured by the process so that the process can clear and release resources before exiting. You can also use kill to send specific signals to the process. For example, kill-2 123 is equivalent to pressing Ctrl + C when running a process with a PID of 123 on the foreground. However, common users can only use the kill command without the signal parameter or use a maximum of-9 signals. 2. kill can contain a process ID as a parameter. When sending signals to these processes with kill, they must be the masters of these processes. If you try to undo a process without permission or undo a non-existent process, you will get an error message. 3. You can send signals to or terminate multiple processes. 4. After kill successfully sends a signal, the shell will display the process termination information on the screen. Sometimes this information is not immediately displayed. It is only displayed when the shell command prompt appears again by pressing Enter. 5. It should be noted that the process is forcibly terminated by a signal, which often brings some side effects, such as data loss or the terminal cannot be restored to normal. When sending a signal, you must be careful. Only when you have to do so can you use the kill signal (9), because the process cannot capture it first. To cancel all background jobs, enter kill 0. Some commands run in the background will start multiple processes, and it is very troublesome to trace and find the PID of all processes to be killed. Kill 0 is an effective method to terminate all processes started by the current shell. Signal Description: only 9th signals (SIGKILL) can terminate processes unconditionally. Other signal processes have the right to ignore them. Below are common signals: HUP 1 terminal disconnection INT 2 interrupt (same as Ctrl + C) QUIT 3 QUIT (same as Ctrl + \) TERM 15 terminate KILL 9 Force terminate CONT 18 continue (opposite to STOP, fg/bg command) STOP 19 pause (same as Ctrl + Z) each Linux Process contains two process IDs: the current process ID (pid) and the parent process ID (ppid ). It can be temporarily considered that all processes have parent processes. Most commands run by the user use Shell as the parent process. The ps-f command can be used to view the current process ID and parent process ID. Under normal circumstances, when a zombie process or orphan process is terminated, the parent process is notified through the SIGCHLD signal. The parent process can clean up or restart a new process. However, in some cases, the parent process is terminated before the child process, so these child processes do not have the "father" and are called orphan processes. The init process becomes the parent process of all orphan processes. The pid of init is 1, which is the first process in Linux and the parent process of all processes. If a process is terminated, but the ps command can still be used to view the process, and the status is Z, then this is a zombie process. Although the zombie process has been terminated, it still exists in the process list. Generally, zombie processes are hard to kill. You can first kill their parent processes and turn them into orphan processes. The init process automatically cleans them up. Resident Process resident processes are generally system-level processes that run in the background with root permissions and can process requests from other processes. The resident process has no terminal and cannot access the/dev/tty file. If you use ps-ef to view the process, the tty column displays the question mark (?). To be more precise, a resident process usually runs in the background and waits for a specified event to occur. For example, a printing process is a resident process and waits for the user to input and print relevant commands for processing. The top command is a useful tool that dynamically displays running processes and sorts processes according to specified conditions, similar to the Windows Task Manager. The top command displays a lot of information about processes, including physical memory, virtual memory, CPU usage, average load, and busy processes. For example, $ top tasks and process tasks are the most abstract. They are a general term that refers to an activity completed by software. A task can be either a process or multiple processes. In short, it refers to a series of operations to achieve a specific goal. For example, read data and put it into memory. This task can be implemented by one process or multiple processes. Each task has a number. Processes are often defined as program execution. A process can be considered as an independent program, with its complete data space and code space in the memory. Data and variables owned by a process only belong to itself. The jobs command can be used to view running tasks in the system, including those running in the background. This command displays the task number and its corresponding process ID. A task can correspond to one or more process numbers. The-l option of the jobs command allows you to view the process ID contained in the current task: $ jobs-l [1] + 1903 running ls ch *. in doc & $, the first column indicates the task number, the second column indicates the process ID corresponding to the task, the third column indicates the running status of the task, and the fourth column indicates the command to start the task. The switch fg command of foreground and background tasks transfers background tasks to the foreground. Syntax: $ fg % jobnumberjobnumber is the sequence number of background tasks obtained by the jobs command. Note that it is not a pid. If there is only one task in the background, you can not specify jobnumber. The bg command transfers the paused tasks in the background to the foreground to continue running. Syntax: $ bg % jobnumber is also the serial number of the background task obtained through the jobs command. Note that it is not a pid. If there is only one task in the foreground, you can not specify jobnumber. If you want to move the current task to the background, you can press Ctrl + z to pause the task and then use the bg command. When the task is transferred to the background, the terminal can be empty and other commands can be entered.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.