Question

C programing language pretend parent process is reading data and child process is writing data. explain...

C programing language

pretend parent process is reading data and child process is writing data.

explain with pipe and fork function.

Homework Answers

Answer #1

// C program to demonstrate use of fork() and pipe()
#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

   char fixed_str[] = "xyz.org";
   char input_str[100];
   pid_t p;

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

   scanf("%s", input_str);
   p = fork();

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

   // Parent process
   else if (p > 0)
   {
       char concat_str[100];

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

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

       // 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("Concatenated string %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 concat_str[100];
       read(fd1[0], concat_str, 100);

       // Concatenate a fixed string with it
       int k = strlen(concat_str);
       int i;
       for (i=0; i<strlen(fixed_str); i++)
           concat_str[k++] = fixed_str[i];

       concat_str[k] = '\0'; // string ends with '\0'

       // Close both reading ends
       close(fd1[0]);
       close(fd2[0]);

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

       exit(0);
   }
}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
A program where parent process counts number of vowels in the given sentence and child process...
A program where parent process counts number of vowels in the given sentence and child process will count number of words in the same sentence. The above programs should use system calls like fork and wait.
Using C, write a program that will create two child processes with one child process then...
Using C, write a program that will create two child processes with one child process then using “pipe” to send “i love you” to another child process.
Please complete the following code for challenge.c in C according to the instructions in the comments....
Please complete the following code for challenge.c in C according to the instructions in the comments. Further instructions are below in INSTRUCTIONS.txt challenge.c #include "challenge.h" // goal: fork the process and have the child execute a process // param argv: the argument vector for the process to be executed // assumptions: // the first argument of argv is the file name of the executable // argv is null terminated // // TODO: complete the function // fork // exec (child),...
Some students are good in mathematics and others are better at reading or writing. The question...
Some students are good in mathematics and others are better at reading or writing. The question is whether there is any relationship between a student’s ability in math and his/her ability in reading or writing. The SAT, a standardized test for college admissions that is widely used in the United States, has three sections, Math, Critical Reading, and Writing. The table below contains SAT Math, Writing, and Critical Reading test scores for 20 randomly chosen students accepted by a university....
Write a C program that creates (or spawns) a child process to execute the Unix's command...
Write a C program that creates (or spawns) a child process to execute the Unix's command (echo) with a string as the command-line argument; while the program itself (the parent process) will execute another Unix command less with a filename as the command-line argument. If you aren't sure how the echo and less commands are meant to behave, experiment with them by running them directly from the command line first.
A common concern of parents is the language development of their child. In some cases, a...
A common concern of parents is the language development of their child. In some cases, a language deficit is an isolated problem; however, in other cases the language deficit could be a symptom of a cognitive deficit such as autism. Say a parent brings 5-yr-old john for an assessment, and john is given a Language test and a seperate Intelligence test. His test score on the Language test reveals 96 correct answers. His test score on the seperate Intelligence test...
Program Assignment 1: Process Management Objective: This program assignment is given to the Operating Systems course...
Program Assignment 1: Process Management Objective: This program assignment is given to the Operating Systems course to allow the students to figure out how a single process (parent process) creates a child process and how they work on Unix/Linux(/Mac OS X/Windows) environment. Additionally, student should combine the code for describing inter-process communication into this assignment. Both parent and child processes interact with each other through shared memory-based communication scheme or message passing scheme. Environment: Unix/Linux environment (VM Linux or Triton...
In the C programming language, how should I go about writing two programs (sender & reciever)...
In the C programming language, how should I go about writing two programs (sender & reciever) which create a reliable communication protocol to demonstrate reliable unicast using UDP? The sender program runs on one host and reads data files as input, and breaks the contents into UDP datagrams. The receiver should simulate unreliable communication by randomly dropping a percentage of incoming packets. Acknowledgements, retransmission, packet resequencing, and timeouts should all be taken into consideration.
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int...
Please explain what will be printed out at LINE1 and LINE2? #include <stdio.h> #include <types.h> int data[4] = {1,3,5,7}; void *runner (void *param); int main(int argc,char *argv[]) { pid_t pid; pthread_t tid; pid = fork(); if (pid==0){ pthread_create(&tid,NULL,runner,NULL); pthread_join(tid,NULL); for(int i=0,i<4,i++) printf("from child process, the values at i is %d",data[i]); } else if (pid>0){ wait(NULL); for(int i=0;i<4,i++) printf("from parent process, the values at i is %d",data[i]); } } void *runner(void *param){ for(int i=0;i<4,i++) data[i]+=3*i; pthread_exit(0); } } } } }
Explain the process of creating flat (text) files and how to store/retrieve data in c++
Explain the process of creating flat (text) files and how to store/retrieve data in c++
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT