Skip to content

Instantly share code, notes, and snippets.

@piscisaureus
Created November 27, 2012 21:50
Show Gist options
  • Save piscisaureus/4157340 to your computer and use it in GitHub Desktop.
Save piscisaureus/4157340 to your computer and use it in GitHub Desktop.
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
volatile int fd;
void* thread_proc(void* arg) {
int r;
sleep(1);
printf("Closing...\n");
r = close(fd);
printf("close %d %s\n", r, r >= 0 ? "" : strerror(errno));
printf("Closed\n");
return NULL;
}
int main(int argc, char* argv[]) {
char buf[1024*1024];
int r;
pthread_t ptid;
fd = open("a", O_RDONLY);
assert(fd >= 0);
printf("fd: %d\n", fd);
pthread_create(&ptid, NULL, thread_proc, NULL);
for (;;) {
r = read(fd, buf, sizeof buf);
printf("read %d %s\n", r, r >= 0 ? "" : strerror(errno));
}
return 0;
}
@piscisaureus
Copy link
Author

Terminal 1

$ mkfifo a
$ cat > a   #leave running, don't enter anything

Terminal 2

$ gcc test.c -lpthread -Wall
$ ./a.out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment