我的叉子输出不正确

我正在尝试创建一个程序,该程序创建2个子进程和4个管道(我知道这并不理想,但是此特定任务的规范要求这样做)。虽然它正确地对5个命令行参数整数中的两个进行了排序,但其余的只是随便吐出来,因为我认为是未初始化的整数,即I.E。 7打印为33234951。

我对管道还很陌生,想缠起来有点困难,所以我相信这个问题与这个问题有关,而不是代码中的一些任意错误。

我仅使用1个父级和子级就可以成功完成此任务,但是一旦我尝试实现多个父项,事情就变得扑朔迷离。

我有很多未使用的内容,只是为了解决问题而搞乱了。

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int argc,char **argv) {

  printf("Starting\n");
  pid_t pid;
  pid_t pid2;
  int mypipe0[2];
  int mypipe1[2];
  int mypipe2[2];
  int mypipe3[2];

  pipe(mypipe0);
  pipe(mypipe1);
  pipe(mypipe2);
  pipe(mypipe3);

  /* Create the child process. */
  pid = fork();

  std::cout << "Fork " << pid << std::endl; 
  // Child: Sorts Array
  if (pid == 0) {

    printf("pid == (pid_t) 0 p2\n");

    /* This is the child process.
       Close other end first. */
    close(mypipe0[1]);
    char valuesArray[5];
    for (int a = 0; a < 5; a++)
      read(mypipe0[0],&valuesArray[a],sizeof(char));

    printf("finish reading mypipe0");
    std::sort(valuesArray,valuesArray + 5);

    printf("sorted");
    close(mypipe1[0]);
    close(mypipe2[0]);

    for (int a = 0; a < 5; a++) {
      write(mypipe1[1],sizeof(char));
      write(mypipe2[1],sizeof(char));
    }

    close(mypipe1[1]);
    close(mypipe2[1]);
    exit(0);
  } 

  else if (pid > 1) {
    std::cout << "pid == (pid_t) 1" << std::endl;
    /* This is the parent process.
        Close other end first. */
    close(mypipe0[0]); // Closes reading
    int valuesArray[5];
    valuesArray[0] = atoi(argv[1]);
    valuesArray[1] = atoi(argv[2]);
    valuesArray[2] = atoi(argv[3]);
    valuesArray[3] = atoi(argv[4]);
    valuesArray[4] = atoi(argv[5]);
    printf("Argv init");
    for (int a = 0; a < 5; ++a)
      write(mypipe0[1],sizeof(char));

    printf("wrote to pipe 1");
    close(mypipe0[1]);

    wait(NULL);

    close(mypipe1[1]); // Closes writing
                       //            char outputArray[6];
    int sortedArray[5];
    for (int a = 0; a < 5; ++a)
      read(mypipe1[0],&sortedArray[a],sizeof(char));

    // Printing Array]
    for (int a = 1; a < 5; ++a)
      printf(",%d",sortedArray[a]);
    printf("]");

    //            wait(NULL);

    //            close(mypipe2[1]); // Closes writing
    //            int median;
    //            read(mypipe1[0],median,sizeof(charian));

    exit(0);
  }


  else {

    pid2 = fork();
    // Other child
    if (pid2 == 0) {

      printf("pid == (pid_t) 0\n");

      /* This is the child process.
         Close other end first. */
      close(mypipe0[1]);
      char valuesArray[5];
      for (int a = 0; a < 5; a++)
        read(mypipe0[0],sizeof(char));

      printf("finish reading mypipe0");
      std::sort(valuesArray,valuesArray + 5);

      printf("sorted");
      close(mypipe1[0]);
      close(mypipe2[0]);

      for (int a = 0; a < 5; a++) {
        write(mypipe1[1],sizeof(char));
        write(mypipe2[1],sizeof(char));
      }

      close(mypipe1[1]);
      close(mypipe2[1]);
      exit(0);
    }
  }
}

我期望输出为4 2 5 6 7的给定命令行参数为2 4 5 67。相反,我得到了[1,28932,5,6,-14276913]

WY190521 回答:我的叉子输出不正确

第一个else调用失败时,将到达fork分支。

您当前的代码结构是

pid = fork();

if (pid == 0)
{
  runFirstChild();
}
else if (pid > 1)
{
  runParent();
}
else // This branch is not taken unless there is a failure
{
  pid2 = fork();
  if (pid2 == 0)
  {
    runSecondChild();
  }
}

您应该改为这样构造

pid = fork();

if (pid == 0)
{
  runFirstChild();
}
else if (pid > 1)
{
  pid2 = fork(); // Fork the second child in the parent process
  if (pid2 == 0)
  {
    runSecondChild();
  }
  else if (pid2 > 1)
  {
    runParent();
  }
}
本文链接:https://www.f2er.com/3135930.html

大家都在问