Awaken processes with signal in C

0

I have two child processes from the same parent, and I want them to do nothing until a signal SIGUSR1 wakes up one or the other.

pid1=fork();
if (pid1>0)
{
    pid2=fork();
}

What would be used? Maybe pause() ? How would you reactivate it later?

    
asked by Sergio 18.05.2017 в 13:20
source

1 answer

0

I understand that we are working under a UNIX system, and that therefore what you are looking for is to perform a signal processing. To do this you must add information to your process to ask it to capture a specific type of signal, and in doing so, execute a function.

I'll give you a small example of how to do this, where:

my_handler is the function that will be executed when they receive a signal (in this example all the signals they capture execute the same function

signal is the function that associates the signal and the function we want to execute.

#include <stdio.h>
#include <signal.h>
#include <stdlib.h>

void mi_handler(int sig){
int i;
        for (i=10;i>=0;i--){
                printf("<%d> %d\n",sig,i);
                sleep(1);
        }
}

void main()
{
        if (signal(SIGINT, mi_handler) == SIG_ERR){
                printf("Error al asignar tratamiento a SIGINT\n");
                exit (-1);
        }
        if (signal(SIGQUIT, mi_handler) == SIG_ERR){
                printf("Error al asignar tratamiento a SIGQUIT\n");
                exit (-1);
        }

        while (1){
                printf("Sigo durmiendo\n");
                sleep(10);
        }
        exit (0);
}
    
answered by 09.03.2018 в 13:16