开发者

Call a function when a child process finishes

开发者 https://www.devze.com 2023-03-01 10:02 出处:网络
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 funct

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

精彩评论

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