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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:59:54+00:00 2026-06-04T16:59:54+00:00

I have a task in Linux and I can’t get it work. I have

  • 0

I have a task in Linux and I can’t get it work.

I have a program that receives a text file as parameter. It then creates a child process using fork() and sends to the child process, line by line the content of the text file received as parameter. The child process needs to count the lines and return to the parent process the number of lines received.

This is what I have until now, but somewhat the child process does not receive all the lines. For my test I used a text file with 9 lines. The parent sent 9 lines as strings but the child process received only 2 or 3 of them.

What am I doing wrong?

#include <stdio.h>      
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{  
  char string[80];
  char readbuffer[80];
  int pid, p[2];
  FILE *fp;
  int i=0;
  if(argc != 2)
  {
    printf("Syntax: %s [file_name]\n", argv[0]);
    return 0;    
  }
  fp = fopen(argv[1], "r");
  if(!fp) 
  {    
    printf("Error: File '%s' does not exist.\n", argv[1]);
    return 0;
  }
  if(pipe(p) == -1)
  {
    printf("Error: Creating pipe failed.\n");
    exit(0);
  } 
  // creates the child process
  if((pid=fork()) == -1)
  {
   printf("Error: Child process could not be created.\n");
    exit(0);
  }  

  /* Main process */
  if (pid) 
  { 
    // close the read
    close(p[0]);    
    while(fgets(string,sizeof(string),fp) != NULL)
    {                
       write(p[1], string, (strlen(string)+1));
       printf("%s\n",string);
    } 

    // close the write
    close(p[1]);
    wait(0);
  }

  // child process
  else 
  {   
    // close the write
    close(p[1]); 

    while(read(p[0],readbuffer, sizeof(readbuffer)) != 0) 
    {
      printf("Received string: %s\n", readbuffer);    
    }

    // close the read
    close(p[0]); 
  } 
  fclose(fp);       
}
  • 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-04T16:59:55+00:00Added an answer on June 4, 2026 at 4:59 pm

    A pipe is a unidirectional interprocess communication channel. You have to create 2 pipes, one to speak to the child process, the other to read data back.

    Remember to close the unused side of the pipe on both processes.

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

Sidebar

Related Questions

I have developed a task manager for linux in java. The output as of
i have task which takes a parameter and has three modes of results Example
I have task involving reading SAS .xpt files using .NET. For that I'm using
I have one task that is checking a url speed, but i want that
I have a PROGRAMMING task (homework) to investigate brute force neural net methods. That
I have about 5 different heterogenous linux dedicated server which can run Java applications.
So here is the story. I have this device that uses Linux and more
I have got a multi-threaded app that process a very large data file. Works
I have some question about the following switch in Linux Kernel, somebody can explain
Can I use task manager to detect huge memory leaks? I have a small

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.