First of all sorry if my English won't be fluent and clear.
I'm working on understanding pipes and communication between processes. I have tried to implement two c programs, the one writes into a certain pipe from what he reads from the standard input and the other one waits until the pipe opens and reads from it and prints to the standard output until EOF.
Here is the code for the writer pipe:
fd = open(filename, O_RDWR);
if(fd == -1) print_error();
while(fgets(buffer, BUFFER_SIZE, stdin) != NULL) {
if(write(fd, buffer, BUFFER_SIZE) == -1) print_error();
}
and here is the code for the reader pipe:
while(1) {
if((fd = open(filename, O_RDWR)) == -1) {
if(errno == ENOENT) sleep(1);
else print_error();
}
else {
while(read(fd, buffer, BUFFER_SIZE) != 0) {
fprintf(stdout, "%s", buffer);
}
}
}
The thing is that when I run those two programs concurrently the basic concept works, I write something to the standard input in the writer program and I see in the other terminal that the reader program prints it to the standard output. The problem is that when I send EOF by hitting CTRL + D for the writer program the reader program still waits for input, and I know for sure that it isn't because the while(1), I saw in the debugger that the read syscall is just waiting to input and didn't understand that we got EOF, the line : read(fd, buffer, BUFFER_SIZE)
didn't evaluate even though there is no input.
I hope that I gave all the data needed to solve the problem, anyone have any ideas what gone wrong?