1、 操作系统原理实验指引书羊四清 编 写合用专业: 计算机科学与技术 网络工程 湖南人文科技学院计算机科学技术系 8 月前 言操作系统是计算机核心和灵魂。操作系统软件设计对整个计算机功能和性能起着至关重要作用,因此此门课也是必不可少,是面向计算机科学与技术、网络工程、软件工程等大多数计算机专业本科生开设一门计算机专业课程。操作系统是计算机系统核心,操作系统课程是计算机科学与技术专业重要必修课。本课程目是使学生掌握当代计算机操作系统基本原理、基本设计办法及实现技术,具备分析现行操作系统和设计、开发实际操作系统基本能力。操作系统实验是操作系统课程重要构成某些,属于学科基本实验范畴。作为与有关教学内容
2、配合实践性教学环节,应在操作系统理论课教学过程中开设。操作系统是计算机科学与技术专业必修专业基本课程,操作系统实验作用是:理解操作系统设计和实现思路,掌握典型算法。基本规定是:理解进程概念,理解死锁,掌握银行家算法;掌握祈求页式存储管理实现原理及页面置换算法。学生应具备高档语言编程能力、具备数据构造等基本知识。阐明:本实验指引书所提供源程序均已在VC.下调试运营过目录实验一 进程创立模仿1实验二 进程撤销模仿9实验三 P、V 原语模仿实现10实验四 带优先级时间片轮换进程调度算法实现16实验五 银行家算法模仿26实验六 持续动态内存管理模仿实现29实验七 祈求页式存储管理中惯用页面置换算法模仿
3、31实验八 SCAN 磁盘调度模仿实现36实验九 UNIX基本操作37实验一 进程创立模仿实验学时:2实验类型:验证实验规定:必修一、实验目1) 理解进程创立有关理论;2) 掌握进程创立办法;3) 掌握进程有关数据构造。二、实验内容本实验针对操作系统中进程创立有关理论进行实验。规定实验者输入实验指引书提供代码并进行测试。代码简化了进程创立各种环节和内容。进程树形构造采用广义二叉树方式进行存储。三、实验原理1)进程控制块为了描述和控制进程运营,系统为每个进程定义了一种进程控制块(PCB),它是进程实体一某些,是操作系统管理进程最重要数据构造。其重要包括四类信息:(1) 进程标记符它唯一地标记一种
4、进程。普通涉及进程号 pid,父进程号 ppid 和顾客号 uid。(2) 解决机状态解决器状态普通由解决机各种寄存器中内容构成。PCB 存储中断(阻塞,挂起)时各寄存器值,当该进程重新执行时,可以从断点处恢复。重要涉及: a) 通用寄存器; b) 指令计数器; c) 程序状态字 PSW; d) 顾客栈指针。(3) 进程调度信息 a) 进程状态; b) 进程优先级(用于描述优先使用 cpu 级别一种整数,高优先级进程先得到cpu,普通状况下,优先值越小优先级越高); c) 其他信息(等待时间、总执行时间等); d) 事件(等待因素)。(4) 进程控制信息 a) 程序和数据地址(程序在内存和外存
5、中首址); b) 进程同步和通信机制; c) 资源列表(进程除 CPU 以外所有资源); d) 链接指针(进程队列中指向下一种进程 PCB 首址)。2) 进程创立流程 (1) 申请空白 PCB 为新进程申请获得唯一数字标记符,并从 PCB 集合中索取一种空白 PCB。如果无空白PCB,可以创立一种新 PCB。在本实验中,每次动态创立 PCB。 (2) 为新进程分派资源 为新进程分派内存空间和栈空间。 (3) 初始化进程控制块 a) 初始化标记信息; b) 初始化解决机状态信息; c) 初始化解决机控制信息。 (4) 将新进程插入就绪队列P1P2P3P4P5P6P7P8P9P10P11P12 3
6、) 进程树 图 1-1 进程树 进程树用于描述进程家族关系,如图 1-1 中可以看出,进程 P1 创立了进程 P2、P3、P4、P5,而 P2 又创立了 P6、P7、P8 。在进程创立过程中,需要对每一种新增长进程加入到进程树中,有了清晰父子关系,可以使资源继承或进程删除等操作变得很以便。4) 进程总链它是一种 PCB 链表,每一种新创立进程必要把其 PCB 放入总链中,该总链可以对破坏进程树进行修复,也以便 PCB 查找。四、程序清单1.basic.h 文献#ifndef basic_h#include#include#include#define basic_hchar *errormsg
7、256;/process control blockstruct pcb int pid;/process id int ppid;/parent process id int prio;/priority int state;/state int lasttime;/last execute time int tottime;/totle execute time;/process nodestruct pnode pcb *node; pnode *sub; pnode *brother; pnode *next;/信号量struct semphore char name5;/名称 int
8、 count;/计数值 int curpid;/当迈进程 id pnode *wlist;/等待链表;#define geterror(eno) printf(%sn,errormsgeno) void initerror() errormsg0 = (char *)malloc(20); errormsg0=error command!; errormsg1 = (char *)malloc(20); errormsg1=error parameter!;/get a substring in string schar * substr(char *s,int start,int end)
9、char *s1; int len = strlen(s); if(start=len | startend) return NULL; s1=(char *)malloc(end-start+2); for(int i=0;i=end-start;i+) s1i = si+start; s1i=0; return s1;/find the location of c in string sint instr(char *s,char c) unsigned i; for(i=0;istrlen(s);i+) if(si=c) return i; return -1;/change the s
10、tring to array dataint * strtoarray(char *s) int *a,count,x1; unsigned int i; char c,*s1,*s2; if(!s) printf(string cant be null!n); return NULL; count=0; s1=s; for(i=0;istrlen(s1);i+) if(s1i=,) count+; count+; a = (int *)malloc(count); c=,; for(i=0;i=0) s2=substr(s1,0,x1-1);else s2=s1;ai=atoi(s2);if
11、(c=,) s1=substr(s1,x1+1,strlen(s1)-1); return a;#endif2、源程序#include basic.hpnode *proot;/system process tree rootpnode *plink;/system process link head/create processint createpc(int *para) /add your code here pnode *p,*p1,*pp; int pflag; pflag=0; for(p=plink;p;p=p-next) if(p-node-pid = para0) /chec
12、k if this pid is already exist printf(pid %d is already exist!n,para0); return -1; if(p-node-pid = para1) /find parent pcb pflag=1; pp = p; if(!pflag) printf(parent id %d is not exist!n,para1); return -2; /init new pcbp1 = new pnode;p1-node=new pcb;p1-node-pid = para0;p1-node-ppid = para1;p1-node-pr
13、io = para2;p1-sub=NULL;p1-next=NULL;p1-brother=NULL;/add to process treeif(!pp-sub) pp-sub=p1;else for(p=pp-sub;p-brother;p=p-brother); p-brother=p1;/ add to process linkfor(p=plink;p-next;p=p-next);p-next=p1;return 0;/show process detailvoid showdetail() /add your code here pnode *p,*p1; p=plink; f
14、or(;p;) /print all pcb info printf(%d(prio %d):,p-node-pid,p-node-prio); p1 = p-sub; for(;p1;) /print sub pcb printf(%d(prio %d),p1-node-pid,p1-node-prio); p1 = p1-brother; printf(n); p = p-next; printf(n);/dont changevoid main() initerror(); short cflag,pflag; char cmdstr32; proot = new pnode; proo
15、t-node=new pcb; proot-node-pid=0; proot-node-ppid=-1; proot-node-prio=0; proot-next=NULL; proot-sub=NULL; proot-brother=NULL; plink=proot; for(;) cflag=0; pflag=0; printf(cmd:); scanf(%s,cmdstr); if(!strcmp(cmdstr,exit) /exit the program break; if(!strcmp(cmdstr,showdetail) cflag = 1;pflag = 1;showd
16、etail(); else int *para;char *s,*s1;s = strstr(cmdstr,createpc);/create process if(s) cflag=1; para = (int *)malloc(3); /getparameter s1 = substr(s,instr(s,()+1,strlen(s)-2);/get param string para=strtoarray(s1);/get parameter createpc(para);/create process pflag=1; if(!cflag) geterror(0); else if(!
17、pflag) geterror(1); 五、实验环节输入实验提供代码后,可以输入 createpc 命令创立进程,输入 showdetail 显示每个进程及其子进程信息,测试命令解释如下:1) createpc 创立进程命令。 参数: 1、pid(进程 id) 2 、ppid(父进程 id)3、prio(优先级)。 示例:createpc(1,0,1) 。创立一种进程,其进程号为 1,父进程号为 0,优先级为 1.createpc(2,1,2) 。创立一种进程,其进程号为 2,父进程号为 1,优先级为 2。2) showdetail 显示进程信息命令。3) exit退出命令行。六、思考题 1)
18、 进程创立核心内容是什么?2) 该设计和实际操作系统进程创立相比,缺少了哪些环节?实验二 进程撤销模仿实验学时:2实验类型:验证实验规定:必修一、实验目1) 理解进程撤销有关理论;2) 掌握进程撤销流程。二、实验内容本实验针对操作系统中进程撤销有关理论进行实验。规定实验者设计一种程序,该程序可模仿撤销各种进程及其子孙进程。1) 采用动态或静态办法生成一颗进程树(进程数目20);2) 设计进程撤销算法;3) 实现进程撤销函数,采用级联方式撤销;4) 可动态撤销进程;5) 可动态观测进程树状况;6) 测试程序并得到对的成果。三、实验原理1)进程创立流程(1) 从 PCB 链中找到该进程 PCB,从
19、中读出该进程状态;(2) 如果该进程处在执行状态,则终结该进程并置调度标志为真;(3) 若该进程有子孙进程,需要撤销其子孙进程;(4) 释放该进程占有资源;(5) 从 PCB 链中移出该进程 PCB。2) 进程子树删除对于已经创立进程树(可以参照实验 1 创立进程),在删除时候,一方面需要考虑把该进程及其子孙从整棵树中脱离出来,这样才不会破坏整棵树完整性。3) 进程总链元素删除对于进程树种撤销所有进程,必要在进程总链中进行删除。四、思考题1) 进程撤销核心内容是什么?2) 进程总链在进程撤销过程中有什么作用?实验三 P、V 原语模仿实现实验学时:2实验类型:验证实验规定:必修一、实验目1) 理
20、解信号量有关理论;2) 掌握记录型信号量构造;3) 掌握 P、V 原语实现机制。二、实验内容本实验针对操作系统中信号量有关理论进行实验,规定实验者输入实验指引书提供代码并进行测试。代码重要模仿信号量 P 操作(down)和 V 操作(up)。1) 信号量信号量也称为信号锁,重要应用于进程间同步和互斥,在用于互斥时,普通作为资源锁。信号量普通通过两个原子操作 dwon(P)和 up(V)来访问。dwon 操作使信号量值+1,up 操作使信号量值-1。2) 记录型信号量记录型信号量采用了“让权等待”方略,存在各种进程等待访问同一临界资源状况,因此记录型信号量需要一种等待链表来存储等待该信号量进程控
21、制块或进程号。在本实验中,使用记录型信号量。三、实验规定1) 输入给定代码;2) 进行功能测试并得出对的成果。3) 分析 dwon 和 up 函数功能模块;4) 在实验报告中画出 dwon 和 up 函数流程图;5) 撰写实验报告。四、程序清单#include basic.h semphore sem5;/deinfe 5 semphorespnode * pr20;/define 0-19 total 20 process/down operation void down(char * sname,int pid) int fflag,pflag; pnode *p,*p1; semphor
22、e *s; fflag=0; pflag=0; for(int i=0;i5;i+) if(!strcmp(semi.name,sname)/find semaphore by name s=; fflag=1; break; for(i=0;inode-pid = pid) p1 = pri; pflag=1; break; if(!fflag) /semaphore is not exist printf(the semphore %s is not exist!n,sname); return; if(!pflag) /pid is not exist printf(the p
23、rocess %d is not exist!n,pid); return; s-count-; /semaphore!s value -1 if(s-count=0) /this pcb get the semaphore s-curpid = p1-node-pid; else if(s-wlist) /the link is not NULL,add the pcb to the last for(p=s-wlist;p-next;p=p-next); p-next=p1; else /this pcb is the first pcb be added to the down list
24、 s-wlist=p1; /up operation void up(char *sname) int fflag=0; for(int i=0;inode-pid;semi.wlist = semi.wlist-next; else printf(the semphore %s is not exist!n,sname); /show semphore infomation void showdetail() int i; pnode *p; printf(n); for(i=0;i5;i+) if(semi.countnode-pid);p=p-next; else printf(%s :
25、 ,semi.name);printf(n); /*/ void init() /init semaphore strcat(sem0.name,s0); strcat(sem1.name,s1); strcat(sem2.name,s2); strcat(sem3.name,s3); strcat(sem4.name,s4); for(int i=0;i5;i+) semi.wlist=NULL;semi.count=1; /init process for(i=0;inode=new pcb;pri-node-pid=i;pri-brother=NULL;pri-next=NULL;pri
26、-sub=NULL; void main() short cflag,pflag; char cmdstr32; char *s,*s1,*s2; initerror(); init(); for(;) cflag=0; pflag=0; printf(cmd:); scanf(%s,cmdstr); if(!strcmp(cmdstr,exit) /exit the program break; if(!strcmp(cmdstr,showdetail) cflag = 1; pflag = 1; showdetail(); else s = strstr(cmdstr,down);/cre
27、ate process if(s) cflag=1; /getparameter s1 = substr(s,instr(s,()+1,instr(s,)-1); s2 = substr(s,instr(s,)+1,instr(s,)-1); if(s1 & s2) down(s1,atoi(s2); pflag=1; else s=strstr(cmdstr,up);/delete process if(s) cflag=1; s1 = substr(s,instr(s,()+1,instr(s,)-1); if(s1) up(s1); pflag=1; if(!cflag) geterro
28、r(0); else if(!pflag) geterror(1); /*/ 五、测试规定1) 至少进行 10 次以上 wait 和 signal 操作;2) 规定 wait 操作浮现进程等待队列;3) 对有等待队列信号量进行 signal 操作。六、实验指引 实验中提供了 5 个信号量(s0-s4)和 20 个进程(pid 0-19)。在程序运营过程中可以键入 down up 命令和 showdetail 命令显示每个信号量状态。详细输入解释如下: 1) dwon 获取信号量操作(P 操作)。 参数: 1 sname 2 pid 。 示例:down(s1,2) 。进程号为 2 进程申请名字为
29、 s1 信号量。 2) up 释放信号量操作(V 操作)。 参数 1 sname。 示例:up(s1)。释放信号量名字为 s1 信号量。 3) showdetail 显示各信号量状态及其等待队列。 4) exit退出命令行。七、实验思考1) 如何修改 dwon 操作,使之能一次申请各种信号量?2)在某个时刻,一种进程最多可以等待多少个信号量?实验四 带优先级时间片轮换进程调度算法实现实验学时:4实验类型:设计实验规定:必修一、实验目(1)掌握进程状态转换过程(2)掌握时间片轮转进程调度算法;(3)掌握带优先级进程调度算法;二、实验内容(1)自定义PCB数据构造;(2)使用带优先级时间片轮转法调
30、度进程,每运营一种时间片,优先级减半。(3)命令集A)create 随机创立进程,进程优先级与所需要时间片随机决定;B)round 执行1次时间片轮转操作,其办法为运营高优先级队列第1个,再减少其优先级,插入到相应队列中。C)ps 查看当迈进程状态D)sleep 命令将进程挂起E)awake 命令唤醒1个被挂起进程F)kill 命令杀死进程G)quit命令退出(4)选用面向对象编程办法。三、实验原理或算法本实验结合了进程状态转换、优先级调度、时间片轮转调度三方面内容,依照进程状态转换图,设立SLEEP命令,将1个进程挂起,AWAKE命令唤醒1个被挂起进程(从阻塞状态到就绪状态)。1) 优先级优
31、先级体现了进程重要限度或急迫限度,在大多数当代操作系统中,都采用了优先级调度方略。优先级从小到大(如 0-127) 优先级最高,127 最低。在本实验中按数值大小决定优先级,数值大优先级高。2) 基于时间片调度将所有就绪进程按照先来先服务原则,排成一种队列,每次调度时,将 cpu 分派给队首进程,并令其执行一种时间片。当时间片用完时,由一种计时器发出时钟中断祈求,调度程序把此进程终结,把该进程放到队尾。在该实验中,时间片以 100ms 为单位(实际要小得多)。在调度过程中,需要通过时间函数检测进程执行时间,当该进程执行时间时间片大小时,进行调度。3) 高优先级调度优先级高进程优先得到 cpu,
32、等该进程执行完毕后,此外进程才干执行。4) 基于时间片高优先级调度在调度算法中,只有处在就绪状态进程才干被调度,调度算法结合了优先级调度和时间片轮转调度算法,商定:从最高优先级队列取第1个就绪状态进程进行调度,时间片到后减少其优先级(减少一半),然后插入到低优先级队列尾部,每次调度后,显示进程状态。四、程序清单#include #include #include /#include #include /定义进程数#define LEN 10/定义最高优先级 #define MAXPIOR 3/ 定义时间片#define QUANTUM 2#define PCB sizeof(struct pc
33、b)struct pcb /PCBint ident;/标记符 int state;/状态 0-就绪,1运营,2堵塞 int pior;/优先级,MAXPIOR为最高优先级*/ int life;/生命期*/ struct pcb *next;/*指针*/ *arrayMAXPIOR;static int idlistLEN;/*标记符表*/int life=0;/*总生命期初始化为0*/char str20;char command710;int killtest=0;void init();int create();void kill(int x);void process();void
34、routine();void ps();void init() int i=0; for (i=0;iMAXPIOR;i+) arrayi=NULL; sprintf(command0,quit); sprintf(command1,ps); sprintf(command2,create); sprintf(command3,kill); sprintf(command4,round); sprintf(command5,sleep); sprintf(command6,awake);int create() int i=0,pior=0; struct pcb *p,*q,*s; whil
35、e (idlisti!=0&iident=i; s-state=0; s-pior=pior; s-life=1+rand()%20;/进程有生命期假设为120 s-next=NULL; life=life+(s-life); p=arraypior;/建立同优先级队列(链表) if (p=NULL) arraypior=s; else while(p!=NULL) q=p; p=p-next; q-next=s; printf(success create process id=%d,current process state disp below:n,s-ident); ps(); /printf(end displayn); return 1;void ps()int i=0; struct pcb *p; for (i=0;iMAXPIOR;i+) p=arrayi; whil
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4008-655-100 投诉/维权电话:4009-655-100