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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:49:08+00:00 2026-06-01T08:49:08+00:00

This program should calculate the value of 2^1+2^2 + … + 2^10: #include <stdio.h>

  • 0

This program should calculate the value of 2^1+2^2 + … + 2^10:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <math.h>

#define N 10

// sommatoria per i che va da 1 a N di 2^i, ogni processo calcola un singolo valore

int main(int argc, char** argv)
{
    pid_t figli[N];
    unsigned int i;
    int status;
    int fd[N][2];
    int msg1=0,msg2;
    int risultato=0;
    bool padre=true;
    for(i=0;i<N && padre;i++)
    {
        pipe(fd[i]);
        figli[i]=fork();
        if(figli[i]<0)
        {
            fprintf(stderr,"Una fork ha fallito\n");
        }
        else if(figli[i]==0)
        {
            padre=false;
        }
        else
        {
            msg1=i+1;
            write(fd[i][1],&msg1,sizeof(int));
        }
    }
    if(!padre)
    {
        read(fd[i][0],&msg2,sizeof(int));
        msg2=pow(2.0,msg2);
        write(fd[i][1],&msg2,sizeof(int));
        exit(0);
    }
    else
    {
        for(i=0;i<N;i++)
        {
            read(fd[i][0],&msg2,sizeof(int));
            risultato+=msg2;
        }
    }
    if(padre)
        fprintf(stderr,"%d\n",risultato);
    return 0;
}

But when ie xecute the program, the father process prints 55.
Why?

  • 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-01T08:49:10+00:00Added an answer on June 1, 2026 at 8:49 am

    Interestingly enough, 55 is the sum of all the numbers from 1 to 10: that should give you an instant clue:

    pipe() creates a pipe, a unidirectional data channel that can be used for interprocess communication. The array pipefd is used to return two file descriptors referring to the ends of the pipe. pipefd[0] refers to the read end of the pipe. pipefd[1] refers to the write end of the pipe.

    Note that well: unidirectional. In other words, the padre is reading back the same values it wrote (hence the 55).

    You normally set up two pipes for bi-directional traffic, one for each direction. So I’ve double the number of pipes, using even ones for padre-to-child and odd ones for the other direction.

    In addition, your children continue with the padre loop whereas they should exit that loop immediately so their value of i is correct. You do have the loop exit based on padre but this happens after i has changed. You can either break where you set padre to false or simply i-- in the if(!padre) bit to restore i to the correct value for this child. I’ve done the latter.

    The following code (with markers showing what changed) works okay:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <assert.h>
    #include <stdbool.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <signal.h>
    #include <math.h>
    
    #define N 10
    
    int main(int argc, char** argv)
    {
        pid_t figli[N];
        unsigned int i;
        int status;
        int fd[N*2][2];  // CHANGED: two unidirectional pipes
        int msg1=0,msg2;
        int risultato=0;
        bool padre=true;
        for(i=0;i<N && padre;i++)
        {
            pipe(fd[i*2]);
            pipe(fd[i*2+1]); // ADDED: create second pipe
            figli[i]=fork();
            if(figli[i]<0)
            {
                fprintf(stderr,"Una fork ha fallito\n");
            }
            else if(figli[i]==0)
            {
                padre=false;
            }
            else
            {
                msg1=i+1;
                write(fd[i*2][1],&msg1,sizeof(int));  // CHANGED: pipe number
            }
        }
        if(!padre)
        {
            i--;  // ADDED: to restore i for the child
            read(fd[i*2][0],&msg2,sizeof(int));  // CHANGED: pipe number
            msg2=pow(2.0,msg2);
            write(fd[i*2+1][1],&msg2,sizeof(int));  // CHANGED: pipe number
            exit(0);
        }
        else
        {
            for(i=0;i<N;i++)
            {
                read(fd[i*2+1][0],&msg2,sizeof(int));  // CHANGED: pipe number
                risultato+=msg2;
            }
        }
        if(padre)
            fprintf(stderr,"%d\n",risultato);
        return 0;
    }
    

    This generates the correct answer of 2046, since 20 + 21 + ... 210 = 211 - 1 and, since you’re leaving out the two-to-the-zero term (equal to 1): 21 + 22 + ... 210 is 211 - 2 (211 = 2048).

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

Sidebar

Related Questions

I have this program that should execute a piece of code base on the
I am working on a bug in this program where it should be able
Consider this problem: I have a program which should fetch (let's say) 100 records
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
I write a program to verify this website: www.alipay.com which root CA cert should
How can I do this in a C# program? I'm pretty sure it should
Dear all,Now i have this question in my java program,I think it should be
The result of this program should be the same with 1 or 2 or
This program should return the weight of the shortest path from left to right(it
This program I use has it's own variables to set when you run it,

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.