The parent process creates a Child. Then I want them both to print to STDOUT alternatively. For this I'm using pause() and kill(pid,SIGUSR1). I've declared a dummy handler for SIGUSR1.
It compiles fine. Runs fine until it gets stuck. And it gets stuck at different occasions.
I think semaphores will be a way better appraoch to this problem, but I want to use pause-kill to increse my understanding of signals.
I think whats happening is both parent and child get paused because one of them gets descheduled just after the kill() and before pause(). I need to find a way to overcome this.
The expected output is both printing till 4999.
void action(int signum){}
int main ()
{
int i = 0, j = 0;
pid_t ret;
int status;
signal(SIGUSR1,action);
ret = fork ();
if (ret == 0)
{
for (i = 0; i < 5000; i++){
printf ("Child: %d\n", i);
kill(getppid(),SIGUSR1);
pause();
}
printf ("Child ends\n");
}
else
{
for (j = 0; j < 5000; j++){
printf ("Parent: %d\n", j);
kill(ret,SIGUSR1);
pause();
}
}
}
Some current output I'm getting are-
.... Child: 3844 Parent resumes. Parent: 3844 //after this both hang(or pause I think)
.... Child: 44 Parent resumes. Parent: 44 //after this both hang(or pause I think)
.... Child: 574 Parent resumes. Parent: 574 //after this both hang(or pause I think)