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
- create a child process using fork()
- If the SIGINT signal is received, print a message but not exit
- If the SIGTSTP signal is received, the first time send the SIGUSR1 signal to the child process and the second time print a message and exit
- If the SIGUSR1 signal is received, send SIGUSR1 to opposing process
- else both processes wait for a signal to be received
What do I need to change/add to this code to make it work correctly?