Search code examples
clinuxpipefork

Navigation function is printing all its output twice


When I call the functions individually in main they work fine, just printing the desired message once. Calling both functions, causes navigation to print its messages twice.

I have looked up different solution. I tried using fflush(stdout) to no avail, i tried using a condition in the main to ensure they run individually.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <wait.h>
#include "RAND_API.h"
#define MAXLEN 500

void lifeSupport();
void navigation();

int main()
{


    lifeSupport();
    navigation();
    return 0;
}

void lifeSupport()
{
        //Create pipe


        ret = write( myPipe[1], lifeSup1, strlen( lifeSup1 ) + 1 );
        //sleep( getRandExponential() * 10 );
        ret = write( myPipe2[1], lifeSup2, strlen( lifeSup2 ) + 1 );
        //sleep( getSRand() * 6 );
        sleep( getRandExponential() * 5 );
        ret = write( myPipe3[1], lifeSup3, strlen( lifeSup3 ) + 1 );
        sleep( getRandExponential() * 4 );
        ret = write( myPipe4[1], lifeSup4, strlen( lifeSup4 ) + 1 );
        close( myPipe[1] );
        close( myPipe2[1] );
        close( myPipe3[1] );
        close( myPipe4[1] );
    }
    else
    {
        //wait(NULL);
        close( myPipe[1] );
        close( myPipe2[1] );
        close( myPipe3[1] );
        close( myPipe4[1] );
        ret = read( myPipe[0], buffer, MAXLEN );
        printf( "%s", buffer );
        ret = read( myPipe2[0], buffer, MAXLEN );
        printf( "%s", buffer );
        ret = read( myPipe3[0], buffer, MAXLEN );
        printf( "%s", buffer );
        ret = read( myPipe4[0], buffer, MAXLEN );
        printf( "%s", buffer );
    }

}

void navigation()
{
       //create pipe

        ret = write( apipe[1], nav1, strlen( nav1 ) + 1 );
        ret = write( apipe2[1], nav2, strlen( nav2 ) + 1 );
        //sleep( getSRand() * 6 );
        ret = write( apipe3[1], nav3, strlen( nav3 ) + 1 );

        close( apipe[1] );
        close( apipe2[1] );
        close( apipe3[1] );

    }
    else
    {
        close( apipe[1] );
        close( apipe2[1] );
        close( apipe3[1] );

        ret = read( apipe[0], buff, MAXLEN );
        printf( "%s\n", buff );

        ret = read( apipe2[0], buff, MAXLEN );
        printf( "%s\n", buff );

        ret = read( apipe3[0], buff, MAXLEN );
        printf( "%s\n", buff );

        close( apipe[0] );
        close( apipe2[0] );
        close( apipe3[0] );

    }

}

output:

Life support system initiiated
Adjusting breathing gas levels
Adjusting enviroment
Life support system terminating
Initiating navigation system

Initiating navigation system
Making adjustments


Making adjustments
Adjustments done. Navigation system terminating.


Adjustments done. Navigation system terminating.

Expected output:

Life support system initiiated
Adjusting breathing gas levels
Adjusting enviroment
Life support system terminating
Initiating navigation system
Making adjustments

Adjustments done. Navigation system terminating.

Solution

  • When you call lifeSupport(), it forks a child. The child writes messages to all the myPipeX pipes, the parent reads from them and prints the messages, then both the parent and child return to main().

    Then both processes call navigation(). They each create another child. The two children write to the apipeX pipes, the two parents read from them, and they each print the messages. So you get two copies of all the messages from navigation().

    In each function, the child should call exit() after it's done, rather than returning. Only the parent should return to main().