Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用wait()控制多个子进程的行为;_C++_C_Unix_Process_Operating System - Fatal编程技术网

C++ 如何使用wait()控制多个子进程的行为;

C++ 如何使用wait()控制多个子进程的行为;,c++,c,unix,process,operating-system,C++,C,Unix,Process,Operating System,我试图创建一个程序,其中父进程执行一些计算并将其发送给子进程,子进程派生另一个进程并执行更多计算并将其发送给child2进程进行进一步处理。 我似乎不知道该怎么做。 我的输出全是乱七八糟的,有时孩子2在孩子1之前跑。有时孩子1最后完成。 我希望其他进程等待它们 我认为代码应该是这样的 #include <iostream> #include <sys/wait.h> #include <unistd.h> using namespace std; int m

我试图创建一个程序,其中父进程执行一些计算并将其发送给子进程,子进程派生另一个进程并执行更多计算并将其发送给child2进程进行进一步处理。 我似乎不知道该怎么做。 我的输出全是乱七八糟的,有时孩子2在孩子1之前跑。有时孩子1最后完成。 我希望其他进程等待它们

我认为代码应该是这样的

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;

int main(int argc, char *argv[])
{
    int fd[6];
    pipe(fd);
    pipe(fd + 2);
    pipe(fd + 4);

    pid_t id = fork();
    if (id == -1)
        return 1;
    if (id == 0)
    {
        wait(NULL);
        pid_t id2 = fork();
        if (id2 == -1)
            return 2;
        if (id2 == 0)
        {
            wait(NULL);
            pid_t id3 = fork();
            if (id3 == -1)
                return 2;
            if (id3 == 0)
            {

                wait(NULL);
                cout << "Child 3" << endl;
                // Read data from pipe and display
            }
            else
            {
                cout << "Child 2" << endl;
                // Read Data from pipe and Display
                // Some Computation
                for (int i = 0; i < 10000; i++)
                {
                }

                // Send Data to Child 3 through pipe
                wait(NULL);
            }
        }
        else
        {
            cout << "Child 1" << endl;
            // Read Data from pipe

            // Some Computation
            for (int i = 0; i < 100000000; i++)
            {
            }

            // Send Data to Child 2 through pipe
            wait(NULL);
        }
    }
    else
    {
        cout << "Parent" << endl;
        // Some Computation
        for (int i = 0; i < 2000; i++)
        {
        }
        // Send Data to Child 1 through pipe
        wait(NULL);
    }
}
#包括
#包括
#包括
使用名称空间std;
int main(int argc,char*argv[])
{
int-fd[6];
管道(fd);
管道(fd+2);
管道(fd+4);
pid_t id=fork();
如果(id==-1)
返回1;
如果(id==0)
{
等待(空);
pid_t id2=fork();
如果(id2==-1)
返回2;
如果(id2==0)
{
等待(空);
pid_t id3=fork();
如果(id3==-1)
返回2;
如果(id3==0)
{
等待(空);
cout当同一进程组中的任何子进程终止(因此包括任何子进程)时,调用就会返回

如果要等待特定进程,请改用。您还应检查退出代码和状态

最后,您应该等待子对象在通过管道与它交互后退出,而不是在之前,并且您不应该在启动子对象之前等待

#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
using namespace std;

void die(const char* msg) {
  perror(msg);
  exit(1);
}

int main(int argc, char* argv[]) {
  int fd[6];
  pipe(fd);
  pipe(fd + 2);
  pipe(fd + 4);

  pid_t id = fork();
  if (id == -1)
    die("fork");
  if (id == 0) {
    pid_t id2 = fork();
    if (id2 == -1)
      die("fork 2");
    if (id2 == 0) {
      pid_t id3 = fork();
      if (id3 == -1)
        die("fork 3");
      if (id3 == 0) {
        cout << "Child 3" << endl;
        // Read data from pipe and display
        // Done. Nothing to wait for - just exit child 3
        cout << "Child 3 exiting" << endl;
      } else {
        cout << "Child 2" << endl;
        // Read Data from pipe and Display
        // Some Computation
        // Send Data to Child 3 through pipe
        // Wait for child 3 to finish before exiting child 2
        if (waitpid(id3, NULL, 0) < 0)
          die("waitpid 3");
        cout << "Child 2 exiting" << endl;
      }
    } else {
      cout << "Child 1" << endl;
      // Read Data from pipe
      // Some Computation
      // Send Data to Child 2 through pipe
      // Wait for child 2 to finish before exiting child 1
      if (waitpid(id2, NULL, 0) < 0)
        die("waitpid 2");
      cout << "Child 1 exiting" << endl;
    }
  } else {
    cout << "Parent" << endl;
    // Some Computation
    // Send Data to Child 1 through pipe
    // Wait for child 1 to finish before exiting parent
    if (waitpid(id, NULL, 0) < 0)
      die("waitpid 1");
    cout << "Parent exiting" << endl;
  }
}

请注意,
Child 1
/
2
/
3
是以随机顺序打印的-这是因为所有的子进程都是并行运行的,这是完全正常的(我们希望进程并行运行,这就是分叉的整个要点)。在读取/写入管道时将强制执行排序-从管道读取的子级将等待数据到达。

在分叉之后,没有必要立即在子级中调用
wait
;它没有什么可等待的。您必须允许子级2在之前运行(或与之同时运行,或与之交错运行)子1,或者您有一个死锁的配方。如果子1生成的数据超过管道缓冲区中的数据量,则不会有任何数据流动。管道的概念是您不必担心它;管道本身就是您的同步机制,进程将根据需要阻止读取。谢谢,我对它的理解是错误的。我理解现在