Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

C Programming (1) Makes to pipes (one for this parent process to talk to a child process, and another for a child process to talk

C Programming

(1) Makes to pipes (one for this parent process to talk to a child process, and another for a child process to talk with this parent)

(2) Makes a child process (how do you do that?)

(2a) if you are the child process then close() the output end of the parent-to-child pipe, and the input end of the child-to-parent pipe

(2b) if you are the child process then redirect standard input (STDIN_FILENO) to the input end of the parent-to-child pipe, and redirect standard output (STDOUT_FILENO) to the output end of the child-to-parent pipe

(3) If you are the parent process close() the input end of the parent-to-child pipe, and the output end of the child-to-parent pipe

(4) Do this (assuming your child-to-parent pipe is called 'fromChild[]'): // FILE* inputPtr = fdopen(fromChild[0],"r");

// This lets you handle the input that the child process gives you with fgets() and the FILE* variable 'inputPtr', which will make the (5) easier.

(5) If you are the parent then in a loop:

(5a) ask the user for input (either a number or the text "end")

(5b) send whatever the user gives you to the child

(5c) If you sent "end" then quit the loop

(5d) otherwise, do an 'fgets()' on 'inputPtr' to get the answer that the child sent you. Print the answer for the user

(6) After the loop wait() for the child to finish.

This is what i have so far, its not working

#include

#include

#include

#include

#include

int main()

{

int to_child[2];

int from_child[2];

pid_t pid;

int num;

int status;

char buf[25];

int fd[2];

if (pipe(to_child) == -1 && pipe(from_child) == -1)

{

printf("Unable to create pipe ");

return 1;

}

pid = fork();

if (pid == 0)

{

close(to_child[1]);

close(from_child[0]);

STDIN_FILENO = dup(to_child[0]);

from_child[1] = dup(STDOUT_FILENO);

//

close(fd[1]);

}

else

{

close(to_child[0]);

close(from_child[1]);

FILE *inputPtr = fdopen(from_child[0], "r");

while (1)

{

printf("Parent> Enter an integer or \"end\" to quit: \t");

fgets(buf, sizeof(buf), stdin);

write(to_child[1], &buf, sizeof(buf));

fgets(buf, sizeof(buf), inputPtr);

printf("Parent> Child says: %s .", buf);

}

close(fd[0]);

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students explore these related Databases questions

Question

What tools might be helpful?

Answered: 3 weeks ago