CODE
#include <stdio.h>
#include <unistd.h>
#define BUFSIZE 128
main()
{
int fd[2];
char n;
int i,c,d;
char line[BUFSIZE];
pipe(fd);
c=fork();
if(c>0){
d=fork();
}
if (c == 0) {
close(fd[0]);
for (i=0; i < 10; i++) {
sprintf(line,"%s","i am child 1");
write(fd[1], line, BUFSIZE);
printf("Child writes: %s\n","i am child 1");
sleep(2);
}
}
if (d == 0) {
close(fd[0]);
for (i=0; i < 10; i++) {
sprintf(line,"%s","i am child 2");
write(fd[1], line, BUFSIZE);
printf("Child writes: %s\n","i am child 2");
sleep(2);
}
}
if(c>0 & d>0) {
close(fd[1]);
for (i=0; i < 10; i++) {
printf("\t\t\t Parent trying to read pipe\n");
read(fd[0], line, BUFSIZE);
sscanf(line,"%s",&n);
printf("\t\t\t Parent reads: %d\n",n);
}
}
}
the problem says:
create 3 processes, one parent and two child
the two child are to write different messages down a single pipe having size 128 bytes(I am child 1,I am child 2). make sure the children do not fill up the pipe i.e are hung waiting for the pipe to be read. if there is a block children will print an error message on screen and continue on. the parent is to continuously read the same pipe and print all the message that come to pipe. the parent is to perform a non blocking read on the pipe
i have tried to work out this problem. but its not exactly what the question asks. please try to solve it