Write a CC program that first creates a child process The pa

Write a C/C++ program that first creates a child process. The parent then reads 10 numbers from the keyboard, calculates average, and sends the result over a Unix pipe to the child. The child process displays the result.

Solution

#include #include #include #include int main() { int status; int pid; pid=fork(); int fd[2]; int val = 0; // create pipe descriptors pipe(fd); if(pid<0) { printf(\"\ Error \"); exit(1); } else if(pid==0) { printf(\"\ the child process \"); printf(\"\ pid is %d \",getpid()); exit(0); } else { wait(&status); printf(\"\ the parent process \"); printf(\"\ pid is %d \ \",getpid()); exit(1); } // fork() returns 0 for child process, child-pid for parent process. if (fork() != 0) { // parent: writing only, so close read-descriptor. close(fd[0]); // send the value on the write-descriptor. val = 100; write(fd[1], &val, sizeof(val)); for(i=0;i
Write a C/C++ program that first creates a child process. The parent then reads 10 numbers from the keyboard, calculates average, and sends the result over a Un

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site