Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an inter process communication program in C language to exchange message b

ID: 3814099 • Letter: W

Question

Write an inter process communication program in C language to exchange message between parent process and child processes. This Assignment is aimed to learn fork() and pipe() functionalities.

You are expected to create a program where parent process P1 should send a message “Hi, I am your parent from CIS 345 ” to the child process P2, P3. To create a child process use fork() functionality. Child processes should send the acknowledgement ”Message from Child 2: RECEIVED” to the parent process after receiving the message.

To exchange/communicate the message use pipe() functionality. pipe() is unidirectional data exchange channel.

Explanation / Answer

code:

/**
* Details:
* Fork is used to create child processes
* Pipe has 2 File Descriptors, 0 is used for reading. 1 is used for writing
* close() locks the File Descriptor of the pipe for that child process
*/
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
    // We use three pipes
    // First pipe to send message from parent
    // Second and third pipes to send acknowledgement from child

    int P1[2]; // Used to store two ends of first pipe
    int P2[2]; // Used to store two ends of second pipe
    int P3[2]; // Used to store two ends of third pipe
    //storing messages to be sent or acknowledged
    char msg_P1[] = "Hi, I am your parent from CIS 345";
    char ack_P2[] = "Message from Child 2: RECEIVED";
    char ack_P3[] = "Message from Child 3: RECEIVED";
    pid_t P2_fork;
    pid_t P3_fork;
    //creating pipes
    if (pipe(P1)==-1)
    {
        fprintf(stderr, "Pipe P1 Failed" );
        return 1;
    }
    if (pipe(P2)==-1)
    {
        fprintf(stderr, "Pipe P2 Failed" );
        return 1;
    }
    if (pipe(P3)==-1)
    {
        fprintf(stderr, "Pipe P3 Failed" );
        return 1;
    }
    //creating fork for child P2
    P2_fork = fork();

    if (P2_fork < 0)
    {
        fprintf(stderr, "fork P2 Failed" );
        return 1;
    }

    // Parent process
    else if (P2_fork > 0)
    {
        char receive_P2[] = "";

        close(P1[0]); // Close reading end of first pipe

        // Write input string and close writing end of first pipe.
        write(P1[1], msg_P1, strlen(msg_P1)+1);
        close(P1[1]);

        // Wait for child to send a string
        wait(NULL);

        close(P2[1]); // Close writing end of second pipe

        // Read string from child, print it and close reading end.
        read(P2[0], receive_P2, 100);
        printf("%s ", receive_P2);
        close(P2[0]);
    }

    // child process
    else
    {
        close(P1[1]); // Close writing end of first pipe

        // Read a string using first pipe
        char receive_P1_P2[100];
        read(P1[0], receive_P1_P2, 100);

        // Close both reading ends
        close(P1[0]);
        close(P2[0]);

        // Write concatenated string and close writing end
        write(P2[1], ack_P2, strlen(ack_P2)+1);
        close(P2[1]);

        exit(0);
    }
    //creatin fork for child P3
    P3_fork = fork();

    if (P3_fork < 0)
    {
        fprintf(stderr, "fork P3 Failed" );
        return 1;
    }

    // Parent process
    else if (P3_fork > 0)
    {
        char receive_P3[] = "";

        close(P1[0]); // Close reading end of first pipe

        // Write input string and close writing end of first pipe.
        write(P1[1], msg_P1, strlen(msg_P1)+1);
        close(P1[1]);

        // Wait for child to send a string
        wait(NULL);

        close(P3[1]); // Close writing end of second pipe

        // Read string from child, print it and close reading end.
        read(P3[0], receive_P3, 100);
        printf("%s ", receive_P3);
        close(P3[0]);
    }

    // child process
    else
    {
        close(P1[1]); // Close writing end of first pipe

        // Read a string using first pipe
        char receive_P1_P3[100];
        read(P1[0], receive_P1_P3, 100);

        // Close both reading ends
        close(P1[0]);
        close(P3[0]);

        // Write concatenated string and close writing end
        write(P3[1], ack_P3, strlen(ack_P3)+1);
        close(P3[1]);

        exit(0);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote