等待所有子进程结束

编程入门 行业动态 更新时间:2024-10-09 17:29:52

等待所有子<a href=https://www.elefans.com/category/jswz/34/1771450.html style=进程结束"/>

等待所有子进程结束

C语言方法一http://stackoverflow/questions/1510922/waiting-for-all-child-processes-before-parent-resumes-execution-unix

通过不指定pid的方式调用waitpid方法,每次等待一个子进程结束,直到所有子进程都结束(waitpid()返回0)

while (pid = waitpid(-1, NULL, 0)) {
   if (errno == ECHILD) {
      break;
   }
}

C语言方法二http://stackoverflow/questions/19461744/make-parent-wait-for-all-child-processes

通过调用wait方法,每次等待一个子进程结束,直到所有子进程都结束(wait()返回0)

pid_t child_pid, wpid;
int status = 0;

//Father code (before child processes start)

for (int id=0; id<n; id++) {
    if ((child_pid = fork()) == 0) {
        //child code
        exit(0);
    }
}

while ((wpid = wait(&status)) > 0); // this way, the father waits for all the child processes 

//Father code (After all child processes end)

Python方法http://stackoverflow/questions/2993487/spawning-and-waiting-for-child-processes-in-python

每创建一个子进程,都将该子进程的pid记录到一个pid的集合中,等所有子进程都创建完毕,逐个wait这个子进程pid集合中的每一个子进程,直到所有子进程都结束

pids.append(subprocess.Popen([RESIZECMD, lot, of, options])
for pid in pids:
	pid.wait()


更多推荐

等待所有子进程结束

本文发布于:2023-08-03 05:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284855.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进程   结束

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!