DarkPain
DarkPain

Reputation: 45

Multiple fork() Children with pipes

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!

Upvotes: 1

Views: 3117

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 755010

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.

Upvotes: 1

Related Questions