Senior Programmer Summary: Analysis of six methods Linux process, I tell you all

Source  | backend technology school

Cover photo | CSDN download the visual China

Operating system "process" is the basic concept of computer science should contact the operating system aside those purely theoretical bottom layer implementation, doing software development in Linux for so many years, every time the program is running problems, we must step by step analysis of each process states, to troubleshoot where the problem is, this lemon with you in a Linux environment practical operation, a step by step to explore uncover those secrets "Linux process" of.

What is the process

First, we say that under the concept of "program", the program is an ordered collection of instructions stored on a disk, is static. Process is a process of program execution, including the dynamic creation, scheduling, and the demise of the whole process, it is the smallest unit of resource management program.

A thread is the smallest unit of operation of the operating system can schedule calculation. In most cases, it is included in the process, the actual operation of the unit process, a process can contain multiple threads, is the smallest unit of resource scheduling. [Quote Wikipedia]

Multi-threaded programming model

The first step in the process of inquiry, Are you there? Are you ok?

  • ps

report a snapshot of the current processes. lists snapshot of the current system processes.

Find the process PID (Process IDentity), pid uniquely identifies a process. Ps use this command, which we should all know it, for white users, first of all he is not Photoshop.

Give you a brief introduction, general usage is listed in the system often ps -ef information, usually with a pipe grep process of interest, like ps -ef | grep intresting first column represents the process ID PID, PPID (parent process ID) on behalf of the parent process ID.

The second step to explore the process, let me see what you have to pay a friend (system call & signal)

  • strace

System call and signal trace system calls and signals inside the tracking process.

What is a "system call"? System call (system call), refers run in "user mode" program requests to the operating system "kernel mode" services require higher privileges to run, the system calls the program provides an interface between the user and the operating system.

Strace followed behind to start a process that can trace system calls and signals the start of the process, this command can see the process executed when they are called system calls which can output time of the system call occurs through a different option is specified, the accuracy can be accurate to microseconds, and even statistical analysis system "call time-consuming," which in the investigation process suspended animation problems, when useful, can help you find the process of card on which system call. You can also specify -p parameter has been running process like gdb attach pid add up the track as attachment.

strace statistics

The third step is to explore the process, let me see your little brother with the (thread).

  • pstack

print a stack trace of a running process print out information about the running program stack.

Run pstack pid you can see the stack trace of the current thread running, before which the pid ps command can be used to obtain, pstack can see the thread number starts within processes, each "stack" content can also be seen in the thread to.

See printed above LWP yet, here is the point of knowledge, LPW means Light-weight process lightweight threads. Extended knowledge:

  1. Linux is not a real thread

  2. Linux is not thread Thread is achieved by a process to simulate so called: lightweight processes

  3. The process is the smallest unit "resource management", the thread is "resource scheduling," the smallest unit (not considered here coroutines)

The fourth step process to explore, so little brother (thread) ranked teams out of it.

  • pstree

display a tree of processes pstree information in a tree structure of the printing operation Process Structure

You can see the relationship between intuitive process and it starts a thread, and can display the process ID.

pstree

The fifth step inquiry process is dead (process crash) is alive (running process) I have to know your secret (stack frame & context).

  • gdb

gdb is the GNU gcc package developed under the Linux debugging tool, you can view the program stack, set breakpoints, print runtime information, and even debugging multithreaded programs, it is very powerful.

Here the gdb as a bit overkill in terms of command, gdb to detail, you can hold up an article length, long story short here, have the opportunity to open it next article in detail.

  • use

Use gdb debugging C / C ++ compiler when the program first to add -g option, g ++ -g test.cpp -o test program generated in this way can be used gdb to debug it.

  1. Can directly debugging with gdb start the program, the command: gdb prog

  2. Using gdb attached to a process already started debugging can be. Command: gdb prog pid

  3. After the program crashes parameters corefile can also use gdb debugging, look left any last words (stack information) to the program before you die. Command: gdb prog corefile, there is one thing to note that some Linux system does not generate corefile default program crashes, then you need to ulimit -c unlimited This will generate a corefile.

Explore the sixth step process, about you all, I want to know.

One step closer

Via / proc / pid file for running processes when information and statistics. / Proc file system is a pseudo-system exists only among the memory, external memory without taking up space in the kernel mode of the file system and the process of providing a communication interface. The system / proc directory:

proc directory

/ There are many digital directory named proc directory, each number represents a process which is the process number PID directory. When the system is currently running in each process under / proc corresponds to a process number as the directory name of the directory / proc / pid, they process information is read interface, we can enter into this file inside knowledge of the workings of the process information and statistics.

  • High-frequency use

/ Proc there are some important file / directory under pid, pick a few frequently used to talk about. List / proc / pid / environ contains the process of the available environment variables. Program a problem if you are unsure whether the environment variable settings to take effect, you can see the cat out of the file to confirm.

/ Proc / pid / fd / directory contains links to every file open for the process. From here you can view the information process open file descriptors, including standard input, output, error streams, the process of open file descriptors socket connection can be seen, lsof command has a similar effect.

/ Proc / pid / stat contains process all status information, process ID, parent process ID, thread group number, the task in time user mode operation, the task with the kernel mode run time, the code segment virtual address space , bitmap information signals like blocking everything.

  • Other statistics

complete command / proc / pid / cmdline This file holds the process line
/ proc / pid / cwd a symbolic link, pointing to the process of the current working directory
/ proc / pid / exe contains a program link is running in the process
/ proc / pid / mem contains the process content in memory
/ proc / pid / statm contains process memory usage information

in conclusion

Well, the operation down meal, you secret process behind it and you have been very understanding, our good friend the next "process" if you encounter any problems (crash coredump, suspended animation, blocking, system calls a timeout, file description Fu abnormal), you should know how to help it deal with it! Let's summarize:

  • ps see the process id, take a look at the process and the process is still not in the state

  • If the case strace, psstack look at the current process information, which is not stuck in the position, comparing each frame of the last call information to find outliers

  • If the process is no longer, if there corefile files directly View corefile information on gdb

  • Other incurable diseases suspected process status information that we look at / proc / pid following process status information may give you inspiration.

  • Finally, if all else fails, we turn a blind eye pray!

Written in the last

Today's share want to help you, I wish you wrote down service never, never coredump, let's teach you to eat gray go above operation. Thank each bit of reading. Head of the article is to share the understanding of knowledge, technical articles of all will be repeated confirmation in order to ensure maximum accuracy, if obvious flaws appear in the text is also welcome that together we learn explored.

At the same time, open to all developers under the Fanger Wei code scanning fill out the "big developers and AI research", just 2 minutes, you can harvest value of 299 yuan, "AI developers million people congress" live online tickets!

推荐阅读:“出道” 5 年采用率达 78%,Kubernetes 的成功秘诀是什么?
一群阿里人如何用 10 年自研洛神云网络平台?技术架构演进全揭秘!
一文告诉你,如何在 Kubernetes 的容器引擎中运行 KVM 和 VMware VM!半小时训练亿级规模知识图谱,亚马逊AI开源知识图谱嵌入表示框架DGL-KE马化腾、马云并列成为中国首富;百度回应“将上线电商直播”;.NET 5 Preview 2 发布 | 极客头条程序员职场背锅甩锅指南警惕!新骗术出现:这些虚假二维码生成器已成功盗取 4.6 万美元!真香,朕在看了!
Released 303 original articles * won praise 1288 · Views 1.41 million +

Guess you like

Origin blog.csdn.net/FL63Zv9Zou86950w/article/details/105383294