Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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_Linux_Ubuntu 14.04 - Fatal编程技术网

C 我怎么能在同一根管子上写两遍呢?

C 我怎么能在同一根管子上写两遍呢?,c,linux,ubuntu-14.04,C,Linux,Ubuntu 14.04,我希望父级读取标准输入并将其写入管道,然后子级读取此管道并将其转换为大写,然后将其发送回另一个管道给父级,然后父级将其打印到标准输出,然后再写入一次 家长不经常写信 #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include

我希望父级读取标准输入并将其写入管道,然后子级读取此管道并将其转换为大写,然后将其发送回另一个管道给父级,然后父级将其打印到标准输出,然后再写入一次

家长不经常写信

#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <unistd.h>
#define bufsize 200

int main(int argc,char*argv[])
{
    char buf[bufsize];
    char buf2[bufsize];
    int x;
    int child;
    int fd[2];
    int fd1[2];
    int nbytes,mbytes;
    int i,j,k;
    int status=0;

    if(pipe(fd)==-1)
        printf("pipe");
    if(pipe(fd1)==-1)
        printf("pipe");

    switch(fork())
    {
        case -1 : printf("fork");
        case 0:{
            //for(k=1;k<3;k++)
            //{close(fd[1]);
            nbytes = read(fd[0], buf, bufsize);   

            for(i=0;i<=strlen(buf);i++){
                if(buf[i]>=97&&buf[i]<=122)
                buf[i]=buf[i]-32;
            }//end for

            close(fd1[0]);
            write(fd1[1], buf, nbytes);
            _exit(EXIT_SUCCESS);   

        }//end child

        default:{
            for(j=1;j<3;j++){
                close(fd[0]);
                printf("Enter any thing:\n");
                x=read(STDIN_FILENO, buf, bufsize);
                write(fd[1],buf,x);

                close(fd1[1]);
                mbytes = read(fd1[0], buf, bufsize);   
                close(fd1[0]); 
                write(STDOUT_FILENO, buf, mbytes);
            }
            //wait(status);
            exit(EXIT_SUCCESS);
        }//end default
    }//end switch
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#定义bufsize 200
int main(int argc,char*argv[])
{
字符buf[bufsize];
字符buf2[bufsize];
int x;
智力儿童;
int-fd[2];
int-fd1[2];
整数字节,兆字节;
int i,j,k;
int status=0;
如果(管道(fd)=-1)
printf(“管道”);
如果(管道(fd1)=-1)
printf(“管道”);
开关(fork())
{
案例1:printf(“fork”);
案例0:{

//为了(k=1;k如果要在同一管道中写入两次,请不要在第一次写入后将其关闭。只有在完成后才能将其关闭。

请不要保存空格键,而是正确缩进您的代码,以便可以读取您打算在那里执行的任何操作。还请注意,如果出现分叉故障,您还将运行
案例0:
!在
案例-1:
的末尾添加一个分隔符。我不明白,各位,你们在做什么(不缩进代码)作为文本编辑器使用?Windows记事本?大多数现代代码编辑器都是为您使用的,您必须努力防止它们使用。这意味着您不会显示“
fd
fd1
这两个文件描述符数组的可怕名称。但您的问题是,首先关闭文件描述符通过
for
循环的时间。因此,第二次,您正在读取和写入闭合文件描述符,这是未定义的行为。