English 中文(简体)
C 管道初始化错误
原标题:C pipe initialization error
  • 时间:2012-05-24 18:11:33
  •  标签:
  • c
  • pipe

我忙于一项任务,但我被困住了。我犯错,我不明白我做错了什么。

所以在主体中,我生三个孩子。在第一个和第二个之间,我有一个管道( ipop12 )。在第二个和第三个之间,我有一个管道( ipop23 )。当第一个孩子( reader )准备好阅读的时候,它关闭了 ipop12 ,但第二个孩子的阅读没有获得 EOF 。第二个孩子想写到 ipop23 ,儿童崩溃。

我想我做错了什么 管道初始化,但什么?

这是父父( 父)

for(childnr=2; childnr>=0;childnr--)
{
    tasks[childnr].pid=fork();
    if(tasks[childnr].pid==-1)
    {
        printf("fork error
");
        exit(1);
    }
    else if(tasks[childnr].pid==0)
    {
        switch(childnr)
        {
            case 0:
                close(pipe12[0]);
                close(pipe23[0]);
                close(pipe23[1]);
                reader();
                break;
            case 1:
                close(pipe12[1]);
                close(pipe23[0]);
                tokenizer();
                break;
            case 2:
                close(pipe12[0]);
                close(pipe12[1]);
                close(pipe23[1]);
                evaluator();
                break;
            default:
                printf("childnr error
");                      //errorhandling
        }           
    }
    else
        close(tasks[childnr].errorpipe[1]);
}
close(pipe12[0]);
close(pipe12[1]);
close(pipe23[0]);
close(pipe23[1]);
... continue with the parent

这是第一个孩子:

void reader(void)
{   
    atexit(*reader_exit);
    if((readfile = fopen(calculatorfile,"r"))==NULL)
    {
        printf("R send error to errorHandler");     //errpipe!
        exit(0);
    }
    char line[50];
    const char *valid_characters = "0123456789 +-/*
";
    while(fgets ( line, sizeof line, readfile ) != NULL)
    {
        printf("R reading ...%s",line);
        char *c = line;
        while(*c)
        {
            if(!strchr(valid_characters,*c))
            {
                printf("R invalid character: %c in %s",*c,line);
                line[0]=0;
                break;
            }
            c++;
        }
        write(pipe12[1],line,sizeof line);
    }
    exit(0);
}

void reader_exit(void)
{
    printf("R reader exit
");
    fclose(readfile);
    close(pipe12[1]);
    close(tasks[childnr].errorpipe[1]);
}

第二个孩子:

void tokenizer(void)
{
    atexit(*tokenizer_exit);
    char buffer[50];
    while(read(pipe12[0],buffer,sizeof buffer)!=EOF)
    {
        printf("T %s",buffer);
        char *token = strtok(buffer," ");
        while(token!=NULL)
        {
            printf("T %s
",token);
            write(pipe23[1],token,sizeof token);
            token = strtok(NULL, " ");
        }
        sleep(2);
    }
    exit(0);
}
最佳回答

您的主要问题是 read () report () report 0 on EOF, 不是 - 1 或 EOF 。

您的代码应该有循环, 例如 :

while (read(pipe12[0], buffer, sizeof buffer) > 0)

我建议避免以 atexit () 注册的函数; 它们迫使您使用全局变量。 由您的主要子函数自行清理。 这样可以更容易执行在评论中提出的建议 :

你可以通过向读者传递文件描述符来提高读者的通俗性, 并且与质记器和评价员相似:

reader(pipe12[1])
tokenizer(pipe12[0], pipe23[1]);
evaluator(pipe23[0]);

对于读者来说,您也许应该把文件名也传过去:

reader(calculatorfile, pipe12[1]);

这个代码只是关于作品的代码:

#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>     /* atexit() */
#include <string.h>

static int pipe12[2];
static int pipe23[2];

struct task
{
    pid_t pid;
};
static struct task tasks[5];

static void evaluator(int i_fd);
static void tokenizer(int i_fd, int o_fd);
static void reader(char const *file, int o_fd);

int main(void)
{
    pipe(pipe12);
    pipe(pipe23);
    for (int childnr=2; childnr>=0;childnr--)
    {
        tasks[childnr].pid=fork();
        if (tasks[childnr].pid==-1)
        {
            printf("fork error
");
            exit(1);
        }
        else if (tasks[childnr].pid==0)
        {
            switch (childnr)
            {
                case 0:
                    close(pipe12[0]);
                    close(pipe23[0]);
                    close(pipe23[1]);
                    reader("data-file", pipe12[1]);
                    break;
                case 1:
                    close(pipe12[1]);
                    close(pipe23[0]);
                    tokenizer(pipe12[0], pipe23[1]);
                    break;
                case 2:
                    close(pipe12[0]);
                    close(pipe12[1]);
                    close(pipe23[1]);
                    evaluator(pipe23[0]);
                    break;
                default:
                    printf("childnr error
");                      //errorhandling
                    break;
            }           
        }
    }
    close(pipe12[0]);
    close(pipe12[1]);
    close(pipe23[0]);
    close(pipe23[1]);

    printf("Parent waiting...
");
    while (wait(0) != -1)
        ;
    printf("Brats are all dead!
");
    return(0);
}

static void reader(char const *file, int o_fd)
{   
    FILE *fp;
    if ((fp = fopen(file, "r"))==NULL)
    {
        printf("R send error to errorHandler");     //errpipe!
        exit(0);
    }
    char line[50];
    const char *valid_characters = "0123456789 +-/*
";
    while (fgets(line, sizeof(line), fp) != NULL)
    {
        printf("RI %s", line);
        char *c = line;
        while (*c)
        {
            if (!strchr(valid_characters, *c))
            {
                printf("R invalid character: %c in %s
", *c, line);
                line[0] =   ;
                break;
            }
            c++;
        }
        if (line[0] !=   )
        {
            printf("RO %s", line);
            write(o_fd, line, strlen(line));
        }
    }
    close(o_fd);
    fclose(fp);
    printf("Reader exiting
");
    exit(0);
}

static void tokenizer(int i_fd, int o_fd)
{
    char buffer[50];
    int nbytes;
    while ((nbytes = read(i_fd, buffer, sizeof(buffer))) > 0)
    {
        buffer[nbytes] =   ;
        printf("TI %*s
", nbytes, buffer);
        char *token = strtok(buffer, " 
");
        while (token!=NULL)
        {
            printf("TO %s
", token);
            write(o_fd, token, strlen(token));
            token = strtok(NULL, " ");
        }
        sleep(2);
    }
    printf("Tokenizer exiting
");
    exit(0);
}

static void evaluator(int i_fd)
{
    char buffer[50];
    int nbytes;
    while ((nbytes = read(i_fd, buffer, sizeof(buffer))) > 0)
    {
        printf("EI %*s
", nbytes, buffer);
        buffer[nbytes] =   ;
        char *token = strtok(buffer, " ");
        while (token!=NULL)
        {
            printf("EO %s
", token);
            token = strtok(NULL, " ");
        }
        sleep(2);
    }
    close(i_fd);
    printf("Evaluator exiting
");
    exit(0);
}

给出包含以下内容的数据文件 :

123 456
123 + 234 * 547 / 987 - 1

制作了一部程序:

Parent waiting...
RI 123 456
RO 123 456
RI 123 + 234 * 547 / 987 - 1
RO 123 + 234 * 547 / 987 - 1
Reader exiting
TI 123 456

TO 123
TO 456

EI 123
EO 123
TI 123 + 234 * 547 / 987 - 1
EI 456

TO 123
TO +
TO 234
TO *

TO 547
TO /
EO 456

TO 987
TO -
TO 1

EI 123+234*547/987-1

EO 123+234*547/987-1

Tokenizer exiting
Evaluator exiting
Brats are all dead!

请注意, 使用 < code> read () 读取的数据必须被明确终止; 使用 < code> fget () 读取的数据没有被终止。 注意基本调试设置的方式; 所有输入都被回响, 下个程序的所有输出也被回响。 这样很容易( 或至少更容易) 发现哪里可能有问题 。 在极端情况下, 最好写到 < code> stderr , 或写到 < code> lashet () < / code>, 或写到 < code> printf () < / code > 。 有许多次最佳的细节, 例如使用单字符串参数的 < code> printf () 。 任务结构在本代码中是多余的。 管道阵列可以是订正代码中的本地变量 。

问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签