Created
November 27, 2012 21:50
-
-
Save piscisaureus/4157340 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Terminal 1
Terminal 2
$ gcc test.c -lpthread -Wall
$ ./a.out