gpt4 book ai didi

c - 处理多个信号

转载 作者:太空狗 更新时间:2023-10-29 15:33:32 25 4
gpt4 key购买 nike

我有一个关于处理信号的问题。假设如果我们收到 SIGINT 信号,我们应该打印“Recieved Signal”。如果在十秒内处理程序收到另一个信号,它应该打印“正在关闭”然后以状态 1 退出。

我的代码是这样的:

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

void handler(int);
void secondhandler(int);
void alrmhandler(int);

void alrmhandler (int alrmsig)
{
alarm(0);
}

void secondhandler(int sig)
{
/* after recieving second signal prints shutting down and exit */
printf("Shutting Down\n");
exit(1);
}

void handler ( int sig )
{
/* recieve first SIGINT signal */
printf ("Recieved Signal\n");
/* handle for the alarm function */
signal(SIGALRM, alrmhandler);
/* start 10s alarm */
alarm(10);
/* catch second SIGINT signal within 10s*/
signal(SIGINT, secondhandler);

}



int main( void )
{
signal(SIGINT, handler);
printf( "Hello World!\n" );
for ( ;; )
{
/* infinite loop */
}

return 0;
}

我尝试用 dev c++ 编译它,但失败了。因为 SIGALRM 未声明(在此函数中首次使用)。

无论如何,我想知道这段代码是否正确。我实际上有点不确定 alrmhandler()。我应该忽略 SIGALRM 吗?

最佳答案

如果您在 Windows 平台上,您将能够发送的唯一信号是:SIGABRT、SIGFPE、SIGILL、SIGINT、SIGSEGV 或 SIGTERM。

关于c - 处理多个信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8343450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com