Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.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++ C和x2B中的管道+;系统调用失败_C++_Linux_System Calls - Fatal编程技术网

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

C++ C和x2B中的管道+;系统调用失败,c++,linux,system-calls,C++,Linux,System Calls,我有以下硬件问题: 阅读管道系统调用的手册页。已提供2个部分完成的项目以帮助 教你吹笛 对于本实验室,您需要创建与实验室5相同的输出(x-5、x/5等)。这次你用的是 尽管如此。由于管道具有用于过程控制的内置机制,因此只需使用2个过程, 每次循环5次(而不是每次循环都创建一个新流程)。等一等,我就不工作了 此实验室。如果需要控制流程顺序的帮助,请尝试使用系统调用sleep()。 以下是将打印到屏幕/终端的输出示例 以下是一个示例输出: x=19530迭代1子:x=19525父:x=3905迭代2

我有以下硬件问题:

阅读管道系统调用的手册页。已提供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

父级读取失败
迭代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;
}
//管道实践
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
int x=19530;//原始输入
size_t XSIZE=sizeof(x);//原始输入的sizeo

您可能对管道的工作方式感到困惑。
pipe
返回两个文件描述符。第一个(索引0)是读取端,第二个(索引1)是写入端

比如说:

 check = read(parent[1], &x, XSIZE)
总是会失败,因为它们试图从写端读取。要修复此问题,只需更改索引:

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

如果您试图使用
子[1]

读取,则需要执行相同的操作。您对管道的工作方式感到困惑。
管道
返回两个文件描述符。第一个(索引0)是读取端,第二个(索引1)是写入端

比如说:

 check = read(parent[1], &x, XSIZE)
总是会失败,因为它们试图从写端读取。要修复此问题,只需更改索引:

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

当你试图使用
child[1]

大量系统调用来阅读时,你也需要这样做。想进一步说明哪些系统调用给你带来了痛苦吗?@user4581301当然,我已经在我的问题中对其进行了编辑。但特别是循环中的调用。这两个
read()
return-1.我不明白为什么会发生这种情况。那里有很多系统调用,老霍斯。想进一步说明哪些系统调用让你感到悲伤吗?@user4581301当然,我已经在我的问题中编辑了它。但具体来说,是循环中的调用。
两个都是read()
return-1。我不明白为什么会发生这种情况。这解决了我的问题,但现在迭代计数到处都是,请参见上文。这解决了我的问题,但现在迭代计数到处都是,请参见上文。基本上,迭代计数不遵循与其他两条打印语句相同的打印顺序。我想这是因为它不依赖于读写调用。知道如何在中工作吗?没关系!我解决了它!非常感谢
管道
的解释,我永远也不会绕过这个错误。这解决了我的问题,但现在迭代次数已经满满当当了,请参见上文。这解决了我的问题,但现在迭代计数到处都是,请看上面。基本上,迭代计数不像其他两个打印语句那样遵循相同的打印顺序。我认为这是因为它不依赖于读写调用。知道如何使用它吗?没关系!我解决了它!非常感谢
管道
的解释n、 我永远也绕不开那只虫子。