I have two processes p0, p1 running in parallel. I'm using named pipe IPC where reader (p0) writes to the pipe and reader (p1) reads from it. Here, the reader starts before writer. The reader has to loop till the writer writes to the pipe and the data is available to read.
In writer.c
int fd;
char * myfifo = "/tmp/myfifo";
char string1[20] = "Hello";
mkfifo(myfifo, 0666);
fd = open(myfifo, O_WRONLY);
write(fd, string, sizeof(string1);
close(fd);
In reader.c , I use
# define MAX_BUF 20
int fd;
char buf[MAX_BUF];
while(1){
fd = open(myfifo, O_RDONLY);
read(fd, buf, MAX_BUF);
printf("Received: %s\n", buf);
if(strlen(buf) != 0){ // buf is not empty, so break from the loop
break;
}
close(fd);
}
When executing this code, the writer blocks. What am I missing here?
Thanks, K