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

时间:2017-03-13 22:06:24

标签: c++ linux system-calls

我有以下问题:

  

阅读管道系统调用的手册页。已提供2个部分完成的计划以提供帮助   教你管道。

     

对于本实验,您将创建与实验室5(x-5,x / 5等)相同的输出。这次你正在使用   管道虽然。由于管道具有用于过程控制的内置机制,因此您只需使用2个过程,   每个循环5次(而不是每次循环时创建一个新进程)。等等然后将无法工作   这个实验室。如果您需要帮助控制流程订单,请尝试使用系统调用sleep()。   以下是将打印到屏幕/终端的示例输出。

以下是示例输出:

  

x = 19530
迭代1
儿童:x = 19525
父母:x = 3905
迭代2
儿童:x = 3900
父母:x = 780
迭代3
儿童:x = 775
父母:x = 155
迭代4
儿童:x = 150
父母:x = 30
迭代5
儿童:x = 25
父:x = 5

我的输出如下:

  

x = 19530

父母阅读失败
  ITERATION 0
  孩子,阅读失败了

我在下面有以下代码,但由于某种原因我的系统调用在父进程和子进程的循环开始时继续返回负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
儿童:x = 19525
ITERATION 2
儿童:x = 3900
ITERATION 3
父:x = 3905

父:x = 780

儿童:x = 775
迭代4家长:x = 155

儿童:x = 150
ITERATION 5
父母:x = 30

儿童:x = 25
父:x = 5

1 个答案:

答案 0 :(得分:2)

你对管道是如何工作感到困惑。 pipe返回两个文件描述符。第一个(索引0)是读端,第二个(索引1)是写端。

所以请致电:

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

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

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

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

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