C 程序执行但不是全部,我不知道';我不知道这个错误是什么意思

C 程序执行但不是全部,我不知道';我不知道这个错误是什么意思,c,string,fork,pipe,C,String,Fork,Pipe,这个程序将创建一个父亲和两个孩子,将有一个链式字符,父亲将填充两个管道,第一个用数字,第二个用字母,第一个孩子将从第一个管道读取,并返回他得到的数字,第二个儿子将从第二个管道读取,并返回他得到的字母 #include <stdio.h> #include <unistd.h> #include <string.h> #include <fcntl.h> main() { printf("I am the father, I will cr

这个程序将创建一个父亲和两个孩子,将有一个链式字符,父亲将填充两个管道,第一个用数字,第二个用字母,第一个孩子将从第一个管道读取,并返回他得到的数字,第二个儿子将从第二个管道读取,并返回他得到的字母

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

main()
{
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n");
    char *word="alibas123sam";

    printf("Now 2 pipes will be created\n");
    int fd1[2];
    int fd2[2];
    pipe(fd1); pipe(fd2);
    printf("Now the father will write numbers in the first pipe, and letters in the second\n");
    int i;
    char numbers[20]; int j=0;
    char caracters[20]; int k=0;
    for (i=0;i<20;i++)
    {
        if(word[i]>='0' && word[i]<='9') //if number
        {
            close(fd1[0]); //closing reading
            write(fd1[1],&word[i],1);

        }
        else
        {
            close(fd2[0]);  
            write(fd2[1],&word[i],1);
        }

    }
    printf("The father has wrote in the 2 pipes, now its time for the sons\n");
    int f=fork();
    if(f==0) //first son
    {
        for(i=0;i<20;i++) {         
            close(fd1[1]); //closing writing
            read(fd1[0],&numbers[j],1);
            j++;

        }
        printf("first son read everything, he got %d Numbers\n", j);
    }
    else
    {
        f=fork();
        if(f==0)
        {
            for(i=0;i<20;i++) {         
            close(fd2[1]); //closing writing
            read(fd2[0],&caracters[k],1);
            k++;

        }   
        printf("second son read everything, he got %d caracters\n", k);
    }
}} 

这个问题可能是因为在fork之前关闭了文件描述符。将代码重新排列为

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

main()
{
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n");
    char *word="alibas123sam";

    printf("Now 2 pipes will be created\n");
    int fd1[2];
    int fd2[2];
    pipe(fd1); pipe(fd2);
    printf("Now the father will write numbers in the first pipe, and letters in the second\n");
    int i;
    char numbers[20]; int j=0;
    char caracters[20]; int k=0;
    for (i=0;i<20;i++)
    {
        if(word[i]>='0' && word[i]<='9') //if number
        {
            close(fd1[0]); //closing reading
            write(fd1[1],&word[i],1);

        }
        else
        {
            close(fd2[0]);  
            write(fd2[1],&word[i],1);
        }

    }
    printf("The father has wrote in the 2 pipes, now its time for the sons\n");
    int f=fork();
    if(f==0) //first son
    {
        for(i=0;i<20;i++) {         
            close(fd1[1]); //closing writing
            read(fd1[0],&numbers[j],1);
            j++;

        }
        printf("first son read everything, he got %d Numbers\n", j);
    }
    else
    {
        f=fork();
        if(f==0)
        {
            for(i=0;i<20;i++) {         
            close(fd2[1]); //closing writing
            read(fd2[0],&caracters[k],1);
            k++;

        }   
        printf("second son read everything, he got %d caracters\n", k);
    }
}} 
#包括
#包括
#包括
#包括
main()
{
printf(“我是父亲,我将创建两个儿子,第一个将读取数字,第二个将读取字母\n”);
char*word=“alibas123sam”;
printf(“现在将创建两个管道\n”);
int-fd1[2];
int-fd2[2];
管道(fd1);管道(fd2);
printf(“现在父亲将在第一根管子里写数字,在第二根管子里写字母”);
int i;
字符数[20];int j=0;
字符[20];int k=0;

对于(i=0;i='0'&&word[i],您应该检查
管道(2)
调用的返回值——它们似乎失败了(原因似乎很奇怪).你想在哪里执行这个程序?它是一个合理的主机还是一个笨拙的程序?我想知道你是否受到诸如、、或之类的工具的限制,但我怀疑任何工具都不会打印出这样一个奇怪的错误。为什么要关闭描述符?我不确定这是否是问题所在,但也许你不应该在分叉之前关闭它们,你可以在分叉之后执行,如果你不需要它们,我想这就是原因:fork()复制了打开的文件描述符,所以如果你在fork之前关闭它们,子对象将错过关闭的描述符。
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>

main()
{
    printf("I am the father, I will create 2 sons, the first will read the numbers , the second will read the letters\n");
    char *word="alibas123sam";

    printf("Now 2 pipes will be created\n");
    int fd1[2];
    int fd2[2];
    pipe(fd1); pipe(fd2);
    printf("Now the father will write numbers in the first pipe, and letters in the second\n");
    int i;
    char numbers[20]; int j=0;
    char caracters[20]; int k=0;
    for (i=0;i<20;i++)
    {
        if(word[i]>='0' && word[i]<='9') //if number
        {
            close(fd1[0]); //closing reading
            write(fd1[1],&word[i],1);

        }
        else
        {
            close(fd2[0]);  
            write(fd2[1],&word[i],1);
        }

    }
    printf("The father has wrote in the 2 pipes, now its time for the sons\n");
    int f=fork();
    if(f==0) //first son
    {
        for(i=0;i<20;i++) {         
            close(fd1[1]); //closing writing
            read(fd1[0],&numbers[j],1);
            j++;

        }
        printf("first son read everything, he got %d Numbers\n", j);
    }
    else
    {
        f=fork();
        if(f==0)
        {
            for(i=0;i<20;i++) {         
            close(fd2[1]); //closing writing
            read(fd2[0],&caracters[k],1);
            k++;

        }   
        printf("second son read everything, he got %d caracters\n", k);
    }
}}