sorry im very new to this but i am supposed to create 3 processes A,B, and C and have a direct link from a to b, b to c, and c to a.
here is my code. It does work, however if you look at what I bolded as long as my final read is p[0] it seems to always work, regardless of the bolded section.
can anyone explain this?
Code:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#define MSGSIZE 14
char *message = "hello, world!";
main() {
char inbuf[MSGSIZE];
int p[3];
pid_t pid;
pid_t pid1;
if (pipe(p) == -1){
perror("pipe call");
exit(1);
}
pid = fork();
pid1 = fork();
if(pid == -1){
perror("Fork failed");
exit(1);
}
if(pid | pid1 == 0)//process A
{
close(p[0]);
write(p[1], message, MSGSIZE);
read(p[1], message, MSGSIZE);
write(p[2], message, MSGSIZE);
}
/*else if(pid1 == 0){
close(p[1]);
//read(p[1], message, MSGSIZE);
write(p[2], message, MSGSIZE);
}*/
else{
//parent process C
close(p[2]);
read(p[0], inbuf, MSGSIZE);
printf("Pipelined message return:%s\n", inbuf);
wait(NULL);
}
exit(0);
}