开发者

Call a function when a child process finishes

In my c++ program several child processes are started using fork and开发者_开发技巧 execv. I don't wait for them so they run in the background. When they finish running, I want to run a certain function. How can this be done?


Listen for the SIGCHLD signal and call your function in the handler. Be careful what you call in the handler, you should only call async-signal-safe functions.


I'm guessing that you do want to wait for the processes to finish, but you want to run the processes in parallel rather than sequentially. If so, start each process as you do now, then use the waitpid() system call on each child pid. When every process has exited, every waitpid will have returned.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜