Search code examples
cfork

3 forks and 6 pipes


I'm writing my first fork-pipe code in order to learn how it works.

The point is that I want to create 3 children that will communicate with his dad and each child will perform different tasks.

This is the code I'm working on:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

int main(void)
{
    int nbytes;
    int     fd1[2], fd11[2]; /*fd1-->child1*/
    int     fd2[2], fd21[2]; /*fd2-->child2*/
    int     fd3[2], fd31[2]; /*fd3-->child3*/
    pid_t   child1, child2, child3;
    char    string[] = "Hello, world!\n";
    char    string1[] = "Shutting Down\n";
    char    readbuffer[80];

    printf("child1-->%d\n", child1);
    if (child1==0 && child2==0 && child3==0){

        printf( "   HOLA-> pid %d\n", getpid() );

        if(pipe(fd1)==-1 || pipe(fd11)==-1){ //Creating Pipes for son 1
            perror("pipe");
            exit(-1);
        }
        if(pipe(fd2)==-1 || pipe(fd21)==-1){ //Creating Pipes for son 2
            perror("pipe");
            exit(-1);
        }
        if(pipe(fd3)==-1 || pipe(fd31)==-1){ //Creating Pipes for son 3
            perror("pipe");
            exit(-1);
        }

        if((child1 = fork()) == -1){
            perror("fork");
            exit(-1);
        }

        /*Creating child 2 and 3*/
        if((child2 = fork()) == -1){
            perror("fork");
            exit(-1);
        }
        if((child3 = fork()) == -1){
            perror("fork");
            exit(-1);
        }
    }
    if(child1==0){ // Child 1

        close(fd2[0]);close(fd21[0]);close(fd3[0]);close(fd31[0]);
        close(fd2[1]);close(fd21[1]);close(fd3[1]);close(fd31[1]);
        close(fd1[1]); /* Son process closes up output side of pipe */
        close(fd11[0]); /* Son process closes up input side of pipe */

        /* Read in a string from the pipe */
        read(fd1[0], readbuffer, sizeof(readbuffer));
        printf("I'm SON1 -->Received string: %s\n", readbuffer);

        printf("LOADING...\n");         
        sleep(2);

        write(fd11[1], "SON1-->Shutting Down\n", (strlen("SON1-->Shutting Down\n")+1));
        printf("I'm SON1 -->send string & shutdown\n");

        exit(0);

    }else if(child2==0){ // Child 2

        close(fd1[0]);close(fd11[0]);close(fd3[0]);close(fd31[0]);
        close(fd1[1]);close(fd11[1]);close(fd3[1]);close(fd31[1]);
        close(fd2[1]); /* Son process closes up output side of pipe */
        close(fd21[0]); /* Son process closes up input side of pipe */

        /* Read in a string from the pipe */
        read(fd2[0], readbuffer, sizeof(readbuffer));
        printf("I'm SON2 -->Received string: %s\n", readbuffer);

        printf("LOADING...\n");         
        sleep(2);

        write(fd21[1], "SON2-->Shutting Down\n", (strlen("SON2-->Shutting Down\n")+1));
        printf("I'm SON2 -->send string & shutdown\n");

        exit(0);    

    }else if(child3==0){ // Child 3

        close(fd2[0]);close(fd21[0]);close(fd1[0]);close(fd11[0]);
        close(fd2[1]);close(fd21[1]);close(fd1[1]);close(fd11[1]);
        close(fd3[1]); /* Son process closes up output side of pipe */
        close(fd31[0]); /* Son process closes up input side of pipe */

        /* Read in a string from the pipe */
        read(fd3[0], readbuffer, sizeof(readbuffer));
        printf("I'm SON3 -->Received string: %s\n", readbuffer);

        printf("LOADING...\n");         
        sleep(2);

        write(fd31[1], "SON3-->Shutting Down\n", (strlen("SON3-->Shutting Down\n")+1));
        printf("I'm SON3 -->send string & shutdown\n");

        exit(0);    

    }else{ // DAD Code      

        close(fd1[0]);close(fd2[0]);close(fd3[0]); /* Father process closes up input side of pipe */
        close(fd11[1]);close(fd21[1]);close(fd31[1]); /* Father process closes up output side of pipe */

        /* Send "string" through the output side of pipe */
        write(fd1[1], string, (strlen(string)+1));
        printf("I'm the father -->send string to SON1\n");

        write(fd2[1], string, (strlen(string)+1));
        printf("I'm the father -->send string to SON2\n");

        write(fd3[1], string, (strlen(string)+1));
        printf("I'm the father -->send string to SON3\n");

        read(fd11[0], readbuffer, sizeof(readbuffer));
        printf("I'm the father -->SON1 Received string: %s", readbuffer);

        read(fd21[0], readbuffer, sizeof(readbuffer));
        printf("I'm the father -->SON2 Received string: %s", readbuffer);

        read(fd31[0], readbuffer, sizeof(readbuffer));
        printf("I'm the father -->SON3 Received string: %s", readbuffer);

        sleep(2);
        printf("Father shutting down\n");
        exit(0);
    }

    return(0);
}

My problem is that when I execute the code, it appears something like that:

I'm the father -->send string to SON1
I'm the father -->send string to SON2
I'm the father -->send string to SON3
I'm SON3 -->Received string: Hello, world!

LOADING...
I'm SON2 -->Received string: Hello, world!

LOADING...
I'm SON1 -->Received string: Hello, world!

LOADING...
I'm SON3 -->send string & shutdown
I'm SON2 -->send string & shutdown
I'm SON1 -->send string & shutdown
I'm the father -->SON1 Received string: SON1-->Shutting Down
I'm the father -->SON2 Received string: SON2-->Shutting Down
I'm the father -->SON3 Received string: SON3-->Shutting Down
Father shutting down

V:~$ I'm SON2 -->Received string: 
LOADING...
I'm SON1 -->Received string: 
LOADING...
I'm SON1 -->Received string: 
LOADING...
I'm SON1 -->Received string: 
LOADING...

My question is: Where the four calls to the sons come from? What am I missing?


Solution

  • Ok I got it now. Your mistake is that you are forking the following way:

    if ((child1 = fork() == -1) {
       // handle error
    }
    
    if ((child2 = fork()) == -1)
    ..
    ..
    ..
    

    After fork, a new process copy is created. It will continue from same position as the parent. It will also execute the next fork
    After first fork, a new child is created. Then after second fork you will have 4 processes which will execute the 3rd fork. Ending up with 8 processes. Assuming fork sys call didn't fail.

    If you want to create only 3 processes then it should be this way:

    Child1 = fork();
    if (Child1 == -1) { // handle error }
    if (Child1 == 0) { // child process }
    if (Child1 > 0) 
    { 
        Child2 = fork();
        if (Child2 == -1) ...
        if (child2 == 0)...
        if (child2 > 0)
        {
           Child3 = fork();....
        }
      .
      .