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 7490377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:35:32+00:00 2026-05-29T15:35:32+00:00

I working my way through a C++ and Operating Systems book and I’ve come

  • 0

I working my way through a C++ and Operating Systems book and I’ve come upon an assignment that requires creation, writing, and reading from pipes. However my program stalls on reading from the second pipe. My program is to accept input and parse out a space delimited string into tokens and classifying those tokens accordingly. My code is bellow with my problem area marked. Any help is as always very appreciated.

edit: This is supposed to have two children. One for processing the space delimited tokens and the other for determining the type of delimited tokens. As far as debugging goes I only have access to cout as a debugger. So I inserted a cout before the read and after the one before the read appeared but the one after did not.

#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

using namespace std;

//declaring the pipes
int pipeOne[2];
int pipeTwo[2];

struct inputStruct {
char str[256]; /* one extra spot for \n */
int  len;      /* length of str         */
int  flag;     /*  0 for normal input, 1 to indicate “done” */
};

struct tokenStruct {
char token[256]; /* tokens can be 255 max */
int  flag;       /* same as inputStruct   */
int  tokenType;  /* a code value          */
};

void dataProcess(){

//new input struct to contain the the input from the parent
inputStruct input;

//the intial read from the pipe to populate the input stuct
read( pipeOne[0], (char*)&input, sizeof(inputStruct));

//set the flag
int flag = input.flag;

while (flag != 1){

    int size = 0;

    //get the size of the array up until the null character
    while (input.str[size] != '\0'){
        size++;
    }

    //Here's the parsing of each token
    for (int i=0; i<size; i++) {

        int tokenLength;

        tokenStruct token;

        //while the char isn't white space or null increment through it
        while (input.str[i] != ' ' && input.str[i] != '\0') {

            //a is the index of the string token 
            int a = 0;

            //write the parsed string
            token.token[a] = input.str[i];

            a++;
            i++;

        }

        //write to process 2
        write(pipeTwo[1], (char*)&token, sizeof(tokenStruct));

    }

    //read again and store the results
    read(pipeOne[0], (char*)&input, sizeof(inputStruct));
    flag = input.flag;

}

tokenStruct token;

token.flag = flag;

//final write to the second child to tell it to commit suicide
write(pipeTwo[1], (char*)&token, sizeof(tokenStruct));

exit(0);
}

void tokenClassifer(){

tokenStruct token;

//Problem area is here on ****************************************************

//the initial read
read(pipeTwo[0], (char*)&token, sizeof(tokenStruct));

while (token.flag != 1){

    int size = 0;

    //get the size of the array up until the null character
    while (token.token[size] != '\0'){
        size++;
    }

    if (size == 1) {
        //check for the one char things first
        switch (token.token[0])
        {
            case '(':
                token.tokenType = 0;
                break;
            case ')':
                token.tokenType = 0;
                break;
            case ';':
                token.tokenType = 0;
                break;
            case '+':
                token.tokenType = 1;
                break;
            case '-':
                token.tokenType = 1;
                break;
            case '/':
                token.tokenType = 1;
                break;
            case '*':
                token.tokenType = 1;
                break;
            default:
                if (isdigit(token.token[0])) {
                    token.tokenType = 2;
                } else {
                    token.tokenType = 3;
                }
                break;
        }

    } else {

        bool isStr;

        int i = 0;
        //check for the more than one character
        while (token.token[i] != '\0'){

            //check if it's a string or digits
            if (isdigit(token.token[0])) {
                isStr=false;
            } else{
                //set up the bools to show it is a string
                isStr=true;
                break;
            }
        }

        //if it is a string token type 3
        if (isStr) {
            token.tokenType = 3;
        } else {
            //if not then it's digits and token type 2
            token.tokenType = 2;
        }

    }

    //print out the token and token type
    cout << "Token type is: " << token.tokenType << "Token value is: " << token.token << "\n";

    //read the pipe again and start the process all over
    read(pipeTwo[0], (char*)&token, sizeof(tokenStruct));
}

exit(0);
}

int main()
{

//create the pipes for reading and writing between processes
pipe(pipeOne);
pipe(pipeTwo);

//fork off both processes
int value = fork();
int value2 = fork();

//do the process for the first fork
if(value == 0){

    //fork one
    dataProcess();

} else {

    wait(0);

}

//do the process for the second fork
if (value2 == 0) {
    //fork two

    //the token classifer function for the second fork
    tokenClassifer();

} else {
    cout << "Type some tokens (or just press enter to quit) \n";
    //this is all of the parent functions        
    for (string line; getline(cin, line); )
    {

        inputStruct input;
        if (line.empty())
        {
            // if the line is empty, that means the user didn't
            // press anything before hitting the enter key
            input.flag = 1;
            write( pipeOne[1], (char*)&input, sizeof(inputStruct));
            break;
        } else {

            //else copy the string into an array
            strcpy(input.str, line.c_str());

            //set the flag to zero to show everthing is ok
            input.flag = 0;
        }

        //write the stuct to the pipe
        write( pipeOne[1], (char*)&input, sizeof(inputStruct));

        cout << "Type some tokens (or just press enter to quit) \n";
    }

    wait(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-05-29T15:35:34+00:00Added an answer on May 29, 2026 at 3:35 pm

    One problem that is evident:

    //fork off both processes
    int value = fork();
    int value2 = fork();
    

    This will fork 3 new processes. The initial fork will leave you with two processes, each of which go on to fork a new process.

    EDIT:

    Proper forking:

    int value = fork();
    if (value == 0) {
        // do child stuff
        exit(0);
    } else if (value == -1) {
        //fork failed
    }
    
    int value2 = fork();
    if (value2 == 0) {
        //do child stuff
        exit(0);
    } else if (value2 == -1) {
        //fork failed
    }
    

    I’m actually not quite clear about how data goes through your program, so I’ll leave it to you to add the waits. I’d actually change the names of value and value2, but that’s just me. Also, I’m only addressing the forking issue here so there may be other problems with your code (which I kind of suspect since you have two pipes).

    EDIT 2:

    Another issue that I see is that you’re not closing the ends of the pipes that you don’t use. If you never close the write end of a pipe, your reads will block until the pipe has data (or there are no more writers to the pipe, that is, the write end is not open). This means that the write end of the pipe should be closed in all processes when you are not using it or are finished with it.

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

Sidebar

Related Questions

I'm working my way through some ASP.NET MVC reading and I have a web
I've been learning Objective-C and Cocoa by working my way through the Hillegass book
I am currently working my way through the Apress ASP.NET MVC2 book, and I
While working my way through the Android tutorials, I came across something I don't
I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
I'm working my way through Agile Web Development with Rails and am running into
I'm still working my way through some pretty basic Actionscript programming (in Flex), and
I've been working my way through Tim Heuer's Silverlight tuturial , in which you
I'm working my way through APress's Beginning Android 2 and I've made it all
I've been slowly working my way through the list of Project Euler problems, and

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.