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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:40:56+00:00 2026-06-18T21:40:56+00:00

I’m writing a C program that parses input from STDIN into words, generates a

  • 0

I’m writing a C program that parses input from STDIN into words, generates a number of sort processes specified by the numsorts variable, pipes the words in a round-robin fashion to each of the sort processes, and sends the output of the sorts to STDOUT.

My program works as desired and exits cleanly if the number of specified sort processes is 1, but the sort child processes don’t die if the number of sort processes is greater than 1, and my program gets stuck waiting for them. The strangest part of this, to me, is that if I open up a separate terminal window and kill all of the children except 1, the last child immediately dies on its own and the program exits cleanly.

Here’s my code for the parser (the pipe file descriptors are stored in a 2-dimensional array):

void RRParser(int numsorts, int **outPipe){ //Round Robin parser
    int i;
    char word[MAX_WORD_LEN];

    //Close read end of pipes
    for(i = 0; i < numsorts; i++){
        closePipe(outPipe[i][0]);
    }

    //fdopen() all output pipes
    FILE *outputs[numsorts];
    for(i=0; i < numsorts; i++){
        outputs[i] = fdopen(outPipe[i][1], "w");
        if(outputs[i] == NULL)
            printf("Error: could not create output stream.\n");
    }

    //Distribute words to them
    i = 0;
    while(scanf("%[^,]%*c,", word) != EOF){
        strtoupper(word);
        fputs(word, outputs[i % numsorts]); //round robin
        fputs("\n", outputs[i % numsorts]); //sort needs newline
        i++;
    }

    //Flush the streams:
    for(i=0; i < numsorts; i++){
        if(fclose(outputs[i]) == EOF)
            printf("Error closing stream.\n");
    }
}

Here’s the code that generates the sort processes (PukeAndExit() just prints out the error message and exits):

int *spawnSorts(int numsorts, int **inPipe){
    //returns an array containing all the PIDs of the child processes
    //Spawn all the sort processes
    pid_t pid;
    int i; 
    int *processArray = (int *)malloc(sizeof(int) * numsorts);
    for(i = 0; i < numsorts; i++){ 
        switch(pid = fork()){
            case -1: //oops case
                    PukeAndExit("Forking error\n");
            case 0: //child case
                //Bind stdin to read end of pipe
                closePipe(inPipe[i][1]); //close write end of input pipe
                if(inPipe[i][0] != STDIN_FILENO){ //Defensive check
                    if(dup2(inPipe[i][0], STDIN_FILENO) == -1)
                        PukeAndExit("dup2 0");
                    closePipe(inPipe[i][0]); //Close duplicate pipe
                }
                execlp("sort", "sort", (char *)NULL);
               break;
            default: //parent case
                processArray[i] = pid;
        }
    }
    return processArray;
}

At the end of main(), here’s the code that waits for the sort processes to die:

for(i=0; i<numsorts; i++){ //wait for child processes to die. 
    wait(NULL);
}
  • 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-18T21:40:58+00:00Added an answer on June 18, 2026 at 9:40 pm

    Nevermind, I figured it out. The bug was actually in the function where I generated the pipes. As soon as I switched to creating the pipes in my spawnSorts process, everything worked. Previously, I was generating an array of pipes in a separate function. I’m not sure why this works now but it does.

    Old generatePipes function:

    int **generatePipesArray(int numpipes){ //returns a 2-dimensional array of pipes
            int **pipesArray = (int **) malloc(sizeof(int *) * (numpipes));
            int i;
            for(i = 0; i < numpipes; i++){
                    pipesArray[i] = (int *) malloc(sizeof(int) * 2);
                    createPipe(pipesArray[i]);
            }
            return(pipesArray);
    }
    

    New function:

    int **generatePipesArray(int numpipes){ //returns an empty 2-dimensional array
            int **pipesArray = (int **) malloc(sizeof(int *) * (numpipes));
            int i;
            for(i = 0; i < numpipes; i++){
                    pipesArray[i] = (int *) malloc(sizeof(int) * 2);
                    //not creating pipes here anymore
            }
            return(pipesArray);
    }
    

    Code added to spawnSorts:

    int *spawnSorts(int numsorts, int **inPipe, int **outPipe){
    //returns an array containing all the PIDs of the child processes
    //Spawn all the sort processes
    pid_t pid;
    int i; 
    int *processArray = (int *)malloc(sizeof(int) * numsorts);
    for(i = 0; i < numsorts; i++){
         //new code here:
        createPipe(inPipe[i]);
    
    • 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 have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am currently running into a problem where an element is coming back from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I am doing a simple coin flipping experiment for class that involves flipping a

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.