#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>

#define CHILD_COUNT 3

int counter=0;

void sig10_handler(void)
{
   //printf("\nSonlaniyorum :: %d", getpid());
}

void sig12_handler(void)
{
  counter++;
  if (counter>=CHILD_COUNT)
  {
    printf("All children terminated\n");
    exit(0);
  }
}

int main(void)
{ 
  int f=1, i;
  int children[CHILD_COUNT]; 

  for (i=0; i<CHILD_COUNT; i++)
  {
    if (f>0)
      f=fork();
      if (f==-1)
      {
        printf("fork error....\n");
	exit(1);
      }	
      if (f==0)
        break;
      else
        children[i]=f;		
  }
  if (f>0)
  {
    signal (12,  (sighandler_t) sig12_handler);
    //for (i=0; i<CHILD_COUNT; i++)
       //kill(children[i], 10);
    
    while(1); 
  }
  else
  {
    signal(10,  (sighandler_t) sig10_handler);
    printf("\n %d. child pid = %d\n",i, getpid());
    pause();
    sleep(i); 
    //printf("\nChild %d: Terminating....\n",getpid());
    kill(getppid(),12);
  } 
  return(0); 
}

