I am trying to write a C program in which the parent process suspends the child process and after a few seconds continues executing it.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void sigstop();
void sigcont();
void sigquit();
int main()
{
int pid;
if ((pid = fork()) < 0) {
perror("fork");
exit(1);
}
if (pid == 0)
{ /* child */
signal(SIGSTOP,sigstop);
signal(SIGCONT,sigcont);
signal(SIGQUIT, sigquit);
for(;;); /* loop for ever */
}
else {
printf("\nPARENT: sending SIGSTOP to suspend the process\n\n");
kill(pid, SIGSTOP);
sleep(5);
printf("\nPARENT: sending SIGCONT to continue the suspended process\n\n");
kill(pid, SIGCONT);
sleep(5);
printf("killing child");
kill(pid,SIGQUIT);
sleep(5);
}
}
void sigstop()
{
printf("CHILD: I've been suspended\n");
}
void sigcont()
{
printf("CHILD: I'm back\n");
}
void sigquit()
{
printf("My DADDY has Killed me!!!\n");
exit(0);
}
printf
inside sigstop()
never gets executed and sigquit()
gets called before printf("killing child");
. How does this happen and how can I get output in proper order ?