Created
July 23, 2015 15:04
-
-
Save stevendanna/9a6f15ef129ffa93fd3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In terminal 1 | |
> mkfifo test.fifo | |
> gcc test.c | |
> ./a.out | |
In terminal 2 | |
> cat >test.fifo | |
(Interrupt with ctl+C) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> | |
#include <sys/select.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
int main() { | |
int fd; | |
int active; | |
int eof_count = 0; | |
int ret; | |
unsigned char buffer[10]; | |
fd_set readfds; | |
FD_ZERO(&readfds); | |
fd = open("test.fifo", 0); | |
if (fd < 0) { | |
perror("open()"); | |
exit(1); | |
} | |
FD_SET(fd, &readfds); | |
while (eof_count < 2) { | |
active = select(fd+1, &readfds, NULL, NULL, NULL); | |
if (active == -1) { | |
perror("select()"); | |
exit(1); | |
} | |
ret = read(fd, &buffer, 10); | |
if (ret == 0) { | |
printf("Saw an end of file\n"); | |
eof_count += 1; | |
} | |
} | |
printf("Saw more than 1 EOF, exiting"); | |
exit(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment