当前位置:
文档之家› 操作系统哲学家就餐问题课程设计C语言
操作系统哲学家就餐问题课程设计C语言
家的自定义标识符以及餐桌上所有几位哲学家标识符及其所坐的位置。 3)设定共有 5 个哲学家需用餐。每位用餐耗时 10 秒钟以上。 4)多个哲学家须共享操作函数代码。
2.总体设计思想及系统平台、语言、工具
2.1 总体设计思想
哲学家就餐问题,即共有 5 个哲学家绕一个圆桌做在 5 个位置上,他们每 2 个人中间有一只筷子,共 5 只筷子,只有当每个哲学家取得他左右两边的筷子时, 哲学家才能开始就餐,其它时间,哲学家只能思考或等待筷子。为避免哲学家互 相等待对方的筷子发生死锁,本次课程设计要求只许 4 个哲学家入席,以保证至 少有一个哲学家能够进餐。
要求完成的主要任务: (包括课程设计工作量及其技术要求,以及说明书撰写等具
体要求)
1.技术要求: 1)为每个哲学家产生一个线程,设计正确的同步算法 2)每个哲学家取得一双筷子开始用餐后,即时显示“Dining…”和该哲 学家的自定义标识符以及餐桌上所有几位哲学家标识符及其所坐 的位置。 3)设定共有 5 个哲学家需用餐。每位用餐耗时 10 秒钟以上。 4)多个哲学家须共享操作函数代码。
增加信号量值函数 sem_post 声明如下: #include <semaphore.h> Sem_post ( sem_t *sem );
减少信号量值函数 sem_wait 声明如下 #include <semaphore.h> Sem_wait ( sem_t *sem );
主要数据结构声明:
{
think();
wait(room);
//请求入席进餐
wait(chopstick[i]);
//请求左手边的筷子
wait(chopstick[(i+1)%5]); //请求右手边的筷子
eat();
signal(chopstick[i]);
//释放左手边的筷子
signal(chopstick[(i+1)%5]); //释放右手边的筷子
for(j=0;j<NUMBERS;j++) {
if((j!=i)&&flag[j]) printf("philosopher %d ,on chairs %d and hungry\n",j,chairs[j]); }
sleep(3); sem_post(&mutex); sem_post(&chopstics[i]); sem_post(&chopstics[(i+1)%NUMBERS]); printf("philosopher %d is full and put down chopstics %d and %d and left\n",i,i,(i+1)%NUMBERS); printf("*********************************************\n\n"); flag[i]=0; sem_post(&room); }
5.运行结果
6.调试记录
(1)将写好的代码进行编译,出现如下错误提示:
5
武汉理工大学《操作系统》课程设计
1.c:(.text+0x37): undefined reference to `sem_init' 1.c:(.text+0x6a): undefined reference to `sem_init' 1.c:(.text+0x86): undefined reference to `sem_init' 1.c:(.text+0xc8): undefined reference to `pthread_create' 1.c:(.text+0x108): undefined reference to `pthread_join' /tmp/ccq8XD3O.o: In function `Share': 1.c:(.text+0x13f): undefined reference to `sem_wait' 1.c:(.text+0x160): undefined reference to `sem_wait' 1.c:(.text+0x1b0): undefined reference to `sem_wait' 1.c:(.text+0x1f7): undefined reference to `sem_wait' 1.c:(.text+0x2ad): undefined reference to `sem_post' 1.c:(.text+0x2c0): undefined reference to `sem_post' 1.c:(.text+0x2f5): undefined reference to `sem_post' 1.c:(.text+0x35d): undefined reference to `sem_post' collect2: ld returned 1 exit status 检查发现,pthread 库不是 Linux 系统默认的库,连接时需要使用库 libpthread.a, 所以在使用 pthread_create 创建线程时,在编译中 要加-lpthread 参数: gcc -lpthread -o 1 1.c (2)重新编译代码,出现如下错误提示: 1.c:9: error: invalid initializer 1.c:10: error: invalid initializer 1.c: In function ‘main’: 1.c:35: warning: incompatible implicit declaration of built-in function ‘exit’ 1.c: In function ‘Share’: 1.c:48: error: incompatible types when assigning to type ‘sem_t’ from type ‘int’ 1.c:71: error: expected ‘;’ before ‘sem_post’ 1.c:76: error: incompatible types when assigning to type ‘sem_t’ from
(2)编译命令可用:
cc
(3)多线程编程方法参见附件。)
3. 调试报告:
1) 调试记录
2) 自我评析和总结
上机时间安排:
18 周一 ~ 五 08:0 - 12:00
-lpthread
-o 目标文件名
源文件名
指导教师签名:
年月日
系主任(或责任教师)签名:
年月 日
用多线程同步方法解决哲学家就餐
问题
武汉理工大学《操作系统》课程设计
error = pthread_create(&threads[i],NULL,(void*)Share,(void *)i);
if(error) {
printf("ERROR: thread create failed!!!"); //exit(-1); } } for(i=0;i<NUMBERS;i++) { pthread_join(threads[i],NULL); } } void *Share(int threadid) { int i = threadid; sem_wait(&room);
(Dining-Philosophers Problem)
1.设计题目与要求
1.1 设计题目描述:
用多线程同步方法解决哲学家就餐问题(Dining-Philosophers Problem)
1.2 要求:
1)为每个哲学家产生一个线程,设计正确的同步算法 2)每个哲学家取得一双筷子开始用餐后,即时显示“Dining…”和该哲学
2.2 系统平台、语言及工具
(1)操作系统:Linux (2)程序设计语言:C 语言 (3)工具:编辑工具 Vi、编译器 gcc
1
武汉理工大学《操作系统》课程设计
3.数据结构与模块说明
线程创建函数 pthread_create 声明如下: #include <pthread.h> int pthread_create (pthread_t *thread,pthread_attr_t *attr,Void*
武汉理工大学《操作系统》课程设计
题 目 : 用 多 线 程 同 步 方 法 解 决 哲 学 家 就 餐 问 题 (Dining-Philosophers
Problem)
初始条件:
1.操作系统:Linux 2.程序设计语言:C 语言 3.共有 5 个哲学家需用餐。只许 4 个哲学家入席且桌上有 5 支筷子。
(*start_routine)(void *),void *arg);
等待其它线程结束函数 pthread_join 声明如下: #include <pthread.h> int pthread_join (pthread_t th,void *thread_return);
信号量的数据类型为结构 sem_t,它本质上是一个长整型的数。 初始化信号量函数 sem_init 声明如下: #include <semaphore.h> sem_init (sem_t *sem, int pshared, unsigned int value);
2. 设计说明书内容要求:
1)设计题目与要求
2)总的设计思想及系统平台、语言、工具等。
3)数据结构与模块说明(功能与流程图)
4)给出用户名、源程序名、目标程序名和源程序及其运行结果。(要