收藏 分享(赏)

死锁问题.doc

上传人:cjc2202537 文档编号:6582642 上传时间:2019-04-18 格式:DOC 页数:9 大小:157KB
下载 相关 举报
死锁问题.doc_第1页
第1页 / 共9页
死锁问题.doc_第2页
第2页 / 共9页
死锁问题.doc_第3页
第3页 / 共9页
死锁问题.doc_第4页
第4页 / 共9页
死锁问题.doc_第5页
第5页 / 共9页
点击查看更多>>
资源描述

1、 哲学家就餐问题与死锁2.1 设计目的理解死锁的概念,掌握死锁预防方法。死锁是进程并发执行过程中可能出现的现象,哲学家就餐问题是描述死锁的经典例子。假设有几位哲学家围坐在一张餐桌旁,桌上有吃不尽的食品,每两位哲学家之间摆放着一根筷子,筷子的个数与哲学家的数量相等,每一位哲学家要么思考,要么等待,要么拿起左右两根筷子进餐。本设计假设有五个哲学家和五根筷子,它们的编号都是从 0 到 4。 如果每位哲学家都拿起左边的筷子,就会发生死锁。为了防止死锁,可以采用资源预分配法或者资源按序分配法。资源预分配法是指进程在运行前一次性地向系统申请它所需要的全部资源,如果系统当前不能够满足进程的全部资源请求,则不

2、分配资源, 此进程暂不投入运行,如果系统当前能够满足进程的全部资源请求, 则一次性地将所申请的资源全部分配给申请进程。资源按序分配法是指事先将所有资源类全排序, 即赋予每一个资源类一个唯一的整数,规定进程必需按照资源编号由小到大的次序申请资源。在哲学家就餐问题中,要采用资源预分配法只需让每个哲学家同时申请左右两根筷子。要采用资源按序分配法只需规定每个哲学家先申请左右两根筷子中编号小的筷子,再申请编号大的筷子。2.2 设计要求利用多线程技术编写哲学家就餐程序,使之在运行时能演示产生死锁的情况,也能演示采用死锁防止方法后不产生死锁的情况。程序要采用简单的控制台界面,运行后在屏幕上显示功能菜单,列出

3、该程序具有的功能,供用户选择,用户选择功能后应该转到相应的处理程序。程序应该包括以下功能:(1)演示死锁现象;(2)通过资源按序分配法防止死锁;(3)通过资源预分配法防止死锁;(4)退出。2.3 设计步骤2.3.1 程序结构设计程序需要六个线程,主线程用于显示主菜单,接收用户的功能选择;五个哲学家线程用于模拟哲学家的活动,即不停地思考、饥饿、进食。相邻的两个哲学家线程需要共享他们中间的同一根筷子,因此对每一根筷子的使用要互斥,用互斥体数组 h_mutex_chopsticks来实现。主线程创建五个哲学家线程后要等待所有哲学家结束,用线程句柄数组 h_thread来表示五个线程,主线程通过等待这

4、五个线程句柄来实现同步。该程序共有 7 个函数,这些函数可以分成 4 组。各组包含的函数及其功能如表 2-1 所示。组别 包括函数 函数功能一 main() 显示主菜单,接收用户的选择并执行相应的功能。二 deadlock_philosopher()deadlock() 演示死锁情况的哲学家线程函数初始化函数:创建五个哲学家并等待它们结束三 ordered_allocation_philosopher()ordered_allocation() 通过按序分配法防止死锁的哲学家线程函数初始化函数:创建五个哲学家并等待它们结束四 pre_allocation_philosopher() 通过预先分

5、配法防止死锁的哲学家线程函数pre_allocation() 初始化函数:创建五个哲学家并等待它们结束图 2-1 函数及其功能2.3.2 算法设计下面给出主要函数的算法描述。(1)deadlock_philosopher 函数while(1)随机等待一段时间;提示等待左边筷子;申请左边筷子;随机等待一段时间;提示等待右边筷子;申请右边筷子;提示正在进餐;放下左边筷子;放下右边筷子;(2)ordered_allocation_philosopher 函数while(1)随机等待一段时间;提示等待左右两边编号较小的筷子;申请编号较小的筷子;随机等待一段时间;提示等待左右两边编号较大的筷子;申请编号

6、较大的筷子;提示正在进餐;放下编号较小的筷子;放下编号较大的筷子;(3)pre_allocation_philosopher 函数while(1)提示等待左边筷子;提示等待右边筷子;同时申请左边和右边两根筷子;提示正在进餐;随机等待一段时间;放下左边筷子;放下右边筷子;(4)deadlock 函数为每根筷子创建一个互斥信号量;创建五个可能产生死锁的哲学家线程;等待五个哲学家线程结束;其他的初始化函数与 deadlock()的算法相同,只不过在创建线程时使用不同的线程函数。在 windows 中可以用系统调用 WaitForMultipleObjects()同时申请两份资源,具体解法如下所示。#

7、define N 5typedef enumthinking, hungry, eatingstatus;status stateN;semaphore selfN;semaphore mutex = 1;void test(int i)if(statei = hungry)V(selfi);void pick_chopsticks(int i)P(mutex);statei = hungry;test(i);V(mutex);P(selfi);void put_chopsticks(int i)P(mutex);statei = thinking;test(i-1)%N);test(i+1)

8、%N);V(mutex);void philosopher(int i)while(1)think();pick_chopsticks(i);eat();put_chopsticks(i);void mainint i;for(i=0;i#include #include #include #include #include #include #define MAX_PHILOSOPHERS 5 /待测试的哲学家数#define ZERO 48 /数字 0 的 ASCII 码#define DELAY rand()%25HANDLE h_mutex_chopsticksMAX_PHILOSOP

9、HERS; /互斥体数组,每根筷子需要一个互斥体int thread_numberMAX_PHILOSOPHERS=0,1,2,3,4;/会产生死锁的哲学家线程int deadlock_philosopher(LPVOID data)int philosopher_number=*(int *)(data); /哲学家编号for(;)srand( (unsigned)time( NULL ) * ( philosopher_number+ 1) );Sleep(DELAY);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“

10、is waiting chopstick “,(ZERO+philosopher_number);WaitForSingleObject(h_mutex_chopsticksphilosopher_number, INFINITE);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ got chopstick “,(ZERO+philosopher_number);Sleep(DELAY/4);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is wai

11、ting chopstick “,(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);WaitForSingleObject(h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS), INFINITE);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ got chopstick “,(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);printf(“%s%c%sn“,“Philo

12、sopher “,ZERO+philosopher_number,“ is eating.“);Sleep(DELAY);ReleaseMutex(h_mutex_chopsticksphilosopher_number);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ released chopstick “,ZERO+philosopher_number);ReleaseMutex(h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS);printf(“%s

13、%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ released chopstick “,(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);Sleep(DELAY); / end forreturn 0;/死锁时的初始化程序void deadlock()int i=0;HANDLE h_threadMAX_PHILOSOPHERS;printf(“可能出现死锁的哲学家就餐问题n“);for(i=0;iMAX_PHILOSOPHERS;i+)h_mutex_chopsticksi=CreateMute

14、x(NULL,FALSE,NULL);for(i=0;iMAX_PHILOSOPHERS;i+)h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_philosopher),;WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);/通过按序分配资源预防死锁的哲学家线程int ordered_allocation_philosopher(LPVOID data)int philosopher_number=*(int *)(data);for(;)srand(

15、 (unsigned)time( NULL ) * ( philosopher_number+ 1) );Sleep(DELAY);if(philosopher_number=MAX_PHILOSOPHERS-1)printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstick “,(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);WaitForSingleObject(h_mutex_chopsticks(1+philosopher_number)%MA

16、X_PHILOSOPHERS, INFINITE);Sleep(DELAY/4);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstick “,ZERO+philosopher_number);WaitForSingleObject(h_mutex_chopsticksphilosopher_number, INFINITE);elseprintf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstic

17、k “,ZERO+philosopher_number);WaitForSingleObject(h_mutex_chopsticksphilosopher_number, INFINITE);Sleep(DELAY/4);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstick “,ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);WaitForSingleObject(h_mutex_chopsticks(1+philosopher_number

18、)%MAX_PHILOSOPHERS, INFINITE);printf(“%s%c%sn“,“Philosopher “,ZERO+philosopher_number,“ is eating.“);Sleep(DELAY);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is releasing chopstick “,ZERO+philosopher_number);ReleaseMutex(h_mutex_chopsticksphilosopher_number);printf(“%s%c%s%cn“,“Philo

19、sopher “,ZERO+philosopher_number,“ is releasing chopstick “,ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);ReleaseMutex(h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS);Sleep(DELAY); / end forreturn 0;/通过按序分配资源预防死锁的初始化程序void ordered_allocation()int i=0;HANDLE h_threadMAX_PHILOSOPHERS;printf

20、(“可能出现死锁的哲学家就餐问题n“);for(i=0;iMAX_PHILOSOPHERS;i+)h_mutex_chopsticksi=CreateMutex(NULL,FALSE,NULL);for(i=0;iMAX_PHILOSOPHERS;i+)h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(ordered_allocation_philosopher),;WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);/通过资源预分配法预防死锁的哲学家线程int pre_

21、alloction_philosopher(LPVOID data)int philosopher_number=*(int *)(data);HANDLE h_mutex_leftandright_chopsticks2;h_mutex_leftandright_chopsticks0=h_mutex_chopsticksphilosopher_number;h_mutex_leftandright_chopsticks1=h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS;for(;)srand( (unsigned)time

22、( NULL ) * ( philosopher_number+ 1) );Sleep(DELAY);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstick “,ZERO+philosopher_number);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is waiting chopstick “,ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);WaitForMulti

23、pleObjects(2, h_mutex_leftandright_chopsticks, TRUE, INFINITE);printf(“%s%c%sn“,“Philosopher “,ZERO+philosopher_number,“ is eating.“);Sleep(DELAY);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is releasing chopstick “,ZERO+philosopher_number);ReleaseMutex(h_mutex_chopsticksphilosopher_

24、number);printf(“%s%c%s%cn“,“Philosopher “,ZERO+philosopher_number,“ is releasing chopstick “,ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS);ReleaseMutex(h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS);Sleep(DELAY); / end forreturn 0;/通过资源预分配法预防死锁的初始化程序void pre_alloction()int i=0;HANDLE h_t

25、hreadMAX_PHILOSOPHERS;printf(“可能出现死锁的哲学家就餐问题n“);for(i=0;iMAX_PHILOSOPHERS;i+)h_mutex_chopsticksi=CreateMutex(NULL,FALSE,NULL);for(i=0;iMAX_PHILOSOPHERS;i+)h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(pre_alloction_philosopher),;WaitForMultipleObjects(MAX_PHILOSOPHERS,h_thread,TRUE,-1);/主程序

26、int main(int argc,char *argv)char select;while(1)printf(“|-|n“);printf(“| 1:deadlock |n“);printf(“| 2:non_deadlock by ordered allocation |n“);printf(“| 3:non_deadlock by pre_allocation |n“);printf(“| 4:exit |n“);printf(“|-|n“);printf(“select a function(14):“);doselect=(char)getch();while(select!=1sy

27、stem(“cls“);switch(select)case 1:deadlock();break;case 2:ordered_allocation();break;case 3:pre_alloction();break;case 4:return 0;printf(“nPress any key to return to main menu.“);getch();system(“cls“);return 0;2.3.4 测试程序图 2-2 程序主界面 图 2-3 死锁情况程序运行的主界面如图 2-2 所示,输入 1 后,可以看到屏幕输出一些提示信息后就不再更新了,程序也停止不动了,这说明发生了死锁,如图 2-3 所示。分析提示信息可以了解死锁的详情,即每个哲学家都得到了左边的筷子,又都在等待右边的筷子。按 ctrl+c 终止程序。重新运行程序,输入 2 或者 3,可以看到屏幕一直更新提示信息,说明没有死锁发生,程序将一直运行下去,要想结束程序必须按 ctrl+c。2.4 不足与改进该程序虽然能防止死锁,但是可能存在饿死现象,请读者改进程序使之既没有死锁,也没有饿死现象。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报