Question

In: Computer Science

please use c write a program that utilizes pipes for Interprocess Communication (IPC). The program will...

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.

Solutions

Expert Solution

#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;

Related Solutions

Write a program to demonstrate the implementation of Inter Process Communication (IPC) using shared memory. Single...
Write a program to demonstrate the implementation of Inter Process Communication (IPC) using shared memory. Single line text.
C++ On linux Write a C++ program using the IPC. Declare two variables a=5 and b=6...
C++ On linux Write a C++ program using the IPC. Declare two variables a=5 and b=6 in writer process. Now their product (a*b) will be communicated to the reader process along with your name. The reader process will now calculate the square root of the number and display it along with your name.
Windows Interprocess communication. WM_CopyData IPC (data copy) - source code (c++) windows data copy IPC code
Windows Interprocess communication. WM_CopyData IPC (data copy) - source code (c++) windows data copy IPC code
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk...
Client AND server using names pipes (mkfifo) in C/C++ Write and client program that will talk to a server program in two separate terminals. Write the server program that can handle multiple clients (so threads will be needed) and with fork() and exec()
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine...
Write a complete C++ program that defines, implements, and utilizes a Lion class and a Pine class. Definition of classes from the implementation of the classes should be split. The program is made of five files: Lion.h, Lion.cpp, Pine.h, Pine.cpp, and TestLionPine.cpp. The components of Lion class are defined in the Lion.h file; however, all constructors and methods should not have any implementation code in this header file. All implementation code, i.e. constructor body and method body, should be written...
use linux or c program. please provide the answer in details. Write a program that will...
use linux or c program. please provide the answer in details. Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1: generate the input data (totally...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console,...
PLease use c++ Write a checkbook balancing program. The program will read in, from the console, the following for all checks that were not cashed as of the last time you balanced your checkbook: the number of each check (int), the amount of the check (double), and whether or not it has been cashed (1 or 0, boolean in the array). Use an array with the class as the type. The class should be a class for a check. There...
please write it in printf and scanf please Please write it in c# program please and...
please write it in printf and scanf please Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT