Results 1 to 3 of 3
Hi friends! I have a doubt with this code. I have to implement two processes, the child and parent. First the parent will block on pause(), waiting signal SIGUSR1 from ...
- 02-03-2012 #1Just Joined!
- Join Date
- Nov 2009
- Posts
- 72
exit from pause()
Hi friends! I have a doubt with this code. I have to implement two processes, the child and parent. First the parent will block on pause(), waiting signal SIGUSR1 from the children, and executing a function handler. This is the code
The problem is that when children exits, the parent is still in pause(), and I want pause() to quit when the children exits. How can I achieve this???Code:#include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> void execute() { printf("function execute()\n"); } int main() { int var; if(fork()!=0)//parent { struct sigaction act_sigexec,old_sigexec; act_sigexec.sa_handler=execute; sigemptyset(&act_sigexec.sa_mask); act_sigexec.sa_flags=0; int ret; sigaction(SIGUSR1, &act_sigexec, &old_sigexec); while(1) { printf("parent waiting for signals\n"); pause(); } } else//child { while(1) { printf("and this is the child\n"); scanf("%d",&var); if(var==1) kill(getppid(),SIGUSR1); else if(var==0) { exit(EXIT_SUCCESS); } } } return 1; }
Thank you very much!
- 02-03-2012 #2
When the child exits, the parent gets a SIGCHILD from the kernel. You could handle this.
"I'm just a little old lady; don't try to dazzle me with jargon!"
- 02-05-2012 #3Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
1. Set up a signal handler to intercept the SIGUSR1 signals.
2. Use the wait() or waitpid() command to wait on the child death.
You will vector to the signal handler if you get a SIGUSR1 signal, and wait()/waitpid() will return if the child has died before it has issued the SIGUSR1 to you, the parent process.
FWIW, using wait() or waitpid() is preferable to pause() since that will also clean up the child's zombie process in the kernel. In fact, in 30+ years of C/C++ programming, I think I can count the number of times I have used pause() on one thumb...
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote