如何按Ctrl-C终止子进程? [英] How does Ctrl-C terminate a child process?

查看:1622
本文介绍了如何按Ctrl-C终止子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解按Ctrl-C终止怎样一个孩子,但不是一个父进程。我看到一些剧本炮弹像庆典在这里你可以开始一些长期运行的进程,然后按下Ctrl-C和控制返回到shell终止这种行为。

I am trying to understand how Ctrl-C terminates a child but not a parent process. I see this behavior in some script shells like bash where you can start some long-running process and then terminate it by entering Ctrl-C and the control returns to the shell.

您能解释一下它是如何工作的,特别是为什么不是父(壳)进程终止?

Could you explain how does it work and in particular why isn't the parent (shell) process terminated?

外壳是否有做按Ctrl-C事件一些特殊的处理,如果是究竟是什么呢?

Does the shell have to do some special handling of Ctrl-C event and if yes what exactly does it do?

推荐答案

默认情况下,信号由内核来处理。老Unix系统已默认值15的信号,现在他们有更多的。您可以检查&LT; /usr/include/signal.h> (或杀死-l)。 <大骨节病> CTRL + <大骨节病> C 是名称信号 SIGINT

Signals by default are handled by the kernel. Old Unix systems has default 15 signals, now they have more. You can check </usr/include/signal.h> (or kill -l). CTRL+C is the signal with name SIGINT.

有关处理每个信号的默认操作是在内核中定义过了,它通常是终止谁是接收到的信号的审判。

The default action for handling each signal is defined in the kernel too, and usually it is terminate the proces who is received the signal.

所有的信号(但 SIGKILL )可以通过程序处理。

All signals (but SIGKILL) can be handled by program.

这是shell做什么:

And this is what the shell do:


  • 当在交互模式下运行的shell,他有一个特殊的信号处理该模式。

  • 当你运行一个程序,例如找到,外壳:

    • 本身

    • 和设置默认的信号处理孩子

    • 与给定​​的命令替换子(例如与find)

    • 当你preSS <大骨节病> CTRL + <大骨节病> C ,父shell处理这个信号,但孩子将接受它 - 用默认的动作 - 终止。 (孩子可以实现信号处理过)

    • When the shell running in interactive mode, he has a special signal handling for this mode.
    • When you run a program, for example find, the shell:
      • the shell fork itself
      • and for the child set the default signal handling
      • replace the child with the given command (e.g. with find)
      • when you press CTRL+C, parent shell handle this signal but the child will receive it - with the default action - terminate. (the child can implement signal handling too)

      您可以陷阱信号,您的shell脚本太...

      You can trap signals in your shell script too...

      ,你可以设置为你的交互shell的信号处理过,尝试在顶部OT你<$ ​​C $ C>〜/ .profile文件进入这个。 (确保比你一个已经登录,并测试它的其他终端 - 你可以锁定你自己)

      And you can set signal handling for your interactive shell too, try enter this at the top ot you ~/.profile. (Ensure than you're a already logged in and test it with another terminal - you can lock out yourself)

      trap 'echo "Dont do this"' 2
      

      现在,当你$ P $每次PSS <大骨节病> CTRL + <大骨节病> C 在你的shell,将打印一条消息。不要忘记删除行!

      Now, everytime when you press CTRL+C in your shell, will print a message. Don't forget remove the line!

      如果有兴趣,你可以查看源$ C ​​$ C的 / bin / sh的信号=HTTP://minnie.tuhs .ORG / cgi-bin目录/ utree.pl?文件= V7 / usr / src目录/ CMD / SH / xec.c>这里

      If interested, you can check the plain old /bin/sh signal handling in the source code here.

      在上述有在评论(现已删除)一些误报,所以如果有人有兴趣在这里是一个很好的链接 - 的信号处理是如何工作的

      At the above there were some misinformations in the comments (now deleted), so if someone interested here is a very nice link - how the signal handling works.

      这篇关于如何按Ctrl-C终止子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆