Process management under Linux

Source: Internet
Author: User
Tags semaphore

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

  1. # The meanings of each field described below
  2. # User processes run as users
  3. # PID Process Descriptor is unique
  4. # Percentage of CPU when the%CPU process is running
  5. # The percentage of memory that the%MEM process runs
  6. # VSZ Virtual Memory size
  7. # RSS resident Memory set, all memory sets that cannot be swapped out
  8. # STAT indicates memory status
  9. # The usual statuses are:
  10. # S: Can sleep in the middle of the state
  11. # R: Run state
  12. # D: Non-disruptive sleep state
  13. # T: Stop state
  14. # Z: Zombie State
  15. # S:session leader leader of the so-called process
  16. # +: Indicates a foreground process
  17. # L: Multithreaded process
  18. # N: Low-priority process
  19. # <: high-priority process
  20. # TTY is used to indicate that the terminal is displayed as "? "is a terminal-independent process
  21. # START Process Starting Time
  22. # Time Process Runtime
  23. # 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

    1. # commonly used are:
    2. # 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

    1. [[email protected] ~]# pidof httpd # Process not started is not displayed
    2. [Email protected] ~]# pidof man
    3. 5283
4. Top command

Real-time Monitoring system resources

    1. # after executing the top command, enter interactive mode

  1. # Some of the interactive commands in top:
  2. # L: Controls whether the first line is displayed, load balancing information
  3. # T: Controls whether to show process information by and CPU information
  4. # m: Controls whether memory is displayed, exchange information
  5. # I or 1 (number): whether to display each CPU information separately
  6. # M: Sorted by%mem, from big to small
  7. # K:kill Kill the process
  8. # s: Modify default refresh time by default is 3 seconds
  9. # The following explains the header information:
  10. Top-21:35:17 up 10:03, 4 users, Load average:0.00, 0.00, 0.00
  11. # system time Start time logon users load balancing: 1min 5min 15min
  12. # What is the system load? This refers to the average number of processes waiting in the process queue
  13. # The information shown is equivalent to the uptime command
  14. tasks:165 Total, 1 running, 164 sleeping, 0 stopped, 0 zombie
  15. # Number of processes running number of sleep states number of dead zombies processes
  16. 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
  17. # 0.0%us:user Space: Percentage of CPU used by programs running user space
  18. # 0.0%sy:system Space: Percentage of CPU used by programs running kernel space
  19. # 0.0%ni:nice value call time accounted for CPU hundred percent
  20. # 100.0%id: Percentage of system CPU idle
  21. # 0.0%wa: Percentage of CPU used to wait for I/O
  22. # 0.0%hi: Percent of CPU for hard interrupts
  23. # 0.0%si: Percent of CPU for soft interrupts
  24. # 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
  25. mem:1012548k Total, 396328k used, 616220k free, 99444k buffers
  26. # Total memory size use memory size the size of the remaining memory size cache
  27. swap:2097144k Total, 0k used, 2097144k free, 144156k cached
  28. # Swap The total size of the partition using the remaining buffer size
  29. # 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

  1. # Common usage:
  2. # Vmstat Display information will be refreshed by default 1 seconds, always displayed
  3. # vmstart-n 2 Display information will be refreshed 2 seconds, always displayed
  4. # vmstat-n 1 4 Display information is refreshed 1 seconds, refreshed 4 times
  5. [Email protected] ~]# Vmstat-n 1 1
  6. procs-----------Memory-------------Swap-------io------System-------CPU-----
  7. R b swpd free buff cache si so bi bo in CS us sy ID WA St
  8. 0 0 0 614392 100468 144776 0 0 2 1 6 5 0 0 100 0 0
  9. # Let's explain what each field means:
  10. # procs fields about the process
  11. # R refers to the number of processes running the queue, which may be low CPU performance if it is too long
  12. # 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
  13. # memory field about RAM use
  14. # Swap memory size used by swap
  15. # free memory size Its value = Total size-buff-cache-used
  16. # Buff buffer size, purpose is to speed up I/O write operations (usually disk)
  17. # cache buffer size, mapping is to speed up the I/O read operation (usually disk)
  18. # Swap field Description Swap memory
  19. # si swapin refers to the rate of data entering the Swap memory unit: KB/S
  20. # so swapout refers to the rate of data-out swap memory in units: kb/s
  21. # If these 2 values are larger, there will be a jitter phenomenon. Recommended additional memory
  22. # Description of Io field I/O
  23. # Bi:block in the rate of memory read from the block device kb/s
  24. # Bo:block out save to block device rate kb/s
  25. # This is what we usually say about disk read and write performance, which can be improved by raid.
  26. # system fields about the systems
  27. # in:interruppt the rate at which interrupts occur
  28. # cs: Rate of context switching (process scheduling)
  29. # CPU Field Description CPU Usage
  30. # Us:user Space: Percentage of CPU used by programs running user space
  31. # Sy:system Space: Percentage of CPU used by programs running kernel space
  32. # ID: Percentage of system CPU idle
  33. # wa: Percentage of CPU waiting for I/O, due to too many differences in CPU and I/O speed
  34. # 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.

    1. # Common parameters:
    2. #-N Nice for example: Nice-n 3 httpd
    3. # in general, nice values are negative settings that are normally set by an administrator. Normal users can only set nice to positive values.
    4. # If you do not specify the-n parameter, the default nice value is 10

Renice Reset the priority of the started process.

    1. # Common options are:
    2. #-N Nice reset the value of Nice
    3. #-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:

  1. [Email protected] ~]# kill-l
  2. 1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP
  3. 6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1
  4. One) (SIGSEGV) (SIGUSR2) sigpipe) sigalrm) SIGTERM
  5. Sigstkflt) (SIGCHLD) Sigcont SIGSTOP) SIGTSTP
  6. (Sigttin) Sigttou () Sigurg) sigxcpu) Sigxfsz
  7. (SIGVTALRM) sigprof) sigwinch SIGIO) SIGPWR
  8. Sigsys) (sigrtmin) sigrtmin+1) sigrtmin+2 Notoginseng) sigrtmin+3
  9. sigrtmin+4) sigrtmin+5 (sigrtmin+6) sigrtmin+7) sigrtmin+8
  10. sigrtmin+9) (sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
  11. (sigrtmin+14) sigrtmin+15 () SIGRTMAX-14) SIGRTMAX-13) SIGRTMAX-12
  12. SIGRTMAX-11) SIGRTMAX-10 SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7
  13. (SIGRTMAX-6) (SIGRTMAX-5) SIGRTMAX-4) SIGRTMAX-3) SIGRTMAX-2
  14. SIGRTMAX-1) Sigrtmax
  15. # The signals we use are:
  16. # 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.
  17. # 2 SIGINT interrupt signal equals CTRL + C
  18. # 9 SIGKILL violence kills
  19. # Sifterm graceful off by default this is the case
  20. # Kill usage is as follows:
  21. # Kill [-signal] PID
  22. # 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.
  23. #
  24. # Killall is killing a class of processes
  25. # 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.

    1. # ping 192.168.0.1 &
    2. # This runs into the background of the job, quit the terminal when the job will be terminated.
    3. # Use the Nohup command to avoid this problem
    4. # nohup Ping 192.168.0.1 &
    5. # You can use the Jobs command to view jobs in the background
    6. # Each job has a job number to identify the job
    7. # Job Control commands
    8. # BG [[%]job_num]: Let the jobs sent backstage continue to run in the background
    9. # FG [[%]job_num]: Returning background jobs to foreground
    10. # 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

Related Article

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.