Skip to content

Instantly share code, notes, and snippets.

@stevendanna
Created July 23, 2015 15:04
Show Gist options
  • Save stevendanna/9a6f15ef129ffa93fd3a to your computer and use it in GitHub Desktop.
Save stevendanna/9a6f15ef129ffa93fd3a to your computer and use it in GitHub Desktop.
In terminal 1
> mkfifo test.fifo
> gcc test.c
> ./a.out
In terminal 2
> cat >test.fifo
(Interrupt with ctl+C)
#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