linux-多线程--面试题

刚无意中看到MoreWindows博主秒杀多线程面试题(http://blog.csdn.net/column/details/killthreadseries.html),第一篇就有他收集的面试题。那我就用来检验一下自己学的怎么样吧。 

   前面的选择题那些跳过,直接看最后的编程题。

第三题(某培训机构的练习题):

子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码。


第四题(迅雷笔试题):

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


第五题(Google面试题)

有四个线程1、234。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A1 2 3 4 1 2....

B2 3 4 1 2 3....

C3 4 1 2 3 4....

D4 1 2 3 4 1....

请设计程序。

第六题

生产者消费者问题

这是一个非常经典的多线程题目,题目大意如下:有一个生产者在生产产品,这些产品将提供给若干个消费者去消费,为了使生产者和消费者能并发执行,在两者之间设置一个有多个缓冲区的缓冲池,生产者将它生产的产品放入一个缓冲区中,消费者可以从缓冲区中取走产品进行消费,所有生产者和消费者都是异步方式运行的,但它们必须保持同步,即不允许消费者到一个空的缓冲区中取产品,也不允许生产者向一个已经装满产品且尚未被取走的缓冲区中投放产品。


第七题

读者写者问题

这也是一个非常经典的多线程题目,题目大意如下:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写。

新增第八题:

是否熟悉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的时候退出


第三题、第四题、第五题第一反应用条件变量来实现。第六题和第七题用读写锁来实现。

第三题、第四题、第五题和我上一篇博客linux多线程学习举得那例子很相似,只需要少量修改就能完成要求。不多说,直接上代码。

第四题代码:

  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <pthread.h>  
  4. #include <unistd.h>  
  5. #include <string.h>  
  6. //#define DEBUG 1  
  7. #define NUM 3  
  8.   
  9. int n=0;  
  10. pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;//互斥量  
  11. pthread_cond_t qready=PTHREAD_COND_INITIALIZER;//条件变量  
  12. void * thread_func(void *arg)  
  13. {  
  14.     int param=(int)arg;  
  15.     char c='A'+param;  
  16.     int ret,i=0;  
  17.     for (; i < 10; i++)  
  18.     {  
  19.         pthread_mutex_lock(&mylock);  
  20.         while (param != n)  
  21.         {  
  22. #ifdef DEBUG  
  23.             printf("thread %d waiting\n", param);  
  24. #endif  
  25.             ret = pthread_cond_wait(&qready, &mylock);  
  26.             if (ret == 0)   
  27.             {  
  28. #ifdef DEBUG  
  29.                 printf("thread %d wait success\n", param);  
  30. #endif  
  31.             } else   
  32.             {  
  33. #ifdef DEBUG  
  34.                 printf("thread %d wait failed:%s\n", param, strerror(ret));  
  35. #endif  
  36.             }  
  37.         }  
  38.        // printf("%d ",param+1);  
  39.         printf("%c ",c);  
  40.         n=(n+1)%NUM;  
  41.         pthread_mutex_unlock(&mylock);  
  42.         pthread_cond_broadcast(&qready);  
  43.     }      
  44.     return (void *)0;  
  45. }  
  46. int main(int argc, char** argv) {  
  47.       
  48.     int i=0,err;  
  49.     pthread_t tid[NUM];  
  50.     void *tret;  
  51.     for(;i<NUM;i++)  
  52.     {  
  53.         err=pthread_create(&tid[i],NULL,thread_func,(void *)i);  
  54.         if(err!=0)  
  55.         {  
  56.             printf("thread_create error:%s\n",strerror(err));  
  57.             exit(-1);  
  58.         }  
  59.     }  
  60.     for (i = 0; i < NUM; i++)  
  61.     {  
  62.         err = pthread_join(tid[i], &tret);  
  63.         if (err != 0)  
  64.         {  
  65.             printf("can not join with thread %d:%s\n", i,strerror(err));  
  66.             exit(-1);  
  67.         }  
  68.     }  
  69.     printf("\n");  
  70.     return 0;  
  71. }  
运行结果:




第五题:

选项A,代码只需要将NUM改为4,printf("%c ",c)改为printf("%d ",param+1);即可

执行结果如下:


选项B,将全局变量n改为1


选项C,将全局变量n改为2


选项D,将全局变量n改为3



下班啦。。后面两道题,等等在贴出代码。。


第六题

方法一,采用互斥量来实现生产者和消费者的同步。
流程图如下所示:

生产者:
  1. 对互斥量加锁
  2. 判断缓冲区是否已满,如满,则跳到步骤4
  3. 放入产品
  4. 解锁互斥量,此时一个循环完成,进入下一循环。
消费者流程图类似与生产者流程图。
代码如下:
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <pthread.h>  
  5. #define NUMS 10  //表示生产,消费的次数  
  6. #define CAPACITY 5 //定义缓冲区最大值  
  7. int capacity = 0; //当前缓冲区的产品个数  
  8. pthread_mutex_t mylock = PTHREAD_MUTEX_INITIALIZER;//互斥量  
  9.   
  10. void *produce(void *args)  
  11. {  
  12.     int i = 0;  
  13.     for (; i < NUMS; )  
  14.     {  
  15.         pthread_mutex_lock(&mylock);//加锁  
  16.         if (capacity >= CAPACITY) //当前产品个数大于等于缓冲区最大值,则不把产品放入缓冲区。  
  17.         {  
  18.             printf("缓冲区已满,无法放入产品\n");  
  19.         } else {//将产品放入缓冲区  
  20.             ++capacity;  
  21.             printf("生产者存入一个产品, 缓冲区大小为:%d\n", capacity);  
  22.             i++;  
  23.         }  
  24.         pthread_mutex_unlock(&mylock);  
  25.     }  
  26.     return ((void *) 0);  
  27. }  
  28.   
  29. void * consume(void *args)  
  30. {  
  31.     int i = 0;  
  32.     for (; i < NUMS; )  
  33.     {  
  34.         pthread_mutex_lock(&mylock);  
  35.         if (capacity > 0)   
  36.         {  
  37.             --capacity;  
  38.             printf("消费者消耗一个产品,缓冲区大小为:%d\n", capacity);  
  39.             i++;  
  40.         } else  
  41.     {  
  42.            printf("缓冲区已空,无法消耗产品\n");  
  43.         }  
  44.         pthread_mutex_unlock(&mylock);  
  45.     }  
  46.     return ((void *) 0);  
  47. }  
  48.   
  49. int main(int argc, char** argv) {  
  50.   
  51.     int err;  
  52.     pthread_t produce_tid, consume_tid;  
  53.     void *ret;  
  54.     err = pthread_create(&produce_tid, NULL, produce, NULL);//创建线程  
  55.     if (err != 0)   
  56.     {  
  57.         printf("线程创建失败:%s\n", strerror(err));  
  58.         exit(-1);  
  59.     }  
  60.     err = pthread_create(&consume_tid, NULL, consume, NULL);  
  61.     if (err != 0)  
  62.     {  
  63.         printf("线程创建失败:%s\n", strerror(err));  
  64.         exit(-1);  
  65.     }  
  66.     err = pthread_join(produce_tid, &ret);//主线程等到子线程退出  
  67.     if (err != 0)   
  68.     {  
  69.         printf("生产着线程分解失败:%s\n", strerror(err));  
  70.         exit(-1);  
  71.     }  
  72.     err = pthread_join(consume_tid, &ret);  
  73.     if (err != 0)  
  74.     {  
  75.         printf("消费者线程分解失败:%s\n", strerror(err));  
  76.         exit(-1);  
  77.     }  
  78.     return (EXIT_SUCCESS);  
  79. }  
执行结果:
结果满足题意。但是这存在一个问题,极端情况下,生产者每次都加锁成功,那缓冲区会满,产品无法放入缓冲区。消费者会被饿死,因为他一直无法获得互斥量。方法二,解决了导致某一方饿死的可能性。
update:
    在第一种方法中,当缓冲区满时,让生产者睡眠;当缓冲区空,让消费者睡眠。这样也能解决某一方老是加锁成功。

方法二:采用两个互斥量来完成
   流程图如下:
运行截图:
上代码:
  1. /*  
  2.  * File:   main.c 
  3.  * Author: root 
  4.  * 
  5.  * Created on 2012年5月22日, 上午9:35 
  6.  */  
  7.   
  8. #include <stdio.h>  
  9. #include <stdlib.h>  
  10. #include <string.h>  
  11. #include <pthread.h>  
  12.   
  13. #define NUMS 10  //表示生产,消费的次数  
  14. #define CAPACITY 5 //定义缓冲区最大值  
  15.   
  16.   
  17. int capacity = 0; //当前缓冲区的产品个数  
  18. pthread_mutex_t mylockA=PTHREAD_MUTEX_INITIALIZER;  
  19. pthread_mutex_t mylockB=PTHREAD_MUTEX_INITIALIZER;  
  20.   
  21. void *produce(void *args)  
  22. {  
  23.     int i = 0,err;  
  24.     for (; i < NUMS; )  
  25.     {  
  26.         pthread_mutex_lock(&mylockA);//加锁  
  27.         if (capacity >= CAPACITY) //当前产品个数大于等于缓冲区最大值,则不把产品放入缓冲区。  
  28.         {  
  29.             printf("缓冲区已满,无法放入产品\n");  
  30.         } else {//将产品放入缓冲区  
  31.             ++capacity;  
  32.             printf("生产者存入一个产品, 产品个数:%d\n", capacity);  
  33.             i++;  
  34.         }  
  35.         err=pthread_mutex_unlock(&mylockB);  
  36.     }  
  37.     return ((void *) 0);  
  38. }  
  39.   
  40. void * consume(void *args)  
  41. {  
  42.     int i = 0;  
  43.     for (; i < NUMS; )  
  44.     {  
  45.         pthread_mutex_lock(&mylockB);  
  46.         if (capacity > 0)   
  47.         {  
  48.             --capacity;  
  49.             printf("消费者消耗一个产品,产品个数:%d\n", capacity);  
  50.             i++;  
  51.         } else  
  52.     {  
  53.            printf("缓冲区已空,无法消耗产品\n");  
  54.         }  
  55.         pthread_mutex_unlock(&mylockA);  
  56.     }  
  57.     return ((void *) 0);  
  58. }  
  59.   
  60. int main(int argc, char** argv) {  
  61.   
  62.     int err;  
  63.     pthread_t produce_tid, consume_tid;  
  64.     void *ret;  
  65.     if(capacity==0)  
  66.         pthread_mutex_lock(&mylockB);  
  67.     else  
  68.         if(capacity==CAPACITY)  
  69.             pthread_mutex_lock(&mylockA);  
  70.     err = pthread_create(&produce_tid, NULL, produce, NULL);//创建线程  
  71.     if (err != 0)   
  72.     {  
  73.         printf("线程创建失败:%s\n", strerror(err));  
  74.         exit(-1);  
  75.     }  
  76.     err = pthread_create(&consume_tid, NULL, consume, NULL);  
  77.     if (err != 0)  
  78.     {  
  79.         printf("线程创建失败:%s\n", strerror(err));  
  80.         exit(-1);  
  81.     }  
  82.     err = pthread_join(produce_tid, &ret);//主线程等到子线程退出  
  83.     if (err != 0)   
  84.     {  
  85.         printf("生产着线程分解失败:%s\n", strerror(err));  
  86.         exit(-1);  
  87.     }  
  88.     err = pthread_join(consume_tid, &ret);  
  89.     if (err != 0)  
  90.     {  
  91.         printf("消费者线程分解失败:%s\n", strerror(err));  
  92.         exit(-1);  
  93.     }  
  94.     return (EXIT_SUCCESS);  
  95. }

posted on 2013-04-19 22:51  胡永光  阅读(207)  评论(0编辑  收藏  举报

导航