簡體   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