首页 > 解决方案 > 如何使用 SIGALRM 终止程序

问题描述

所以基本上我的程序中有 4-5 个函数。这是很多行代码以及从文件中读取和写入的内容,它可能以无限循环结束(更糟糕的情况),如果它超过 20 秒,我想终止我的程序。下面的代码不起作用,我的程序挂起,操作系统为我终止它,而不是程序自行终止。我认为我遇到的主要问题是在主程序中设置了警报,当达到警报时间限制时,进程正在另一个函数中执行,这导致程序在没有关闭文件并杀死子进程的情况下关闭。这就是我现在所拥有的:

volatile sig_atomic_t keep_going = 1;

/* The signal handler just clears the flag and re-enables itself. */
void
catch_alarm (int sig)
{
    printf("Alarm went off");
  exit(EXIT_SUCCESS);
}
void function1
{}
void forkingfunction()
{
or(i=0;i<size;i++,temp++)
        {

             pid_t pID = vfork();

               if (pID == 0)                // child
               { 
                   printf("\nchild pid %d\n",getpid());
                    //open some files and read and write
                     function1();
                      exit(EXIT_SUCCESS);
                       kill(pID,SIGKILL);
                       }
               }
               else if (pID < 0)            // failed to fork
               {
                   perror("Failed to fork:");
               }
}
void function2
{
function1();
}
int main()
{
int options
while(options){
switch (options)
{
case 1:
case 2:
}
}
signal (SIGALRM, catch_alarm);

          alarm (0.1);//testing for 0.1 seconds 
function1();

return 0;
}

标签: cvfork

解决方案


只有一组特定的函数可以从信号处理程序中安全地执行。而且exit不是其中之一。也不是printf

您也许可以改用该_exit()函数(前面带有下划线)。但是它只会退出最顶层的进程,让子进程继续运行。

你仍然可以使用 杀死所有东西kill(0, signal),就像这里一样。

void catch_alarm (int sig) {
   kill(0, SIGTERM);
}

这是一个工作 poc 代码的示例:

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

void catch_alarm (int sig) {
    kill (0,SIGTERM);
}

void forkingfunction()
{
    int i;
    for(i=0;i<4;i++,i++) {
        pid_t pID = fork();
        if (pID == 0) { 
            sleep(5);
            printf("\nchild pid %d\n",getpid());
            exit(EXIT_SUCCESS);
        }
        else if (pID < 0) {
            perror("Failed to fork:");
        }
    }
}
int main()
{
    signal (SIGALRM, catch_alarm);
    alarm (1);//testing for 0.1 seconds 
    forkingfunction();
    sleep(10);
    printf("\nnormal exit\n");
    return 0;
}

推荐阅读