The articles are updated every week, and your “three companies” are the biggest affirmation for me. You can search the public account “Back-end Technology School” on wechat to see the latest articles in advance (usually updated one or two articles earlier than the blog).
Operating system “process” is the basic concept of computer to learn to want to contact, aside from the pure theory of operating system, the underlying implementation under Linux software development for so many years, each time the program is running, will step by step analysis of all kinds of process state, and to screen where the problem is, the lemon with your field in a Linux environment, Step by step, explore the secrets of “Linux processes.”
What is a process
Let’s start with the concept of a program. A program is an ordered collection of instructions stored on disk that are static. A process is a process executed by a program, including the entire process of dynamic creation, scheduling, and extinction. It is the smallest unit of program resource management.
A thread is the smallest unit in which an operating system can schedule operations. In most cases, it is contained within the process and is the actual operational unit of the process. A process can contain multiple threads and is the smallest unit of resource scheduling. [Citation from Wikipedia]
❝
Step one of the quest. Are you there? Ok?
❞
ps
report a snapshot of the current processes. Snapshots of current system processes are displayed.
Find the Process IDentity (PID), which uniquely identifies a Process. Use ps this command, this command should be known to the little white users, first of all, he is not Photoshop.
To give you a brief introduction, the general usage isps -ef
Lists the current information in the system, usually with pipesgrep
Figure out what you’re interested in, like thisps -ef|grep intresting
The first column PID indicates the process ID, and the parent process ID (PPID) indicates the parent process ID.
❝
Step 2 of the exploration process, let me see what friends you have (system calls & signals)
❞
strace
Trace System calls and signals Traces system calls and signals inside a process
❝
What is a “system call”? System call refers to a program running in the “user mode” to the “kernel mode” of the operating system to request services that require higher permissions. System call provides the interface between the user program and the operating system.
❞
strace
Followed by the start a process, can trace after start-up process of system calls and signal, this command can see the process execution time what system calls, call by specifying different options can output the time of the system call, precision can be accurate to microseconds, can even “call takes” statistical analysis system, This is useful when troubleshooting process feign death and can help you find out which system call the process is stuck on. Processes that are already running can also be specified-p
Parameters andpid
likegdb attach
Attach to track like that.
❝
Step 3 of the exploration process, show me your little brothers (threads).
❞
pstack
Print a stack trace of a running process Print a stack trace of a running program.
Execute the commandpstack pid
You can see the current thread running stack information, where the PID is available beforeps
Command get,pstack
You can see the number of threads started within the process, as well as the “stack” contents of each thread within the process.
LPW refers to a light-weight process. Extended knowledge:
❝
- There are no real threads in Linux
- Threads that do not exist in Linux
Thread
It is implemented by process simulation so it is called lightweight process- Processes are the smallest unit of “resource management” and threads are the smallest unit of “resource scheduling” (coroutines are not considered here)
❞
❝
Step 4 of the exploration process, let the boys (threads) come out and line up.
❞
pstree
Display a tree of processes PSTree Displays the structure of running processes in a tree
You can intuitively view the relationship between the process and the thread it starts, and display the process identity.
❝
Step 5: I want to know your secrets (stack frames & context) if it’s dead (crash) or alive (running).
❞
gdb
GDB is GNU developed GCC suite under Linux program debugging tool, you can view the program stack, set breakpoints, print the program running information, and even debugging multithreaded programs, very powerful function.
Here, GDB as a command is a bit overqualified, to elaborate on GDB, can support the length of an article, here to make a long story short, have the opportunity to open another article to introduce it in detail.
use
To debug C/C++ programs with GDB, use the -g option, g++ -g test. CPP -o test.
- You can start debugging directly with GDB:
gdb prog
- Debugging with GDB attached to an already started process is also possible. Command:
gdb prog pid
- The coreFile parameter can also be debugged using GDB after a crash to see what message the program left to you before it died. Command:
gdb prog corefile
It is important to note that some Linux systems do not generate crashes by defaultcorefile
That’s when you needulimit -c unlimited
And then you can formcorefile
.
❝
Step six, I want to know everything about you.
❞
One step closer to
Use the /proc/pid file to learn about the process runtime information and statistics. The /proc system is a pseudo-file system that exists in memory and does not occupy external memory, providing a file-system interface for the kernel to communicate with processes. Go to the system /proc directory:
The /proc directory has a number of directories named with each number representing the process number PID. These are process directories. Each running process in the system has a directory named /proc/pid under /proc, which is the interface for reading process information. You can enter this file to learn the running information and statistics of the process.
High frequency use
The /proc/pid directory contains some important files. /proc/pid/environ contains a list of available environment variables for the process. If you are not sure whether the environment variable is valid, you can check the cat file to make sure.
The /proc/pid/fd/ directory contains a link to every file opened by the process. You can view the open file descriptors of the process, including standard input, output, and error streams. You can also view the open socket file descriptors of the process. The lsof command also has a similar function.
/proc/pid/stat contains all the status information of the process, including the process number, parent process number, thread group number, the running time of the task in user mode, the running time of the task in kernel mode, the code segment of the virtual address space, and the bitmap of the blocking signal.
Other statistics
/proc/pid/cmdline contains the command used to start the process. /proc/pid/cwd contains a link to the current process’s working directory. /proc/pid/exe contains a link to the program that is running in the process /proc/pid/statm contains the memory usage information of the process
To summarize
Now that you know the process and its secrets, the next time our good friend “process” encounters a problem (crashing coredump, suspended animation, blocking, system call timeout, file descriptor exception), you should know how to handle it. Let’s sum it up:
- Ps View the process ID to check whether the process is running and its status
- If there are
strace
,psstack
Look at the current process information, is not stuck in which position, compare each frame last call information to find the exception - If the process is no longer, if there is
corefile
File, go straight to itgdb
To viewcorefile
information - Look at other difficult cases where process status information is in doubt
/proc/pid
The following process status information may give you some inspiration. - Finally, if all else fails, close your eyes and pray!
Write in the last
Today’s share of hope to help you, I wish you to write the service never down, never coredump, let the above operation to teach you to eat dust.
Finally, thank you for reading. The purpose of the article is to share the understanding of knowledge. I will repeatedly verify technical articles to ensure the accuracy to the maximum extent. If there are obvious mistakes in the article, please point out them, and we will study together in the discussion.
reference
https://man.linuxde.net/gdb
https://blog.csdn.net/dan15188387481/article/details/49450491
https://blog.csdn.net/m0_37925202/article/details/78759408
https://blog.csdn.net/enweitech/article/details/53391567
More and more
Original is not easy, see here move a finger, your “three even” is the biggest support for my continuous creation.
You can search the public number “back-end technology School” on wechat and reply to “information” with all kinds of programming learning materials I prepared for you. This article is updated every week, and we’ll see you next time!