Send message between parent and child process

2

Starting from this code

void enviar(char palabra){
         printf("Escribe una palabra para enviar de H a P: ");
         scanf(" %c",&palabra);

 }

void my_handler(int signum){
        if (signum == SIGUSR1){
            printf("Recibido SIGUSR1! \n");
        }
}

int main ()
    {
    pid_t id_padre; /* PID del proceso padre */
    pid_t id_hijo;  /* PID del proceso hijo */
    int   estado;   /* Estado de salida */


    printf ("Inicio proceso padre. PID=%d\n", getpid ());
    id_padre = getpid ();
    if ((id_hijo = fork ()) == 0)
        {       /* Proceso hijo */
        printf ("Inicio proceso hijo. PID=%d, PPID=%d\n",
        getpid (), id_padre);
        sleep (3);
        printf ("Salida proceso hijo. PID=%d\n", getpid ());
        exit (getpid () > id_padre);        /* 1, si PID > PPID */
        }
    else
        {
        signal (SIGINT, SIG_IGN);        /* Ignorar CTRL-C */
        while (waitpid (id_hijo, &estado, 0) != id_hijo);
        if (WIFSIGNALED (estado))
            printf ("El proceso hijo ha recibido la señal %d\n", WTERMSIG (estado));
        if (WIFEXITED (estado))
           {
           printf ("Estado de salida del proceso hijo: %d\n", WEXITSTATUS (estado));
           if (WEXITSTATUS (estado) == 1)
               printf ("PID hijo > PID padre.\n");
           else
               printf ("PID padre > PID hijo.\n");
           }
        printf ("Fin del proceso %d\n", getpid ());
        exit (0);
    }


}

I would like to know how to send the signals SIGUSR1 Y SIGUSR2 to the father

    
asked by Armandix23 29.11.2017 в 11:28
source

1 answer

2

What you can try is, initially, to map the signals that interest you using signal .

static int SIGUSR1_recibida = 0;
static int SIGUSR2_recibida = 0;

void procfunc(int sig)
{
  switch( sig )
  {
    case SIGUSR1:
      SIGUSR1_recibida = 1;
      break;
    case SIGUSR2:
      SIGUSR2_recibida = 1;
      break;
  }
}

int main()
{
  signal(SIGUSR1,procfunc);
  signal(SIGUSR2,procfunc);
}

As you can see, if one of these signals is launched, the corresponding flag will be activated.

To launch the signal, just use the kill method:

if ((id_hijo = fork ()) == 0)
{       /* Proceso hijo */
  kill(id_padre,SIGUSR1);
  kill(id_padre,SIGUSR2);
}

And to receive them, just read the corresponding flags:

else
{
  // espera activa
  while (!SIGUSR1_recibida) ;

  printf("SIGUSR1 recibida\n");

  while(!SIGUSR2_recibida) ;
  printf("SIGUSR2 recibida\n");
}

Putting it all together:

static int SIGUSR1_recibida = 0;
static int SIGUSR2_recibida = 0;

void procfunc(int sig)
{
  switch( sig )
  {
    case SIGUSR1:
      SIGUSR1_recibida = 1;
      break;
    case SIGUSR2:
      SIGUSR2_recibida = 1;
      break;
  }
}

int main()
{
  signal(SIGUSR1,procfunc);
  signal(SIGUSR2,procfunc);

  pid_t id_padre = getpid ();
  pid_t id_hijo = fork();
  if(id_hijo < 0 )
  {
    printf("Error al crear el proceso hijo!!!!\n");
  }
  else if(id_hijo == 0)
  {       /* Proceso hijo */
    sleep(2);
    kill(id_padre,SIGUSR1);
    sleep(2);
    kill(id_padre,SIGUSR2);
  }
  else
  {
    // espera activa
    while (!SIGUSR1_recibida) ;

    printf("SIGUSR1 recibida\n");

    while(!SIGUSR2_recibida) ;
    printf("SIGUSR2 recibida\n");
  } 
}

The example can be supplemented with time(NULL) to implement waits with timeout .

    
answered by 29.11.2017 / 11:51
source