Chinaunix首页 | 论坛 | 博客
  • 博客访问: 271818
  • 博文数量: 41
  • 博客积分: 397
  • 博客等级: 二等列兵
  • 技术积分: 325
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 23:10
文章分类
文章存档

2014年(3)

2013年(20)

2012年(14)

2011年(4)

分类: LINUX

2013-05-30 16:55:18

废话少说直接上代码!


  1. /*
  2. * 有四个线程1、2、3、4。线程1的功能就是输出A,线程2的功能就是输出B,以此类推.........
  3. * 现在有四个文件file1, file2,file3, file4。初始都为空。现要让四个文件呈如下格式:
  4. * file1:A B C D A B....
  5. * file2:B C D A B C....
  6. * file3:C D A B C D....
  7. * file4:D A B C D A....
  8. */
  9. #include <stdio.h>
  10. #include <pthread.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <stdlib.h>

  16. pthread_t threads[4];
  17. char writer_char[4] = {'A','B', 'C', 'D'};
  18. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  19. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

  20. struct file_res{
  21.     pthread_t *writer; /*当前文件可以被那个线程写入*/
  22.     int fd; /*文件描述符*/
  23. }file_res[4] =
  24. {
  25.     {.writer=&threads[0]},/*file1初始化可以被线程1写入'A'*/
  26.     {.writer=&threads[1]},/*file2初始化可以被线程2写入'B'*/
  27.     {.writer=&threads[2]},/*file3初始化可以被线程3写入'C'*/
  28.     {.writer=&threads[3]},/*file4初始化可以被线程4写入'D'*/
  29. };

  30. void *writer_routine(void *arg)
  31. {
  32.     int index = (int)arg;
  33.     int file_index[4] ;
  34.     int can_write_file_cnt = 0;
  35.     int i = 0;
  36.     int next_index=0;
  37.     printf("thread %d is running, and will write '%c' to files\n", index, writer_char[index]);
  38.     
  39.     while(1)
  40.     {
  41.         if (0!=pthread_mutex_lock(&mutex))
  42.             exit(-1);
  43.         for(;;) {
  44.          /*下面的for语句查询本线程可以写的文件id*/
  45.             for(i=0; i<(sizeof(file_res)/sizeof(file_res[0])); i++) {
  46.                 if (&threads[index]==file_res[i].writer) {
  47.                     file_index[can_write_file_cnt++] = i;
  48.                     next_index = index+1;
  49.                     if (next_index>3)next_index=0;
  50.                     file_res[i].writer = &threads[next_index];
  51.                 }
  52.             }
  53.             /*找到本线程可以写的文件id,则退出for(;;), 执行写操作*/
  54.             if (can_write_file_cnt != 0)
  55.                 break;
  56.             /*等待其他线程唤醒*/
  57.             pthread_cond_wait(&cond,&mutex);
  58.         }        
  59.         
  60.         /*本线程可以写的文件,执行写操作*/
  61.         for(i=0; i<can_write_file_cnt; i++) {
  62.             write(file_res[file_index[i]].fd, &writer_char[index], sizeof(writer_char[index]));
  63.         }
  64.         can_write_file_cnt = 0;
  65.         
  66.         /*唤醒下一批线程*/
  67.         pthread_cond_broadcast(&cond);
  68.         if (0!=pthread_mutex_unlock(&mutex))
  69.             exit(-1);    
  70.     }
  71. }
  72.  
  73. int main(int argc, char* argv[])
  74. {
  75.     int i;
  76.     
  77.     for(i=0; i<4; i++)
  78.     {
  79.         char file_name[] = "filex";
  80.         sprintf(file_name, "file%d", i+1);
  81.         if ((file_res[i].fd = open(file_name, O_RDWR|O_CREAT|O_TRUNC, 0666))<0)
  82.         {
  83.             printf("open %s error.\n", file_name);
  84.             exit(-1);
  85.         }
  86.     }
  87.     
  88.     for (i=0; i<(sizeof(threads)/sizeof(pthread_t)); i++)
  89.     {
  90.         if(pthread_create(&threads[i], NULL, writer_routine, (void *)i))
  91.         {
  92.             printf("create writer thread error\n");
  93.             exit(-1);
  94.         }
  95.     }
  96.     pthread_exit(NULL);
  97.     return 0;
  98. }

阅读(2149) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

hwayw2013-06-12 09:48:02

您好!
   您的问题我已经收到,我们将尽快处理,谢谢!