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

  • Home
  • SEARCH
  • 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 8777281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:14:38+00:00 2026-06-13T19:14:38+00:00

I posted a question previously about using fork() and pipes in C . I

  • 0

I posted a question previously about using fork() and pipes in C
. I changed the design a little bit so that it reads a regular txt file and sorts the words in the file. So far, this is what I came up with:

 for (i = 0; i < numberOfProcesses; ++i) {
    // Create the pipe
    if (pipe(fd[i]) < 0) {
      perror("pipe error");
      exit(1);
    }

    // fork the child
    pids[i] = fork();
    if (pids[i] < 0) {
      perror("fork error");
    } else if (pids[i] > 0) {     
      // Close reading end in parent
      close(fd[i][0]);

    } else {     
      // Close writing end in the child
      close(fd[i][1]);

      int k = 0;
      char word[30];

      // Read the word from the pipe
      read(fd[i][0], word, sizeof(word));

      printf("[%s]", word);    <---- **This is for debugging purpose**

     // TODO: Sort the lists  
    }
  }


  // Open the file, and feed the words to the processes
  file_to_read = fopen(fileName, "rd");

  char read_word[30];
  child = 0;

  while( !feof(file_to_read) ){
    // Read each word and send it to the child
    fscanf(file_to_read," %s",read_word);

    write(fd[child][1], read_word, strlen(read_word));
    ++child;
    if(child >= numberOfProcesses){
      child = 0;
    }
  }

where numberOfProcesses is a command-line argument. So what it does is that it reads each word in the file and send it to a process. This however, does not work. When I print the word in the child process, it doesn’t give me the correct output. Am I writing/reading the words correctly to/from the pipes?

  • 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-13T19:14:39+00:00Added an answer on June 13, 2026 at 7:14 pm

    Are the words being printed in the wrong order or interleaved? The thing is that when you write a word to a pipe, you are expecting the process handling that pipe to be scheduled immediately and to print the word. Then you expect the main process to run again and the next word to be written to the next pipe etc.

    But that is not guaranteed to happen. Your main loop might write all of the words to all of the pipes before any of the other processes is scheduled. Those processes might not be scheduled in the order you expect. And the printf calls might interfere with each other so that their output becomes interleaved.

    If you really want to do what you set out to, then Posix threads would be better. If you just wanted to learn something about using multiple processes, then I guess you have 🙂

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

Sidebar

Related Questions

I posted one question previously and all of them answered that there is some
I've just posted the question about general usage of using/try/catch construction and all is
I had previously posted a question about my query speed with an XML column.
I recently posted this question about codes for a gift-card-like voucher that users can
I have previously posted a question about this but I did not get an
I previously posted a question on sharing resources across assemblies using Themes/generic.xaml and the
I've seen this question posted here previously but I'm not satisfied that I understand
I know that there is a previously posted question that is very similar to
Previously I posted a question regarding multithreading. Actually my intension is to send SMS
Yesterday I've posted question about SQLite performance issues ( Terrible performance in Zend Db

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.