first of all i think the logic of the first and second while loops is not the best the while(1) thing would not stop since its always true. don't you have a better way to check the fork() function without including an infinite loop inside???

10-05-2012

JM1082

Quote:

Originally Posted by Nyah Check

first of all i think the logic of the first and second while loops is not the best the while(1) thing would not stop since its always true. don't you have a better way to check the fork() function without including an infinite loop inside???

The program is never meant to stop, just continually print whichever process isn't paused! Hence infinite loops!

10-05-2012

christop

Why are you calling pause from inside your signal handler? I'm pretty sure that won't do anything useful unless you explicitly make signals reentrant (but you might end up with a stack overflow if you do that in your current code).

NOTES
alarm() and setitimer(2) share the same timer; calls to one will interfere with use of the other.

sleep(3) may be implemented using SIGALRM; mixing calls to alarm() and sleep(3) is a bad idea.

10-05-2012

JM1082

Quote:

Originally Posted by christop

Why are you calling pause from inside your signal handler? I'm pretty sure that won't do anything useful unless you explicitly make signals reentrant (but you might end up with a stack overflow if you do that in your current code).

So would you recommend I send the SIGALRM from the handler?

10-05-2012

JM1082

No Salem, that's not what I want. But its good to see the pause & alarm are working together!
The idea is to have the parent print line after line for 5 seconds & pause, then the child print line after line for 5 seconds & pause, restarting the parent & looping like that continuously!

No... still not quite right, but I like the way you've used the kill( pid ) thing! The messages are printing one at a time rather than very quickly, repeatedly until the process change... not far off though!

10-08-2012

AndiPersti

Have you found already a solution?

The reason why your initial approach doesn't work is that after fork() both processes are independent from each other and thus don't see the alarm call from the other (both will pause infinitely after the reach the signal handler the first time). alarm() only signals a SIGALRM to the calling process.
You need some kind of interprocess communication between both processes.

Quote:

Originally Posted by JM1082

The idea is to have the parent print line after line for 5 seconds & pause, then the child print line after line for 5 seconds & pause, restarting the parent & looping like that continuously!

How about that (I use one second cycles to make the effect more distinct):