C++ 系统调用中的管道失败

Piping in C++ system calls failing

我有以下硬件问题:

Read the man page of pipe system call. 2 partially completed programs have been provided to help teach you pipes.

For this lab you are to create an output identical to that of lab 5(x-5, x/5, etc.). This time you are using pipes though. Since pipes have a built in mechanism for process control, you only need to use 2 processes, each which loop 5 times (instead of creating a new process every time you loop). Wait then won’t work for this lab. If you need help controlling process order, try using the system call sleep(). Following is a sample output that will be printed to the screen/terminal.

下面是示例输出:

x = 19530
Iteration 1
Child: x = 19525
Parent: x = 3905
Iteration 2
Child: x = 3900
Parent: x = 780
Iteration 3
Child: x = 775
Parent: x = 155
Iteration 4
Child: x = 150
Parent: x = 30
Iteration 5
Child: x = 25
Parent: x = 5

我的输出如下:

x = 19530

Parent read failed
ITERATION 0
Child, read failed

我有下面的代码,但由于某种原因,我的系统调用在父进程和子进程的循环开始时一直返回负数 1,我不明白为什么。谁能解释一下?我在 Ubuntu 16.04 linux。

// Pipe practice
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<sys/types.h>
#include<iostream>
#include<fcntl.h>
using namespace std;

int main()
{

    int x = 19530; // Original input
    size_t XSIZE = sizeof(x); // sizeo of the original input
    cout << "x = " << x << endl << endl; // first line of test output

    int child[2]; // for child pipe
    int parent[2]; // for parent pipe
    pid_t ID; // for fork() later
    ssize_t check; // ssize_t type for error checking

// opening the pipes, error handling
    if ((pipe(child)) < 0)
    { // child pipe
        cout << "Child has no pipe\n";
        return 1;
    }

    if ((pipe(parent)) < 0)
    { // parent pipe
        cout << "Parent has no pipe\n";
        return 1;
    }
// initial write to parent pipe
    if ((check = write(parent[1], &x, XSIZE)) <= 0)
    { // swap first 2 params q
        cout << "Pre-write failed\n";
        return 1;
    }

    ID = fork(); // forking, each fork will have two loops which iterate 5 times passing values back and forth
    if (ID < 0)
    {
        cout << "Fork failed \n"; // error handling for fork
        return 1;
    }

    else if (ID == 0)
    { // child does x = x-5
        for (int i = 0; i < 5; i++)
        {
            check = 0; // sets check to 0 each time to prevent error
            cout << "ITERATION " << i << endl;
            if ((check = read(parent[1], &x, XSIZE)) < 0)
            { // read the new value of x into x from parent[1]
                cout << "Child, read failed \n";
                return 1;
            }
            x = x - 5; // do the subtraction
            if ((check = write(child[1], &x, XSIZE)) < 0)
            { // write the new value into child[1] for piping for parent
                cout << "Child, write failed \n";
                return 1;
            }
            cout << "Child : x = " << x << endl;
        }
    }

    else
    { // parent does x = x/5
        for (int i = 0; i < 5; i++)
        {
            check = 0; // again, error prevention
            if ((check = read(child[1], &x, XSIZE)) < 0)
            { // read new x value from child[1]
                cout << "Parent read failed \n";
                return 1;
            }
            x = x / 5; // do division
            if ((check = write(parent[1], &x, XSIZE)) < 0)
            {
                cout << "Parent write failed \n"; // write new value to parent[1] for piping back to child
                return 1;
            }
            cout << "Parent : x = " << x << endl << endl;
        }
    }
    return 0;
}

编辑 现在我的输出如下:

x = 19530

ITERATION 1
Child : x = 19525
ITERATION 2
Child : x = 3900
ITERATION 3
Parent : x = 3905

Parent : x = 780

Child : x = 775
ITERATION 4
Parent : x = 155

Child : x = 150
ITERATION 5
Parent : x = 30

Child : x = 25
Parent : x = 5

您对管道的工作原理感到困惑。 pipe returns 返回两个文件描述符。第一个(index 0)为读端,第二个(index 1)为写端

所以像这样调用:

 check = read(parent[1], &x, XSIZE)

总是会失败,因为他们试图从写端读取。要修复,只需更改索引:

 check = read(parent[0], &x, XSIZE)

您需要在尝试使用 child[1]

阅读的地方执行相同的操作