1、操作系统实验指导书 操作系统原理实验指导书羊四清 编 写适用专业: 计算机科学与技术 网络工程 湖南人文科技学院计算机科学技术系 2008年 8 月前 言操作系统是计算机的核心和灵魂。操作系统软件的设计对整个计算机的功能和性能起着至关重要的作用,所以此门课也是必不可少的,是面向计算机科学与技术、网络工程、软件工程等大多数计算机专业本科生开设的一门计算机专业课程。操作系统是计算机系统的核心,操作系统课程是计算机科学与技术专业的重要必修课。本课程的目的是使学生掌握现代计算机操作系统的基本原理、基本设计方法及实现技术,具有分析现行操作系统和设计、开发实际操作系统的基本能力。操作系统实验是操作系统课程
2、的重要组成部分,属于学科基础实验范畴。作为与相关教学内容配合的实践性教学环节,应在操作系统理论课教学过程中开设。操作系统是计算机科学与技术专业必修的专业基础课程,操作系统实验的作用是:理解操作系统的设计和实现思路,掌握典型算法。基本要求是:理解进程的概念,理解死锁,掌握银行家算法;掌握请求页式存储管理的实现原理及页面置换算法。学生应具有高级语言编程能力、具有数据结构等基础知识。说明:本实验指导书所提供的源程序均已在VC.下调试运行过实验一 进程创建模拟实验学时:2实验类型:验证实验要求:必修一、实验目的1) 理解进程创建相关理论;2) 掌握进程创建方法;3) 掌握进程相关数据结构。二、实验内容
3、本实验针对操作系统中进程创建相关理论进行实验。要求实验者输入实验指导书提供的代码并进行测试。代码简化了进程创建的多个步骤和内容。进程的树形结构采用广义二叉树的方式进行存储。三、实验原理1)进程控制块为了描述和控制进程的运行,系统为每个进程定义了一个进程控制块(PCB),它是进程实体的一部分,是操作系统管理进程最重要的数据结构。其主要包含四类信息:(1) 进程标识符它唯一地标识一个进程。通常包括进程号 pid,父进程号 ppid 和用户号 uid。(2) 处理机状态处理器的状态通常由处理机的各种寄存器中的内容组成。PCB 存放中断(阻塞,挂起)时的各寄存器值,当该进程重新执行时,可以从断点处恢复
4、。主要包括: a) 通用寄存器; b) 指令计数器; c) 程序状态字 PSW; d) 用户栈指针。(3) 进程调度信息 a) 进程状态; b) 进程优先级(用于描述优先使用 cpu 级别的一个整数,高优先级的进程先得到cpu,通常情况下,优先值越小优先级越高); c) 其它信息(等待时间、总执行时间等); d) 事件(等待原因)。(4) 进程控制信息 a) 程序和数据的地址(程序在内存和外存中的首址); b) 进程同步和通信机制; c) 资源列表(进程除 CPU 以外的所有资源); d) 链接指针(进程队列中指向下一个进程的 PCB 首址)。2) 进程创建流程 (1) 申请空白 PCB 为新
5、进程申请获得唯一的数字标识符,并从 PCB 集合中索取一个空白 PCB。如果无空白PCB,可以创建一个新的 PCB。在本实验中,每次动态创建 PCB。 (2) 为新进程分配资源 为新进程分配内存空间和栈空间。 (3) 初始化进程控制块 a) 初始化标识信息; b) 初始化处理机状态信息; c) 初始化处理机控制信息。 (4) 将新进程插入就绪队列P1P2P3P4P5P6P7P8P9P10P11P12 3) 进程树 图 1-1 进程树 进程树用于描述进程家族关系,如图 1-1 中可以看出,进程 P1 创建了进程 P2、P3、P4、P5,而 P2 又创建了 P6、P7、P8 。在进程创建过程中,需
6、要对每一个新增加的进程加入到进程树中,有了清晰的父子关系,可以使资源继承或进程删除等操作变得很方便。4) 进程总链它是一个 PCB 链表,每一个新创建的进程必须把其 PCB 放入总链中,该总链可以对破坏的进程树进行修复,也方便 PCB 查找。四、程序清单1.basic.h 文件#ifndef basic_h#include#include#include#define basic_hchar *errormsg256;/process control blockstruct pcb int pid;/process id int ppid;/parent process id int prio
7、;/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 count;/计数值 int curpid;/当前进程 id pnode *wlist; /等待链表;#define geterror(eno) printf(%sn,errorm
8、sgeno) 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) char *s1; int len = strlen(s); if(start=len | startend) return NULL; s1=(char *)malloc(end
9、-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 string to array dataint * strtoarray(char *s) int *a,count,x1; unsigned int i; char c, *s1,
10、*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(c=,) s1=substr(s1,x1+1,strlen(s1)-1); return a;#endif2、源程序#include basic.hpnode *proo
11、t;/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) /check if this pid is already exist printf(pid %d is already exist!n,para0); return -1; if(
12、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-prio = para2;p1-sub=NULL;p1-next=NULL;p1-brother=NULL;/add to process treeif(!pp-sub) pp
13、-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; for(;p;) /print all pcb info printf(%d(prio %d):,p-node-pid,p-node-prio); p1 = p-sub; f
14、or(;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; proot-node=new pcb; proot-node-pid=0; proot-node-ppid=-1; proot-node-prio=0; proot-next=NU
15、LL; 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; showdetail(); else int *para; char *s,*s1; s = strstr(cmdstr,createpc); /create process i
16、f(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(!pflag) geterror(1); 五、实验步骤输入实验提供的代码后,可以输入 createpc 命令创建进程,输入 showdetail 显示每个进程及
17、其子进程的信息,测试命令解释如下: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) 进程创建的核心内容是什么?2) 该设计和实际的操作系统进程创建相比,缺少了哪些步骤?实验二 进程撤销模拟实验学时:2实验类型:验证实验要求:必修一、实
18、验目的1) 理解进程撤销相关理论;2) 掌握进程撤销流程。二、实验内容本实验针对操作系统中进程撤销相关理论进行实验。要求实验者设计一个程序,该程序可模拟撤销多个进程及其子孙进程。1) 采用动态或静态方法生成一颗进程树(进程数目20);2) 设计进程撤销算法;3) 实现进程撤销函数,采用级联方式撤销;4) 可动态撤销进程;5) 可动态观察进程树的情况;6) 测试程序并得到正确结果。三、实验原理1)进程创建流程(1) 从 PCB 链中找到该进程的 PCB,从中读出该进程的状态;(2) 如果该进程处于执行状态,则终止该进程并置调度标志为真;(3) 若该进程有子孙进程,需要撤销其子孙进程;(4) 释放
19、该进程占有的资源;(5) 从 PCB 链中移出该进程的 PCB。2) 进程子树的删除对于已经创建的进程树(可以参考实验 1 创建进程),在删除的时候,首先需要考虑把该进程及其子孙从整棵树中脱离出来,这样才不会破坏整棵树的完整性。3) 进程总链元素的删除对于进程树种撤销的所有进程,必须在进程总链中进行删除。四、思考题1) 进程撤销的核心内容是什么?2) 进程总链在进程撤销过程中有什么作用?实验三 P、V 原语的模拟实现实验学时:2实验类型:验证实验要求:必修一、实验目的1) 理解信号量相关理论;2) 掌握记录型信号量结构;3) 掌握 P、V 原语实现机制。二、实验内容本实验针对操作系统中信号量相
20、关理论进行实验,要求实验者输入实验指导书提供的代码并进行测试。代码主要模拟信号量的 P 操作(down)和 V 操作(up)。1) 信号量信号量也称为信号锁,主要应用于进程间的同步和互斥,在用于互斥时,通常作为资源锁。信号量通常通过两个原子操作 dwon(P)和 up(V)来访问。dwon 操作使信号量的值+1,up 操作使信号量的值-1。2) 记录型信号量记录型信号量采用了“让权等待”的策略,存在多个进程等待访问同一临界资源的情况,所以记录型信号量需要一个等待链表来存放等待该信号量的进程控制块或进程号。在本实验中,使用记录型信号量。三、实验要求1) 输入给定代码;2) 进行功能测试并得出正确
21、结果。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; semphore *s; fflag=0; pflag=0; for(int i=0;i5;i+) if(!str
22、cmp(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 process %d is not exist!n,pid); return; s-count-; /
23、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 s-wlist=p1; /up operation void up(char *sname)
24、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 : ,semi.name); printf(n); /*/ void init() /init
25、 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-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)
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1