Question

In: Computer Science

Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then...

Write a “C” program(Linux) that creates a pipe and forks a child process. The parent then sends the following message on his side of the pipe “I am your daddy! and my name is <pid-of the-parent-process>\n”, the child receives this message and prints it to its stdout verbatim. The parent then blocks reading on the pipe from the child. The child writes back to its parent through its side ofthe pipe stating “Daddy, my name is <pid-of-the-child>”. The parent then writes the message received from the child to its stdout. Make sure the parent waits on the child exit as to not creating orphans or zombies. Include Makefile.

Solutions

Expert Solution

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
   // We use two pipes
   // First pipe to send input string from parent
   // Second pipe to send concatenated string from child

   int fd1[2]; // Used to store two ends of first pipe
   int fd2[2]; // Used to store two ends of second pipe


  
   pid_t p;

   if (pipe(fd1)==-1)
   {
       fprintf(stderr, "Pipe Failed" );
       return 1;
   }
   if (pipe(fd2)==-1)
   {
       fprintf(stderr, "Pipe Failed" );
       return 1;
   }

  
   p = fork();

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

   // Parent process
   else if (p > 0)
   {
       char concat_str[100];
       char input_str[]="I am your daddy! and my name is ";
char s[100];
       close(fd1[0]); // Close reading end of first pipe
       int i=getpid();
sprintf(s, "%d", i);
strcat(input_str,s);
       // Write input string and close writing end of first
       // pipe.
  
       write(fd1[1], input_str, strlen(input_str)+1);
      
close(fd1[0]); // Close reading end of first pipe
       close(fd1[1]); // Close writing end of first pipe

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

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

       // Read string from child, print it and close
       // reading end.
       read(fd2[0], concat_str, 100);
       printf("string received from child in parent Process: %s\n", concat_str);
       close(fd2[0]);
   }

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

       // Read a string using first pipe
       char parent_str[100];
       read(fd1[0], parent_str, 100);
  
       printf("string received from parent in child process : %s\n", parent_str);

       // Close both reading ends
       close(fd1[0]);
       close(fd2[0]);
       char child_str[]="Daddy, my name is ";
char s[100];
       int i=getpid();
sprintf(s, "%d", i);
strcat(child_str,s);
       // Write concatenated string and close writing end
       write(fd2[1], child_str, strlen(child_str)+1);
       close(fd2[1]);

       exit(0);
   }
}


Related Solutions

Write a C program that creates a toy scheduler for the child processes in part A....
Write a C program that creates a toy scheduler for the child processes in part A. This program takes as input the number of processes, and all of the PIDs that are being echoed. HINT: Look up redirecting echo output. The program will schedule the ”processes” (note that these are not true processes, this is a toy system. You are effectively only scheduling echo statements). The result of the scheduler will be to echo the PID and current system time...
You will write a C program to fork a child process. The child process will print...
You will write a C program to fork a child process. The child process will print the current time/date. The parent will wait for the child to complete. Here is how you can programmatically call the Linux date command from a C program. execlp("/bin/date","date",NULL); Your C file should be named myfork.c The binary will be named myfork.
C programming language The program first creates a child process CP. So, there are two processes:...
C programming language The program first creates a child process CP. So, there are two processes: The parent process does the following: a. compute the summary of all the even number from 1, 2, .. 1000, and output this summary; b. wait for the termination of the child process CP, then terminate; The child process does the following: a. compute the summary of all the odd number from 1, 2, .. 1000, and output this summary; b. terminates;
In C language, Write a program called minishell that creates two child processes: one to execute...
In C language, Write a program called minishell that creates two child processes: one to execute 'ls -al' and the other to execute ‘grep minishell.c’. After the forks, the original parent process waits for both child processes to finish before it terminates. The parent should print out the pid of each child after it finishes. The standard output of 'ls -al' process should be piped to the input to the 'grep minishell.c' process. Make sure you close the unnecessary open...
Write a C program which fork the process and have the child execute a process.You have...
Write a C program which fork the process and have the child execute a process.You have to implement void forkexec(char** argv).This takes in an array of strings representing arguments.The first argument is the filename of an executable (which will be given as a "./a").The remaining terms would be arguments for said executable.The array is null terminated and you need to fork your process.The child needs to call exec/execvp to execute the specified file with the specified arguments. Also have the...
Write a program (in C, or Java, or C++, or C#) that creates three new threads...
Write a program (in C, or Java, or C++, or C#) that creates three new threads (besides the already existing main thread) and synchronizes them in such a way that each thread displays it's thread id in turn for 5 iterations. The output of the program should look like this: Thread 1 - iteration no. 1 Thread 2 - iteration no. 1 Thread 3 - iteration no. 1 Thread 1 - iteration no. 2 Thread 2 - iteration no. 2...
Introduction Write in C++ at the Linux command line a program that is the same as...
Introduction Write in C++ at the Linux command line a program that is the same as the previous collection app project but now uses a class to store the items and also can save the items to a file that can be read back into the array by the user when the program is re-started. You can use your project 1 submission as a starting point or you can do something new as long as it meets the listed requirements....
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...
Write a program in C or C++ that spawns three child processes. The first child sends...
Write a program in C or C++ that spawns three child processes. The first child sends a signal to the parent (you'll need the parent's pid!), which the parent shall catch. The second child waits for 10 seconds and then terminates. Once the parent detects that both of these has happened, it should signal the third child to terminate.
please write in c using linux or unix Write a program that will simulate non -...
please write in c using linux or unix 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 10 jobs) and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT