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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:01:09+00:00 2026-05-31T09:01:09+00:00

I’m trying to properly connect three processes in order to allow inter-process communication between

  • 0

I’m trying to properly connect three processes in order to allow inter-process communication between them. I have one process, scanner, which takes the parent’s STDIN and then processes the words within the stream. If a word length is odd, it sends it to one process, if it is even, it sends it to another. These processes should take in these words via STDIN (I assume) and then output some info back to the scanner process via STDOUT. The STDOUT of even/odd should be redirected to scanner, which will then read (using read) and then output/process the words. It’s an academic exercise, not a practical one. Here’s what a picture of it would look like:

Pipe setup

Here’s what my code currently looks like. The problem is I’m not exactly sure what to dup and what to close. Once I figure that out I should be good to go! Any advice would be appreciated.

File descriptors:

int scannertoeven[2]; int scannertoodd[2];
int eventoscanner[2]; int oddtoscanner[2];
//Pipe stuff here (ommitted)

Code:

 //Create the child processes
 if ((scanner_pid = fork())  == 0) {

      //We need the scanner pid so even and odd can send signals to it
      char pidofparent[sizeof(getpid())];
      sprintf(pidofparent, "%i", getpid());

      //Even stuff
      if ((even_pid = fork()) == 0) {
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Not sure which ones to close
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);


           //Correct?
           close(STDIN_FILENO);
           dup2(scannertoeven[0], STDIN_FILENO);
           close(STDOUT_FILENO);
           dup2(eventoscanner[1], STDOUT_FILENO);


          if(execl("./evenodd", "even", pidofparent,  NULL ) == -1) {
               printf("execl Error!");
               exit(1);
          }

     //Odd Stuff
     } else if ((odd_pid = fork()) == 0){
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);

           //Not sure which ones to close
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Correct?
           close(STDIN_FILENO);
           dup2(scannertoodd[0], STDIN_FILENO);
           close(STDOUT_FILENO);
           dup2(oddtoscanner[1], STDOUT_FILENO);

          if(execl("./evenodd", "odd", pidofparent,  NULL ) == -1) {
               printf("execl Error!");
               exit(1);
          } 


      //Actual Scanner
      } else {

           // Not sure which ones to close- this is very wrong
           close(scannertoeven[0]); close(scannertoeven[1]);
           close(eventoscanner[0]); close(eventoscanner[1]);
           close(scannertoodd[0]); close(scannertoodd[1]);
           close(oddtoscanner[0]); close(oddtoscanner[1]);

           //Not sure what to dup either
           dup2(scannertoodd[1], STDOUT_FILENO);
           dup2(scannertoeven[1], STDOUT_FILENO);
           if(execl("./scanner", "scanner", stoeven, stoodd, eventos, oddtos, NULL ) == -1) {
                printf("execl Error!");
                exit(1);
           }

           //Wait twice here, or three times in main?
           waitpid(odd_pid, &status2, 0);
           waitpid(even_pid, &status3, 0);
      }
 //Main
 } else {
      //Close Pipes
      close(scannertoodd[0]); close(scannertoeven[0]); close(eventoscanner[0]); close(oddtoscanner[0]);
      close(scannertoodd[1]); close(scannertoeven[1]); close(eventoscanner[1]); close(oddtoscanner[1]);
      //Wait for children to finish
      waitpid(scanner_pid, &status1, 0);
      printf("Done\n");
 }
  • 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-31T09:01:11+00:00Added an answer on May 31, 2026 at 9:01 am

    Not sure about the logic. But the way you use dup2 is not right.

    The following code in the “Even” process:

    close(STDIN_FILENO);
    dup2(scannertoeven[0], STDIN_FILENO);
    close(STDOUT_FILENO);
    dup2(eventoscanner[1], STDOUT_FILENO);
    

    should be:

    dup2(scannertoeven[0], STDIN_FILENO);
    // You should close scannertoeven[0], not STDIN. After this dup2, the even
    // process will receive input from scannertoeven[0]
    close(scannertoeven[0]);
    // Note the the scannertoeven[0] is not "really" closed, just that the file
    // is "attached" to STDIN
    
    dup2(eventoscanner[1], STDOUT_FILENO);
    // Same as above. After this dup2, all the even process's output will go
    // to eventoscanner[1]
    close(eventoscanner[1]);
    

    The same as the “Odd” process.

    Here is an example of dup2, for your reference.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.