繁体   English   中英

如何在 20 秒后终止 c 程序

[英]How to terminate a c program after 20 seconds

我试图在 20 秒后终止具有多种功能的 c 程序(杀死所有子进程和父进程,关闭文件)。 我试过闹钟(),定时器(),时钟()。 当我们只有一个 main 和一个处理程序 function 时,它可以工作。 即使我将变量保持在全局范围内,clock() 也会在每个 function 中从 0 重新开始。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include<stdbool.h>
#include <ctype.h>
#include<sys/wait.h>
#include<signal.h>
#include <sys/mman.h>
#include<sys/time.h>
#define INTERVAL 2
int t=0;
void display_message()
{
    printf("In the handler");
    //kill(0,SIGKILL);
    t=1;
}
void calling2()
{
    signal(SIGALRM, display_message);

    sleep(3);

}
void calling()
{

    signal(SIGALRM, display_message);
    alarm(2);

        int i;
        for(i=0;i<3;i++)
                {
                    //printf("\nStarting fork for loop i=%d \n",i);


                     pid_t pID = fork();

                       if (pID == 0)                // child
                       {
                           calling2();
                           if(t==1)
                           {
                               printf("we have exceeded 2 seconds killing the process");
                               kill(0,SIGKILL);
                               exit(0);
                           }
                        exit(0);
                        kill(pID,SIGKILL);
                       }
                       else if(pID>0)
                       {
                             //  printf("\nhello from the father");
                            if(t==1)
                                                       {
                                                           printf("killing the process");
                                                           kill(0,SIGKILL);
                                                           exit(0);
                                                       }
                            printf("\nhello from the father");
                       }
                }
}

如您所见,我尝试从不同的函数调用信号,以便它可以捕获信号并且处理程序可以执行,但处理程序永远不会执行。

编辑:再试一次

# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <time.h>
# include <stdlib.h>
# include <dirent.h>
# include <stdio.h>
# include <string.h>
# include <getopt.h>
# include<stdbool.h>
# include <ctype.h>
# include<sys/wait.h>
# include<signal.h>
# include <sys/mman.h>
# include<sys/time.h>
# define INTERVAL 2
int t=0;
void display_message()
{

    kill(0,SIGKILL);
    t=1;
}
void calling2()
{

    sleep(3);

}
void calling()
{

    signal(SIGALRM, display_message);


        int i;
        for(i=0;i<3;i++)
                {

                     pid_t pID = fork();

                       if (pID == 0)                // child
                       {
                           calling2();
                           if(t==1)
                           {
                               printf("killing the process");
                               kill(0,SIGKILL);
                               exit(0);
                           }
                        exit(0);

                       }
                       else if(pID>0)
                       {
                                                        if(t==1)
                                                       {
                                                           printf("killing the process");
                                                           kill(0,SIGKILL);
                                                           exit(0);
                                                       }
                            printf("\nhello from the father");
                       }
                }
}
int main()
{
    signal(SIGALRM, display_message);
    alarm(2);
    calling();
}
O/P:
hello from the father
hello from the father
hello from the father
hello from the father
hello from the father
hello from the father
error: Failed with return code 22

主要问题是您的主线程在处理警报信号之前完成。 您必须至少让它存活,直到发出警报信号。 此外,正如 Jonathan Leffler 所建议的那样,良好的缩进/间距确实很有帮助。

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <getopt.h>
#include <stdbool.h>
#include <ctype.h>
#include <sys/wait.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/time.h>

#define INTERVAL 2
int t=0;

void display_message()
{
    kill(0,SIGKILL);
    t=1;
}

void calling2()
{

    sleep(3);
}

void calling()
{

    signal(SIGALRM, display_message);
    int i;
    for(i=0;i<3;i++)
    {
        pid_t pID = fork();
        if (pID == 0)                // child
        {
            calling2();
            if(t==1)
            {
                printf("killing the process");
                kill(0,SIGKILL);
                exit(0);
            }
            exit(0);
        }
        else if(pID>0)
        {
            if(t==1)
            {
                printf("killing the process");
                kill(0,SIGKILL);
                exit(0);
            }
            printf("\nhello from the father");
        }
    }
}

int main()
{
    signal(SIGALRM, display_message);
    alarm(2);
    calling();

    //  wait until alarm callback before terminating main thread
    sleep(100);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM