我们什么时候需要在信号处理程序中再次建立信号功能?

When do we need to establish signal function again inside a signal handler?

我在 linux 下有一个简单的程序并且运行良好:

#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<sys/types.h>
void fStop(int signo){
    printf("Hup\n");
}
int main(){
    signal(SIGHUP,fStop);
    for(;;){
        pause();
    }
    return 0;
}

它运行并等待信号。每次我向它发送“kill -1 xxx”时,它都会打印一行“Hup”。好的

但我的问题是:

  1. I found if I remove the "for" loop, seems the pause() won't work after receiving SIGHUP signal. Does it mean that signal will interupt any kind of sleep and suspension functions?

  2. In some linux programs, I see people re-estbalish signal function inside a signal handler like:

void fStop((int signo){
     printf("Hup\n");
     signal(SIGHUP,fStop);//When is this necessary?
}

只是有点困惑 linux 信号处理程序应该如何设计,何时重新建立处理程序取决于不同的信号? 谢谢

Does it mean that signal will inter[r]upt any kind of sleep and suspension functions?

可能不是所有,但它会中断pause(),是的。

来自pause()'s documentation (Linux)

RETURN VALUE

pause() returns only when a signal was caught and the signal-catching function returned. In this case, pause() returns -1, and errno is set to EINTR.