943,749 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 668
  • C RSS
Aug 8th, 2009
0

Need help getting the correct signal for child process?

Expand Post »
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.
Similar Threads
Reputation Points: 31
Solved Threads: 0
Newbie Poster
guyod is offline Offline
6 posts
since Jul 2009
Aug 8th, 2009
-1

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

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.
Reputation Points: 431
Solved Threads: 17
Posting Whiz in Training
Hiroshe is offline Offline
255 posts
since Jun 2008
Aug 8th, 2009
0

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

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Aug 9th, 2009
0

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

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.
Reputation Points: 31
Solved Threads: 0
Newbie Poster
guyod is offline Offline
6 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Nid help with this (Beginner)
Next Thread in C Forum Timeline: Mastermind program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC