为什么Linux的总输出" ^ C"在按Ctrl + C的pressing? [英] Why Linux always output "^C" upon pressing of Ctrl+C?

查看:250
本文介绍了为什么Linux的总输出" ^ C"在按Ctrl + C的pressing?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Linux中一直在研究的信号。而我做了一个测试程序来捕捉SIGINT。

I have been studying signals in Linux. And I've done a test program to capture SIGINT.

#include <unistd.h>
#include <signal.h>
#include <iostream>
void signal_handler(int signal_no);
int main() {
  signal(SIGINT, signal_handler);
  for (int i = 0; i < 10; ++i) {
  std::cout << "I'm sleeping..." << std::endl;
  unsigned int one_ms = 1000;
  usleep(200* one_ms);
  }
  return 0;
}
void signal_handler(int signal_no) {
  if (signal_no == SIGINT)
    std::cout << "Oops, you pressed Ctrl+C!\n";
  return;
}

当输出看起来是这样的:

While the output looks like this:

I'm sleeping...
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
^COops, you pressed Ctrl+C!
I'm sleeping...
I'm sleeping...
I'm sleeping...

据我了解,当pressing按Ctrl + C,在前台进程组处理所有接收到SIGINT(如果没有过程选择忽略它)。

I understand that when pressing Ctrl+C, processes in foreground process group all receives a SIGINT(if no process chooses to ignore it).

那么,它的外壳(bash)的和上述程序的实例都接收到的信号?凡在做了^ C每个哎呀呀从何而来?

So is it that the shell(bash) AND the the instance of the above program both received the signal? Where does the "^C" before each "Oops" come from?

该操作系统是CentOS的,而shell是bash。

The OS is CentOS, and the shell is bash.

推荐答案

这是终端(驱动程序)截获了^ C,并将其转换为发送到连接的过程(也就是壳)的信号的stty不及物动词^ B 将指示终端驱动程序拦截^ B来代替。这也是呼应了^ C回到码头终端驱动程序。

It is the terminal (driver) that intercepts the ^C and translates it to a signal sent to the attached process (which is the shell) stty intr ^B would instruct the terminal driver to intercept a ^B instead. It is also the terminal driver that echoes the ^C back to the terminal.

外壳就是这样坐上了线的另一端​​,并接收它的从终端通过终端驱动程序标准输入(例如/ dev / ttyX)的过程,它的标准输出(和标准错误)也与同样的终端。

The shell is just a process that sits at the other end of the line, and receives it's stdin from your terminal via the terminal driver (such as /dev/ttyX), and it's stdout (and stderr) are also attached to the same tty.

注意,(如果启用呼应)的终端发送键击的两个的处理(群),并返回给上述终端。 stty命令只是围绕的ioctl(外壳)■对控制的tty进程tty驱动。

Note that (if echoing is enabled) the terminal sends the keystrokes to both the process (group) and back to the terminal. The stty command is just wrapper around the ioctl()s for the tty driver for the processes "controlling" tty.

更新:证明外壳不参与,我创建了下面的小程序。应该由它的父shell通过 EXEC ./a.out 执行(它显示一个交互式shell会fork一个女儿外壳,反正)程序设定生成的关键SIGINTR到^ B,开关关闭呼应,比等待从标准输入。

UPDATE: to demonstrate that the shell is not involved, I created the following small program. It should be executed by its parent shell via exec ./a.out (it appears an interactive shell will fork a daughter shell, anyway) The program sets the key that generates the SIGINTR to ^B, switches echo off, and than waits for input from stdin.

#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>

int thesignum = 0;
void handler(int signum);

void handler(int signum)
{ thesignum = signum;}

#define THE_KEY 2 /* ^B */

int main(void)
{
int rc;
struct termios mytermios;

rc = tcgetattr(0 , &mytermios);
printf("tcgetattr=%d\n", rc );

mytermios.c_cc[VINTR] = THE_KEY; /* set intr to ^B */
mytermios.c_lflag &= ~ECHO ; /* Dont echo */
rc = tcsetattr(0 , TCSANOW, &mytermios);
printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );

printf("Setting handler()\n" );
signal(SIGINT, handler);

printf("entering pause()\n... type something followed by ^%c\n", '@'+THE_KEY );
rc = pause();
printf("Rc=%d: %d(%s), signum=%d\n", rc, errno , strerror(errno), thesignum );

// mytermios.c_cc[VINTR] = 3; /* reset intr to ^C */
mytermios.c_lflag |= ECHO ; /* Do echo */
rc = tcsetattr(0 , TCSANOW, &mytermios);
printf("tcsetattr(intr,%d) =%d\n", THE_KEY, rc );

return 0;
}

intr.sh:

intr.sh:

#!/bin/sh
echo $$
exec ./a.out
echo I am back.

这篇关于为什么Linux的总输出&QUOT; ^ C&QUOT;在按Ctrl + C的pressing?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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