Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 未打印信号处理程序语句_C_Unix_Signals - Fatal编程技术网

C 未打印信号处理程序语句

C 未打印信号处理程序语句,c,unix,signals,C,Unix,Signals,在检查信号时,我发现处理程序中的语句没有打印出来。以下是我使用的代码: #include"stdio.h" #include"signal.h" #include"unistd.h" void handlerSIGINT(int sig) { if(sig == SIGINT) printf("\nFinally caught SIGINT..."); } int main() { printf("Hello ... soon u'll receive a sign

在检查信号时,我发现处理程序中的语句没有打印出来。以下是我使用的代码:

#include"stdio.h"
#include"signal.h"
#include"unistd.h"
void handlerSIGINT(int sig)
{
    if(sig == SIGINT)
       printf("\nFinally caught SIGINT...");
}
int main()
{
    printf("Hello ... soon u'll receive a signal");
    if(signal(SIGINT, handlerSIGINT) == SIG_ERR)
    {
        printf("error in SIGINT  handling");
    }
    while(1)
        sleep(1);
    /*now press control + C to see the effect */
    return 0;
}
我在运行程序时得到以下输出:

[root@node1 mytest]# ./a.out
^CHello ... soon u'll receive a signal
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^CFinally caught SIGINT...
^Z
[1]+  Stopped                 ./a.out
我的困惑是:当我第一次按下“Ctrl+C”时,它没有在处理程序中打印消息,即“Finally Capture SIGINT…”。但从第二次开始,它开始打印消息。谁能解释一下为什么会发生这种事

void handlerSIGINT(int sig)
{
    if (sig == SIGINT)
        printf("\nFinally caught SIGINT...");
}
在这里的信号处理程序中,输出不会被新行终止,默认情况下,标准输出是行缓冲的,因此只有在下次看到开头的
\n
时才会显示。将其更改为:

printf("Finally caught SIGINT...\n");
您可能会看到预期的结果。然而,请注意

在这里的信号处理程序中,输出不会被新行终止,默认情况下,标准输出是行缓冲的,因此只有在下次看到开头的
\n
时才会显示。将其更改为:

printf("Finally caught SIGINT...\n");
您可能会看到预期的结果。然而,请注意