I am trying to send a string, "Hi" from Child1 to Child3 which are two sibling processes. The code runs, however i do not receive the input from Child1 in Child3.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MSGSIZE 1024
int main (int argc, char *argv[]){
int fd;
char * myfifo = "/desktop/myfifo";
char l[MSGSIZE];
pid_t child1, child3;
mkfifo(myfifo, 0666);
child1 = fork();
if (child1 == 0) {
printf("I am Child 1: %d \n", (int)getpid());
fd = open(myfifo, O_WRONLY);
write(fd, "Hi", MSGSIZE);
close(fd);
}
else {
if (child1 > 0 ) {
printf("I am parent: %d \n", (int)getpid());
wait(0);
}
child3 = fork();
if (child3 == 0) {
printf("I am Child 3: %d \n", (int)getpid());
fd = open(myfifo, O_RDONLY);
read(fd, l, MSGSIZE);
printf("Received: %s \n", l);
close(fd);
}
}
wait(0);
unlink(myfifo);
return 0;
}
Hope someone can point me in the right direction.