Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47225
  • 博文数量: 17
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 106
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-11 11:04
文章分类

全部博文(17)

文章存档

2014年(17)

我的朋友

分类: LINUX

2014-10-14 17:52:02

1.单个消费者和单个生产者模型
利用锁和条件变量完成代码


点击(此处)折叠或打开

  1. #include
  2. #include
  3. #include
  4. #include
  5. #include
  6. enum{
  7.     CONSUMMER = 0,
  8.     PRODUCER
  9. };

  10. pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
  11. pthread_cond_t g_eat = PTHREAD_COND_INITIALIZER;
  12. pthread_cond_t g_empty = PTHREAD_COND_INITIALIZER;



  13. void *consummer(void *arg)
  14. {
  15.     unsigned int *carrot = (unsigned int *)arg;
  16.     while(1)
  17.     {
  18.         while( pthread_mutex_trylock(&g_mutex) != 0)
  19.         {
  20.             printf("consummer wait!!\n");
  21.         }
  22.         while (*carrot == 0)
  23.         {
  24.             pthread_cond_signal(&g_empty);
  25.             pthread_cond_wait(&g_eat , &g_mutex);
  26.         }
  27.         *carrot = *carrot - 1;
  28.         printf("consummer eat: carrot-- %d\n",*carrot);
  29.         pthread_mutex_unlock(&g_mutex);
  30.         
  31.         sleep(1);
  32.      }
  33. }

  34. void *producer(void *arg)
  35. {
  36.     unsigned int *carrot = (unsigned int *)arg;
  37.     while(1)
  38.     {
  39.         while( pthread_mutex_trylock(&g_mutex) != 0)
  40.         {
  41.             printf("producer wait!\n");
  42.         }

  43.         while (*carrot > 0)
  44.         {
  45.              pthread_cond_wait(&g_empty , &g_mutex);
  46.         }
  47.              *carrot = *carrot + 5;
  48.              printf("producer produced :carrort + 5--%d\n",*carrot);
  49.              pthread_cond_signal(&g_eat);
  50.              pthread_mutex_unlock(&g_mutex);
  51.         
  52.         sleep(1);
  53.     }

  54. }



  55. int main(int argc ,char *argv[])
  56. {
  57.     unsigned int carrot = 9;
  58.     pthread_t pthreads[2];

  59.     if (pthread_create(&pthreads[CONSUMMER], NULL , &consummer , &carrot) != 0)
  60.     {
  61.         puts("pthread_create consummer failed!\n");
  62.         return 0;
  63.     }

  64.     if (pthread_create(&pthreads[PRODUCER], NULL , &producer , &carrot) != 0)
  65.     {
  66.         puts("pthread_create consummer failed!\n");
  67.         return 0;
  68.     }

  69.     pthread_join(pthreads[CONSUMMER],NULL);
  70.     pthread_join(pthreads[PRODUCER],NULL);

  71.     pthread_cond_destroy(&g_empty);
  72.     pthread_cond_destroy(&g_eat);
  73.     pthread_mutex_destroy(&g_mutex);

  74. }
2.编写一个程序,开启3个线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC.依次递推(迅雷笔试题)

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <pthread.h>

  6. #define MAX_THREAD 3



  7. pthread_cond_t g_awakeup = PTHREAD_COND_INITIALIZER;
  8. pthread_cond_t g_bwakeup = PTHREAD_COND_INITIALIZER;
  9. pthread_cond_t g_cwakeup = PTHREAD_COND_INITIALIZER;
  10. pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;

  11. enum {
  12.     paRun = 0,
  13.     pbRun,
  14.     pcRun
  15. };

  16. void *paWork(void *arg)
  17. {
  18.     unsigned int *stats = (unsigned int *)arg;
  19.     int i = 0;
  20.     while(1)
  21.     {
  22.        pthread_mutex_lock(&g_mutex);
  23.        while (*stats != paRun) {pthread_cond_wait(&g_awakeup, &g_mutex);}
  24.        printf("A");
  25.        *stats = pbRun;
  26.        pthread_mutex_unlock(&g_mutex);
  27.        pthread_cond_signal(&g_bwakeup);
  28.        if (++i % 10 == 0) break;
  29.     }

  30. }
  31. void *pbWork(void *arg)
  32. {
  33.     unsigned int *stats = (unsigned int *)arg;
  34.     int i = 0;
  35.     while(1)
  36.     {
  37.        pthread_mutex_lock(&g_mutex);
  38.        while (*stats != pbRun) {pthread_cond_wait(&g_bwakeup, &g_mutex);}
  39.        printf("B");
  40.        *stats = pcRun;
  41.        pthread_mutex_unlock(&g_mutex);
  42.        pthread_cond_signal(&g_cwakeup);
  43.        if (++i % 10 == 0) break;
  44.     }

  45. }
  46. void *pcWork(void *arg)
  47. {
  48.     unsigned int *stats = (unsigned int *)arg;
  49.     int i = 0;
  50.     while(1)
  51.     {
  52.        pthread_mutex_lock(&g_mutex);
  53.        while (*stats != pcRun) {pthread_cond_wait(&g_cwakeup, &g_mutex);}
  54.        printf("C");
  55.        *stats = paRun;
  56.        pthread_mutex_unlock(&g_mutex);
  57.        pthread_cond_signal(&g_awakeup);
  58.        if (++i % 10 == 0) break;
  59.     }
  60. }


  61. int main(int argc ,char *argv[])
  62. {
  63.     int i = 0;
  64.     unsigned int stats = paRun;
  65.     pthread_t pthreads[MAX_THREAD];


  66.     if (pthread_create(&pthreads[paRun] , NULL, &paWork, &stats ) != 0)
  67.     {
  68.         printf("pthread_create failed!\n");
  69.         return 0;
  70.     }
  71.     if (pthread_create(&pthreads[pbRun] , NULL, &pbWork, &stats ) != 0)
  72.     {
  73.         printf("pthread_create failed!\n");
  74.         return 0;
  75.     }
  76.     if (pthread_create(&pthreads[pcRun] , NULL, &pcWork, &stats ) != 0)
  77.     {
  78.         printf("pthread_create failed!\n");
  79.         return 0;
  80.     }

  81.     for (i = 0; i< MAX_THREAD; i++)
  82.     {
  83.         pthread_join(pthreads[i], NULL);
  84.     }

  85.     pthread_cond_destroy(&g_awakeup);
  86.     pthread_cond_destroy(&g_bwakeup);
  87.     pthread_cond_destroy(&g_cwakeup);
  88.     pthread_mutex_destroy(&g_mutex);
  89.     printf("\nGAME OVER!\n");

  90. }
当然也可以将三个线程函数代码合成一个。
测试结果:
ABCABCABCABCABCABCABCABCABCABC
GAME OVER!

3.
是否熟悉POSIX多线程编程技术?如熟悉,编写程序完成如下功能:

1)有一int型全局变量g_Flag初始值为0;

2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

4) 线程序1需要在线程2退出后才能退出

5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出
分析:线程1的退出条件是线程2 退出之后才能退出。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <pthread.h>

  6. #define NUM 2

  7. int g_Flag = 0;
  8. int count = 0;

  9. pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
  10. pthread_cond_t g_cond = PTHREAD_COND_INITIALIZER;

  11. void *R1(void *arg)
  12. {
  13.     pthread_mutex_lock(&g_mutex);
  14.     puts("This is thread1!\n");
  15.     count++;
  16.     if (g_Flag == 2)
  17.     {
  18.         g_Flag = 1;
  19.         puts("R2 end ,So R1 end!\n");
  20.         pthread_mutex_unlock(&g_mutex);
  21.     } else if (g_Flag == 0)
  22.         {
  23.             puts("R1 waiting R2 end!\n");
  24.             pthread_cond_wait(&g_cond,&g_mutex);
  25.             puts("R1 wakeup, end!\n");
  26.             pthread_mutex_unlock(&g_mutex);
  27.         } else {
  28.             puts("unknow err!\n");
  29.             pthread_mutex_unlock(&g_mutex);
  30.         }
  31. }
  32. void *R2(void *arg)
  33. {
  34.         pthread_mutex_lock(&g_mutex);
  35.         puts("This is thread2!\n");
  36.         count++;
  37.         g_Flag = 2;
  38.         puts("R2 end!\n");
  39.         pthread_mutex_unlock(&g_mutex);
  40.         pthread_cond_signal(&g_cond);
  41. }

  42. int main(int argc, char *argv[])
  43. {    

  44. int i = 0;
  45. pthread_t pthreads[2];
  46. pthread_attr_t attr;    
  47. pthread_attr_init(&attr);
  48. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  49.     if (pthread_create(&pthreads[0], &attr,&R1, NULL) != 0)
        {
            puts("pthread_create failed! 1\n");
            return 0;
        }
        pthread_detach(pthreads[0]);
        if (pthread_create(&pthreads[1], &attr,&R2, NULL)!= 0)
        {
            puts("pthread_create failed! 2\n");
            return 0;
        }
        pthread_detach(pthreads[1]);
        while(1)
        {
           pthread_mutex_lock(&g_mutex);
           if (count > 1) {pthread_mutex_unlock(&g_mutex);break;}
           pthread_mutex_unlock(&g_mutex);
        }
        puts("main pthread over!\n");
        pthread_cond_destroy(&g_cond);
        pthread_mutex_destroy(&g_mutex);
        pthread_attr_destroy(&attr);

}










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