| | |
Need help getting the correct signal for child process?
![]() |
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
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.
C Syntax (Toggle Plain Text)
Parent function: signal(SIGCHLD, handler) while(child process is running) print the number of seconds Child function: does something for some random amount of seconds {
Thanks in advance guys, I am not sure if i posted in the correct thread. So sorry if i got the incorrect one.
What do you want to do exactly? It sounds like your talking about functions.
C Syntax (Toggle Plain Text)
void child(void); int main(void) { child(); /* Call child process */ return 0; } void child(void) { /* Random stuff */ }
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
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...
Hope this helps.
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...
C Syntax (Toggle Plain Text)
#include <sys/types.h> #include <sys/wait.h> #include <unistd.h> ... int status; int seconds = 0; do { /* Pause for a second */ usleep( 1000000 ); /* Tell the user the number of elapsed seconds */ printf( "\r%d seconds...", ++seconds ); /* Check the child's current status */ waitpid( (pid_t)(-1), &status, WNOHANG ); /* Only break if it has terminated */ } while (!WIFEXITED( status ));
Last edited by Duoas; Aug 8th, 2009 at 11:59 pm.
•
•
Join Date: Jul 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
I am very thankful for all the help guys.
Last edited by guyod; Aug 9th, 2009 at 2:27 pm.
![]() |
Similar Threads
- get data from child process through pipe (C++)
- creating a child process using c program (C)
- child process (C)
- spawning a child process (C++)
- problem with exec function for child process (C)
- Need help writing a wrapper function for fork() and signal functions. (C)
- fork->how parent refer child process results (C)
Other Threads in the C Forum
- Previous Thread: Nid help with this (Beginner)
- Next Thread: prime number question
| Thread Tools | Search this Thread |
#include adobe api array arrays asterisks binarysearch calculate char cm copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory dynamic feet fflush fgets file fork forloop frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hacking hardware highest homework i/o include incrementoperators input interest kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix meter microsoft motherboard mqqueue mysql number odf open opensource owf pattern pdf performance pointer posix probleminc process program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling segmentationfault send sequential shape socket socketprograming stack standard string systemcall turboc unix user voidmain() wab win32api windows.h






