Linux Process Management parent process and child process

Source: Internet
Author: User

Linux Process Management is a commonly used software for computer gamers. Then I will study and study Linux Process Management in depth. Here I will discuss with you how to use Linux Process Management, I hope it will be useful to you. A program is a software designed to complete a task. For example, OpenOffice is a program.

What is a process? A process is a running program. A running program may have multiple processes. For example, LinuxSir. the WWW server used by Org is an apache server. When the Administrator starts the service, many people may access it. That is to say, many users request the httpd service at the same time, the apache server will create multiple httpd processes to serve them.

1. programs and processes; process classification;

Processes are generally divided into three types: interaction process, batch processing process, and daemon process. It is worth mentioning that the daemon process is always active and generally runs in the background. The daemon process is generally started by the system automatically activated by the script or by the super administrator root at startup.

For example, in Fedora or Redhat, we can define the running level of the startup script of the httpd server. This file is located in/etc/init. in the d directory, the file name is httpd,/etc/init. d/httpd is the daemon of the httpd server. When the running level is set to 3 and 5, it starts when the system starts.

[Root @ localhost ~] # Chkconfig -- level 35 httpd on because the daemon is always running, its status is waiting for request processing tasks. For example, whether we access the httpd servers of LinuxSir. Org and LinuxSir. Org is running, waiting for the user to access, that is, waiting for the task to be processed.

1.2 process attributes;

Process IDPID): It is a unique value used to differentiate processes; IDPPID of the parent process and the parent process); IDUID of the user who starts the process) and GID of the group to which the process belongs); Process status: states include running R, sleeping S, and zombie Z;
Priority of process execution; terminal name connected to the process; process resource usage: such as resource usage, memory usage, and CPU usage );

1.3 Parent and Child processes;

Their relationship is the relationship between management and management. When the parent process is terminated, the child process also terminates. But the child process is terminated, and the parent process is not necessarily terminated. For example, when the httpd server is running, we can kill its child processes. The parent process will not be terminated because of the termination of the child process. In Linux Process Management, when we find that a process occupies too many resources or cannot be controlled, we should kill it to protect the stable and secure operation of the system;

2. Linux Process Management;

Linux Process Management is implemented through Linux Process management tools, such as ps, kill, and pgrep;

The ps tool monitors processes. The ps tool provides a one-time View of processes. The results are not dynamically consecutive. If you want to monitor the process time, you should use the top tool; ps parameter description; ps provides many option parameters, which are commonly used in the following scenarios;

L long format output;
U displays processes in the username and start time order;
J. display processes in the task format;
F process is displayed in a tree format;
A. All processes of all users, including other users, are displayed );
X displays the process of the uncontrolled terminal;
R: displays running processes;
Ww to avoid detailed parameter truncation;

Our common options are combination of aux or lax, and the application of parameter f; ps aux or lax output interpretation;
Owner of the USER process;
PID process ID;
PPID parent process;
% Percentage of CPU used by CPU processes;
% MEM memory usage percentage;
NICE value of the NI process. The value is large, indicating that the CPU time is less occupied;
Virtual size of VSZ process;
Number of pages in RSS resident;
WCHAN
TTY terminal ID
STAT Process status
D Uninterruptible sleep (usually IO)
R is running and can be crossed in the queue;
S is in sleep state;
T stop or be tracked;
W indicates that the switch to the memory is invalid from kernel 2.6 );
X dead processes have never been seen );
Z botnets;

<Process with high priority
N processes with lower priority
L some pages are locked into the memory;
S process leader has sub-processes under it );
L is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+ Process group in the background;
WCHAN is waiting for Process resources;
The START time of the process;
TIME the cpu time consumed by the process;
COMMAND name and parameters;

2.1.2 ps application example; instance 1: ps aux is the most commonly used [root @ localhost ~] # Ps-aux | more can be used | the MPs queue is connected to more for pagination;
[Root @ localhost ~] # Ps-aux> ps001.txt
[Root @ localhost ~] # More ps001.txt
This shows all the processes and outputs them to the ps001.txt file. Then, you can view them by page through more. Example 2: Combine with grep to extract the process of the specified program;
[Root @ localhost ~] # Ps aux | grep httpd
Root 4187 0.0 1.3 24236 10272? Ss/usr/sbin/httpd
Apache 4189 0.0 0.6 24368 4940? S/usr/sbin/httpd
Apache 4190 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4191 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4192 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4193 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4194 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4195 0.0 0.6 24368 4932? S/usr/sbin/httpd
Apache 4196 0.0 0.6 24368 4932? S/usr/sbin/httpd
Root 4480 0.0 0.0 5160 708 pts/3 R + grep httpd

Example 2: a friendly relationship between parent and child Processes
[Root @ localhost ~] # Ps auxf | grep httpd
Root 4484 0.0 0.0 5160 704 pts/3 S + \ _ grep httpd
Root 4187 0.0 1.3 24236 10272? Ss/usr/sbin/httpd
Apache 4189 0.0 0.6 24368 4940? S \ _/usr/sbin/httpd
Apache 4190 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4191 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4192 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4193 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4194 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4195 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
Apache 4196 0.0 0.6 24368 4932? S \ _/usr/sbin/httpd
The f parameter is used here; the parent-child relationship is clear at a glance;

2.2 pgreppgrep is a tool used to query processes by program names. It is generally used to determine whether a program is running. In server configuration and management, this tool is often used and simple and clear;

Usage: # ps parameter option common program name Parameters
-L list the program name and process ID;
-The ID of the start of the o process;
-N indicates the ID of the process to terminate;

Example:
[Root @ localhost ~] # Pgrep-lo httpd
4557 httpd
[Root @ localhost ~] # Pgrep-ln httpd
4566 httpd
[Root @ localhost ~] # Pgrep-l httpd
4557 httpd
4560 httpd
4561 httpd
4562 httpd
4563 httpd
4564 httpd
4565 httpd
4566 httpd
[Root @ localhost ~] # Pgrep httpd
4557
4560
4561
4562
4563
4564
4565
4566


3. Linux Process Management Tool kill, killall, pkill, and xkill to terminate a process or terminate a running program, generally, kill, killall, pkill, and xkill are used. For example, if a program is dead but cannot exit, you should consider using these tools. In addition, in server management, you can use these tools to stop the parent process that does not involve database server programs. Why cannot the parent process of the database server be killed using these tools? The reason is simple. When these tools force terminate the database server, more file fragments will be generated for the database. When the fragmentation reaches a certain level, the database may crash. For example, it is best to close the mysql server according to its normal program, instead of using pkill mysqld or killall mysqld as dangerous actions. Of course, we should use kill to kill database sub-processes that occupy too much resources.

3.1 kill

Kill applications are used together with ps or pgrep commands. kill usage: kill [signal code] process ID. Note: The signal code can be omitted; our commonly used signal code is-9, indicating forced termination;

Example:
[Root @ localhost ~] # Ps auxf | grep httpd
Root 4939 0.0 0.0 5160 708 pts/3 S + \ _ grep httpd
Root 4830 0.1 1.3 24232 10272? Ss/usr/sbin/httpd
Apache 4833 0.0 0.6 24364 4932? S \ _/usr/sbin/httpd
Apache 4834 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4835 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4836 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4837 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4838 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4839 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd
Apache 4840 0.0 0.6 24364 4928? S \ _/usr/sbin/httpd

We can view the httpd server process. You can also use pgrep-l httpd to view the process. We can see the second column in the above example is the process PID column, among them, 4830 is the httpd server's parent Linux Process Management. All processes from 4833-4840 are its 4830 sub-processes. If we kill the parent process 4830, the sub-processes under it will also die;
[Root @ localhost ~] # Kill 4840 Note: kill the 4840 process;
[Root @ localhost ~] # Ps-auxf | grep httpd Note: What are the results? Is the httpd server still running?
[Root @ localhost ~] # Kill 4830 Note: kill the httpd parent process;
[Root @ localhost ~] # Ps-aux | grep httpd Note: Check whether other httpd sub-processes exist and whether the httpd server is still running?

For zombie processes, you can use kill-9 to force termination and exit. For example, if a program has completely died and kill fails to exit without signal strength, the best way is to add signal strength-9, followed by killing the parent process; for example;
[Root @ localhost ~] # Ps aux | grep gaim
Beinan 5031 9.0 2.3 104996 17484? S gaim
Root 5036 0.0 0.0 5160 724 pts/3 S + grep gaim
Or [root @ localhost ~] # Pgrep-l gaim
5031 gaim
[Root @ localhost ~] # Kill-9 5031

3.2 killallkillall uses the program name to directly kill all processes. Let's just put it down. Usage: killall is a running program name. killall is also used with ps or pgrep for convenience. You can use ps or pgrep to check which programs are running;

Example:
[Root @ localhost beinan] # pgrep-l gaim2979 gaim [root @ localhost beinan] # killall gaim

3.3 pkillpkill is similar to the killall application method. It also directly kills the running program. If you want to kill a single process, kill it. Application Method: # pkill example of running program name: [root @ localhost beinan] # pgrep-l gaim2979 gaim [root @ localhost beinan] # pkill gaim

3.4 xkillxkill is a program used on the desktop to kill the graphic interface. For example, when firefox crashes and cannot exit, you can click the mouse to kill firefox. When xkill is running and the icon of the personal brain is displayed, it will be okay if any graphics program crashes. If you want to terminate xkill, right-click it and cancel it. The xkill call method is [root @ localhost ~]. # Xkill

4. top tool for monitoring system tasks; compared with ps, top is a tool for dynamically monitoring system tasks, and top outputs are continuous;

4.1 top command usage and parameters; top call method top selection parameters
Parameters:
-B runs in batch mode, but cannot accept command line input;
-C: displays the command line, not just the command name;
-D N indicates the interval between two refreshes. For example,-d 5 indicates that the interval between two refreshes is 5 seconds;
-I. Do Not Display idle or zombie processes;
-N NUM: displays the number of updates and then exits. For example,-n 5 indicates that the top node will exit after 5 data updates;
-P PID only monitors the ID of the specified process; PID is a value;
-Q: refresh without any delay;
-S runs in safe mode, and some mutual effect commands are disabled;
-S cumulative mode: outputs the total CPU time of each process, including dead sub-processes;


Interactive command key:
Space is updated immediately;
C switch to the command name display, or display the entire command including parameters );
F and F add or delete display fields;
H ,? Displays help information about the security mode and cumulative mode;
K indicates the ID of the process to be killed, which is used to kill the process. The message number is 15)
I. Prohibit idle and zombie processes;
L average load switching to the display mode and normal running time;
M switches to the memory information and sorts it by memory usage;
N indicates the number of processes displayed. For example, if you enter 3, three processes are displayed on the screen;
O and O change the field display order;
R applies renice to a process and prompts you to enter the PID and renice values;
S changes the interval between two refreshes, in seconds;
T switch to display process and CPU status information;
A is sorted by process lifecycle, and the latest process is displayed at the beginning;
M is sorted by memory usage, from large to small;
N is sorted by process ID size, from large to small;
P is sorted by CPU usage, from large to small
S switches to the cumulative time mode;
T sort tasks by time/accumulation time;
W write the current configuration ~ /. Toprc;


4.2 top application example; [root @ localhost ~] # Top then, according to the preceding interactive commands, try one by one. For example, sort by M by memory usage. This example is unnecessary. Oh ...... Of course, you can upload the top output to a file; [root @ localhost ~] # Top> mytop.txt then we can view the mytop file to slowly analyze the system process status;

5. Linux Process Management priority: nice and renice;

In Linux, processes compete for resources such as CPU and memory usage. This competition is achieved through a numerical value, that is, modesty. High modesty indicates that the Linux Process Management Optimization level is the lowest. A negative value or 0 indicates that it is superior to other processes, that is, it has the right to preferentially occupy system resources. The value of humility ranges from-20 to 19.

Currently, hardware technology is developing fast. In most cases, you do not need to set the priority of a process. Unless the process gets out of control and consumes resources wildly, we may set the priority, but I personally don't feel that there is much need. We can kill the out-of-control process if we have.

Nice can specify a modest value for Linux Process Management when creating a process. The priority value of the process is the priority value of the parent process SHELL and the sum of the modesty value we specify. So when we use nice to set the priority of the program, the specified value is an increment, not the absolute value of the priority;

Nice application example: [root @ localhost ~] # Nice-n 5 gaim & Note: run the gaim program and specify a modest increment of 5 for it. So the most common application of nice is:
Nice-n increasing value program of humility
Renice uses the IDPID of the process to change the modesty, and then the Linux Process Management priority is changed.
Renice modest PID
The modesty set by renice is the absolute value of the process. See the following example;
[Root @ localhost ~] # Ps lax | grep gaim
4 0 4437 3419 10-5 120924 20492-S <pts/0 gaim
0 0 4530 3419 10-5 5160 708-R <+ pts/0 0: 00 grep gaim
[Root @ localhost ~] # Renice-6 4437
4437: old priority-5, new priority-6
[Root @ localhost ~] # Ps lax | grep gaim
4 0 4437 3419 14-6 120924 20492-S <pts/0 gaim
0 0 4534 3419 11-5 5160 708-R <+ pts/0 0: 00 grep gaim

6. About this article, Linux Process Management is still needed. Although on desktop applications, we can click the mouse to complete most of the work, but in server management, linux Process Management is still very important. Some would say, why don't you say that you are using a graphical management process tool in a desktop environment. I don't feel much necessary. If you click it, you should be able to find the tool for Linux Process Management.

Some may say: Windows Linux Process Management is really convenient. Press CTRL + ALT + DEL to make it easy to kill and cut it. I feel that the process management in Windows Linux is not very good. If some programs really need CTRL + ALT + DEL, then the system will be suspended. Or program errors. If this is not the case, restart the instance. Windows Linux Process Management is not good. It's just a friendly interface. I guess I'm right;

7. postscript;

I have been planning basic network documents for the past few days. Of course, I also write my own documents by hand, such as this article. I also want to sort out some excellent tutorials from the Forum, but later I thought that if I submitted it to LinuxSir. on the Org homepage, you must make some modifications. If I modify it, it is better to let the author modify it by myself. What I write is what I understand most, right ???

When preparing the network document plan, I asked etony about some basic network basics. I am not familiar with basic network theory. While listening to tony's explanation, I also made notes. At the same time, he also discussed the layout and ideas of the network basics with Brother tony, which is related to the problem that beginners start with. It seems like a trivial matter. In fact, things are quite large. If a newbie does not understand the document, and laruence thinks it has no value, I think it is better not to write it.

  1. Detailed anatomy of Linux Process Management
  2. Rational Thinking and selection of the Linux world makes the Linux World a reality
  3. The development status and trend of Linux operating system in various fields
  4. Linux is more secure than Microsoft Windows.
  5. One of the most popular alternatives for Linux systems

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.