Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8014577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:58:43+00:00 2026-06-04T19:58:43+00:00

I’m busy with an assignment but I’m stuck. I get errors and I don’t

  • 0

I’m busy with an assignment but I’m stuck. I get errors and I don’t understand what I’m doing wrong.

So in the main I make three children. Between the first and second I have a pipe (pipe12). Between the second and the third I have a pipe (pipe23). Now when the first child (reader) is ready with reading, it closes pipe12, but read of the second child doesn’t get an EOF. Secondly when the second child wants to write to pipe23, the child crashes.

I guess I do something wrong in the initialization of the pipes, but what?

This is the parent

for(childnr=2; childnr>=0;childnr--)
{
    tasks[childnr].pid=fork();
    if(tasks[childnr].pid==-1)
    {
        printf("fork error\n");
        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\n");                      //errorhandling
        }           
    }
    else
        close(tasks[childnr].errorpipe[1]);
}
close(pipe12[0]);
close(pipe12[1]);
close(pipe23[0]);
close(pipe23[1]);
... continue with the parent

This is the first child:

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 +-/*\n";
    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\n");
    fclose(readfile);
    close(pipe12[1]);
    close(tasks[childnr].errorpipe[1]);
}

And the second child:

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\n",token);
            write(pipe23[1],token,sizeof token);
            token = strtok(NULL, " ");
        }
        sleep(2);
    }
    exit(0);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T19:58:45+00:00Added an answer on June 4, 2026 at 7:58 pm

    Your primary problem is that read() returns 0 on EOF, not -1 or EOF.

    Your code should have loops like:

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

    I suggest avoiding the functions registered with atexit(); they force you to use global variables. Have your primary child functions do their own cleanup. This will make it easier to implement the suggestion made in a comment:

    You could improve the generality of your reader by passing the file descriptor to it, and similarly with the tokenizer and the evaluator:

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

    For the reader, you should probably pass the file name too:

    reader(calculatorfile, pipe12[1]);
    

    This code just about works:

    #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\n");
                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\n");                      //errorhandling
                        break;
                }           
            }
        }
        close(pipe12[0]);
        close(pipe12[1]);
        close(pipe23[0]);
        close(pipe23[1]);
    
        printf("Parent waiting...\n");
        while (wait(0) != -1)
            ;
        printf("Brats are all dead!\n");
        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 +-/*\n";
        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\n", *c, line);
                    line[0] = '\0';
                    break;
                }
                c++;
            }
            if (line[0] != '\0')
            {
                printf("RO %s", line);
                write(o_fd, line, strlen(line));
            }
        }
        close(o_fd);
        fclose(fp);
        printf("Reader exiting\n");
        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] = '\0';
            printf("TI %*s\n", nbytes, buffer);
            char *token = strtok(buffer, " \n");
            while (token!=NULL)
            {
                printf("TO %s\n", token);
                write(o_fd, token, strlen(token));
                token = strtok(NULL, " ");
            }
            sleep(2);
        }
        printf("Tokenizer exiting\n");
        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\n", nbytes, buffer);
            buffer[nbytes] = '\0';
            char *token = strtok(buffer, " ");
            while (token!=NULL)
            {
                printf("EO %s\n", token);
                token = strtok(NULL, " ");
            }
            sleep(2);
        }
        close(i_fd);
        printf("Evaluator exiting\n");
        exit(0);
    }
    

    Given a data file containing:

    123 456
    123 + 234 * 547 / 987 - 1
    

    One run of the program produced:

    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!
    

    Note that data read with read() has to be explicitly null terminated; data read with fgets() does not. Note also how the basic debugging was put in place; all inputs are echoed, all outputs to the next program are echoed too. This makes it easy (or, at least, easier) to see where there might be problems. In extreme cases, it would be better to write to stderr, or to fflush() after every printf(). There are many details that are sub-optimal, such as using printf() with just a single string argument. The tasks structure is redundant in this code. The pipe arrays could be local variables in the revised code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.