Don’t panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Time:2021-7-30

The article is continuously updated every week. Your “three links” are the greatest affirmation to me. WeChat can search the public for the first time to read the official account of “backend Technology School” (usually one to two updates earlier than blogs).

Operating systemprocess” is a basic concept that you should contact when learning computers. Regardless of the underlying implementation of pure theoretical operating system, you have been doing software development under Linux for so many years. Every time a program runs a problem, you have to analyze the various states of the process step by step to find out where the problem is. This time lemon takes you to practice under Linux, Explore and uncover the secrets of “Linux Process” step by step.

What is a process

First, let’s talk about the concept of “program”. A program is an ordered collection of instructions stored on disk. It is static. Process is the process of program execution, including the whole process of dynamic creation, scheduling and extinction. It is the smallest unit of program resource management.

Thread is the smallest unit that the operating system can schedule operations. In most cases, it is included in the process, which is the actual operation unit in the process. A process can contain multiple threads, which is the smallest unit of resource scheduling. ” Quote Wikipedia “

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

The first step in the inquiry process, are you there? Are you okay?

ps

report a snapshot of the current processes.Lists snapshots of the current system process.

Find the process PID (process ID entity), which uniquely identifies a process. usepsThis command, this command everyone should know. For Xiaobai user, first of all, he is not Photoshop.

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

To give you a brief introduction, the general usage isps -efThe information in the pipeline is usually listed in the systemgrepOut of the process you are interested in, like thisps -ef|grep intrestingThe first column PID represents the process number, and PPID (parent process ID) represents the parent process number.
Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Explore the second step of the process and let me see what friends you have made (system call & signal)

strace

strace - trace system calls and signalsTracks system calls and signals within a process

What is “system call”? System call refers to that a program running in “user state” requests a service requiring higher permission from the operating system in “kernel state”. System call provides an interface between the user program and the operating system.

straceThen start a process, which can track the system calls and signals of the process after startup. This command can see which system calls are called when the process is executing. By specifying different options, you can output the time of system calls, which can be accurate to microseconds, and even count and analyze the “time-consuming” of system calls, This is very useful when troubleshooting the problem of process fake death. It can help you find out which system call the process card is on. Processes that are already running can also be specified-pParameter pluspidimagegdb attachLike that.
Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Explore the third step of the process. Let me see your little brothers (threads).

pstack

print a stack trace of a running processPrint out the stack information of the running program.

Execute commandpstack pidYou can see the stack information of the current thread running, in which the PID is availablepsCommand obtained,pstackYou can see the thread number started in the process and the “stack” content of each thread in the process.
Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

See the LWP printed above? This is a knowledge point. LPW refers toLight-weight processLightweight threads. Extended knowledge:

  1. There are no real threads in Linux
  2. There are no threads in LinuxThreadIt is simulated and implemented by a process, so it is called a lightweight process
  3. Process is the smallest unit of resource management, and thread is the smallest unit of resource scheduling (collaboration is not considered here)

Explore the fourth step of the process, let the little brothers (threads) come out and line up.

pstree

pstree - display a tree of processesPstree prints the running process structure information according to the tree structure

You can intuitively view the relationship between the process and the thread it starts, and display the process ID.

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

The fifth step of exploring the process is whether it is dead (process crash) or alive (process running). I need to know your secret (stack frame & Context).

gdb

GDB is a program debugging tool under Linux in the GCC suite developed by GNU. You can view the program stack, set breakpoints, print program runtime information, and even debug multithreaded programs. It is very powerful.

It’s a bit overqualified to regard GDB as an order here. If you want to talk about GDB in detail, you can hold up the length of an article. To make a long story short, I have the opportunity to open another article to introduce it in detail.

use

To debug C / C + + programs with GDB, you should add it when compiling first-gOptions,g++ -g test.cpp -o testIn this way, the generated program can be debugged with GDB.

  1. You can directly start program debugging with GDB. The command is:gdb prog
  2. GDB can also be used to attach to a started process for debugging. Command:gdb prog pid
  3. After the program crashes, the parameter corefile can also be debugged with GDB to see what last words (stack information) are left for you before the program dies. Command:gdb prog corefile, it should be noted that some Linux systems do not generate default program crashescorefile, then you needulimit -c unlimitedThis will generatecorefileYes.

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Step 6 of the inquiry process, I want to know everything about you.

One step closer

adopt/proc/pidFile to understand the runtime information and statistics of the process./procThe system is a pseudo file system, which only exists in memory and does not occupy external memory space. It provides a communication interface between the kernel and processes in the form of file system. Enter the system/proccatalog:

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

/There are many directories named by numbers under the proc directory. Each number represents the process number PID. They are the process directory. Each process currently running in the system corresponds to a directory with the process number as the directory name under / proc/proc/pid , they are interfaces for reading process information. We can enter this file to understand the runtime information and statistics of the process.

High frequency use

/proc/pidThere are some important files in the directory. Pick some that are used frequently.
/proc/pid/environContains a list of available environment variables for the process. There is something wrong with the program. If you are not sure whether the environment variable setting is effective, you cancatCome out and check this file for confirmation.

/proc/pid/fd/This directory contains links to each file opened by the process. From here, you can view the file descriptor information opened by the process, including standard input, output, error flow, and the file descriptor information opened by the processsocketThe connection file descriptor can also be seen,lsofCommands have a similar effect.

/proc/pid/statIt contains all the status information of the process, including process number, parent process number, thread group number, the time when the task runs in user mode, the time when the task runs in kernel mode, the code segment of virtual address space, the bitmap of blocking signal and so on.

Other statistics

/proc/pid/cmdlineContains commands to start a process
/proc/pid/cwdContains a link to the current process working directory
/proc/pid/exe Contains links to programs running in process
/proc/pid/mem Contains the contents of the process in memory
/proc/pid/statm Contains the memory usage information of the process

Well, after a meal, you have a good understanding of the process and the secrets behind it. Next time, if our good friend “process” encounters any problems (crashes)coredump, false death, blocking, system call timeout, file descriptor exception), you should know how to handle it! Let’s summarize:

  • PS view the process ID to see if the process is still in and the process status
  • If you’re herestracepsstackLook at the current information of the process, which is not stuck where, compare the final call information of each frame to find the outliers.
  • If the process is no longer, if there iscorefileFile, directgdbseecorefileinformation
  • When other difficult and miscellaneous diseases doubt the process status information, take a look/proc/pidThe following process status information may inspire you.
  • Finally, if none of the above works, close your eyes and pray!

Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Write at the end

I hope today’s sharing will be helpful to you. I wish the services you write will never go down and never coredump. Let the operations taught above eat ash.
Don't panic when there is a problem with the process under Linux. Senior programmers teach you 6 tricks to do it!

Finally, thank you for reading. The purpose of the article is to share the understanding of knowledge. I will repeatedly verify technical articles in order to ensure the accuracy to the greatest extent. If there are obvious mistakes in the article, we are also welcome to point out. We will study together in discussion.

You can search the official account of WeChat for “back end technology school” to reply to “information”. The article is continuously updated every week. See you in the next issue!

reference

https://man.linuxde.net/gdb

https://blog.csdn.net/dan1518…

https://blog.csdn.net/m0_3792…

https://blog.csdn.net/enweite…

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]