In: Computer Science
please use c
write a program that utilizes pipes for Interprocess Communication (IPC). The program will create a pipe(s) to allow the parent and child to communicate.
The parent process will generate a random number based on the pid of the child process created. The parent will send this random number to the child and wait for a response (Received) from the child. After receiving a response from the child, the parent will send another message to the child telling the child it is okay to end. This message should be “BYE”.
The child process should wait to receive the random number from the parent. After receiving the random number, it should send a response back to the parent. This message should be “Received”. The child should then wait for another message (BYE) from the parent indicating it is okay for the child to end.
Both processes should print to standard output the message it will write to the pipe and any message it receives from the pipe. Each message printed should be the process (“Parent” or “Child”) and if the message will be sent to the pipe or if it was received by the pipe. Example output might be similar to this:
Parent sending to pipe: 784392
Child received from pipe: 784392
Child sending to pipe: Received
Parent received from pipe: Received
Parent sending to pipe: BYE
Child received from pipe: BYE
Make sure you follow good programming practices, such as the use of functions, closing file descriptors as soon as not needed, in-line comments, error checking, freeing dynamic memory, etc.
use any programming language that allows the creation of new processes similar to the fork() system call in C and the use of pipes similar to the pipe() system call in C.
#include<stdio.h>
#include<unistd.h>
int main() {
int pipefds1[2], pipefds2[2];
int returnstatus1, returnstatus2;
int pid;
char pipe1writemessage[20] = "Received";
char pipe2writemessage[20] = "BYE";
char readmessage[20];
returnstatus1 = pipe(pipefds1);
if (returnstatus1 == -1) {
printf("Unable to create pipe 1 \n");
return 1;
}
returnstatus2 = pipe(pipefds2);
if (returnstatus2 == -1) {
printf("Unable to create pipe 2 \n");
return 1;
}
pid = fork();
if (pid != 0) // Parent process {
int sleep_seconds = (rand() % 10) + 1;
close(pipefds1[0]); // Close the unwanted pipe1 read side
close(pipefds2[1]); // Close the unwanted pipe2 write side
printf("Parent sending to pipe %d\n", sleep_seconds);
write(pipefds1[1], sleep_seconds, sizeof(sleep_seconds));
printf("Parent sending to pipe %s\n", pipe1writemessage);
write(pipefds1[1], pipe1writemessage, sizeof(pipe1writemessage));
read(pipefds2[0], readmessage, sizeof(readmessage));
printf("Parent received from pipe %s\n", readmessage);
} else { //child process
close(pipefds1[1]); // Close the unwanted pipe1 write side
close(pipefds2[0]); // Close the unwanted pipe2 read side
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("Child received from pipe %d\n", readmessage);
read(pipefds1[0], readmessage, sizeof(readmessage));
printf("Child received from pipe %s\n", readmessage);
printf("Child sending to pipe %s\n", pipe2writemessage);
write(pipefds2[1], pipe2writemessage, sizeof(pipe2writemessage));
}
return 0;