Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/6.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_Pipe_Parent Child_Parent_Child Process - Fatal编程技术网

在C中使用管道

在C中使用管道,c,pipe,parent-child,parent,child-process,C,Pipe,Parent Child,Parent,Child Process,我尝试使用一个管道来创建3个子进程,其中每个子进程都应该提示用户输入int,然后将其写入管道。父进程应该读取管道并显示最大int 以下是我有限的知识: #include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <unistd.h> main(int argc, char argv[]) { int inpu

我尝试使用一个管道来创建3个子进程,其中每个子进程都应该提示用户输入int,然后将其写入管道。父进程应该读取管道并显示最大int

以下是我有限的知识:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>

main(int argc, char argv[]) {
    int input[3];
    int i;
    int fd[2];
    int result;

    if(pipe(fd) == (-1))
        fprintf(stderr, "pipe failed\n");

        for(i = 0; i < 3; i++) {
            if(fork() == 0){//child
                printf("Enter a number: ");
                scanf("%d", &input[i]);
                printf("\n");
            }
        }
        if(input[0] >= input[1]){
            if(input[0] >= input[2])
                result = input[0];
            else
                result = input[2];
        }
        else if(input[1] >= input[2]){
            if(input[1] >= input[0])
                result = input[1];
            else
                result = input[0];
        }
        else if(input[2] >= input[0]){
            if(input[2] >= input[1])
                result = input[2];
            else
                result = input[1];
        }
        printf("The max of the three is: %d", result);
    }

请告诉我一些情况。

你需要在管道上写东西,从管道上读东西。实际上,您根本没有使用管道,并且在每个进程中读取的输入值都包含垃圾。fork运行后,每个进程都有自己独立的输入副本。也就是说,一个进程写入输入的内容不会被任何其他进程看到。@AlanAu但我实际上如何做到这一点呢?我以前从未使用过读/写或管道..我不打算为您编写程序。如果您对管道/读/写一无所知,那么可以从手册页开始。本文甚至包含了一个例子。读一读,尝试一些代码,如果你仍然有困难的话,带着更新的问题回来。
The max of the three is: 4195760Enter a number: $ Enter a number:
Enter a number:
The max of the three is: 4195760The max of the three is: 4195760Enter a number:
The max of the three is: 4195760