"Alarm Clock" interruption after first loop of the code

697 views Asked by At

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
 
 
}
}}
 
 
 
 
 
 
}
1

There are 1 answers

1
Rachid K. On BEST ANSWER

Take in account the comments pointing out close(fd[0]) instead of close(fd[1]) in 1st child process.

But the main problem comes from the fact that you don't initialize correctly the "sigaction" structure. You must reset it otherwise you have garbage values in the fields :

   struct sigaction action;
   memset(&action, 0, sizeof(action));
   action.sa_handler = passage;
   sigaction(SIGALRM,&action,NULL);

You have other problems in your code :

. "ab" global variable is not shared between the child processes. So, when you modify it in the 1st child, the second child does not see the modification.

. The "while(1);" loops consume too much CPU. You should add a pause : while(1) pause();