Need help getting the correct signal for child process?

Reply

Join Date: Jul 2009
Posts: 6
Reputation: guyod is an unknown quantity at this point 
Solved Threads: 0
guyod guyod is offline Offline
Newbie Poster

Need help getting the correct signal for child process?

 
0
  #1
Aug 8th, 2009
Hello, I am learning C, POSIX, and Unix.

I am trying to write a code that wait until the child process is done then terminate, however, I do not know what to put in the while loop.

  1. Parent function:
  2. signal(SIGCHLD, handler)
  3. while(child process is running)
  4. print the number of seconds
  5.  
  6. Child function:
  7. does something for some random amount of seconds
  8.  
  9. {
The teacher advised me to use signal(SIGCHLD, handler) part. I do not complete know what it does, all i know is that it does not ignore signal from the child. How do i know the child has ended? Basically, what do i put in the "child process is running" of my while loop. It can also be something like, while child process is not exited.

Thanks in advance guys, I am not sure if i posted in the correct thread. So sorry if i got the incorrect one.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 255
Reputation: Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough Hiroshe is a jewel in the rough 
Solved Threads: 16
Hiroshe's Avatar
Hiroshe Hiroshe is offline Offline
Posting Whiz in Training

Re: Need help getting the correct signal for child process?

 
-1
  #2
Aug 8th, 2009
What do you want to do exactly? It sounds like your talking about functions.
  1. void child(void);
  2.  
  3. int main(void) {
  4. child(); /* Call child process */
  5.  
  6. return 0;
  7. }
  8.  
  9. void child(void) {
  10. /* Random stuff */
  11. }
Last edited by Hiroshe; Aug 8th, 2009 at 12:26 pm.
"Sometimes, when I lie in bed at night and look up at the stars, I think to myself, "Man! I really need to fix that roof."-Jack Handy
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need help getting the correct signal for child process?

 
0
  #3
Aug 8th, 2009
If you have actually fork()ed (or spawn()ed) a child process, and you just want to check whether or not it has terminated, you want the waitpid() function.

You can also use the usleep() function to cause the parent to spin its wheels for a short bit. There are more modern and/or better ways to do this, but this is short and simple...

  1. #include <sys/types.h>
  2. #include <sys/wait.h>
  3. #include <unistd.h>
  4. ...
  5. int status;
  6. int seconds = 0;
  7. do {
  8. /* Pause for a second */
  9. usleep( 1000000 );
  10. /* Tell the user the number of elapsed seconds */
  11. printf( "\r%d seconds...", ++seconds );
  12. /* Check the child's current status */
  13. waitpid( (pid_t)(-1), &status, WNOHANG );
  14. /* Only break if it has terminated */
  15. } while (!WIFEXITED( status ));
Hope this helps.
Last edited by Duoas; Aug 8th, 2009 at 11:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 6
Reputation: guyod is an unknown quantity at this point 
Solved Threads: 0
guyod guyod is offline Offline
Newbie Poster

Re: Need help getting the correct signal for child process?

 
0
  #4
Aug 9th, 2009
Thanks guys, what i really wanted is exactly what Duoas's answer gave. I was having a hard time being able to check for the child status. I wanted the child to run, however, I do not want the parent to exit and terminate the child's time is up. I will try out the code below and let you guys know how it goes.

I am very thankful for all the help guys.
Last edited by guyod; Aug 9th, 2009 at 2:27 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC