Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <time.h>
- #include <fcntl.h>
- #include <pthread.h>
- struct iter {
- int in, out;
- };
- static long counter;
- static void * inc(void *arg)
- {
- struct iter *iter = (struct iter *)arg;
- int buf;
- while (read(iter->in, &buf, sizeof(buf)) > 0) {
- buf++;
- counter = buf;
- write(iter->out, &buf, sizeof(buf));
- }
- close(iter->in);
- close(iter->out);
- return NULL;
- }
- int main(int argc, char *argv[])
- {
- pthread_t t1, t2;
- int fds[4];
- pipe(fds);
- pipe(fds+2);
- struct iter iter1 = {
- .in = fds[0],
- .out = fds[3],
- };
- struct iter iter2 = {
- .in = fds[2],
- .out = fds[1],
- };
- pthread_create(&t1, NULL, inc, &iter1);
- pthread_create(&t2, NULL, inc, &iter2);
- write(iter1.out, &counter, sizeof(counter));
- sleep(1);
- pthread_cancel(t1);
- pthread_cancel(t2);
- printf("messages/sec: %lu\n", counter);
- printf("time for message: %luns\n", 1000000000 / counter);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement