免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7830 | 回复: 3
打印 上一主题 下一主题

求多个线程死锁的简单例子. [复制链接]

论坛徽章:
1
IT运维版块每日发帖之星
日期:2016-08-10 06:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-06-09 17:20 |只看该作者 |倒序浏览
本帖最后由 asdf93945 于 2011-06-09 17:27 编辑

设有5个哲学家,共享一张放有五把椅子的桌子,每人分得一把椅子。但是,桌子上总共只有5支筷子。在每人两边分开各放一支。求哲学家饿死的线程死锁代码。


那位高手有空? 帮下忙。

论坛徽章:
2
白羊座
日期:2013-11-18 19:52:42辰龙
日期:2014-09-07 07:46:06
2 [报告]
发表于 2011-06-09 20:13 |只看该作者
不卫生阿!

论坛徽章:
0
3 [报告]
发表于 2011-06-09 23:47 |只看该作者
楼上的回复我表示很无语

论坛徽章:
0
4 [报告]
发表于 2011-06-10 00:28 |只看该作者
每人按序取得1根筷子,这时筷子已经取完,其中任何一个人再想取得筷子,这时已经没有筷子了,这个时候就死锁了吧。代码我随便写的(RedHat下测试通过):

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

  9. pthread_mutex_t         dinner_lock = PTHREAD_MUTEX_INITIALIZER;
  10. pthread_cond_t         dinner_cond = PTHREAD_COND_INITIALIZER;
  11. int                                 order = 0;
  12. sem_t                         dinner_sem;

  13. void *dinner_proc(void *param)
  14. {
  15.         int                index = *(int *)param;
  16.        
  17.         printf("enter %d thread\n", index);
  18.        
  19.         pthread_detach(pthread_self());                /* reclaim by itself */
  20.        
  21.         pthread_mutex_lock(&dinner_lock);
  22.        
  23.         while (order == index)
  24.                 pthread_cond_wait(&dinner_cond, &dinner_lock);
  25.        
  26.         sem_wait(&dinner_sem);                /* get a chopstick */
  27.         printf("%d got the first chopstick\n", index);
  28.        
  29.         pthread_mutex_unlock(&dinner_lock);
  30.        
  31.         ++order;
  32.         pthread_cond_signal(&dinner_cond);
  33.        
  34.         sleep(1);
  35. //        printf("ready to get second chopstick..\n");
  36.        
  37.         sem_wait(&dinner_sem);        /* dead lock */
  38.         printf("%d got second chopstick..\n", index);
  39.        
  40.         sleep(3);
  41.         printf("eat dinner...\n");
  42.        
  43.         sem_post(&dinner_sem);
  44.        
  45.         pthread_exit(0);
  46. }

  47. int main(void)
  48. {
  49.         int                i = 0;
  50.         pthread_t        tid;
  51.        
  52.         if (sem_init(&dinner_sem, 0, 5) < 0)        /* 5 chopsticks initilized */
  53.         {
  54.                 fprintf(stderr, "sem_init error: %s\n", strerror(errno));
  55.                 exit(-1);
  56.         }
  57.        
  58.         for (i = 0; i < 5; ++i)
  59.         {
  60.                 int        *pindex;
  61.                
  62. //                printf("create %d thread\n", i);
  63.                 pindex = (int *)malloc(sizeof(int));
  64.                 *pindex = i;
  65.                 if (pthread_create(&tid, NULL, dinner_proc, (void *)pindex) < 0)
  66.                 {
  67.                         fprintf(stderr, "pthread_create[%d] error: %s\n", (i+1), strerror(errno));
  68.                         exit(-1);
  69.                 }
  70.         }
  71.        
  72.         ++order;
  73.         pthread_cond_signal(&dinner_cond);
  74.        
  75.         pause();
  76.        
  77.         return 0;
  78. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP