I am trying to terminate my c program with multiple functions after 20 seconds (kill all child and parent processes, close files). I tried alarm(), itimer(), clock(). It works when we only have a main and a handler function. clock() restarts from 0 in every function even if I keep the variables global.
#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");
                       }
                }
}
As you can see I tried calling signal from different functions so it can catch the signal and the handler can execute but the handler is never executed.
EDIT: Tried this again
# 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
 
    