In operating system systems, processes are a very important concept.
I. Knowledge of processes in Linux
1. What is a process?
In layman's terms, the process is a running program. The process descriptor (PID) is the only indicator of the process, and the process is defined and managed by Task_struck and Task_list in the Linux kernel.
2, the classification of the process
1) According to the different modes of Linux operation is divided into:
Kernel mentality: This type of process runs in kernel mode and executes some kernel instructions (Ring 0).
User state: This type of process works in user mode and executes user instructions (Ring 3).
If the user-state process is to perform some kernel-mentality instructions, a system call is generated, and the system call requests the kernel to complete the relevant request and return the result to the user-state process.
2) According to the status of the process can be divided into:
Running state: Running running process
Can interrupt sleep state: The process is asleep, but can be interrupted
Non-disruptive sleep state: the process is asleep but cannot be interrupted
Stop state: stoped will not be dispatched by the kernel
Zombie State: Zombie is caused by the fact that after the process is over, its parent process does not wait for it.
3) According to the intensity of the operation
CPU Intensive: Processes that consume more CPU time while they are running.
I/O intensive: Processes that consume more I/O time during runtime.
Typically, I/o-intensive priorities are higher than CPU-intensive.
4) According to the processing method of the process
Batch process:
Interactive process:
Real-time processes:
3. Priority of the process
The priority of the process is represented by 0-139 numbers, and the number priority is from small to large, in order: 0-99, 139-100.
Priority is divided into 2 categories:
Real-time priority: 0-99, which is maintained by the kernel
Static priority: 100-139, you can use nice to adjust, the value of the Nice value range is [ -20,19], respectively, corresponding to 100 to 139. The nice default value is 0.
Dynamic priority: Dynamically maintained by the kernel and dynamically adjusted.
II. management tools for the process
1. Pstree command to view the number of processes. common usage is as follows:
2. PS command to view the related status of the process. support for SYSV and BSD two style options.
Common selection:
A terminal-related process
x non-terminal processes
U show the user running the process
Common combination options: PS aux
- # The meanings of each field described below
- # User processes run as users
- # PID Process Descriptor is unique
- # Percentage of CPU when the%CPU process is running
- # The percentage of memory that the%MEM process runs
- # VSZ Virtual Memory size
- # RSS resident Memory set, all memory sets that cannot be swapped out
- # STAT indicates memory status
- # The usual statuses are:
- # S: Can sleep in the middle of the state
- # R: Run state
- # D: Non-disruptive sleep state
- # T: Stop state
- # Z: Zombie State
- # S:session leader leader of the so-called process
- # +: Indicates a foreground process
- # L: Multithreaded process
- # N: Low-priority process
- # <: high-priority process
- # TTY is used to indicate that the terminal is displayed as "? "is a terminal-independent process
- # START Process Starting Time
- # Time Process Runtime
- # command to execute a process if the command is surrounded by "[]", the kernel thread
-E Show All processes
-F Display Completion format information
Commonly used together: PS-EF
But in some cases, our commands sometimes show incomplete
When you want to display the completion of PS-EFWW
-F: Show additional information
-H: Show hierarchy of processes
Common Combination Method: Ps-efh
We may use the most options in the future:
-O We can customize the display field
- # commonly used are:
- # pid command psr pri ni%cpu%mem rsz vsz etc
3, Pgrep,pidof
Pgrep Common selection:
-U View the process number of the specified user
-G View the process number of the specified user group
-L Show process name and process number
Pidof: Displays only the PID of the started process
- [[email protected] ~]# pidof httpd # Process not started is not displayed
- [Email protected] ~]# pidof man
- 5283
4. Top command
Real-time Monitoring system resources
- # after executing the top command, enter interactive mode
- # Some of the interactive commands in top:
- # L: Controls whether the first line is displayed, load balancing information
- # T: Controls whether to show process information by and CPU information
- # m: Controls whether memory is displayed, exchange information
- # I or 1 (number): whether to display each CPU information separately
- # M: Sorted by%mem, from big to small
- # K:kill Kill the process
- # s: Modify default refresh time by default is 3 seconds
- # The following explains the header information:
- Top-21:35:17 up 10:03, 4 users, Load average:0.00, 0.00, 0.00
- # system time Start time logon users load balancing: 1min 5min 15min
- # What is the system load? This refers to the average number of processes waiting in the process queue
- # The information shown is equivalent to the uptime command
- tasks:165 Total, 1 running, 164 sleeping, 0 stopped, 0 zombie
- # Number of processes running number of sleep states number of dead zombies processes
- Cpu (s): 0.0%us,0.0%sy, 0.0%ni, 100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
- # 0.0%us:user Space: Percentage of CPU used by programs running user space
- # 0.0%sy:system Space: Percentage of CPU used by programs running kernel space
- # 0.0%ni:nice value call time accounted for CPU hundred percent
- # 100.0%id: Percentage of system CPU idle
- # 0.0%wa: Percentage of CPU used to wait for I/O
- # 0.0%hi: Percent of CPU for hard interrupts
- # 0.0%si: Percent of CPU for soft interrupts
- # 0.0%st: The percentage of the CPU that the system was "stolen", which generally refers to the CPU used to run the virtual machine
- mem:1012548k Total, 396328k used, 616220k free, 99444k buffers
- # Total memory size use memory size the size of the remaining memory size cache
- swap:2097144k Total, 0k used, 2097144k free, 144156k cached
- # Swap The total size of the partition using the remaining buffer size
- # The information shown here is equivalent to the free command
Common options:
-D #: Specify the Refresh time interval
-B: Show top refresh in batches
-N #: Displayed batches
Example: top-d 4-b 2-n 3
4, Htop
The Htop command is an upgraded version of the top command, which is better than the top command, both functionally and in the interface display.
U: Interactively Select the process that displays the specified user
L: Displays the list of files opened by the process where the cursor is located
S: Displays the system call that the process executing the cursor
A: Bind the process to the specified CPU
#: Quickly position the cursor to the PID # process
is the Htop interface. This tool is more powerful, use more functions, here first to do a brief introduction. In the future I will write Htop blog specifically.
5, Vmstat
Wmstat Viewing virtual usage
- # Common usage:
- # Vmstat Display information will be refreshed by default 1 seconds, always displayed
- # vmstart-n 2 Display information will be refreshed 2 seconds, always displayed
- # vmstat-n 1 4 Display information is refreshed 1 seconds, refreshed 4 times
- [Email protected] ~]# Vmstat-n 1 1
- procs-----------Memory-------------Swap-------io------System-------CPU-----
- R b swpd free buff cache si so bi bo in CS us sy ID WA St
- 0 0 0 614392 100468 144776 0 0 2 1 6 5 0 0 100 0 0
- # Let's explain what each field means:
- # procs fields about the process
- # R refers to the number of processes running the queue, which may be low CPU performance if it is too long
- # b blocks the length of the queue, which is typically used to wait for I/O to complete. If it is too large, it indicates low I/O performance
- # memory field about RAM use
- # Swap memory size used by swap
- # free memory size Its value = Total size-buff-cache-used
- # Buff buffer size, purpose is to speed up I/O write operations (usually disk)
- # cache buffer size, mapping is to speed up the I/O read operation (usually disk)
- # Swap field Description Swap memory
- # si swapin refers to the rate of data entering the Swap memory unit: KB/S
- # so swapout refers to the rate of data-out swap memory in units: kb/s
- # If these 2 values are larger, there will be a jitter phenomenon. Recommended additional memory
- # Description of Io field I/O
- # Bi:block in the rate of memory read from the block device kb/s
- # Bo:block out save to block device rate kb/s
- # This is what we usually say about disk read and write performance, which can be improved by raid.
- # system fields about the systems
- # in:interruppt the rate at which interrupts occur
- # cs: Rate of context switching (process scheduling)
- # CPU Field Description CPU Usage
- # Us:user Space: Percentage of CPU used by programs running user space
- # Sy:system Space: Percentage of CPU used by programs running kernel space
- # ID: Percentage of system CPU idle
- # wa: Percentage of CPU waiting for I/O, due to too many differences in CPU and I/O speed
- # st: Percentage of the CPU that the system was "stolen", typically referring to the percentage of time spent on the CPU used by the virtual machine to run
6, Nice,renice
Adjusts the priority of the process.
Nice sets the priority when the process starts.
- # Common parameters:
- #-N Nice for example: Nice-n 3 httpd
- # in general, nice values are negative settings that are normally set by an administrator. Normal users can only set nice to positive values.
- # If you do not specify the-n parameter, the default nice value is 10
Renice Reset the priority of the started process.
- # Common options are:
- #-N Nice reset the value of Nice
- #-P PID setting process PID
7, Kill,killall
For people with Linux C programming experience, we know that one of the ways of IPC communication is through the semaphore (signal), so for the kill and Killall commands, they have a lot to do with the semaphore, or kill, The Killall command allows us to manually pass signals to the process to control the process by signaling the semaphore.
The common semaphore is as follows:
- [Email protected] ~]# kill-l
- 1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP
- 6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1
- One) (SIGSEGV) (SIGUSR2) sigpipe) sigalrm) SIGTERM
- Sigstkflt) (SIGCHLD) Sigcont SIGSTOP) SIGTSTP
- (Sigttin) Sigttou () Sigurg) sigxcpu) Sigxfsz
- (SIGVTALRM) sigprof) sigwinch SIGIO) SIGPWR
- Sigsys) (sigrtmin) sigrtmin+1) sigrtmin+2 Notoginseng) sigrtmin+3
- sigrtmin+4) sigrtmin+5 (sigrtmin+6) sigrtmin+7) sigrtmin+8
- sigrtmin+9) (sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
- (sigrtmin+14) sigrtmin+15 () SIGRTMAX-14) SIGRTMAX-13) SIGRTMAX-12
- SIGRTMAX-11) SIGRTMAX-10 SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7
- (SIGRTMAX-6) (SIGRTMAX-5) SIGRTMAX-4) SIGRTMAX-3) SIGRTMAX-2
- SIGRTMAX-1) Sigrtmax
- # The signals we use are:
- # 1 SIGHUP When the process is not closed, reread the configuration file. Ngnix is doing quite well in this area and can even be upgraded online.
- # 2 SIGINT interrupt signal equals CTRL + C
- # 9 SIGKILL violence kills
- # Sifterm graceful off by default this is the case
- # Kill usage is as follows:
- # Kill [-signal] PID
- # for signal there are three kinds of representations: for example: 1) -9-15-1-2 2)-sigkill-sighup-sigterm 3)-hup-kill-term-int etc.
- #
- # Killall is killing a class of processes
- # For example: Killall httpd equivalent to kill ' pidof httpd '
8, JOBS,BG,FG
What is a job? A job is a process in which many processes work together to accomplish a specific task. The job has 2 kinds of foreground job and background job.
Use & or CTRL + Z to get a process into the background.
- # ping 192.168.0.1 &
- # This runs into the background of the job, quit the terminal when the job will be terminated.
- # Use the Nohup command to avoid this problem
- # nohup Ping 192.168.0.1 &
- # You can use the Jobs command to view jobs in the background
- # Each job has a job number to identify the job
- # Job Control commands
- # BG [[%]job_num]: Let the jobs sent backstage continue to run in the background
- # FG [[%]job_num]: Returning background jobs to foreground
- # Kill%job_num: Terminates the specified job
9. Dstat command to view various status information
Usage: dstat [-AFV] [Options ...] [Delay [Count]]
Common parameters |
Specific meaning |
-C |
Display CPU statistics, if there are multiple CPU summary statistics |
-D |
Show disk statistics, if there are multiple disks, summary statistics |
-D DEVICE |
Display information for a specific disk |
-G |
Display page information (specifically what the page is and what he does, as specified in a later Memory tuning blog) |
-I. |
Show Broken Statistics |
-M |
Display statistics for memory |
-L |
Display the load information of the system |
-N |
To display the related properties of a network interface |
-S |
Display System Properties |
-N Inter_face_name |
Display properties for a specific interface |
-S |
Display properties of swap memory |
-P |
Show Process Queue |
--ipc |
Displays IPC Message Queuing, semaphores, and shared memory usage |
-A |
Equivalent to-cdngy display CPU, disk, NIC, page, System Properties |
-F |
Display all information in full format, |
--TCP,--UDP |
Display TCP,UDP status information |
See the Man Manual for more parameters.
Example:
10. View Memory Mappings
Pmap PID View memory mapping of the corresponding process, commonly used is: Pman ' pidof process_name '. Of course, this information can also view the/proc/pid/pmap file view.
11. Glances command
A powerful system monitoring tool: real-time monitoring of information such as Cpu,meomory,load,swap,network,mount,disk.
For other features, use H to get help. Because this tool is more powerful, so here is a simple introduction, later in the Htop tool to do a special introduction.
Process management under Linux