Linux Learning-Process Management

Source: Internet
Author: User
Tags cpu usage


Directory

    • Parent and child processes

    • Service

    • Job Management (& | Jobs | FG | BG | Kill

    • PS command

    • Priority and Nice priorities

Parent and child processes


A child process is a program derived from a parent process. To give a common example, every time we log in the shell, we will trigger a process, we are in this shell input command to work, in fact, you enter the command will also trigger a new process, the new process is a child process, and the beginning of the shell process is a login to the parent process. we did a little experiment, we landed a new shell under the current landing shell, and then entered a command to view the status information of the process

PID is the identification code of the process, Ppid is the identification code of the parent process . We found that the first bash PID and the second bash's ppid are both the ppid of the 2446,ping command and the PID of the second bash is 2660, which means that the commands entered by bash are the result of the current bash process.

Service (resident memory)


The processes that are initiated during the system boot process, which we call daemons, are also called services , and such processes continue to provide the functionality necessary for the system and the network.


Work Management


Usually under the current bash to perform a task, you must wait for the task to complete before you can enter the command to perform the next task, now we want to do the document replication, data query, software installation and many other tasks at the same time work, it is necessary to use the knowledge of job management.


Move a task to the background (&)

Add & directly behind the command that executes the task, and then we can enter a command to perform another task for multitasking. For example, we will back up the files and directories under/etc without having to wait for

[[email protected] ~]# tar-zpc-f/app/etc.tar.gz/etc/* &[1] 2948 # 2948 for Pid[[email protected] ~]# tar:removing Leading '/' from member names# after a period of time, the following information appears to represent the completion of the task [1]+ done Tar-zpc-f/app/etc.tar.gz/etc/*

There is a problem here, if for those who will have the result of the command, such as ping command, after using &, the terminal will always show the ping command return results, this time because it is a background process, with CTRL + C also does not work, we can use data redirection , such as:

Ping 172.18.0.1 >/tmp/txt 2>&1 &


View background work status (jobs)

If you want to know how many processes are working in the background, you can use the Jobs command to view the process for my system now working in the background, showing the results indicate the working status of the acquired process, and note the preceding +- number, they have a certain meaning, they represent the order of FG fetching.

Jobs-l # Lists details of background processes, including PID

-R # list only running background processes

-S # list only paused background processes


Converting a background process to foreground process (FG)

We want the background process to go to the foreground to run, and you can use the FG command.

FG # defaults to the jobs command display results with + in the go to the foreground

-# Turn the belt-to the front desk

%num # The process of showing jobs the number specified in the results to the foreground


Background process stopped to running (BG)

, I enter the command/bin/ping 172.18.0.1 >/tmp/txt, press ENTER after pressing CTRL + Z to pause the command, the process of the ping command is in the stopped state, and after using BG, it becomes running state.

BG # A process that displays the results of the jobs command by default

-# for the band-process

%num # Process for jobs to display the numbers specified in the results


Kill background process (kill)

The KILL command directly terminates the background process, which is a bit special and requires a signal to be processed, so let's take a look at the use of the KILL command:

Kill-l # View the signal you can use, then man 7 signal to see the actual effect

Kill-signal%num | PID # KILL command format,%num for the jobs command display the first column number in the result

First, introduce several common signal

KILL-1 # re-reading a configuration parameter file

Kill-2 # is equivalent to CTRL + C

Kill-9 # Forces a process to be deleted, for example, to edit a file for vim without actively deleting it. file.swp

KILL-15 # Normal termination of a process, for example, for VIM, active deletion. FILE.SWP


PS command


PS shows the health of the process

Ps-l # shows only the status of the process under the current bash

Ps-la # Displays information about all processes in the system, and each column field represents the following meanings

F
The flag of the process, which is the summary permission, 4 means that the permission is root,1 on behalf of the sub-program is only replicated
S Run status (STAT),r=> run,s=> sleep,d=> clog,t=> stop State,z=> Zombie state, terminated but unable to remove memory
Uid/pid/ppid Initiator/Process number/Parent process number of the process
C
CPU usage, percent per unit
Pri/ni
Priority/nice Priority level
Addr/sz/wchan
Addr represents the part of the process in memory,- represents the running state, SZ represents the memory size of the process, Wchan represents whether the process is running,- indicates that it is running
Tty
The terminal location of the lander, if remote login is pts/n
Time
The time it takes for the CPU to run the process
Cmd What the command triggers for this process


PS aux # without-, display all the process information of the system, each column field represents the following meanings

USER
Initiator of the process
Pid
Process number
%cpu
Process consumes CPU percent
%MEM Percentage of process consuming physical memory
Vsz/rss
Process consumes the size of virtual memory/the size of the fixed memory that the process occupies, which can be understood as the number of pages
Tty

The ID of the control terminal, which represents the terminal-independent process, TTY1-TTY6 is the native login terminal, and pts/n is the remote login terminal

STAT Process status
Start/time Time the process started/process actually consumes CPU time
COMMAND
The command that triggers the process, if it is followed by a <defunct>, indicates that this is a zombie process


PS AXJF # Same without--process has dependencies between processes, the command can display process information in the form of a similar program tree


Looking at a section of the picture, we can see that the current I am using SSHD to provide remote network services Telnet host, the program (parent process) provides a bash environment (child process), I am in this bash environment (parent process) INPUT command PS AXJF trigger a new process (child process) .


Priority and Nice -Level


Linux gives the process a priority precedence (PRI), the lower the priority , the higher the PRI is the system core dynamic control adjustment, the user cannot make changes, then if we want to adjust the priority of the process, and the PRI can not be adjusted, We have introduced nice (NI) to allow users to manually modify the process's run priority level.

The nice value has a range of -20--19, but the adjustment of the friendly value is also limited to the user, androot can adjust his or her own and other user-friendly values , and the normal user can only adjust their nece value and adjust the range to 0--19. To prevent nice from being a negative value when the resource is preempted, and the nice value can only be adjusted higher and lower than the nice values , the PRI and NI have some relevance:

PRI (new) =pri (old) +ni

It is important to note that, from a relational standpoint, although NI can affect the priority of the process, the priority of the process is ultimately determined by the core of the system, and all of us cannot simply understand Pri+ni.


Nice # Set a nice value for the newly created process


Format: nice-n num COMMAND # num range is -20--19

We set the nice value to 5 for the VIM command, and the originally preset PRI value is Bash's PRI (80), which now becomes 85


Renice # re-adjusting the nice value of an existing process


Format: Renice num PID # num range is -20--19, if this range is exceeded, it is automatically adjusted to the nearest-20 or 19

Look at the process Bash's pri and NI information, and then enter the Renice command to modify the bash process NI, after the modification found that the PRI and NI changes, and we also found that the child process inherits the parent process of the PRI and NI.



Linux Learning-Process Management

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.