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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:50:25+00:00 2026-05-26T11:50:25+00:00

I am trying to make a program in C++ that is creating childs with

  • 0

I am trying to make a program in C++ that is creating childs with fork().
It should take childs number from argv and create these childs,every child is creating another one and communicate each other with pipes ….

Example ./a.exe 2

**OUTPUT**
P1 exists
P2 created
Write message: Hello
P1 sending message (“Hello”) to P2
P2 received message (“Hello”) from P1

I get the number from argv, creating the correct number of childs(i think),functions for read and write to pipe,all are OK.
But I have trouble to make it with multiple child!!

  1. My first problem is that if i put more than 2 in argv, child order
    is not ascending as it should!(Created shows later)
  2. But my big problem is that if i write
    a message with 2 words i can only read first word before space!!I am using scanf.

SOME OF MY CODE

//GETTING,CHECKING ARGV
//OPENING PIPE

         pid=fork();
         if (pid!=0){


                      waitpid(pid,&child_status,0);
                      printf("\n\n****Parent Process:ALL CHILD FINISHED!****");

                     }
          else if (pid==0)
          {

                 printf("\n P%d Exists \n",i); 
                     close (mypipe[READ_END]);
                      write(mypipe[WRITE_END], msg, 256);  /* write pipe*/                   
                       close (mypipe[WRITE_END]);          
                    printf("Write a message: \n");
                     scanf("%s",msg);
                       printf("\n P%d sending message: '%s' to P%d \n",i,msg,i+1); 

         do{
            childpid[i] = fork();
           if (childpid[i] > 0){   


                       /* wait for child to terminate */
                             waitpid(childpid[i],&child_status,0);  
                 }
           else if (childpid[i] == 0)
           {
               /*child process childpid = 0*/
               printf("\n P%d Created \n",i+1);
               close (mypipe[WRITE_END]);   
                 read(mypipe[READ_END], msg, 256);  /* read pipe */
                 close (mypipe[READ_END]);
                 printf("\n P%d received message: '%s' from P%d \n",i+1,msg,i);          
                 exit(0);
                 return;

            }
           else{
                   printf("Child Fork failed");
                   }
                      i++;
                   }

                   while (i<x);


                   }
                   else{
                   printf("Fork failed");
                   }    
                          }

I had read other similar questions and tried many things but didn’t help!!
Any help will be appreciated!!
Thank you!

  • 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-26T11:50:25+00:00Added an answer on May 26, 2026 at 11:50 am

    You have multiple problems in the code you show, and there is code missing as diagnosed in a comment.

    One problem is that you close both ends of the only pipe in the first child, and then close it a second time (but fortunately you ignore the error). More seriously, any subsequent children only have a closed pipe to work with, and that isn’t going to help them.

    Another problem is that the parent process doesn’t close the pipe; it may not matter in this program since you don’t try reading to EOF, but in most programs, it would matter.

    Another problem is that you don’t read the message from standard input until after you’ve tried to write an uninitialized message to the pipe. It isn’t immediately clear whether you should hook up the standard input of the child to the read end of the pipe. You write 256 bytes to the pipe even if the message is not that long.

    You run into problems with multiple words because scanf("%s", msg) is designed to read up to the first white space (blank, newline, tab, etc). In this context, I’d probably use fgets() to read the information.

    I think you need a new pipe for each child. You should probably be error checking each system call, but that is easier if you have a simple error reporting function, like this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdarg.h>
    #include <errno.h>
    
    static char *arg0 = 0;
    
    static void err_exit(const char *fmt, ...)
    {
        int errnum = errno;
        va_list args;
        va_start(args, fmt);
        fprintf(stderr, "%s (%d): ", arg0, (int)getpid());
        vfprintf(stderr, fmt, args);
        va_end(args);
        if (errnum != 0)
            fprintf(stderr, "Error %d: %s\n", errnum, strerror(errnum));
        exit(1);
    }
    
    int main(int argc, char **argv)
    {
        int i=1;
        int x;
        int pid, mypipe[2];
        pid_t childpid[256];
        int child_status;
        char msg[256];
    
        arg0 = argv[0];
    
        if (argc != 2 || (x = atoi(argv[1])) <= 0)
            err_exit("Usage: %s num-of-children\n", argv[0]);
        if (pipe(mypipe) < 0)
            err_exit("pipe error\n");
    

    You have some serious thinking about how you want to organize things so that a message is relayed from one process to the next. This is just a start…the error reporting function may be of some use to you.

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

Sidebar

Related Questions

I am trying to make a program that grabs all the links from an
I'm trying to make a program that check the ISBN number and sees if
I am trying to make a program that automatically corrects the perspective of a
I'm trying to make a program that reads the value at a certain address.
Hey, I am trying to make a program that minimises any program to the
I'm trying to make a small program that could intercept the open process of
I am trying to make a program in Java that checks for three specific
Okay so I'm trying to make a little gag program that will run away
I'm trying to make a program in Visual C# that has my one created
I'm trying to make a program that solves the logic wheels puzzle. I construct

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.