当前位置:文档之家› 电子科大计算机操作系统实验报告级

电子科大计算机操作系统实验报告级

电子科技大学实验报告学生姓名:郫县LBJ 学号:指导教师:温柔可爱的刘杰彦实验地点:主楼A2-413 实验时间:2017年4月22日上午一、实验室名称:计算机学院主楼机房二、实验项目名称:进程与资源管理实验分工:郫县LBJ 进程管理设计郫县小胖子资源管理设计郫县威斯布鲁克进程调度与时钟中断设计三、实验学时:2四、实验原理:此处的实验原理在指导书上非常丰富,因此不照搬过来,主要写出所要使用到知识点,具体实现过程中的原理分析见报告第八部分“实验步骤”处。

(一)总体设计系统总体架构如图1所示,最右边部分为进程与资源管理器,属于操作系统内核的功能。

要求能够设计与实现一个简单的进程与资源管理器,具有如下功能:完成进程创建、撤销和进程调度;完成多单元 (multi_unit)资源的管理;完成资源的申请和释放;完成错误检测和定时器中断功能。

图1 系统总体结构(二) Test shell设计应具有的功能:1、从终端或者测试文件读取命令;2、将用户需求转换成调度内核函数(即调度进程和资源管理器);3、在终端或输出文件中显示结果:如当前运行的进程、错误信息等。

(三)进程管理设计1、进程状态与操作2、进程控制块结构PCB3、主要函数:创建进程、撤销进程(四)资源管理设计1、主要数据结构RCB2、请求资源3、释放资源(五)进程调度与时钟中断设计关键:使用基于优先级的抢占式调度策略,在同一优先级内使用时间片轮转算法。

参考课上ppt:五、实验目的:设计和实现进程与资源管理,并完成Test shell的编写,以建立系统的进程管理、调度、资源管理和分配的知识体系,从而加深对操作系统进程调度和资源管理功能的宏观理解和微观实现技术的掌握。

六、实验内容:设计与实现一个简单的进程与资源管理器,要求具有如下功能:完成进程创建、撤销和进程调度;完成多单元 (multi_unit)资源的管理;完成资源的申请和释放;完成错误检测和定时器中断功能。

通过编写测试脚本(test shell)来完成对进程与资源管理器的测试。

七、实验环境(设备、元器件):Windows 7、Visual Studio 2015八、实验步骤:(一)系统功能需求分析:(二)总体框架设计:1、具体原理和总体工作流程分析:首先,通过test shell从测试文件中读入各种命令。

然后,对命令进行分析,将用户的需求转换成调度内核函数,也就是说,通过调度进程和资源管理器,实现创建进程、撤销进程、进程调度、对资源进行管理、申请和释放资源、检测错误和定时器中断等功能,从而模拟一个操作系统对进程进行调度和对资源进行管理的过程。

最后,在终端或者输出文件中,把一系列操作后的结果显示出来,包括当前运行的进程、错误信息等。

2、相关方法和算法:(1) C语言中的结构struct,用来实现PCB、RCB等(2) C语言中的指针、链表操作,用来实现将PCB和RCB加入队列尾部、从队列中删除、转移至阻塞队列等操作,以及进程的调度执行等。

本实验中我们采用的带头结点的链表来实现各种操作。

(3)基于优先级的调度算法、时间片轮转调度算法、抢占式调度算法的综合应用。

3、模块调用关系:本实验中,我们组共编写了三个头文件(pcb.h、rcb.h、test_shell_data.h)和四个源文件(main.c、pcb.c、rcb.c、test_shell_data.c),因此可以分为主函数设计模块、进程管理设计模块、资源管理设计模块和test shell设计模块。

在主函数模块中,需要调用其他三个模块,如创建进程、展示父子子进程等操作,需要调用进程管理设计模块;调度算法的执行、展示各种队列等,需要调用test shell设计模块;在进程管理设计模块中,像销毁PCB等操作,需要执行对RCB的释放,则需调用testshell设计模块;在资源管理设计模块中,提供一些最小的操作,不调用其他模块;在test shell设计模块中,设计到对资源和进程的各种操作,需要调用资源管理设计模块和进程管理设计模块。

(三)进程管理设计模块详细设计(本部分我负责实现)我们的计划是在在进程管理设计模块中,实现关于进程的各种最基础的结构和操作,具体包括:实现PCB结构体、PCB链表、PCB子节点链表;实现对PCB链表的初始化、对子节点链表的初始化、新建PCB、对PCB链表中进行删除、插入、移除(不free)、从等待和阻塞队列中获取PCB得知、打印当前PCB父节点、打印当前PCB父节点、打印当前PCB子节点链表、插入子队列的尾部、从子队列尾部删除。

1、进程状态与操作(1)进程状态共ready/running/blocked三种状态,通过结构struct实现,代码如下:struct{int running;int blocked;int ready;}PCB_STATUS;//定义pcb状态的三种情况(2)进程操作:在本次实验中,将会读进程进行以下操作,结合这些操作的具体内容和所学知识,很容易考虑到通过链表来实现这些操作。

创建(create): (none) -> ready撤销(destroy): running/ready/blocked -> (none)请求资源(Request): running -> blocked (当资源没有时,进程阻塞)释放资源(Release): blocked -> ready (因申请资源而阻塞的进程被唤醒)时钟中断(Time_out): running -> ready调度:ready -> running / running ->ready2、主要数据结构实现:(1)进程控制块结构PCB进程控制块PCB是进程存在的唯一标识,并且常驻内存,进程控制块中有许多信息,在本次实验中,根据我们的需求,所设计的进程控制块结构如下:结合实验指导书,我们通过结构struct实现进程控制块结构PCB,包含以下信息:PID(name)Other_resources //: resource which is occupiedStatus: Type & List// type: ready, block, running…., //List: RL(Ready list) or BL(block list)Creation_tree: Parent/ChildrenPriority: 0, 1, 2 (Init, User, System)主要代码及注释如下:struct PCB {//pcb结构体char name[64];//Pnameunsigned int pid;//Pidstruct RCB_LIST* rcb_list;//Other resourcesstruct PCB_LIST* parent_pcb;//父进程struct CHILD_PCB_LIST* child_pcb;//子进程int state;//Type&Listint priority;//0,1,2};(3)实现一个PCB的链表,方便后面的操作:struct PCB_LIST {//pcb链表struct PCB pcb;struct PCB_LIST * next_pcb;};(4)实现PCB的子节点链表:struct CHILD_PCB_LIST {//pcb子节点链表struct PCB_LIST* node;struct CHILD_PCB_LIST* next_node;};3、主要操作设计实现过程(1)初始化PCB链表(添加了头结点):void init_pcb_list(struct PCB_LIST **list){if(*list){return;}struct PCB_LIST *p =(struct PCB_LIST*)malloc(sizeof(struct PCB_LIST));p->next_pcb =NULL;memset(p,NULL,sizeof(struct PCB));*list = p;}(2)初始化子节点链表:void init_child_pcb_list(struct CHILD_PCB_LIST **list){if(*list){return;}struct CHILD_PCB_LIST *p =(struct CHILD_PCB_LIST*)malloc(sizeof(struct CHILD_PCB_LIST));p->next_node =NULL;memset(p,NULL,sizeof(struct CHILD_PCB_LIST));*list = p;}(3)创建一个新的PCB:struct PCB_LIST*create_pcb(char*name,unsigned int pid,int state,unsigned int priority,struct PCB_LIST* parent_pcb){struct PCB pcb;strcpy(,name,strlen(name));pcb.pid = pid;pcb.rcb_list =NULL;pcb.state = state;pcb.priority = priority;pcb.parent_pcb = parent_pcb;pcb.child_pcb =NULL;struct PCB_LIST* pcb_node =(struct PCB_LIST*)malloc(sizeof(struct PCB_LIST));pcb_node->pcb = pcb;pcb_node->next_pcb =NULL;return pcb_node;}(4)从PCB链表中进行删除:void destory_from_pcb_list(struct PCB_LIST* list,char*name){struct PCB_LIST* pr_temp,*temp;pr_temp = temp = list;int ret =1;while(temp){if(!strcmp(name, temp->)&& ret){release_resource(temp);pr_temp = temp = list;ret =0;}if(!strcmp(name, temp->)){pr_temp->next_pcb = temp->next_pcb; free(temp);return;}pr_temp = temp;temp = temp->next_pcb;}}(5)插入pcb链表:void insert_into_pcb_list(struct PCB_LIST** list,struct PCB_LIST*node) {if(!*list)init_pcb_list(list);struct PCB_LIST *pr_temp,*temp;pr_temp = temp =*list;while(temp){pr_temp = temp;temp = temp->next_pcb;}pr_temp->next_pcb = node;}(5)从PCB链表中移除,并不释放该PCB占用的空间:void delete_from_pcb_list(struct PCB_LIST* list,char*name) {struct PCB_LIST* pr_temp,*temp;pr_temp = temp = list;while(temp){if(!strcmp(name, temp->)){pr_temp->next_pcb = temp->next_pcb;return;}pr_temp = temp;temp = temp->next_pcb;}}(6)从等待和阻塞队列中获取PCB的地址:struct PCB_LIST* get_pcb(char* name){struct PCB_LIST* temp;for(int i =2; i >=0; i--){temp = READY_LIST[i]->next_pcb;while(temp){if(!strcmp(temp->, name)){return temp;}temp = temp->next_pcb;}}if(BLOCKED_LIST)temp = BLOCKED_LIST->next_pcb;while(temp){if(!strcmp(temp->, name)){return temp;}temp = temp->next_pcb;}return NULL;}(7)打印当前PCB的父节点void show_pcb_parent(struct PCB_LIST* node){printf("%s parent node is %s \n", node->, node->pcb.parent_pcb->);}(8)打印当前PCB的子节点链表void show_pcb_child(struct PCB_LIST* node){printf("%s child is ", node->);struct CHILD_PCB_LIST* temp = node->pcb.child_pcb;if(temp)temp = temp->next_node;while(temp){printf(" --> |%s|", temp->node->);temp = temp->next_node;}printf("\n");}(9)插入子队列的尾部void insert_into_child_pcb_list(struct CHILD_PCB_LIST** list,struct PCB_LIST*node){if(!*list)init_child_pcb_list(list);struct CHILD_PCB_LIST *pr_temp,*temp;pr_temp = temp =*list;while(temp){pr_temp = temp;temp = temp->next_node;}struct CHILD_PCB_LIST *p =(struct CHILD_PCB_LIST*)malloc(sizeof(struct CHILD_PCB_LIST));p->node = node;p->next_node =NULL;pr_temp->next_node = p;}(10)从子队列尾部进行删除void delete_from_child_pcb_list(struct CHILD_PCB_LIST* list,char*name) {struct CHILD_PCB_LIST* pr_temp,*temp;if(!list)return;pr_temp = list;temp = pr_temp->next_node;while(temp){if(!strcmp(name, temp->node->)){pr_temp->next_node = temp->next_node;return;}pr_temp = temp;temp = temp->next_node;}}(四)其他模块设计(组内其他人设计)1、Test shell设计Test shell将调度我们设计的进程与资源管理器,从而完成测试,具有以下功能:(1)从终端或者测试文件读取命令;(2)将用户需求转换成调度内核函数(即调度进程和资源管理器);(3)在终端或输出文件中显示结果:如当前运行的进程、错误信息等。

相关主题