linux中ctrl c的作用,为什么Linux在按下Ctrl C时总是输出“^ C”?

终端(驱动程序)拦截^ C并将其转换为发送到附加进程(即shell)的信号,stty intr ^ B将指示终端驱动程序拦截^ B.它也是将^ C回送到终端的终端驱动程序.

shell只是一个位于该行另一端的进程,并通过终端驱动程序(例如/ dev / ttyX)从终端接收它的stdin,并且它的stdout(和stderr)也附加到同一个tty .

注意(如果启用了回显),终端将击键发送到进程(组)并返回到终端. stty命令只是用于“控制”tty的进程的tty驱动程序的ioctl()的包装.

更新:为了证明shell没有参与,我创建了以下小程序.它应该由它的父shell通过exec ./a.out执行(看起来交互式shell会分叉一个子shell,无论如何)程序将生成SIGINTR的键设置为^ B,关闭echo,然后等待来自stdin的输入.

#include

#include

#include

#include

#include

#include

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,rc );

return 0;

}

intr.sh:

#!/bin/sh

echo $$

exec ./a.out

echo I am back.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值