简体   繁体   English

Waitpid和fork / exec在系统调用方面的非阻塞优势?

[英]Waitpid and fork/exec's non-blocking advantage over a syscall?

I always hear that you should never use system() and instead fork/exec because system() blocks the parent process. 我总是听到您永远不要使用system() ,而应该使用fork/exec因为system()会阻止父进程。

If so, am I doing something wrong by calling waitpid() , which also blocks the parent process when I do a fork/exec ? 如果是这样,我是否通过调用waitpid()做错了事,当我执行fork/exec时, waitpid()也阻塞了父进程? Is there a way around calling waitpid ...I always thought it was necessary when doing a fork/exec . 有没有办法解决waitpid ...我一直认为在进行fork/exec时有必要。

pid_t pid = fork();

if (pid == -1)
{
    // failed to fork
} 
else if (pid > 0)
{
    int status;
    waitpid(pid, &status, 0);
}
else 
{
    execve(...);
}

The WNOHANG flag (set in the options argument) will make the call to waitpid() non-blocking. WNOHANG标志(在options参数中设置)将使对waitpid()的调用无阻塞。

You'll have to call it periodically to check if the child is finished yet. 您必须定期调用它以检查孩子是否已经完成。

Or you could setup SIGCHLD to take care of the children. 或者,您可以设置SIGCHLD来照顾孩子。

If you want to do other stuff whilst the child process is off doing it's thing, you can set up a trap for SIGCHLD that handles the child finishing/exiting. 如果您想在子进程关闭时做其他事情,则可以为SIGCHLD设置陷阱,以处理子进程的完成/退出。 Like in this very simple example. 就像在这个非常简单的示例中一样。

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

pid_t pid;
int finished=0;

void zombie_hunter(int sig)
    {
    int status;
    waitpid(pid, &status, 0);
    printf("Got status %d from child\n",status);
    finished=1;
    }

int main(void)
    {
    signal(SIGCHLD,zombie_hunter);

    pid = fork();

    if (pid == -1)
        {
        exit(1);
        } 
    else if (pid == 0)
        {
        sleep(10);
        exit(0);
        }

    while(!finished)
        {
        printf("waiting...\n");
        sleep(1);
        }
    }

I always hear that you should never use system() and instead fork/exec because system() blocks the parent process. 我总是听到您永远不要使用system() ,而应该使用fork/exec因为system()会阻止父进程。

Never say never. 永远不要把话说绝了。 If system() has the semantics you want, including, but not limited to, blocking the calling process, then by all means, use it! 如果system()具有您想要的语义,包括但不限于阻止调用过程,则请务必使用它! Do be sure that you understand all those semantics, though. 不过,请确保您了解所有这些语义。

If your objective is to avoid blocking the parent process, then it is important to understand that the parent can perform an unbounded amount of work between forking a child and collecting it via one of the wait() family of functions. 如果您的目标是避免阻塞父进程,那么了解父进程可以在派生孩子与通过其中的一个wait()函数集进行收集之间执行无穷的工作量,这一点很重要。 This is very much analogous to starting a new thread, proceeding on with other work, and then eventually joining the thread. 这非常类似于启动新线程,继续进行其他工作,然后最终加入线程。

Moreover, if the parent doesn't need to know or care when the child terminates, then it is possible to avoid any need to wait for it at all, ever. 而且,如果父母不需要知道或关心孩子何时终止,那么就可以完全避免等待。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM