1、操作系统实验报告操作系统实验报告 班 级:计算机1007班 姓 名: 陶向东 学 号: 100511726 日 期: 2012.5.27 实验二 进程调度1目的和要求通过这次实验,理解进程调度的过程,进一步掌握进程状态的转变、进程调度的策略,进一步体会多道程序并发执行的特点,并分析具体的调度算法的特点,掌握对系统性能的评价方法。2实验内容阅读教材计算机操作系统第二章和第三章,掌握进程管理及调度相关概念和原理。编写程序模拟实现进程的轮转法调度过程,模拟程序只对PCB进行相应的调度模拟操作,不需要实际程序。假设初始状态为:有n个进程处于就绪状态,有m个进程处于阻塞状态。采用轮转法进程调度算法进行调
2、度(调度过程中,假设处于执行状态的进程不会阻塞),且每过t个时间片系统释放资源,唤醒处于阻塞队列队首的进程。程序要求如下:1)输出系统中进程的调度次序;2)计算CPU利用率。3实验环境Windows操作系统、VC+6.0C语言4设计思想1) 程序中进程可用PCB表示,其类型描述如下: struct PCB_type int pid ; /进程名int state ; /进程状态 2表示“执行”状态 1表示“就绪”状态 0表示“阻塞”状态 int cpu_time ; /运行需要的CPU时间(需运行的时间片个数) 2) 设置两个队列,将处于“就绪”状态的进程PCB挂在队列ready中;将处于“阻
3、塞”状态的进程PCB挂在队列blocked中。 队列类型描述如下: struct QueueNode struct PCB_type PCB; Struct QueueNode *next;并设全程量:struct QueueNode *ready_head=NULL, /ready队列队首针*ready_tail=NULL , /ready队列队尾指针*blocked_head=NULL, /blocked队列队首针*blocked_tail=NULL; /blocked队列队尾针3)设计子程序: start_state(); /读入假设的数据,设置系统初始状态dispath(); /模拟调
4、度 calculate(); /计算CPU利用率5. 源程序#include #include #include #include using namespace std;struct PCB_type int pid ; /进程名 int state ; /*进程状态 2-表示执行状态 1-表示就绪状态 0-表示阻塞状态*/ int cpu_time ; /运行需要的CPU时间(需运行的时间片个数) ; struct QueueNode struct PCB_type PCB; struct QueueNode *next; struct QueueNode *ready_head=NULL
5、; /ready队列队首指针 struct QueueNode *ready_tail=NULL; /ready队列队尾指针 struct QueueNode *blocked_head=NULL; /blocked队列队首指针 struct QueueNode *blocked_tail=NULL; /blocked队列队尾指针 int use_cpu,unuse_cpu; void start_state() int i,m,n,k; struct QueueNode *p; cout请输入就绪队列个数m和阻塞队列个数nmn; p=(struct QueueNode *)malloc(si
6、zeof(struct QueueNode); p-next=NULL; ready_head=ready_tail=p; cout就绪队列进程:endl; for(i=1;inext=NULL; cout请输入第i个就绪进程的pid和cpu_timep-PCB.pidp-PCB.cpu_time; p-PCB.state=1; ready_tail-next=p; ready_tail=p; k=1; p=ready_head-next; while(p) cout第k个就绪进程的pid和cpu_time分别为:PCB.pid PCB.cpu_timenext; k+; p=(struct
7、QueueNode *)malloc(sizeof(struct QueueNode); p-next=NULL; blocked_head=blocked_tail=p; cout阻塞队列进程:endl; for(i=1;inext=NULL; cout请输入第i个阻塞进程的pid和cpu_timep-PCB.pidp-PCB.cpu_time; p-PCB.state=0; blocked_tail-next=p; blocked_tail=p; k=1; p=blocked_head-next; while(p) cout第k个阻塞进程的pid和cpu_time分别为:PCB.pid P
8、CB.cpu_timenext; k+; void dispath() struct QueueNode *p; int x=0,t; coutt; use_cpu=0; unuse_cpu=0; while(ready_head!=ready_tail|blocked_head!=blocked_tail) if(ready_head!=ready_tail) p=ready_head-next; ready_head-next=p-next; p-next=NULL; if(ready_head-next=NULL) ready_tail=ready_head; p-PCB.state =
9、2; cout运行进程:PCB.pidPCB.cpu_time-; use_cpu+; if(p-PCB.cpu_time) p-PCB.state=1; ready_tail-next=p; ready_tail=p; ready_tail-next=NULL; else coutendl; cout进程PCB.pid运行完成!endl; coutendl; free(p); else coutCPU未运行!next; blocked_head-next=p-next; p-next=NULL; if(blocked_head-next=NULL) blocked_tail=blocked_
10、head; ready_tail-next=p; ready_tail=p; ready_tail-next=NULL; p-PCB.state=1; x=0; void calculate() coutCPU利用率为:(use_cpu/double(use_cpu+unuse_cpu)*100%next=NULL; / 创建自由链头结点busy_head=busy_tail=(struct busylink*)malloc(sizeof(struct busylink); busy_head-next=NULL; / 创建占用链头结点 p=( struct freelink *)malloc
11、(sizeof(struct freelink); p-address=64; p-len=640-64; (OS占用了64K) p-next=NULL; free_head-next=p; q=( struct busylink *)malloc(sizeof(struct busylink); q-name=S; /* S表示操作系统占用 */ q-len=64; q-address=0; q-next=NULL; busy_head-next=q; busy_tail=q; void requireMemo(char name, int require); /*模拟内存分配*/ void
12、 freeMemo(char name); /* 模拟内存回收*/ void past(int time); /* 模拟系统过了time 时间*/ void printlink(); /* 输出内存空闲情况(自由链的结点) */ 3.设计主函数:main() start(); past(t1); requireMemo(A,8); requireMemo(B,16); requireMemo(C,64); requireMemo(D,124); printlink(); past(t2); freeMemo(C); printlink(); past(t3); requireMemo(E,50
13、); printlink(); freeMemo(D ); printlink();5. 源程序#include #include #include #include using namespace std;struct freelink int len, address; /* len为分区长度 */ /* address为分区起始地址 */ struct freelink *next;struct busylink char name; /* 作业或进程名 name=S 表示OS占用*/ int len , address; struct busylink *next;struct fre
14、elink *free_head=NULL; /自由链队列(带头结点)队首指针 struct busylink *busy_head=NULL; /占用区队列队(带头结点)首指针 struct busylink *busy_tail=NULL; /占用区队列队尾指针void start(void) /* 设置系统初始状态*/ struct freelink *p; struct busylink *q; free_head=(struct freelink*)malloc(sizeof(struct freelink); free_head-next=NULL; / 创建自由链头结点 busy
15、_head=busy_tail=(struct busylink*)malloc(sizeof(struct busylink); busy_head-next=NULL; / 创建占用链头结点 /初始空闲链表 p=( struct freelink *)malloc(sizeof(struct freelink); p-address=64; p-len=640-64; /(OS占用了64K) p-next=NULL; free_head-next=p; /初始被占用链表 q=( struct busylink *)malloc(sizeof(struct busylink); q-name
16、=S; /* S表示操作系统占用 */ q-len=64; q-address=0; q-next=NULL; busy_head-next=q; busy_tail=q; void requireMemo(char name, int require) /*模拟内存分配*/ if(free_head-next-lenname=name; p-address=free_head-next-address; p-len=require; p-next=NULL; busy_tail-next=p; busy_tail=p; w=free_head-next; free_head-next=w-n
17、ext; if(w-len=require) free (w); else w-address=w-address+require; w-len=w-len-require; struct freelink *u,*v; u=free_head; v=free_head-next; while(v!=NULL)&v-lenw-len) u=v; v=v-next; u-next=w; w-next=v; void freeMemo(char name) /* 模拟内存回收*/ struct busylink *p,*q; struct freelink *w,*u,*v; int len,ad
18、dress; q=busy_head; p= busy_head-next; while(p!=NULL&(p-name!=name) q=p; p=p-next; if(p=NULL) printf(%c is not exist,name); else if(p=busy_tail) busy_tail=q; else q-next=p-next; len=p-len; address=p-address; free(p); w=( struct freelink*) malloc(sizeof(struct freelink); w-len=len; w-address=address;
19、 u=free_head; v=free_head-next; while(v!=NULL&v-lenw-len) u=v; v=v-next; u-next=w; w-next=v; void past(int time)/* 模拟系统过了time 时间*/ cout经过时间time后:next; while(v!=NULL) cout第i个空闲分区信息为:; coutaddress:address len:lennext; i+; coutnext; while(v!=NULL) cout第i个工作分区信息为:; coutaddress:address len:lennext; i+; c
20、outendl; void main(void) start(); past(1); requireMemo(A,8); requireMemo(B,16); requireMemo(C,64); requireMemo(D,124); printlink(); printlink2(); past(3); freeMemo(C); printlink(); printlink2(); past(4); requireMemo(E,50); printlink(); printlink2(); past(5); freeMemo(D ); printlink(); printlink2();6.实例运行结果7.实验总结此次实验通过C语言程序动态地模拟了操作系统中内存的动态分区分配。 此次实验通过建立结点来模拟空闲分区和已占用分区,每个表示空闲分区或已占用分区的结点都包括有次空闲分区的大小和内存地址以及一个指向下一个空闲或占用分区的结构体指针,通过结点形成空
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1