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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:34:35+00:00 2026-06-13T03:34:35+00:00

I am learning process managing in Linux and I need to make child and

  • 0

I am learning process managing in Linux and I need to make child and parent communicate though a pipe. I have declared two structures:

typedef struct
{
    double min, max, avg;   /*Number stats*/
} Stats;

typedef struct {
    pid_t pid;      /*Process ID*/
    int first;      /*First number to process*/
    int last;       /*Last number to process*/
    int fd[2];      /*Pipe descriptor*/
    Stats stats;    /*Stats computed by process*/
}Process_list;

I need the M child processes to compute some stats out of an array of numbers (dividing the work). Then, the child process would read the Process_list structure, process the numbers in the array from first to last (specified there) and save the computed stats into the stats structure.

The most relevant parts to my trouble of the code are the following (I am adding comments instead of other pieces of code to explain what is done OK):

int main(int argc, char *argv[])
{
    pList=(Process_list *)malloc((M+1)*sizeof(Process_list));
    /*Correct allocation is checked*/

    int i;
    pid_t pid;
    for (i=0 ; i<M ; i++)       /*M clones*/
    {
        /*Fork and pipe creation*/
        pid = fork ();
        pipe(pList[i].fd);

        /*If it's the child*/
        if ( pid == 0 ) 
        {
            pList[i].pid=pid; 
            printf("CHILD %d: %d a %d\n",i, pList[i].first,pList[i].last);

            /*A function here computes stats and saves them OK in the struct*/
            /*(e.g. min is saved in pList[i].stats.max, when printed it's a reasonable value)*/

            /*Now I need to send the info to the parent*/
            ret1=close(pList[i].fd[0]);
            /*ret1=0 => OK */

            ret2=write(pList[i].fd[1], &(pList[i].stats), sizeof(Stats));
            printf("return write: %d\n",ret2);
            /*TROUBLE HERE! This isn't even printed. sizeof(Stats)=24 which I think is OK*/

            exit(EXIT_SUCCESS);
        }

        /*Parent*/
        else if ( pid > 0 )
        {
            wait(NULL); /*Is this really neccesary?*/

            ret1=close(pList[i].fd[1]);
            ret2=read(pList[i].fd[0], &(pList[i].stats), sizeof(Stats));
            /*Both ret1 and ret2 = 0*/

            printf("[p]Mín: %lf\n Max: %lf\nAverage: %lf\n",pList[i].stats.min,pList[i].stats.max,pList[i].stats.avg);
            /*Everything printed here  = 0.0000000000*/


        }

        else /*fork error*/
                return -1;
    }

So my problem is that childs compute their stats perfectly, but the parent doesn’t receive them. The write() function does nothing. This happens for every value of M (including M=1 -just one process).

Also I don’t know if wait(NULL) is necessary, as I’ve seen some working examples that doesn’t use it. However, if I don’t write it there, the parent’s printfs will appear before the child’s, so I think it just doesn’t wait for the child to write in the pipe. It doesn’t work without it, anyway.

Or maybe the structure approach is just not a good one?

Thank you very much in advance!

  • 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-13T03:34:36+00:00Added an answer on June 13, 2026 at 3:34 am

    You have to create the pipe before you fork it. Otherwise you are creating two pipes. One on your parent and one on your child which are not connected.

    And wait for the child after you got all the data, else you may end up waiting forever if the child is blocking on write for the pipe buffer to clear. wait frees up resources associated with a dead child. If your child is not dead this will wait until it is.

    Another thing: use fwrite for writing blocks of data (arrays of structs) to a file descriptor. fread for reading. You can convert a file descriptor to a FILE* with fdopen.

    int main(int argc, char *argv[])
    {
    pList=(Process_list *)malloc((M+1)*sizeof(Process_list));
    /*Correct allocation is checked*/
    
    int i;
    pid_t pid;
    for (i=0 ; i<M ; i++)       /*M clones*/
    {
        /*Fork and pipe creation*/
        if(pipe(pList[i].fd)) {
            perror("pipe");
            return -1;
        }
        pid = fork ();
    
        /*If it's the child*/
        if ( pid == 0 ) 
        {
            pList[i].pid=pid; 
            printf("CHILD %d: %d a %d\n",i, pList[i].first,pList[i].last);
    
            /*A function here computes stats and saves them OK in the struct*/
            /*(e.g. min is saved in pList[i].stats.max, when printed it's a reasonable value)*/
    
            /*this just closes your read end of the pipe*/
            close(pList[i].fd[0]);
            /*ret1=0 => OK */
    
            FILE* fp = fdopen(pList[i].fd[1], "w");
            while (!fwrite(&(pList[i].stats), sizeof(Stats), 1, fp) && !feof(fp));
            if (feof(fp)) {
                fclose(fp);
                fprintf(stderr, "reader closed his end of the pipe\n");
                return -1;
            }
    
            // sends eof to reader
            fclose(fp);
    
            printf("%d bytes written successfully",sizeof(Stats));
            /*TROUBLE HERE! This isn't even printed. sizeof(Stats)=24 which I think is OK*/
    
            exit(EXIT_SUCCESS);
        }
    
        /*Parent*/
        else if ( pid > 0 )
        {
    
            // this is actually nessesary to ensure that the pipe properly closes
            close(pList[i].fd[1]);
            FILE* fp = fdopen(pList[i].fd[0], "r");
    
            if (!fread(&(pList[i].stats), sizeof(Stats), 1, fp)) {
                fprintf(stderr, "writer sent eof, but not enough data\n");
                return -1;
            }
    
            fclose(fp);
    
            printf("[p]Mín: %lf\n Max: %lf\nAverage: %lf\n",pList[i].stats.min,pList[i].stats.max,pList[i].stats.avg);
            /*Everything printed here  = 0.0000000000*/
    
            wait(0);
    
        }
    
        else /*fork error*/
                return -1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm in the process of learning Databases and SQL. From what I have read
I have encountered a major problem for myself in the learning process of WPF
I am in the process of learning backbone.js and have written some very simple
I'm still in the process of learning how to make android apps. I'm currently
In the process of learning WCF. To examplify, lets say I have 3 assemblies
I'm using firefox as my default browser for my basic webdev learning process and
Im the process of learning C++. I got stuck debugging some of my code
On the process of learning Assembly i got one question If i do the
I am in the process of learning about big oh notation. For the code
I'm in the process of learning jQuery and the tutorial I'm following uses Dreamweaver

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.