This paper is participating in the30 years of Linux”Topic essay activity
Introduction to the
I have worked with many programmers who are familiar with several ways of writing for traversal, but have no idea about the environment in which the written program is deployed. I bet very few programmers have any idea how Tomcat actually works since Spring Boot came along. For them, running a JAR package is enough.
The advanced nature of the tools does bring us a lot of convenience, but also improve the development efficiency of programmers, but also reduce the entry barrier for programmers. The kill command is used in Linux.
Many of you first encountered the kill command when a colleague told you to kill a process. Is kill really used to kill processes?
Use kill to kill the process
One of the most basic and common uses of kill is to kill processes. We need to find the process ID before we can kill the process.
Typically, use the ps command to find the process ID. Join the process whose ID is 54321.
Then you can use kill 54321 to kill the process.
More advanced students might also use kill -9 54321 to force the process to be killed.
Is there a deeper use? Yes, let’s have a look.
An in-depth use of kill
The kill command has the following parameters:
kill
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Copy the code
You can see that the parameter to kill is sig, or signal. In other words, the essence of kill is to send a signal to the program.
If we use kill -l, we can get how many signals kill can send:
kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAXCopy the code
There are 64 signals in total, which may vary from kill version to kill version, but almost all of them cover common signals.
Here are the meanings of some common signals:
HUP 1 Terminating INT 2 Terminating (same as Ctrl + C) QUIT 3 terminating (same as Ctrl + \) TERM 15 Terminating KILL 9 forcibly terminating CONT 18 Continuing (opposite to STOP, Fg /bg command) STOP 19 Pause (same as Ctrl + Z)Copy the code
What about the kill version?
/bin/kill --version
kill from util-linux 2.23.2
Copy the code
If the kill does not pass the SIG, the default sig=TERM, which is 15, will be passed. So kill 54321 is equivalent to kill -15 54321.
In general, SIGTERM signals are preferred. This is because when a program receives a SIGTERM signal, it does some cleanup, or graceful closing, of the program.
If kill -9, also known as SIGKILL, is passed, the application will not be able to catch this signal, resulting in the application being forced to shut down, possibly causing exceptions such as data not being saved, data transfer not being completed, and so on.
Sig also has a special value called 0. If 0 is passed in, no actual signal is sent. This is only used for exception detection.
Pid is the process ID, which can be understood as the process ID. In addition to the process number, special values can be passed, such as:
- 0 indicates all processes in the current process group
- -1 Indicates all processes whose PID is greater than 1
There is also a special PID =1. This PID represents the initial process init, which cannot be killed.
In addition to PID, we see that kill also accepts JobSpec. Job ids can be listed using the jobs command.
Zombie process and kill
Initial processes with pid=1 cannot be killed. Another type of process that cannot be killed is called a zombie.
A zombie process is a unique state in Linux programs that indicates that the process is finished, but not completely dead, like a zombie.
RUNNING: RUNNING or waiting for RUNNING, UNINTERRUPTABLE: INTERRUPTABLE: blocking, STOPPED: suspended, and ZOMBIE: ZOMBIE.
So what is a zombie process?
A zombie process is a process that does not disappear immediately after a program exits, but preserves a data structure called a zombie. This data structure is special because it has no memory, no executable code, and certainly cannot be scheduled. It simply takes a place in the process list and records all sorts of information about when the process exits.
Zombie processes are stored at the exit site for analysis by the parent process or the system administrator. Therefore, zombie processes are collected and released by the parent process. The zombie process has already exited, so using kill is useless. You have to wait for its parent process to exit.
How do I check zombie processes? The easiest way to do this is to use the top command:
Top-14:34:38 UP 305 days, 4:23, 2 Users, Load Average: 0.20, 0.29, 0.47 Tasks: 93 total, 1 running, 92 sleeping, 0 stopped, 0 zombie %Cpu(s): 2.0 us, 0.sy, 0.0Ni, 97.3 ID, 0.0wa, 0.0hi, 0.0Si, 0.0st KiB Mem: 1882008 total, 525524 free, 311440 used, 1045044 buff/cache KiB Swap: 0 total, 0 free, 0 used. 1382560 avail MemCopy the code
In the output above, we can see that there are zero zombies in there.
java thread dump
Kill also generates thread dumps for Java programs, which can be used for analysis, such as deadlock analysis.
How to do thread dump on Java processes? You can simply use the kill -3 command:
kill -3 <pid>
Copy the code
From our introduction above we can specify that the signal 3 represents is SIGQUIT. This means that the JVM has a built-in catch for this signal, and if it is received, the current thread information is dumped.
Java Thread dump is very useful for thread analysis of Java.
conclusion
This article introduces the in-depth use of kill and the underlying working principle, but also introduces several applications of kill, hope that the next time someone asks you what kill is, everyone can be proud to tell him!
This article is available at www.flydean.com/01-that-is-…
The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!
Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!