Question

Write a C program that does the following. Parent sends “Hello, there”, child responds with “Hi,...

Write a C program that does the following. Parent sends “Hello, there”, child responds with “Hi, I’m child”. Then the parent sends “Are you available now?” and the child responds with “Yes, I’m”. Make appropriate usage of pipes.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <stdio.h>

#include <unistd.h>

#include <sys types.h="">

#include <stdlib.h>

#include <memory.h>

int main() {

//for parent to child

int parentToChild[2];

//for child to parent

int childToParent[2];

//string data


char message1[] = "Hello, there";

char message2[] = "Hi, I’m child";

char message3[] = "Are you available now?";

char message4[] = "Yes, I’m";

char readBuffer[120];

pipe(parentToChild);

pipe(childToParent);

int processId , bytes;

if((processId = fork())== -1){

printf("error while creating a child process");

}


if(processId !=0 ){

//parent process here

//send message from parent to child

close(parentToChild[0]);

write(parentToChild[1] , message1 , strlen(message1)+1);

//recieve message from child

close(childToParent[1]);

bytes = read(childToParent[0] , readBuffer , sizeof(readBuffer));

printf("%s", readBuffer);

// exit(1);

}







if(processId == 0){

//child process here

//send message from child to parent

close(childToParent[0]);

write(childToParent[1] , message2 , strlen(message2)+1);

//recive message from parent

close(parentToChild[1]);

bytes = read(parentToChild[0] , readBuffer , sizeof(readBuffer));

printf("%s",readBuffer);

// exit(1);

}

if(processId !=0 ){

//parent process here

//send message from parent to child

close(parentToChild[0]);

write(parentToChild[1] , message3 , strlen(message3)+1);

//recieve message from child

close(childToParent[1]);

bytes = read(childToParent[0] , readBuffer , sizeof(readBuffer));

printf("%s", readBuffer);

// exit(1);

}

if(processId == 0){

//child process here

//send message from child to parent

close(childToParent[0]);

write(childToParent[1] , message4 , strlen(message4)+1);

//recive message from parent

close(parentToChild[1]);

bytes = read(parentToChild[0] , readBuffer , sizeof(readBuffer));

printf("%s",readBuffer);

// exit(1);

}


return 0;

}

Add a comment
Know the answer?
Add Answer to:
Write a C program that does the following. Parent sends “Hello, there”, child responds with “Hi,...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT