程序执行但不是全部,我不知道这个错误意味着什么

时间:2012-04-23 05:54:38

标签: c string fork pipe

这个程序会创建一个父亲和2个孩子,会有一个连锁字符,父亲将填充2个管道,第一个带数字,第二个带字母,第一个孩子将从第一个管道读取,并且返回他得到多少数字,第二个儿子将从第二个管道读取,并返回他收到多少信件。

#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);
    }
}} 

错误:

Disallowed system call: SYS_pipe

1 个答案:

答案 0 :(得分:1)

问题可能源于您在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);
    }
}} 

似乎有效。