This infinite program creates 2 child processes and in a while() loop set an alarm() for 3s then send signals to both child processes so they print something out. when I execute, the first loop of the code works and one of the child processes print something, but then I get "Alarm Clock" interruption in Terminal, why is that pleas? ( check the while loop in the end, I'mguessing the problem is there).
#include <stdio.h>
#include <unistd.h>
#include<stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
#include <sys/wait.h>
#include<string.h>
#include<signal.h>
void passage(int);
void AB(int);
void BA(int);
bool ab =false;
int pid[2];
int i =1;
void passage(int signum) //handler
{
printf("Voiture %d passée\n", i); i++; ab = !ab;
}
void AB(int sig)
{
if(ab == false)
{
printf("feu1: ROUGE | feu2: VERT\n"); ab = !ab;
}
}
void BA(int sig)
{
if(ab == true)
{
printf("feu1: VERT | feu2: ROUGE\n");
}
}
int main()
{
struct sigaction action;
action.sa_handler = passage;
sigaction(SIGALRM,&action,NULL);
int fd[2];
if(pipe(fd)==-1) { printf("pipe error"); return 1;}
pid[0]=fork();
if(pid[0] != -1 )
{
if (pid[0]==0)
{
close(fd[0]);
bool y;
read(fd[0], &y, sizeof(_Bool));
close(fd[0]);
// Code du child 1
printf("test1\n");
signal(SIGUSR1,AB);
while(1);
} else
{
pid[1]=fork();
if(pid[1]==0)
{
// Code child 2
printf("test2\n");
signal(SIGUSR2,BA);
while(1);
}else
{
// parent precessus
//send signals to the two childs after 3s
printf("PONT OUVERT\n");
close(fd[0]);
write(fd[1], &ab, sizeof(_Bool));
close(fd[1]);
while(1)
{
alarm(3);
pause();
kill(pid[0],SIGUSR1);
kill(pid[1],SIGUSR2);
}
while( wait(NULL) != -1); // gcc test.c -o test
}
}}
}