Question
Here i have C codes for a parent-children communication using a pipe, please answer me the below questions regarding the code you see. #include #include
Here i have C codes for a parent-children communication using a pipe, please answer me the below questions regarding the code you see.
#include
#include
#include
int main()
{
int pipefd[2];
pid_t pid1, pid2;
/* create the pipe */
pipe(pipefd);
/* fork child 1 */
pid1 = fork();
if(pid1 == 0)
{
/* child 1 */
close(pipefd[0]); /* close read end */
dup2(pipefd[1], 1); /* redirect stdout to pipe */
execlp("ls", "ls", "-F", (char*)NULL); /* execute */
}
/* fork child 2 */
pid2 = fork();
if(pid2 == 0)
{
/* child 2 */
close(pipefd[1]); /* close write end */
dup2(pipefd[0], 0); /* redirect stdin from pipe */
execlp("nl", "nl", (char*)NULL); /* execute */
}
/* parent process */
/* close both ends in parent */
close(pipefd[0]);
close(pipefd[1]);
/* wait for the 2 children to end */
wait(NULL);
wait(NULL);
/* message to mark end of parent process, can be commented out if not needed */
printf("Parent process end ");
return 0;
}
1. What form of exec() function you used? Why? 2. How many times you used fork? Why? 3. How many pipes this assignment required? Why? 4. What form of wait() you used? How many timesStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started