Process Management in Linux and process management in Linux

Source: Internet
Author: User

Process Management in Linux and process management in Linux

In Linux, the name of the currently running program instance

Is a process. For example, when you start Apache, the system assigns a process ID to it. Then you can use

This ID monitors and controls this program.

Process monitoring and control are the core tasks of any Linux system administrator. An administrator can terminate ("kill"

), Restart a process, or even specify a different priority for it. Standard Linux commands "ps" and

"Top" is usually used to view the current process list. Next I will explain how to use these commands and other commands to manage

Processes in Linux.

Monitor processes with ps
A standard tool for Linux monitoring is "ps", short for Process status. This command returns a running

Program information. This information may include the user name under which the program runs and the CPU usage

And how long it takes to run. If you want to manually terminate the program or determine which program slows down the system

Information is useful.

If you just type the "ps" command, you can only list the processes running on the current terminal. The following example

Sub-statements are output by running "ps" via remote shell:

$ Ps
PID TTY TIME CMD
4684 pts/14 00:00:00 bash
27107 pts/14 00:00:00 ps

The output shows that only the Bash shell and ps commands are assigned to the user/terminal. You also

The PID (process ID), TTY, TIME, and CMD listed for each process are displayed. TTY indicates that the process is

On which terminal is running, TIME indicates the cpu time used by the process, and CMD starts

Command name of the process.

You can see that standard ps commands can only list basic information. To obtain

You must add some command line parameters.

After adding the commonly used aux parameter of ps, you can display the processes started by other users (a) and view their own

Process (x) and the user who started the process and the time at which it was started (u ).

Table A shows the output of the ps aux command.

Now there is more information, including USER, % CPU, % MEM, VSZ, RSS, STAT, and START.

Several domains. Now let's take a look at the meaning of the information.

First, you can see all the processes, not just those running on the terminal. USER field indicates yes

Which user has started this command. Many processes start together with the system, and the root or some system accounts

Column as USER. Of course, other processes run independently. This information can help you narrow down the problem.

Range. Assume that a user starts the script and occupies a large amount of I/O on the server. If you can find out who is

Running this program can greatly speed up problem solving.

% CPU, % MEM, VSZ, and RSS are all related to system resources. First, you can view a process

CPU usage. This information is displayed in real time, so it is difficult to use ps to capture the peak value. Users may find

To find the process that causes the problem, you need to run the ps command continuously.

In addition to CPU usage, you can also see the memory usage, VSZ (virtual memory size), and RSS (resident set size ).

Small ). VSZ indicates how much memory space is needed if a program fully occupies the memory, while RSS indicates

Memory occupied. If you can understand how much memory a process currently occupies, you can

To determine whether the process is running normally or has encountered an exception. Programs usually consume more than normal

Memory and CPU. Although programmers are trying their best to ensure that the code correctly uses resources

The Administrator decides whether to terminate or restart the process.

The user may notice that in the output result of the ps aux command, most TTY fields have "? ". This is because

Some programs or start running when the system starts, or by the initialization script (init script)

Started. These processes do not have control terminals, so they are marked. In addition, the linux-sanity-check Command has

The value of a TTY is pts/14. This command is run remotely and has a terminal associated with it. When you

And you want to determine the window in which a command runs.

Very useful.

STAT displays the current status of the process. In our example, many processes are in sleep state

"S" indicates this status. This only indicates that these processes are waiting for some events to happen-possibly user input or

Availability of system resources. The linux-sanity-check Command has an R state, which indicates the process

Currently running. Sometimes you can browse this list and find the R-State processes. If most

If the sub-process is sleep and a problem occurs, it is best to focus only on the running processes.

. The status may not indicate a problem, but sometimes it may mean that if the process runs for too long

There are some deep-seated problems.


Use top to monitor processes
Another useful program is top. This program is similar to ps, but it is usually displayed in full screen and

The Process status changes constantly. For programs that frequently cause problems and are hard to view with ps

This command is very useful. The information of the entire system will also be displayed, which facilitates you to find problems.

. The total number of CPU and memory resources and load balancing among other information in the system is very useful.

The sequence table, the current state of the program, and their respective statistical information can help you understand why top is so common.

.

Don't forget pstree
Finally, another command that allows you to quickly and easily view processes is pstree. This command will list the current process

And their tree structure. When a process starts, it may generate its own sub-process. Run pstree

Command to easily see this information.

USD pstree-cp 125
Httpd (125)-+-httpd (126)
|-Httpd (1, 127)
|-Httpd (1, 129)
'-Httpd (130)

Httpd is a good example because it often produces sub-processes. In the above example, you can see that the PID is 125

Tree. If you want to terminate httpd but do not want to end all single child processes, find the parent process. Pstree

Command to list the trees corresponding to all individual processes or all processes in the system. It not only helps you find

An abnormal process can also be used as a learning tool. You can learn about Linux by executing these commands

For more information, see the Help Page.

Manage processes
If you have learned to monitor processes with the ps and top commands, you should also know how to manage processes. You can use

Kill, killall, and renice commands implement process management.

The "kill" Command sends a signal to a running process. The most common application is to use it to stop program execution.

First, you need to obtain the PID of the running process (for example, you can use the ps aux command), and then you can use the following command to terminate

Process:

$ Killed 125

$ Kill-9 125

Generally, this command can terminate process 125. It must be emphasized that you are either the owner of the process.

Root User. Otherwise, the process cannot be terminated. Sometimes the process cannot be terminated simply by using a kill command,

You also need to perform the following operations:

If the process hangs and does not respond normally, you can use the "-9" flag to end the process.

This process. The Common kill command sends a sigterm signal, while-9 sends a sigkill signal.

Signal forced program termination. Other signals can be used to terminate or start a process. You can run "kill

-L "command, then you can see the content.

The Killall command is similar to the kill command, but receives different parameters. You can send a program name to it

Instead of PID. All processes run with the program name will be terminated. As long as you are the owner of the program or

The root user can terminate the program. Therefore, running the killall tcpdump command will terminate all tcpdump

Program instance. This is useful if many processes run with the same name.

Pay attention to what process you are terminating, especially when you are the root user. An inappropriate process may be terminated.

Terminate your session and even stop the entire system. You should be familiar with standard running processes and their

Resource usage. If you want to prevent system problems, it is best to set a baseline.

Do you still remember that I mentioned earlier that the priority of a process can be changed? You can use the renice command. Change Priority

Level can notify the system to give a process more or less CPU time. Process Priority ("niceness ")

The range is from-20 to 20, and-20 is the highest priority. So if you want to reduce the httpd process priority by 125

Run the following command:

$ Renice + 20 125

You can save system resources by changing the priority. The system can automatically change the priority.

It is likely that a program occupies more resources than normal.

Accelerated Problem Solving Process
It is necessary to monitor and control processes in Linux. Ps, top, kill, and renice

To let you see the running status of the process and control it. The more you know about each process, the more

It is easy to precisely locate the problem of the process. The system usually encounters various problems for some reason, such as speed.

Slow or unstable, and using these tools can help you improve your ability to locate problems.

From: http://www.douban.com/group/topic/2551368/ [Mr. chicken (married uncle]
Implementation of linux Process Management

Is the management system process or self-derived process about the system's process management principle?
If you want to manage your own processes, you can use the dispatch process, and then manage sub-processes through message queues, pipelines, and shared memory areas.

The process implementation principle of the system is complex.

In linux, how does one manage processes, memory, devices, and files?

Brother Ren's book on the first floor is a bit too deep and very thick. If you want to know about the kernel briefly, you can refer to robert love's "Linux kernel design and implementation", which is very thin, I gave a general lecture about it, which is suitable for beginners.

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.