What does waitpid() do?

醉酒当歌 提交于 2020-01-02 03:25:22

问题


What is the use of waitpid()?


回答1:


It's used generally to wait until a specific process finishes (or otherwise changes state if you're using special flags), based on its process ID (otherwise known as a pid).

It can also be used to wait for any of a group of child processes, either one from a specific process group or any child of the current process.

See here for the gory details.




回答2:


It blocks the calling process until a nominated child process exits (or makes some other transition such as being stopped.)

Typically you will use waitpid rather than generic wait when you may have more than one process and only care about one.

A typical use is

pid_t child_pid;
int status;

child_pid = fork();

if (child_pid == 0) {
     // in child; do stuff including perhaps exec
} else if (child_pid == -1) {
     // failed to fork 
} else {
     if (waitpid(child_pid, &status, 0) == child_pid) {
          // child exited or interrupted; now you can do something with status
     } else {
          // error etc
     }
 }


来源:https://stackoverflow.com/questions/3664854/what-does-waitpid-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!