Question

In: Computer Science

Creating a pipe in C language requires simple command which is a s follows. SYSTEM CALL:...

Creating a pipe in C language requires simple command which is a s follows.

SYSTEM CALL: pipe();                                                         

PROTOTYPE: int pipe( int fd[2] );                                            

    RETURNS: 0 on success                                                       

             -1 on error: errno = EMFILE (no free descriptors)                 

                                  EMFILE (system file table is full)           

                                  EFAULT (fd array is not valid)               

NOTE: fd[0] is set up for reading, fd[1] is set up for writing

Design a simple C program using ordinary pipes in which a parent and child processes exchange greeting messages. For example, the parent process may send the message “Hello Child Process”, and the child process may return “Hi Parent Process”. Use UNIX pipes to write this program. You need to have bidirectional behavior in your program for which you will need two pipes.

Solutions

Expert Solution

Idea : We need to implement two way communication between Child and Parent process using Pipe

Approach :

  1. Initialize Pipe_A for the Child process to write and Parent process to read.
  2. Initialize Pipe_B for the Parent process to write and Child process to read.
  3. Since, a single pipe is for uni-directional purpose . So, at one time only one functionality of pipe will be used . Thus, close unwanted ends of the pipe from child and parents process.
  4. Parent process writes , "Hello Child Process" , and Child process reads this and print on screen.
  5. Child process writes , "Hello Parents Process" , and Parent process reads this and print on screen.

CODE:

#include<stdio.h>
#include<unistd.h>

int main()
{   //Initializing Pipe_A and Pipe_B
   int Pipe_A[2], Pipe_B[2];
   //Initializing status flags for both Pipes
   int return_status_pipe_a, return_status_pipe_b;
   int pid;
   //Messages That will be exchanged
   char parent_msg[30] = "Hello Child Process";
   char child_msg[30] = "Hello Parent Process";
   //Initializing Buffer variable
   char read_msg[30];
   //Creating Pipe_A and Checking whether it has been created or not
   return_status_pipe_a=pipe(Pipe_A);
   if(return_status_pipe_a == -1)
   {
       printf("Unable to Create Pipe A\n");
       return 1;
   }
   //Creating Pipe_B and Checking whether it has been created or not
   return_status_pipe_b=pipe(Pipe_B);
   if(return_status_pipe_b == -1)
   {
       printf("Unable to Create Pipe B\n");
       return 1;
   }
   pid = fork();
   //Parent Process
   if(pid !=0)
   {   //Disable Child Write
       close(Pipe_A[1]);
       //Disable Parent Read
       close(Pipe_B[0]);

       printf("Parent (Writing to Child) : %s\n",parent_msg);
       write(Pipe_B[1], parent_msg, sizeof(parent_msg));

       read(Pipe_A[0],read_msg,sizeof(read_msg));
       printf("Parent (Reading from Child) : %s\n",read_msg);
   }
   //Child Process  
   else
   {   //Disable Child Read
       close(Pipe_A[0]);
       //Disabe Parent Write
       close(Pipe_B[1]);

       read(Pipe_B[0],read_msg,sizeof(read_msg));
       printf("Child (Reading from Parent): %s\n", read_msg);
  
       printf("Child (Writing to Parent): %s\n",child_msg);
       write(Pipe_A[1],child_msg,sizeof(child_msg));
      
   }
   return 0;
}

Console Output :


Related Solutions

C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line...
C programming in Shell Implement a MS-DOS style pipe command. Make sure it allows for command-line arguments to be passed to the programs. You only need to support one pipe command at a time. For example, when you type ls | wc the shell should write the output of ls to a temporary file by redirecting standard output when running ls, and run wc, redirecting standard input so it reads from the temporary file written to by ls.
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT...
PROGRAM LANGUAGE IN C NOT C# or C++ KEEP IT SIMPLE EVEN IF IT IS REDUNDANT PLEASE. Problem: Driving Function driving(): Write a function driving(), that updates the odometer and fuel gauge of a car. The function will take in a reference to the variables storing the odometer and fuel gauge readings, along with a double representing the miles per gallon (mpg) the car gets and the number of miles the driver intends to go. The function should update the...
This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one.(in detail) #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include<errno.h> #include<stdlib.h> #include<unistd.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> int main(int argc, char *argv[]) { char ch; int src, dst; struct stat st; if(argc != 3) { printf("argument error \n"); printf("usage: ./a.out src dest \n"); exit(0); } if (stat(argv[1], &st) == -1) { perror("stat : "); exit(-1); } src = open(argv[1], O_RDONLY); if(src...
This code is an expression of cp command in c language. But I don't understand this...
This code is an expression of cp command in c language. But I don't understand this code very well. Please explain in notes one by one. #define SIZE 1024 #include<string.h> #include<stdio.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char *argv[]){ if(argc != 3){ perror("argument 부족\n"); exit(0); } struct stat frstatbuf; FILE* fr = fopen(argv[1], "r"); if(fr == NULL){ perror("read file 읽기 오류\n"); exit(0); } int frfd = fileno(fr); fstat(frfd, &frstatbuf); FILE* fw=fopen(argv[2], "w+"); int fwfd=fileno(fw); fchmod(fwfd,frstatbuf.st_mode&(S_IRWXU|S_IRWXG|S_IRWXO)); char buf[1024]; while(1){...
Week 4 Assignment HTML – Creating a Simple Web Page “HTML or HyperText Markup Language as...
Week 4 Assignment HTML – Creating a Simple Web Page “HTML or HyperText Markup Language as it is formally known is the main markup language for creating web pages and other information that can be displayed in a webbrowser”. It was created by Tim Berners-Lee in 1989 as a user friendly way of sharing information on the Internet. http://en.wikipedia.org/wiki/HTML Assignment Instructions - Create a simple HTML Web Page that includes hyperlinks to three of your favorite websites. Use the online...
In C++ Consider the language L = { s$s' : s is a possibly empty string...
In C++ Consider the language L = { s$s' : s is a possibly empty string of characters other than $ , s' = reverse( s )} as defi ned in Chapter 6 . Write a recognition algorithm for this language that uses both a queue and a stack. Thus, as you traverse the input string, you insert each character of s into a queue and each character of s' into a stack. Assume that each input string contains exactly...
Write a simple shell in C language and show some outputs.
Write a simple shell in C language and show some outputs.
C language and it has to be a while loop or a for loop. Use simple...
C language and it has to be a while loop or a for loop. Use simple short comments to walk through your code. Use indentations to make your code visibly clear and easy to follow. Make the output display of your program visually appealing. There is 10 points deduction for not following proper submission structure. An integer n is divisible by 9 if the sum of its digits is divisible by 9. Develop a program that: Would read an input...
(IN C LANGUAGE) The cost to become a member of a fitness center is as follows:...
(IN C LANGUAGE) The cost to become a member of a fitness center is as follows: (a) the senior citizens discount is 30% (b) if the membership is bought and paid for 12 or more months, the discount is 15% (c) if more than five personal training sessions are bought and paid for, the discount on each session is 20% Write a that determines the cost of a new membership. Your program must contain: a function that displays the general...
For which of the following reactions will the entropy of the system decrease? 2 C(s) +...
For which of the following reactions will the entropy of the system decrease? 2 C(s) + O2(g) → 2 CO(g) CaO(s) + CO2(g) → CaCO3(s) N2O4(g) → 2 NO2(g) KOH(s) → K+(aq) + OH–(aq) 2 NH3(g) → N2(g) + 3 H2(g)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT