Advertisement
Guest User

Untitled

a guest
Jun 25th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11.  
  12. #define WRITE_CHUNK (64*1024*1024)
  13.  
  14.  
  15. void
  16. show_timeval (const struct timeval *tv)
  17. {
  18.     printf("%d.%06d\n", (int)tv->tv_sec, (int)tv->tv_usec);
  19. }
  20.  
  21. void
  22. elapsed_time (struct timeval *t, const struct timeval *a, const struct timeval *b)
  23. {
  24.     /* insure that a < b */
  25.     if ((a->tv_sec > b->tv_sec) ||
  26.         ((a->tv_sec == b->tv_sec) && (a->tv_usec > b->tv_usec)))
  27.     {
  28.         const struct timeval *tmp;
  29.         tmp = a;
  30.         a = b;
  31.     b = tmp;
  32.     }
  33.     *t = *b;
  34.     t->tv_sec -= a->tv_sec;
  35.     t->tv_usec -= a->tv_usec;
  36.     while (t->tv_usec < 0) {
  37.         t->tv_sec -= 1;
  38.     t->tv_usec += 1000000;
  39.     }
  40.     while (t->tv_usec >= 1000000) {
  41.         t->tv_sec += 1;
  42.     t->tv_usec -= 1000000;
  43.     }
  44. }
  45.  
  46. void *
  47. writer (void *dummy)
  48. {
  49.     int fd;
  50.     char *data;
  51.     int i;
  52.     struct timeval start;
  53.     struct timeval end;
  54.     struct timeval diff;
  55.     ssize_t write_ret;
  56.  
  57.     fd = open("test.dat", O_WRONLY | O_CREAT, 0666);
  58.     if (fd == -1) {
  59.         fprintf(stderr, "Error %d opening file; %s\n", errno, strerror(errno));
  60.         exit(1);
  61.     }
  62.     data = (char *)calloc(WRITE_CHUNK, 1);
  63.     if (data == NULL) {
  64.         fprintf(stderr, "Error allocating memory\n");
  65.         exit(1);
  66.     }
  67.     for (i=0; i<16; i++) {
  68.     if (lseek(fd, 0, SEEK_SET) != 0) {
  69.             fprintf(stderr, "Error %d seeking to start of file; %s\n", errno, strerror(errno));
  70.             exit(1);
  71.         }
  72.     gettimeofday(&start, NULL);
  73.         write_ret = write(fd, data, WRITE_CHUNK);
  74.     if (write_ret != WRITE_CHUNK) {
  75.         fprintf(stderr, "write() returned %d\n", write_ret);
  76.         if (write_ret == -1) {
  77.             fprintf(stderr, "Error is %d; %s\n", errno, strerror(errno));
  78.         }
  79.         exit(1);
  80.     }
  81.     gettimeofday(&end, NULL);
  82.         elapsed_time(&diff, &start, &end);
  83.     printf("%d bytes in %d.%03d seconds\n", write_ret, (int)diff.tv_sec, (int)diff.tv_usec / 1000);
  84.     }
  85.     free(data);
  86.     if (remove("test.dat") != 0) {
  87.         fprintf(stderr, "WARNING: could not remove file, error %d; %s\n", errno, strerror(errno));
  88.     }
  89.     return NULL;
  90. }
  91.  
  92. void *
  93. happy (void *dummy)
  94. {
  95.     int cnt;
  96.     for (cnt = 0; ; cnt++) {
  97.         printf("Not blocked... (%d)\n", cnt);
  98.         usleep(200000);
  99.     }
  100.     return NULL;
  101. }
  102.  
  103.  
  104. int
  105. main (int argc, char *argv[])
  106. {
  107.     pthread_t writer_tid;
  108.     pthread_t happy_tid;
  109.     int errnum;
  110.  
  111.     errnum = pthread_create(&writer_tid, NULL, writer, NULL);
  112.     if (errnum != 0) {
  113.         fprintf(stderr, "pthread_create() failed, error %d\n", errnum);
  114.     return 1;
  115.     }
  116.     errnum = pthread_create(&happy_tid, NULL, happy, NULL);
  117.     if (errnum != 0) {
  118.         fprintf(stderr, "pthread_create() failed, error %d\n", errnum);
  119.     return 1;
  120.     }
  121.     errnum = pthread_join(writer_tid, NULL);
  122.     if (errnum != 0) {
  123.         fprintf(stderr, "pthread_join() failed, error %d\n", errnum);
  124.     return 1;
  125.     }
  126.  
  127.     return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement