continuous write on a pipe from 2 child processes and continuous read,
from parent
Problem to generate 10 random numbers (In two child), send it to parent
(one by one) and find the maximum among them
PS: Parent reads twice (one stream of random numbers from one child, does
the calculation to take the maximum, and then from the other another
child)
My question is: What is the mechanism of Read and Write system calls. How
does the control moves within the two system calls?
Problem to generate 10 random numbers (In two child), send it to parent
and finf the maximim among them. Do not use more than 4 variables to do
that. Parent reads twice (one stream of random numbers from one child,
takes the maximum, and then from another child)
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#define READ_END 0
#define WRITE_END 1
int main(int argc, char *argv[]){
pid_t child1, child2, pid, child1Pid, child2Pid;
int pipe1[2],status, bufChild1,bufChild2;
int maxChild1, minChild1, maxChild2, minChild2;
char Array1,Array2;
//create a pipe
if (pipe(pipe1) == -1)
{
fprintf(stderr, "Pipe failed");
}
//child1 created
child1 = fork();
//if creation failed
if (child1 < 0) {
fprintf(stderr, "Fork Failed");
return 1;
}
// child1 processes being created
else if (child1 == 0) {
printf("Inside child 1 with pid %d\n", getpid());
printf("The Parent pid is %d\n", getppid());
srand(456);
//Work of child1
for (int i=0;i<10;i++) //10 Random numbers for child 1 and
contunuous streaming to parent
{
bufChild1 = rand()%1000;
fprintf(stdout, "written value from child 1 is: %d\n",
bufChild1);
close(pipe1[READ_END]);
write(pipe1[WRITE_END],&bufChild1,sizeof(bufChild1));
//write into the Parent
close(pipe1[WRITE_END]);
}
usleep(1000);
exit(0);
}``
//Child2 created
child2 = fork();
//if creation failed
if (child2 < 0) {
fprintf(stderr, "Fork Failed");
return 1;
}
// child2 gets created
else if (child2 == 0) {
printf("\ninside child2 with pid %d\n", getpid());
printf("\nThe Parent pid is %d\n", getppid());
srand(789);
//Work of child2
for (int j=0;j<20;j++)////20 Random numbers for child 2,
continuously write to the pipe while generating random numbers
{
bufChild2 = rand()%1000;
fprintf(stdout, "written value from child 2 is: %d\n",
bufChild2);
close(pipe1[READ_END]);
write(pipe1[WRITE_END],&bufChild2,sizeof(maxChild2));
//write into the Parent
close(pipe1[WRITE_END]);
}
usleep(1000);
exit(0);
}
// Parent Process...Wait for children to exit.
pid = wait(&status);
printf("Inside Parent Process\n");
close(pipe1[WRITE_END]);
//Reading from Child 1 (keep reading and comparing till the loop pf
child 1 continues
// I AM STUCK HERE. HOW CAN I DO THE COMPARISION.
//I intend to compare the new coming number with the previous one,
and if its greater, it should be saved else get the new number
//if (read(pipe1[READ_END],&Array1,sizeof(Array1)) > maxChild1);
printf("Maximum number received from child2 : \%d\n",maxChild2);
//Similar Logic for child 2 ...
//``if (read(pipe1[READ_END],&Array2,sizeof(Array1)) > maxChild2);
return (1);
}
No comments:
Post a Comment