c - For waitpid, how do I know if the child finishes its process? -
first, fork child something, , use waitpid(-1, &child_status, wnohang)
; in parent let parent continue instead of waiting child finish.
how know when child finished process?
you can set signal handler sigchld
gets sent automatically when child process exits.
the signal processor can set global flag can periodically checked in other parts of program. if flag set, call wait
or waitpid
child's exit status.
int child_exit_flag = 0; void child_exit(int sig) { child_exit_flag = 1; } ... signal(sigchld, child_exit); ... if (child_exit_flag) { pid_t pid; int status; child_exit_flag = 0; pid = wait(&status); printf("child pid %d exited status %d\n", pid, status); }
Comments
Post a Comment