rendered paste body#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
int p[2];
void *run(void *arg) {
ssize_t res;
char tmp[128];
fprintf(stderr, "reading...\n");
res = read(p[0], tmp, sizeof(tmp));
fprintf(stderr, "read result: %d\n", (int)res);
if (res < 0) {
perror("read");
}
return arg;
}
int main(int argc, char **argv) {
pthread_t t;
int d = open("/dev/null", O_RDONLY);
if (pipe(p) != 0) {
perror("pipe");
return 1;
}
if (pthread_create(&t, NULL, run, NULL) != 0) {
perror("thread create");
return 1;
}
sleep(1);
d = open("/dev/null", O_RDONLY);
if (d < 0) {
perror("open dev null");
exit(1);
}
if (dup2(d, p[0]) < 0) {
perror("dup2");
exit(1);
}
if (pthread_join(t, NULL) != 0) {
perror("thread join");
exit(1);
}
return 0;
}