Linux provides us with some tools to view some of the status of the process, we can view the status of the process through top dynamic real-time information such as CPU, memory information, and so on, we can also use PS to statically view the current process information, while we can also be used Pstree To view the tree structure of the current active process
Pstree displaying processes in a tree structure
$ pstree-p Work | grep ad
Ps-lf 4551 or ps-elf |grep server
Pstack Show stack traces per process $ pstack 4551
The PS command is used to list those processes that are currently running on the system. The PS command lists the current snapshots of those processes, which are those that are executing the PS command at the moment, and if you want to display process information dynamically, you can use the top command.
To monitor and control the process, you must first understand the current process, that is, you need to see the current process, and the PS command is the most basic and very powerful process view command. Use this command to determine which processes are running and running, whether the process is complete, if the process is zombie, which processes are consuming too many resources, and so on. In short, most of the information can be obtained by executing the command.
PS gives us a one-time view of the process, it provides a view of the results is not dynamic continuous, if you want to monitor the process, you should use the top tool.
The KILL command is used to kill the process.
There are 5 states of processes on Linux:
1. Running (running or waiting in the running queue)
2. Interrupt (dormant, blocked, waiting for a condition to form or receive a signal)
3. Non-interruptible (receive signal does not wake up and not run, process must wait until interrupt occurs)
4. Zombie (The process has been terminated, but the process descriptor exists until the parent process calls WAIT4 () after the system call is released)
5. Stop (process received Sigstop, SIGSTP, Sigtin, Sigtou signal after stop running run)
PS Tool identifies 5 status codes for the process:
D non-interruptible uninterruptible sleep (usually IO)
R run runnable (on run queue)
S Interrupt Sleeping
T stop traced or stopped
Z Zombie a defunct ("zombie") process
1. Command format:
ps[parameters]
2. Command function:
Used to display the status of the current process
3. Command parameters:
A Show All Processes
-a displays all programs under the same terminal
-A Show All processes
C Displays the real name of the process
-N Reverse Selection
-e equals "-A"
e Display Environment variables
F show the relationship between programs
-H Display tree structure
R shows the process of the current terminal
T displays all programs for the current terminal
U specify all processes for the user
-au Show more detailed information
-aux Show all itineraries that contain other users
-c< commands > lists the status of the specified command
--lines< lines > number of rows displayed per page
--width< characters > number of characters displayed per page
--HELP Display Help information
--version Display version display
4. Usage examples:
Example 1: Show all process information
Command: Ps-a
Example 2: Displaying specified user information
Command: Ps-u Root
Example 3: Show all process information, along with command line
Command: PS-EF
Example 4:ps and grep common use combinations to find specific processes
Command: Ps-ef|grep SSH
Example 5: Make a list of the PID and related information that is currently your own log in
Command: Ps-l
Description
The meaning of the relevant information:
F represents the flag of this program, 4 represents the user as Super user
S represents the status of this program (STAT), and the significance of each stat will be described in the text
The UID program is owned by the UID.
PID is the ID of this program!
PPID is the ID of its parent program
Percentage of resources used by the C CPU
PRI this is the abbreviation for priority (precedence order), which is described in detail later
NI This is a nice value, and in the next section we will continue to introduce
ADDR This is the kernel function, which points out the part of the program that is in memory. If it's a running program, it's usually "-"
SZ-Used memory size
Wchan whether the program is currently in operation, if it is-indicates that it is operating
TTY Login Terminal Location
Time used to consume the CPU.
What are the commands that CMD has issued?
In the case of presets, PS only lists the PID associated with the current bash shell, so when I use ps-l, there are only three PID.
Example 6: List all currently in-memory programs
Command: PS aux
Description
User: The process belongs to the user account
PID: The number of the process
%cpu: Percentage of CPU resources that the process uses
%MEM: Percentage of physical memory occupied by this process
VSZ: The amount of virtual memory that the process uses (Kbytes)
RSS: The amount of fixed memory that the process occupies (Kbytes)
TTY: The process is operating on that terminal, if it is not related to the terminal, then display?, in addition, TTY1-TTY6 is the machine above the login program, if it is pts/0 and so on, it is represented by the network connected to the host computer program.
STAT: The current state of the program, the main state has
R: The program is currently in operation or can be operated
S: The program is currently sleeping (can be said to be idle) but can be awakened by certain signals (signal).
T: The program is currently detecting or stopping
Z: The program should have been terminated, but the parent program could not properly terminate him, causing the state of the zombie (Xinjiang Corpse) program
Start: The time that the process was triggered to start
Time: The process actually uses the CPU to run
Command: The actual instruction of the program
Example 7: Listing programs showing similar program trees
Command: PS-AXJF
Other examples: 1. Can be used | Pipe and more connect up paging view
Command: Ps-aux |more
2. Display all the processes and output to the Ps001.txt file
Command: Ps-aux > Ps001.txt
3. Output the specified field
Command: Ps-o Pid,ppid,pgrp,session,tpgid,comm
Multi-threaded viewing tool under Linux (Pstree, PS, Pstack)
Pstree [-A] [-c] [-h|-hpid] [-l] [-n] [-P] [-u] [-z] [-a|-g|-u] [Pid|user]
Pstree-v
Options: -A: Displays the complete instructions for each program, including the path, parameters, or the ID of the resident service- L: Display tree in long column format -P: Display the PID number of the Process- u: Display user name- V: Display version information [Pid|user]: According to the PID or user information to display the information we need
Example 1: Displaying a process in a tree view also shows the process number and process ID
# pstree-p
Example 2: Show all details of all processes
# pstree-a
Example 3: A tree view of process PID for <pid> and descendant processes
# Pstree <pid>
Example 4: A tree view of process PID for <pid> and descendant processes, showing the PID of each process
# pstree-p <pid>
Linux strace, pstack command use detailed
Strace is a program debugging tool in the Linux environment that monitors the system calls used by an application and the system information it receives.
Linux is a debugging tool, not only can be used to find program errors, why the system hangs dead, command why error, can also be used to find which programs occupy system resources.
Command instance
For example, but the Web server load is too large, the number of sites, I do not know that the site occupies too much resources, then strace to use.
The first step is to view the php-cgi process number:
PS aux | grep php-cgi
The second step is to view the system call
Strace-p Process Number
Alternatively, the recorded results exist in the Output.txt file
Strace-o output.txt-t-tt-e trace=all-p Process number
The command under Mac is Dtruss
Pstack see the stack inside the active process
Pstack Process Number
Use top to view information for a process, top-p process number
Pstack:
The pstack command displays stack traces for each process.
The Pstack command must be run by the owner or root of the corresponding process.
You can use Pstack to determine where the process hangs.
The only option that this command allows to use is the PID of the process to be checked.
Pstack see the stack inside the active process
Usage:
root# Pstack PID
Gstack:
Gstack-Print a stack trace of a running process
How to use:
Gstack PID
Describe
Gstack connecting to the active process of the PID in the command line
Prints the execution stack trace. if the elf symbol exists in binary (USU-)
If you do not run the Strip (1), then this example will be printed
The same is true.
If the process is part of a thread group, then gstack prints out a stack
Each thread in the group is tracked.
The effect on Pstack can be summed up as follows:
1). View the number of threads (compared to Pstree, which contains detailed stack information)
2). Can simply verify whether the execution is performed according to the predetermined call order/Call stack
3). When using high frequency multiple sampling, can discover where the program is currently blocked, and where is the performance consumption point?
4). Can reflect the suspect deadlock phenomenon (multiple threads simultaneously at wait lock, need further verification)
Of course, can also give examples of more role, I believe that the use of Jstack coder, must be deeply thought.
Pstack principle:
Pstack use is very big, then what is the principle behind it?
It can be observed that pstack is actually a /usr/bin/gstack soft link , and gstack itself is a shell script based on the GDB wrapper.
Let's simply analyze this powerful shell script:
Note: Because the code is too long, this side selects the most core fragment, backtrace= "thread apply all BT"
The shell uses here document to complete the interactive work of GDB (note the EOF identifier, and the interactive commands within the range).
It is important to enter the thread apply all BT this interactive command. This command requires the output of all thread stack information.
The results of the GDB output are replaced and filtered by pipelines and with sed commands .
Summarize:
Pstack is actually a functional encapsulation of GDB, but its implementation is really very practical. This article describes the use and rationale of Pstack, as well as common uses, and the following describes a mechanism for deadlock detection, which is welcome attention.
Process some commands pstree,ps,pstack,top