我试图潜入C中的信号并试图理解它是如何工作的。我知道SIGKILL和SIGSTOP不能被处理/捕获。我的问题是谁能解释下面的代码中while-loop
的作用?如果没有一个时间循环会怎么样。。 while循环的使用
#include<stdio.h>
#include<signal.h>
#include<unistd.h>
void sig_handler(int signo)
{
if (signo == SIGUSR1)
printf("received SIGUSR1\n");
else if (signo == SIGKILL)
printf("received SIGKILL\n");
else if (signo == SIGSTOP)
printf("received SIGSTOP\n");
}
int main(void)
{
if (signal(SIGUSR1, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGUSR1\n");
if (signal(SIGKILL, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGKILL\n");
if (signal(SIGSTOP, sig_handler) == SIG_ERR)
printf("\ncan't catch SIGSTOP\n");
// A long long wait so that we can easily issue a signal to this process
while(1)
sleep(1);
return 0;
}
当你用while()循环运行你的程序时,发生了什么事? –