#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
#include <math.h>
int pfds[2];
char buf[100000];
void * thread(void * arg)
{
fd_set readfds;
struct timeval timeout;
int n;
while (1) {
usleep(200000);
FD_ZERO(&readfds);
FD_SET(pfds[0], &readfds);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (! (n = select(pfds[0] + 1, &readfds, NULL, NULL, &timeout)))
continue;
if (n < 0) {
perror("select");
return NULL;
}
n = read(pfds[0], buf, 100000);
printf("%d bytes read\n", n);
}
}
int main()
{
pthread_t th;
int n;
if (pipe(pfds) < 0) {
fprintf(stderr, "Couldn't create pipe\n");
exit(1);
}
pthread_create(&th, NULL, thread, NULL);
usleep(1000);
while (1) {
n = write(pfds[1], buf, 100000);
usleep(1000);
}
}