[已弄懂,未解决]为何这段程序不能被 kill -SIGINT 结束?

软件和网站开发以及相关技术探讨
回复
科学之子
帖子: 2284
注册时间: 2013-05-26 6:58
系统: Debian 9

[已弄懂,未解决]为何这段程序不能被 kill -SIGINT 结束?

#1

帖子 科学之子 » 2017-07-07 1:19

为何这段程序不能被 kill -SIGINT 结束?

代码: 全选

#include<stdio.h>
#include<signal.h>
#include<assert.h>

int main(int argc,char *argv[])
{
	for(;;)system("date;sleep 1");
}
好像调用system时就会出现无法结束
但我直接把SIGINT发送给这个程序而非子进程也不行吗?为什么?
Mon Jul 10 12:35:49 CST 2017补充:
经过2楼的引导,我搜索了glibc的system实现:
https://code.woboq.org/userspace/glibc/ ... c.html#129
尽管没看懂,但感觉意思上system确实会在某些时候忽略信号,然后在某些时候恢复信号.
上次由 科学之子 在 2017-07-10 12:46,总共编辑 3 次。
烟波钓叟
帖子: 112
注册时间: 2015-04-04 23:20
系统: linux & windows

Re: 为何这段程序不能被 kill -SIGINT 结束?

#2

帖子 烟波钓叟 » 2017-07-09 20:55

最近正好在看linux信号,运行了一下.

代码: 全选

$ ./a.out
$ pstree -p -g 462
bash(462)───a.out(1995)───sh(2001)───sleep(2003)
$ cat /proc/1995/status | grep Sig
SigQ:   0/15000
SigPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000000086
SigCgt: 0000000000000000
$ cat /proc/2001/status | grep Sig
SigQ:   0/15000
SigPnd: 0000000000000000
SigBlk: 0000000000010000
SigIgn: 0000000000000084
SigCgt: 0000000000010002
$ cat /proc/2003/status | grep Sig
SigQ:   0/15000
SigPnd: 0000000000000000
SigBlk: 0000000000000000
SigIgn: 0000000000000080
SigCgt: 0000000000000000
输出的解释请参考:
https://unix.stackexchange.com/question ... stening-to
好像是a.out进程忽略了int信号.
头像
astolia
论坛版主
帖子: 6396
注册时间: 2008-09-18 13:11

Re: [已弄懂,未解决]为何这段程序不能被 kill -SIGINT 结束?

#3

帖子 astolia » 2017-07-27 15:09

都不去看manpage的吗?

代码: 全选

SYSTEM(3)                  Linux Programmer's Manual                 SYSTEM(3)

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       The  system()  library  function uses fork(2) to create a child process
       that executes the shell command specified in command using execl(3)  as
       follows:

           execl("/bin/sh", "sh", "-c", command, (char *) 0);

       system() returns after the command has been completed.

       During  execution  of  the command, SIGCHLD will be blocked, and SIGINT
       and SIGQUIT will be ignored, in the process that calls system()  (these
       signals  will  be  handled according to their defaults inside the child
       process that executes command).

       If command is NULL, then system() returns a status indicating whether a
       shell is available on the system
回复