How to send a signal to a child several times avoiding zombie state? C language -


i need send signal child process 3 times.

the problem child receives signal once , transforms zombie.

the expected output be:

 i'm child 11385 , received sigusr1

 i'm child 11385 , received sigusr1

 i'm child 11385 , received sigusr1

but real output is:

 i'm child 11385 , received sigusr1

#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <signal.h> #include <sys/types.h> #include <sys/wait.h>  void my_handler() {   printf("\n i'm child %i , received sigusr1\n", getpid()); }   int main (int argc, char **argv) {     int *array;     int n = 10;     int i;     pid_t pid1;     array=(int*)malloc(sizeof(int)*n);       signal(sigusr1,my_handler);      (i = 0; i< n; i++)      {         pid1 = fork();         if(pid1 < 0)         {             exit(exit_failure);         }         else if (pid1 > 0)         {             array[i]= pid1;         }          else         {                sleep(100);             exit(exit_success);         }     }      i=0;     while(i<3) // need call son 3 times     {         kill(array[1], sigusr1);         i++;     } } 

when child receives signal, waiting sleep terminate. first signal interrupt sleep if time hasn't expired, causing return errno set eintr. if want keep sleeping, need call sleep again.


Comments