Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40959
  • 博文数量: 7
  • 博客积分: 162
  • 博客等级: 入伍新兵
  • 技术积分: 80
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-20 06:47
文章分类
文章存档

2011年(7)

我的朋友

分类: C/C++

2011-09-09 10:18:54

reader_writer1.c:
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. #include <semaphore.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <signal.h>
  8. #include <errno.h>

  9. #define SIZE 1000        // the size of the buffer
  10. #define SECS 10            // the time to run

  11. pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  12. sem_t sem_empty, sem_full;

  13. long buffer[SIZE] = {0};

  14. // record the times to meet lock
  15. int read_lock_times = 0;
  16. int write_lock_times = 0;

  17. // record the times of reading or writing
  18. int read_num = 0;
  19. int write_num = 0;

  20. // signal handler
  21. void sig_handler(int sig_num)
  22. {
  23.     printf("Meet %d read locks\n", read_lock_times);
  24.     printf("Meet %d write locks\n", write_lock_times);
  25.     printf("Read %d numbers\n", read_num);
  26.     printf("Write %d numbers\n", write_num);

  27.     exit(0);
  28. }

  29. void * read_thread(void * args)
  30. {
  31.     while(1)
  32.     {

  33.         sem_wait(&sem_empty);

  34.         if( pthread_mutex_trylock(&mut) == EBUSY)
  35.         {
  36.             ++ read_lock_times;

  37.             pthread_mutex_lock(&mut);
  38.         }

  39.         ++ read_num;

  40.         pthread_mutex_unlock(&mut);

  41.         sem_post(&sem_full);
  42.     }
  43. }

  44. void * write_thread(void * args)
  45. {
  46.     while(1)
  47.     {

  48.         sem_wait(&sem_full);

  49.         if( pthread_mutex_trylock(&mut) == EBUSY)
  50.         {
  51.             ++ write_lock_times;

  52.             pthread_mutex_lock(&mut);
  53.         }

  54.         buffer[write_num%SIZE] = write_num;
  55.         ++write_num;

  56.         pthread_mutex_unlock(&mut);

  57.         sem_post(&sem_empty);
  58.     }
  59. }

  60. int main()
  61. {    
  62.     // install the signal handler
  63.     struct sigaction action;
  64.     action.sa_handler = sig_handler;
  65.     sigaction(SIGALRM, &action, NULL);

  66.     alarm(SECS);

  67.     sem_init(&sem_empty, 0, 0);
  68.     sem_init(&sem_full, 0, SIZE);

  69.     pthread_t tid1, tid2;
  70.     pthread_create(&tid1, NULL, read_thread, NULL);
  71.     pthread_create(&tid2, NULL, write_thread, NULL);

  72.     pthread_join(tid1, NULL);
  73.     pthread_join(tid2, NULL);

  74.     sem_destroy(&sem_empty);
  75.     sem_destroy(&sem_full);

  76.     pthread_mutex_destroy(&mut);

  77.     return 0;
  78. }
背景无需多说。
 
上述代码假定缓冲池容量固定,大小由 SIZE 控制。
 
在 SECS 秒后,会输出 读线程、写线程 遇到的锁的个数,以及 读写的数目。
 
以下是上面程序的输出:
 
/home/adamas:~$ time ./read_writer1
 
Meet 1 read locks
Meet 0 write locks
Read 1137205 numbers
Write 1137794 numbers
 
real 0m10.017s
user 0m4.252s
sys 0m5.640s
显然,这里的输出数目与 SIZE 密切相关,并不是越大越好,SIZE 定义为 10,000,反而不如定义为 1,000 快。
 
下一篇中谈谈使用 C++ 中的 vector 来代替本文中的 固定容量数组。
阅读(2440) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~