关于Linux下关闭进程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/coderALEX/article/details/88053820

前言

在Linux下写C++程序的过程中,有时需要关闭进程,但这种行为对进程状态有一些要求,以下进行总结

定位进程

一般使用pstop两个命令来查看当前Linux中运行了哪些进程(使用其中之一即可)

我一般使用 ps aux

a: Show processes for all users
u: Display the user who is using the process
x: Show all processes. (Without this, ps won’t show processes running in a GUI environment.)
ps aux 结果

进程状态(PROCESS STATE CODES)

   Here are the different values that the s, stat and state output
   specifiers (header "STAT" or "S") will display to describe the state
   of a process:

           D    uninterruptible sleep (usually IO)
           I    Idle kernel thread
           R    running or runnable (on run queue)
           S    interruptible sleep (waiting for an event to complete)
           T    stopped by job control signal
           t    stopped by debugger during the tracing
           W    paging (not valid since the 2.6.xx kernel)
           X    dead (should never be seen)
           Z    defunct ("zombie") process, terminated but not reaped by
                its parent

在上述状态中,如果进程处于Z或者D状态,那么此进程是不可以关闭的,进程状态在 ps aux结果中的STAT列显示

杀死进程

ps aux结果中我们看到第二列的PID号就是该进程的ID号,查看需要关闭的进程的ID号,例如

3139

使用如下命令杀死该进程:

kill -9 3139

该命令等价于:

kill -SIGTERM 3139

关于SIGTERM,在wiki上有如下解释

SIGTERM
The SIGTERM signal is sent to a process to request its termination. Unlike the SIGKILL signal, it can be caught and interpreted or ignored by the process. This allows the process toperform nice termination releasing resources and saving state ifappropriate. SIGINT is nearly identical to SIGTERM.

参考

猜你喜欢

转载自blog.csdn.net/coderALEX/article/details/88053820