#include #include #include #include #include #include #define ITERATIONS 524768 #define NUM_PAIRS 512 #define TIMEOUT 1000000 //#define DEBUG int fds[NUM_PAIRS][2]; pthread_t tid[2 * NUM_PAIRS]; int map[NUM_PAIRS]; void write_then_poll(int *map) { struct pollfd fd = { .fd = fds[*map][0], .events = POLLIN }; int i; char data; for (i = 0; i < ITERATIONS; i++) { data = '0'; write(fds[*map][0], &data, 1); #ifdef DEBUG printf("Data written: %c from %ld \n", data, pthread_self()); #endif if ((poll(&fd, 1, TIMEOUT)) == 0) { printf("No data available for %ld \n", pthread_self()); pthread_exit(NULL); } read(fds[*map][0], &data, 1); #ifdef DEBUG printf("Data read: %c from %ld \n", data, pthread_self()); #endif } } void poll_then_write(int *map) { struct pollfd fd = { .fd = fds[*map][1], .events = POLLIN }; int i; char data; for (i = 0; i < ITERATIONS; i++) { if ((poll(&fd, 1, TIMEOUT)) == 0) { printf("No data available for %ld \n", pthread_self()); pthread_exit(NULL); } read(fds[*map][1], &data, 1); #ifdef DEBUG printf("Data read %c from %ld \n", data, pthread_self()); #endif data = '1'; write (fds[*map][1], &data, 1); #ifdef DEBUG printf("Data written: %c from %ld \n", data, pthread_self()); #endif } } int main(void) { int i, ret; for (i = 0; i < NUM_PAIRS; ++i) { ret = socketpair(AF_UNIX, SOCK_STREAM, 0, fds[i]); if (ret < 0) perror("Error in socketpair()\n"); } for (i = 0; i < NUM_PAIRS; i++) { map[i] = i; pthread_create(&tid[2*i], NULL, (void *) write_then_poll, &map[i]); pthread_create(&tid[(2*i)+1], NULL, (void *) poll_then_write, &map[i]); } for (i = 0; i < 2 * NUM_PAIRS; ++i) pthread_join(tid[i], NULL); return (0); }