grr169
grr169

Reputation: 11

Sending signals between parent and child process in c

I'm trying to write a program that sends signals between a parent and child process. This is what I have so far:

void parent_sig(int signo){
    if(signo == SIGINT){
        printf("*** Parent received SIGINT ***\n");
    }else if(signo == SIGTSTP){
        //first time
        signal(SIGUSR1, c_sig_usr);

        //second time
        printf("*** Parent received SIGTSTP ***\n");
        exit(0);

    }else if(signo == SIGUSR1){
        printf("*** Parent received SIGUSR1 ***\n");
        sleep(3);
        signal(SIGUSR1, c_sig_usr); 
    }else{
            pause();
    }

}

void child_sig(int signo){
    if(signo == SIGINT){
        printf("*** Child received SIGINT ***\n");
    }else if(signo == SIGTSTP){
        printf("*** Child received SIGTSTP ***\n");
    }else if(signo == SIGUSR1){
        printf("*** Child received SIGUSR1 ***\n");
        sleep(3);
        signal(SIGUSR1, p_sig_usr); 
    }else{
        pause();
    }
}

int main(void)
{
    pid_t child, parent;
    parent = getpid();

    struct sigaction p_sig;
    p_sig.sa_handler = &parent_sig;


    if((child = fork()) < 0) { 
        perror("**ERROR**: failed to fork process"); 
        exit(EXIT_FAILURE); 
    }else if (child == 0){
        struct sigaction c_sig;
        c_sig.sa_handler = &child_sig;
    }else{

    }


    return 0;
}

I want it to

What do I need to change/add to this code to make it work correctly?

Upvotes: 1

Views: 1788

Answers (1)

Stian Skjelstad
Stian Skjelstad

Reputation: 2335

signal() is the call you use to configure that a function should be called when a specific signal is received.

kill() is the call you use to send a specific signal to a specific process id.

Upvotes: 0

Related Questions