




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、_ 成绩(五级制):_武汉科技大学城市学院操作系统实验报告院 系 武汉科技的大学城市学院学生专业 信科年级 班 大三课程名称 操作系统实验题目 进程调度学生姓名 宋骋指导教师 郭冀生2013年 11 月 4 日实验二 进程调度一、实验目的进程是操作系统最重要的概念之一,进程调度又是操作系统核心的重要内容。通过该实验,要求同学们了解各进程在执行过程中的状态和参数的变化情况,以便于观察诸进程的调度过程。二、实验内容及要求 按剥夺式优先数法对三个进程P1,p2,p3进行模拟调度,各进程的优先数静态设置,其中P1的优先数最高,P3的优先数最低。每个进程都处于执行E(execute),就绪R(ready
2、)和等待W(wait)三种状态之一,并假定初始状态均为R.。三个进程有如下同步关系:P1因等待事件1被阻塞后由P2发现并唤醒之,P2因等待事件2被阻塞后由P3发现并唤醒之。当系统进入运行,在完成必要的初始化工作以后便进入进程调度,首先选择优先数最高的进程使其进入执行(分配CPU)。当执行进程因等待某个事件被阻塞或唤醒某个等待进程时,转入进程调度。如果被唤醒的进程的优先数大于现行的执行进程,则剥夺现行进程的执行权,而将CPU分配给被唤醒的进程。当系统处于死锁或三个进程都执行完毕时系统退出运行。系统中应用到如下数据结构:*进程控制块PCB;*信号量sem;*其它需要的数据结构。由自己设计。三、实验
3、原理及步骤 根据现代操作系统的特征1并发性(concurrence);2共享性(sharing);3虚拟性(virtual);4异步性(asynchronism) 。模拟出进程在执行中的状态变化过程;体会进程申请资源、使用资源、归还资源;体会死锁。步骤(参考框图)4、算法和流程图可强占优先调度算法实现过程流程图(如下图): 四、程序运行1 选择输入执行程序(如下图)2 可强占优先调度算法图(如下图)五.设计总结:通过该课程设计,加深了对系统进程调度机制的理解。在抢占方式中实践了“抢占” 必须遵循的原则:优先权原则。认识了几种进程调度算法的优缺点以及应用范围。加强C+的编程能力,实现类的封装。附
4、录:程序及注释(用红色黑体标注自己设计的函数)/进程PCB类和模拟cpu的进程类的声明#include <stdio.h>#include <stdlib.h>#include <windows.h>#include <conio.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#include <io.h>#include <string.h>#define MAX_PHILOSOPHERS 3 /待测试的哲学家数#def
5、ine ZERO 48 /数字0的ASCII码#define DELAY rand()%25struct PCB char p_name20; int p_priority; int p_needTime; int p_runTime; char p_state;char deadlock(); struct PCB* next;void HighPriority();void deadlock();void Information();/形参的改变映射给实参 说白了就是实参传过去不用return 返回就可以把实参改变char Choice();struct PCB* SortList(PCB
6、* HL);int main(int argc,char *argv) Information(); char choice = Choice(); switch(choice) case '1': system("cls"); HighPriority(); break; case '2': system("cls"); void deadlock(); break; default: break; system("pause"); return 0;void Information() printf
7、("nn"); printf(" * n"); printf(" 模拟进程调度算法n"); printf(" * nnn"); printf("静态优先级调度算法"); printf("死锁问题"); printf("按回车键进入演示程序"); getchar(); system("cls");system("cls");char Choice() printf("nn"); printf(&qu
8、ot; * n"); printf(" 进程调度演示n"); printf(" * nnn"); printf(" 1.演示最高优先数优先算法。n"); printf(" 2.演示死锁问题。n"); printf(" 3.退出程序。nnnn"); printf(" 选择进程调度方法:"); printf("select a function(13):"); char ch = getchar(); return ch; system("
9、cls");void HighPriority() struct PCB *processes, *pt; /pt作为临时节点来创建链表 processes = pt = (struct PCB*)malloc(sizeof(struct PCB); for (int i = 1; i != 4; +i) struct PCB *p = (struct PCB*)malloc(sizeof(struct PCB); printf("进程号No.%d:n", i); printf("输入进程名:"); scanf("%s",
10、p->p_name); printf("输入进程优先数:"); scanf("%d", &p->p_priority); printf("输入进程运行时间:"); scanf("%d", &p->p_needTime); p->p_runTime = 0; p->p_state = 'W' p->next = NULL; pt->next = p; pt = p; printf("nn"); getchar(); /接受回
11、车 /processes作为头结点来存储链表 processes = processes->next; int cases = 0; struct PCB *psorted = processes; while (1) +cases; pt = processes; /对链表按照优先数排序 /psorted用来存放排序后的链表 psorted = SortList(psorted); printf("The execute number: %dnn", cases); printf("* 当前正在运行的进程是:%sn", psorted->p
12、_name); psorted->p_state = 'R' printf("qname state super ndtime runtimen"); printf("%st%ct%dt%dt%dtnn", psorted->p_name, psorted->p_state, psorted->p_priority, psorted->p_needTime, psorted->p_runTime); pt->p_state = 'W' psorted->p_runTime+;
13、 psorted->p_priority-; printf("* 当前就绪状态的队列为:nn"); /pt指向已经排序的队列 pt = psorted->next; while (pt != NULL) printf("qname state super ndtime runtimen"); printf("%st%ct%dt%dt%dtnn", pt->p_name, pt->p_state, pt->p_priority, pt->p_needTime, pt->p_runTime); p
14、t = pt->next; /pt指向已经排序的链表,判断链表是否有已用时间啊等于需要时间的 pt = psorted; struct PCB *ap; ap = NULL; /ap指向pt的前一个节点 while (pt != NULL) if (pt->p_needTime = pt->p_runTime) if (ap = NULL) pt = psorted->next; psorted = pt; else ap->next = pt->next; ap = pt; pt = pt->next; if (psorted->next =
15、NULL) break; getchar(); struct PCB* SortList(PCB* HL) struct PCB* SL; SL = (struct PCB*)malloc(sizeof(struct PCB); SL = NULL; struct PCB* r = HL; while (r != NULL) struct PCB* t = r->next; struct PCB* cp = SL; struct PCB* ap = NULL; while (cp != NULL) if (r->p_priority > cp->p_priority)
16、break; else ap = cp; cp = cp->next; if (ap = NULL) r->next = SL; SL = r; else r->next = cp; ap->next = r; r = t; return SL;/HANDLE h_mutex_chopsticksMAX_PHILOSOPHERS; /互斥体数组,每根筷子需要一个互斥体int thread_numberMAX_PHILOSOPHERS=1,2,3;/定义死锁的个数/会产生死锁的哲学家线程int deadlock_philosopher(LPVOID data)int ph
17、ilosopher_number=*(int *)(data); /哲学家编号for(;)srand( (unsigned)time( NULL ) * ( philosopher_number+ 1) ); Sleep(DELAY); printf("%s%c%s%cn","Philosopher ",ZERO+philosopher_number," is waiting chopstick ",(ZERO+philosopher_number); WaitForSingleObject(h_mutex_chopsticksphi
18、losopher_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 waiting chopstick ",(ZERO+(1+phi
19、losopher_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%s
20、n","Philosopher ",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_
21、mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS);printf("%s%c%s%cn","Philosopher ",ZERO+philosopher_number," released chopstick ",(ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS); Sleep(DELAY); / end forreturn 0;/死锁时的初始化程序void deadlock()char choice;int i=0;HANDLE
22、h_threadMAX_PHILOSOPHERS;printf("可能出现死锁的哲学家就餐问题n");for(i=0;i<MAX_PHILOSOPHERS;i+)h_mutex_chopsticksi=CreateMutex(NULL,FALSE,NULL);for(i=0;i<MAX_PHILOSOPHERS;i+)h_threadi=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)(deadlock_philosopher),&thread_numberi,0,NULL);WaitForMultipleObje
23、cts(MAX_PHILOSOPHERS,h_thread,TRUE,-1);dochoice=(char)getch();while( choice!='2');system("cls");deadlock();printf("nPress any key to return to main menu.");getch();system("cls");/通过按序分配资源预防死锁的哲学家线程int ordered_allocation_philosopher(LPVOID data)int philosopher_nu
24、mber=*(int *)(data);for(;)srand( (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)
25、; WaitForSingleObject(h_mutex_chopsticks(1+philosopher_number)%MAX_PHILOSOPHERS, INFINITE); Sleep(DELAY/4); printf("%s%c%s%cn","Philosopher ",ZERO+philosopher_number," is waiting chopstick ",ZERO+philosopher_number); WaitForSingleObject(h_mutex_chopsticksphilosopher_num
26、ber, INFINITE);else printf("%s%c%s%cn","Philosopher ",ZERO+philosopher_number," is waiting chopstick ",ZERO+philosopher_number); WaitForSingleObject(h_mutex_chopsticksphilosopher_number, INFINITE); Sleep(DELAY/4); printf("%s%c%s%cn","Philosopher ",ZE
27、RO+philosopher_number," is waiting chopstick ",ZERO+(1+philosopher_number)%MAX_PHILOSOPHERS); WaitForSingleObject(h_mutex_chopsticks(1+philosopher_number)%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);
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 涉及跨境婚姻的离婚协议民族法解读合同
- 离婚房产分割及个人财产分割与债务承担协议
- 动力供应合同(一级)新3篇
- 房产中介房屋租赁合同范本3篇
- 可再生能源在医院中的应用-洞察及研究
- 智能材料与设计结合-洞察及研究
- 2025至2030中国建筑雕刻制品行业发展分析及发展趋势分析与未来投资战略咨询研究报告
- 交通网络复杂性分析方法-洞察及研究
- 机械绘图竞赛试题及答案
- 常州物理竞赛初赛试题及答案
- 浙江水运交通工程安全管理台帐
- 蒙台梭利教学法(学前教育专业)全套教学课件
- 无犯罪证明委托书模板
- 朗文3000词汇表大全
- YYT 1898-2024 血管内导管导丝 亲水性涂层牢固度试验方法
- 铅锌矿开采中的环境影响评估与风险防范
- 旅游咨询服务培训课件
- 铁路交通事故调查处理-铁路交通事故救援
- 妇科宫腔镜诊治规范课件
- 除尘布袋更换应急救援预案
- 肩关节病护理查房
评论
0/150
提交评论