美文网首页
你无法用ctrl c 杀死的进程

你无法用ctrl c 杀死的进程

作者: millerix | 来源:发表于2018-03-22 10:46 被阅读0次

有如下shell 脚本 pingoo.sh

#!/bin/bash
while true
do     
    ping 127.0.0.1
done;
$> pingoo.sh

键盘键入 ctrl +c(^c)
ooooops!
你会发现无论你按多少次都无法终止该脚本的运行.

使用ps 命令找到进程树

$> ps -j f
 PID  PGID   SID TTY      STAT   TIME COMMAND
21994 21994 21994 pts/29   Ss     0:00 /bin/bash
28833 28833 21994 pts/29   S+     0:00  \_ /bin/bash ./pingoo.sh
28834 28833 21994 pts/29   S+     0:00      \_ ping 127.0.0.1

STAT 进程状态标志:
S: 可中断的sleep,
s: session leader;
+: 前台进程;

首先要明确一个重要假设:当用户按下Ctrl c 时, 他是要终止前台进城组的运行, 出发进程注册了自定义的信号处理函数;

但是如果子进程capture sigint, 则 shell 会假设用户通过发送sigint 使子进程做特定工作(比如ping的统计信息), 也就说用户发送sigint的目的并不是结束前台进程组, 而是触发特定动作;ping 就是这类安装了自定义的信号处理函数的进程;

当你按下ctrl c后发生了什么?

ctrl +c 被终端驱动程序解释为 sigint 信号, 由kernel 发送给 前台进程组 . ping 进程 capture 该信号, 调用信号处理函数。 同时, ping的父进程也收到sigint信号(父进程处理interruptible sleep 状态, 也就出出于wait系统调用中), 父进程被迫退出wait系统调用,检查退出原因(是否是EINTR,也就是中断的系统调用),然后通过WIFSIGNALED宏可以判断子进程是否注册了信号处理函数 。如果 注册了信号处理函数,capture sigint, 父进程继续运行(当前例子是开始下一次循环); 如果是子进程是因收到sigint信号终止的(按照default 方式处理sigint), 父进程会终止运行.

当用户键入ctrl+c后,tty driver 产生SIGINT信号给前台进程, pingoo.sh和其子进程 都看见sigint信号,(他们是前台进程组成员)。 ping.sh作为父进程处于waitpid blocking状态, 收到sigint 后waitpid 立即返回 -1, 设置errorno为 EINTR。同时 bash要求child 立即死亡(也就是sigint的默认动作 terminate)。最后设置 child_caught_sigint = 0, bash 随后会根据该flag 退出;

如果child capture signal,设置 child_caught_sigint = 1; bash 根据该flag 不退出, 因为子进程capture sigint后, 触发信号处理函数。 其信号处理函数需要做特定处理,如果子进程退出,则 父进程会受到 sigchld 信号,父进程会认为子进程正常退出, 循环继续。

源代码解析:

bash 4.3.3: jobs.c

  1. waitpid
    pid<-1 等待进程组识别码为 pid 绝对值的任何子进程。
    pid=-1 等待任何子进程,相当于 wait()。
    pid=0 等待进程组识别码与目前进程相同的任何子进程。
    pid>0 等待任何子进程识别码为 pid 的子进程。

Process-Completion-Status

  1. WIFSIGNALED:
    This macro returns a nonzero value if the child process terminated because it received a signal that was not handled.
  2. WTERMSIG:
    If WIFSIGNALED is true of <var style="background-color: inherit;">status</var>, this macro returns the signal number of the signal that terminated the child process
  /* If waitpid returns -1/EINTR and the shell saw a SIGINT, then we
     assume the child has blocked or handled SIGINT.  In that case, we
     require the child to actually die due to SIGINT to act on the
     SIGINT we received; otherwise we assume the child handled it and
     let it go. */
// status = waitpid(pid), pid <0 
      if (pid < 0 && errno == EINTR && wait_sigint_received)
    child_caught_sigint = 1;  //waitpid被 sigint中断,立即 假设此时 子进程也 capture signit 

      if (pid <= 0)
    continue;    /* jumps right to the test */

      /* If the child process did die due to SIGINT, forget our assumption
     that it caught or otherwise handled it. */
      if (WIFSIGNALED (status) && WTERMSIG (status) == SIGINT)
        child_caught_sigint = 0; 

  if (JOBSTATE (job) == JDEAD)
    {
      /* If we're running a shell script and we get a SIGINT with a
     SIGINT trap handler, but the foreground job handles it and
     does not exit due to SIGINT, run the trap handler but do not
     otherwise act as if we got the interrupt. */
// wait_sigint_received :  wait 阻塞中收到sigint,
// child_caught_sigint : 子进程 capture sigint
// IS_FOREGROUND : 前台进程组
// interactive_shell : 交互shell?
// signal_is_trapped : 
      if (wait_sigint_received && interactive_shell == 0 &&
      child_caught_sigint && IS_FOREGROUND (job) &&
      signal_is_trapped (SIGINT))
    {
      int old_frozen;
      wait_sigint_received = 0;
      last_command_exit_value = process_exit_status (child->status);

      old_frozen = jobs_list_frozen;
      jobs_list_frozen = 1;
      tstatus = maybe_call_trap_handler (SIGINT); // 
      jobs_list_frozen = old_frozen;
    }

如何终止该脚本的运行呢:

$> kill -9 28833 28834

一定要先杀死父进程

相关文章

  • 你无法用ctrl c 杀死的进程

    有如下shell 脚本 pingoo.sh 键盘键入 ctrl +c(^c)ooooops!你会发现无论你按多少次...

  • bash快捷键

    ctrl键组合 ctrl+a:光标移到行首。ctrl+b:光标左移一个字母ctrl+c:杀死当前进程。ctrl+d...

  • mac终端提示:进程已完成,无法输入新的命令如何解决

    今天在安装Fish过程中一顿操作,突然就出现进程已完成,用Ctrl+C无法终止进程,退出重新登录还是进程已完成,无...

  • linux命令行学习(37):控制进程的方法

    一、中断进程用ctrl+c,暂停进程用ctrl+z 二、使程序在后台运行,在命令后面加& 三、查看终端启动的所有作...

  • Linux常用命令

    1.常用快捷键 (1) ctrl+c:停止进程(2) ctrl+l:清屏(3) ctrl+q:退出(4) 善于用t...

  • ctrl+z, ctrl+c

    ctrl+c:终端进程 ctrl+z:挂起进程jobs:查看挂起的进程, 比如下图2个进程都被挂起了 fg %N:...

  • Ubuntu16.04杀死进程

    发现Ubuntu16.04 用pkill 844无法杀死进程844,也不报错 查询资料知道 用 kill 8...

  • Linux 暂停进程

    导言 在使用linux中是不是需要切换进程 使用 ctrl+Z挂起进程ctrl+C强行终止进程bg [%N]恢复挂...

  • linux进程的终止、暂停、挂起

    ctrl+c 结束进程ctrl+z 挂起进程并放入后台jobs 显示当前暂停的进程bg %N 使第N个任务在后台运...

  • shell脚本(9)管理重任

    1、杀死进程以及发送或响应信号 一般来说按下Ctrl+C会终止应用程序,因为这是操作系统提供的处理程序的默认行为。...

网友评论

      本文标题:你无法用ctrl c 杀死的进程

      本文链接:https://www.haomeiwen.com/subject/iiwoqftx.html