东北大学操作系统实验报告

上传人:z**** 文档编号:52702213 上传时间:2022-02-09 格式:DOC 页数:29 大小:198KB
返回 下载 相关 举报
东北大学操作系统实验报告_第1页
第1页 / 共29页
东北大学操作系统实验报告_第2页
第2页 / 共29页
东北大学操作系统实验报告_第3页
第3页 / 共29页
亲,该文档总共29页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
计算 机科学 与工程 学院实 验报告实验课程名称操作系统实验实验成绩专业计算机科学 与技术班级1507 班指导教师签字学号20154377姓名罗艺博实验报告批改 时间实验项目目录1. 实验一熟悉Linux系统2. 实验二进程状态3. 实验三进程同步和通信4. 实验四进程的管道通信5. 实验五页面置换算法实验报告正文实验一熟悉Linux系统一、实验目的熟悉和掌握Linux系统基本命令,熟悉 Linux编程环境,为以后的实验打下基础。二、实验原理基于linux系统的基础操作三、实验内容(源码、注释、基础内容、扩展点等)、rm启动、退出、Is (显示目录内容)、cp (文件或目录的复制)、mv (文件、目录更名或移动)(删除文件或目录)、mkdir (创建目录)、rmdir (删除空目录)、cd (改变工作目录)C语言编辑、编译四、实验结果(截图)Is mkdir:cd:rmdir:实验二进程状态一、实验目的自行编制模拟程序,通过形象化的状态显示,使学生理解进程的概念、进程之间的状态转换及其所带来的PCB内容、组织的变化,理解进程与其PCB间的对应关系。二、实验原理1. 进程在内存中存在三种基本状态:就绪态、执行态、阻塞态2. 三种状态在满足某种条件时会发生转换: 就绪一一运行:调度程序选择一个新的进程运行 运行一一就绪:运行进程用完了时间片运行进程被中断,因为一高优先级进程处于就绪状态 运行一一阻塞:当一进程等待某一事件的发生时,如请求系统服务;初始化I/O 且必须等待结果;无新工作可做;等待某一进程提供输入(IPC) 阻塞一一就绪:当所等待的事件发生时三、实验内容(源码、注释、基础内容、扩展点等)#in clude #in elude #i nclude #i nclude /for sort in vectorusing n amespace std;class Pro /process classpublic:char n ame;string status;vector ru,re,bl;ru-runnin g,re-ready,bl-blocked/function declarati onint helloUI();int in iQ();int showPCB();int ruTOre();int ruTObl();int blTOre();int neTOre();int ruTOex();int helloUI() /start UIcout Hello!Welcome to come back. en dl;cout ”#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#” endl;cout en dl;cout en dl;return 0;int in iQ()/in itialize the processint i;cout Please en ter processes n ames and their status. en dl;cout en dl;for(i=0; i5; i+)15 process a to oPro process;char n am;string sta;cout Please en ter i processes n ames. nam;process .n ame = n am;cout Please en ter processes status. en dl;cout Status contains r1(r unnin g),r2(ready) and b(blocked). sta;process.status = sta;if(sta = r1) /judge which statusif(ru.size()1)ru.push_back(process);cout yes en dl;elsecout Error! en dl;else if(sta = r2)re.push_back(process);else if(sta = b)bl.push_back(process);elsecout Error! en dl;cout AAAAAAAAAAAAAAAAAAAAAAAAAAAAA en dl;cout en dl; showPCB(); return 0;int showPCB() int i;cout runnin g:;for(i=0; iru.size(); i+)cout rui. name cout en dl;cout ready:;for(i=0; ire.size(); i+)cout rei.name ,;cout en dl;cout blocked:;for(i=0; ibl.size(); i+)cout bli.name ,;cout en dl;return 0;int ruTOre()if(!ru.empty() /runnin gQueue is being used.re.push_back(ru.fr on t();/runnin gs first process go to readylastru.erase(ru.begi n();/delete runnin gs first processru.push_back(re .fron t();re.erase(re.begi n();elsecout Error in ruTOre en dl;showPCB();return 0;int ruTObl()if(!ru.empty() /runnin gQueue is being used.bl.push_back(ru .fron t();ru.erase(ru.begi n();if(!re.empty()ru.push_back(re .fron t();re.erase(re.begi n();else elsecout Error in ruTObl2. r unning ru.push_back(re .fron t();re.erase(re.begi n();elsecout Error in blTOre en dl;showPCB();return 0;int neTOre()int i;cout Please en ter processes n ames and their status. en dl; cout en dl;Pro process;char n am;string sta;cout Please en ter processes n ames. n am;process .n ame = nam;process.status = r2;re.push_back(process);cout AAAAAAAAAAAAAAAAAAAAAAAAAAAAA en dl;if(ru.empty()ru.push_back(re .fron t(); re.erase(re.begi n();showPCB();return 0;int ruTOex()if(!ru.empty()/runnin gQueue is being used.ru.erase(ru.begi n();if(!re.empty()ru.push_back(re .fron t(); re.erase(re.begi n();elsecout Error in ruTOex1. en dl;elsecout Error in ruTOex2. endl;showPCB();return 0;int mai n()int act; /choose acti onhelloUI();cout Please in itialize the process. en dl; ini Q();while(1)cout AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA en dl;cout Please select the action to take. en dl;cout ready en dl; cout blocked en dl; cout ready en dl; cout ready en dl;cout exit act;if(act = 2)ruTOre();else if(act = 3)ruTObl();else if(act = 4)blTOre();else if(act = 5)n eTOre();else if(act = 6)ruTOex();elsecout Error in select.e ndl;cout # endl;cout en dl;return 0;拓展点:五状态模型四、实验结果(截图)创建进程:状态runningready状态runningblocked:状态blockedready创建新进程:情况一有进程正在运行情况二无进程正在运行终止进程:实验三进程同步和通信一、实验目的调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信 的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。通过补充新功 能,使学生能灵活运用相关知识,培养创新能力。、实验原理假定缓冲区可以容纳8个数据;因为缓冲区是有限的,因此当其满了时生产者进程应该等待;当消费者取走一个数据后,应唤醒正在等待的生产者进程;当缓冲区空时,消费者进程应该等待;当生产者向缓冲区放入了一个数据时,应唤醒正在等待的消费者进程。这就是生产者和消费者之间的同步 三、实验内容(源码、注释、基础内容、扩展点等)基础内容:编写程序使其模拟两个进程,即生产者(producer)进程和消费者(Consumer)进程工作;生产者每次产生一个数据,送入缓冲区中;消费者每次从缓冲区中取走一个数据。每次写入和读出数据时,都将读和写指针加一。当指针到达缓冲区尾,重新将指针退回起点;/*/*PROGRAM NAME:PRODUCER CONSUMER*/*This program simulates two processes, producer which*/*/* continuesto produce message and put it into a buffer /* impleme nted by PIPE, and con sumer which continues to get */*/* message from the buffer and use it./*The program also dem on strates the synchronism betwee n*/*/* processes and uses of PIPE./*/#in clude#in clude#in clude #defi ne PIPESIZE 8 #defi ne PRODUCER 0#defi ne CONSUMER 1#defi ne RUN/* statu of process */#defi ne WAIT/* statu of process */#defi ne READY/* statu of process */#defi ne NORMAL #defi ne SLEEP #defi ne AWAKE #in clude /Process Con trol blockstruct pcb char *n ame;int statu; int time;/* times of execution */Buffer/typestruct pipetype char type;int writeptr;/Write poin terint readptr;/Read poin terstruct pcb *po intp;/* write wait point*/struct pcb *po intc; /* read wait point*/in t pipePIPESIZE;/Buffer arraystruct pipetype pipetb;struct pcb process2;nu mber of producer - nu mber of con sumer,buffer. count = 8:too many prodecers; nu mber of times, ret-prese nt statuschar in 2;int run p(),r un c(),pr n();pipetb.type = c; pipetb.writeptr = 0; pipetb.readptr = 0;pipetb.po intp = pipetb.po intc = NULL;processPRODUCER. name = Producer、。;processCONSUMER. name = Con sumer0;processPRODUCER.statu = processCONSUMER.statu = READY;processPRODUCER.time = processCONSUMER.time = 0;output = 0;prin tf(Now starti ng the program!n);printf( Press p1 to run PRODUCER1, press p2 to run PRODUCER2.n press c to run CONSUMER.n);/PRODUCER1-product 1 new data, PRODUCER2-product 2 new dataprintf(” Press e to exit from the program.n);for(i=0;i1000;i+)in 0=N;while(i n0=N)sca nf(%s,i n);/whe n not p,c,e con ti nueif(in 0!=e&in 0!=p&in 0!=c)in 0=N:/producer and readyif(in 0=p&processPRODUCER.statu=READY)if(i n1 = 2)/producedint m;for(m=0;m 3)/the nu mber of waitti ng producer over 4,waitqueue=4prin tf(wro ng!n);con ti nue;if(cou ntstatu=READY;run c(process,pipe,&pipetb,CONSUMER); coun tr-;prin tf(co un tr=%dn,co untr);if(co un tr=0) pipetb.po in tc=NULL;else if(i n1 = 1) /producer1if(coun tp 3)prin tf(wro ng!n);con ti nue;if(cou ntstatu=READY;run c(process,pipe, &pipetb,CONSUMER);coun tr-;prin tf(co un tr=%dn,co un tr);if(co un tr=0)pipetb.po in tc=NULL;if(in 0=c&processCONSUMER.statu=READY)/con sumer and readyif(ret=ru nc(process,pipe, &pipetb,CONSUMER)=SLEEP) /sleeppipetb.poi ntc = &processCONSUMER;if(ret=AWAKE) /awake(pipetb.poi ntp)-statu=READY;output=(output+1)%100;run p(output,process,pipe,& pipetb,PRODUCER);coun tp-;prin tf(co un tp=%dn,co un tp);if(co un tp=0)pipetb.po in tp=NULL;if(in 0=p&processPRODUCER.statu=WAIT)/producer and waitif(i n1 = 2)/producedint m;for(m=0;m 3)prin tf(wro ng!n ”);printf(PRODUCER is waiting, cant be scheduled.n”);con ti nue;coun tp+;prin tf(co un tp=%dn,co un tp);else if(i n1 = 1)/producedif(coun tp 3)prin tf(wro ng!in);printf(PRODUCER is waiting, cant be scheduled.n);con ti nue;coun tp+;prin tf(co un tp=%dn,co un tp);prin tf(Look out.n);/printf(PRODUCER is waiting, cant be scheduled.n);if(in 0=c&processCONSUMER.statu=WAIT)/con sumer and waitif(co untr 3)prin tf(wro ng!in);printf(CONSUMER is waiting, cant be scheduled.n);con ti nue;coun tr+;prin tf(co un tr=%dn,co untr);prin tf(Look out!n);/printf(CONSUMER is waiting, cant be scheduled.n);if(i n0=e) exit(1);prn( process,pipe,pipetb); in0 =N:run p(out,p,pipe,tb,t) /* run producer */ int out,pipe,t;struct pcb p;struct pipetype *tb;pt.statu=RUN;,out);/buffer over 8prin tf(run PRODUCER. product %dif(co un t=8)pt.statu=WAIT; return(SLEEP);/only has 01234567tb-writeptr=tb-writeptr%8;pipetb-writeptr=out; /cha nge tb-writeptr+;prin tf(writeptr%dn,tb-writeptr);coun t+;prin tf(co un t=%dn,co un t); pt.time+;/prin tf(time+%dn,pt.time); pt.statu=READY;if(tb-poi ntc)!=NULL)/printf(返回 AWAKE); return(AWAKE); return(NORMAL);run c(p,pipe,tb,t) in t pipe,t; struct pcb p; struct pipetype *tb; int c;/* run con sumer */pt.statu = RUN;/buffer too lessprin tf(r un CONSUMER.);if(coun treadptr; pipetb_readptr=O; tb_readptr+; tb-readptr=tb-readptr%8;prin tf(readptr=%dn,tb-readptr); printf(” use %d,c);coun t-;prin tf(co un t=%dn,co un t); pt.time+;/prin tf(time+%dn,pt.time); pt.statu=READY;if(tb-poi ntp!=NULL)/printf(” 返回 AWAKEn); return(AWAKE);return(NORMAL);prn(p,pipe,tb)in t pipe;struct pipetype tb;struct pcb p;int i;prin tf(n);for(i=0;iPIPESIZE;i+)printf(”);I);prin tf(n/pipe in sidefor(i=0;iPIPESIZE;i+)if(pipei!=O)pipei has someth ingelseprintf(%2d|,pipei);printf(I);prin tf(n);for(i=0;i=1)nu mber of producer =1elseprin tf(%d PRODUCER wait ,co un tp);prin tf(PRODUCER ready );if(pCONSUMER.statu=WAIT)|cou ntr=1)prin tf(%d CONSUMER wait ,cou ntr);elseprin tf(CONSUMER ready );prin tf(n);prin tf(n# n);拓展点:设置了等待队列的长度,并显示等待队列内部情况四、实验结果(截图)生产者:情况一正常生产情况二超过缓冲区情况三超过等待队列消费者:情况一正常情况二无产品可消费又开始生产: 实验四进程的管道通信、实验目的 加深对进程概念的理解,明确进程和程序的区别;学习进程创建的过程,进一步认识并发执行的实质;分析进程争用资源的现象,学习解决进程互斥的方法;学习解决进程同步的方法;掌握Linux系统进程间通过管道通信的具体实现方法。二、实验原理1. 基本概念:进程的概念;进程与程序的区别;并发执行的概念进程互斥的概念;进程通信的基本原理2. 系统调用:设置系统调用号:设置多条系统调用命令,并赋予每条系统调用命令一个唯一的系统调用号处理系统调用:OS中有一张系统调用入口表,表中每个表目对应一条系统调用命令,包含该系统调用自带参数的数目、系统调用命令处理程序的入口地址等。OS内核便是根据所输入的系统调用号在该表中查找到相应的系统调用,进而转入它的入口地址去执行系统调用程序。Linux的系统调用机制:通过中断机制实现三、实验内容(源码、注释、基础内容、扩展点等)#i nclude #i nclude #in elude #in elude #in clude int mai n()in t pid1,pid2,pid3;int fd2;char outpipe50,i npipe50;pipe(fd);while (pid 1= fork() = -1);if(pid1 = 0)prin tf(s1n);lockf(fd1,1,0);/prin tf(s11 n);spri ntf(outpipe,l am the 1st!); write(fd1,outpipe,50); sleep (3);lockf(fd1,0,0);/sleep (2);exit(0);elsewhile(pid2=fork() = -1);if(pid2 = 0)prin tf(s2n ”);/if(pid1 != 0)/wait(0);/prin tf(s22n);lockf(fd1,1,0);/prin tf(f1);spri ntf(outpipe,l am the 2nd!);write(fd1,outpipe,50);sleep(2);lockf(fd1,0,0);/sleep(2); exit(0);elsewhile(pid3=fork() = -1);if (pid3 = 0)prin tf(s3n);lockf(fd1,1,0);spri ntf(outpipe,l am the 3rd!); write(fd1,outpipe,50); sleep(2);lockf(fd1,0,0);exit(O);elsewait(O);read(fdO,i npipe,50);prin tf(%s n,i npipe);/prin tf(f1);wait(0);read(fd0,i npipe,50);/prin tf(f2);prin tf(%s n,i npipe);wait(0);read(fd0,i npipe,50);prin tf(%s n,i npipe);exit(0);return 0;四、实验结果(截图) 三次运行顺序不同: 实验五页面置换算法一、实验目的进一步理解父子进程之间的关系理解内存页面调度的机理掌握页面置换算法的实现方法 通过实验比较不同调度算法的优劣 培养综合运用所学知识的能力页面置换算法是虚拟存储管理实现的关键,通过本次试验理解内存页面调度的机制,在模拟 实现FIFO、LRU等经典页面置换算法的基础上,比较各种置换算法的效率及优缺点,从而了解 虚拟存储实现的过程。将不同的置换算法放在不同的子进程中加以模拟,培养综合运用所学知识 的能力。二、实验原理1. FIF0算法:FIFO表示先进先出,类似于对列,在数据的结构上使用对列来实现。结构图:(1) 新访问的数据插入FIFO队列尾部,数据在 FIFO队列中顺序移动;(2)淘汰FIFO队列头部的数据;2. LRU算法:LRU表示很少使用算法,其核心思想是“如果数据最近被访问过,那么将来被访问 的几率也更高。最常见的实现是使用一个链表保存缓存数据,结构图如下:(1)新数据插入到链表头部;(2)每当缓存命中(即缓存数据被访问),则将数据移到链表头部;(3)当链表满的时候,将链表尾部的数据丢弃。三、实验内容(源码、注释、基础内容、扩展点等)#i nclude #i nclude #in clude #in clude #i nclude #in clude #in clude #in clude #defi ne total_i nstruction 20/Tatal Page Hits/LRUint LRU(i nt Acess_series)int M_Frame3; /memoryint M3; /Record whether memory is hitedfloat diseffect = 0;int p = 0; /p-where to use/in itialize memoryfor (int i = 0; i 3; i+)M_Framei = 0;Mi = 0;/sleep (2);prin tf( n Start LRU!n ”);for (int i = 0; i hitif (M_FrameO = Acess_seriesi)M0 = -1;/page faultelseM_Frame1 = Acess_seriesi; diseffect+;p+;1 emptyelse if (p = 2)0-hitif (M_Frame0 = Acess_seriesi)M_Frame0 = M_Frame1;M_Frame1 = Acess_seriesi; M0 = -1;1-hitelse if (M_Frame1 = Acess_seriesi)M1 = -1;/page faultelseM_Frame2 = Acess_seriesi; diseffect+;p+;/all usedelse0-hitif (M_FrameO = Acess_seriesi)M_Frame0 = M_Frame1;M_Frame1 = M_Frame2;M_Frame2 = Acess_seriesi; M0 = -1;1-hitelse if (M_Frame1 = Acess_seriesi) M_Frame1 = M_Frame2;M_Frame2 = Acess_seriesi; M1 = -1;2-hitelse if (M_Frame2 = Acess_seriesi)M2 = -1;/page faultelseM_Frame0 = M_Frame1;M_Frame1 = M_Frame2;M_Frame2 = Acess_seriesi; diseffect+;/print memoryprin tf(|);for (i nt j = 0; j where to use/in itialize memoryfor (int i = 0; i 3; i+)M_Framei = 0;/sleep (2);prin tf( n Start FIFO!n);for (int i = 0; i total_ in struct ion; i+)/all emptyif (p = 0)M_Frame0 = Acess_seriesi;diseffect+;p+;/2 emptyelse if (p = 1)/page faultif (M_Frame0 != Acess_seriesi)M_Frame1 = Acess_seriesi; diseffect+;p+;/hit elseM = 1;11l emptyelse if (p = 2)/hitif (M_FrameO = Acess_seriesi |M_Frame1 = Acess_seriesi)M = 1;/page faultelseM_Frame2 = Acess_seriesi; diseffect+;P+;/all usedelse/hitif (M_Frame0 = Acess_seriesi |M_Frame1 = Acess_seriesi |M_Frame2 = Acess_seriesi) M = 1;/page faultelseM_Frame0 = M_Frame1;M_Frame1 = M_Frame2;M_Frame2 = Acess_seriesi; diseffect+;/print memoryprin tf(|);for (i nt j = 0; j where to use/in itialize memoryfor (int i = 0; i 4; i+)M_Framei = 0;/sleep (2);prin tf( n Start FIFO!n);for (int i = 0; i total_ in struct ion; i+)/all emptyif (p = 0)M_Frame0 = Acess_seriesi;diseffect+;p+;/2 emptyelse if (p = 1)/page faultif (M Frame0 != Acess seriesi)M_Frame1 = Acess_seriesi; diseffect+;P+;/hitelseM = 1;1 emptyelse if (p = 2)/hitif (M_FrameO = Acess_seriesi |M_Frame1 = Acess_seriesi)M = 1;/page faultelseM_Frame2 = Acess_seriesi; diseffect+;p+;else if (p = 3)/hitif (M_Frame0 = Acess_seriesi |M_Frame1 = Acess_seriesi |M_Frame2 = Acess_seriesi)M = 1;/page faultelseM_Frame3 = Acess_seriesi; diseffect+;p+;/all used else/hitif (M_FrameO = Acess_seriesi |M_Frame1 = Acess_seriesi |M_Frame2 = Acess_seriesi |M_Frame3 = Acess_seriesi) M = 1;/page faultelseM_Frame0 = M_Frame1;M_Frame1 = M_Frame2;M_Frame2 = M_Frame3;M_Frame3 = Acess_seriesi;diseffect+;/print memoryprin tf(|);for (i nt j = 0; j 4; j+)printf(” %d |, M_Framej);/judge whether hitif (M = 1)prin tf(hit);M = 0;elseprintf( page fault);prin tf(n);printf(The Page fault Rate is:%f. n, diseffect/total_instruction); prin tf(The Hit Rate is:%f. n, 1-diseffect/total_i nstructio n); return 0;int mai n()int n, m;sran d(i nt)time(NULL);/In itializati on of ran dom nu mber gen erators/In itialize page access seque nee 1 2 3 4 5int Acess_seriestotal_ in struct ion;for (int i = 0; i total_ in struct ion; i+)Acess_seriesi = (in t)ra nd()%5 + 1;prin tf(The Acess_series are:n);for (int i = 0; i
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 活动策划


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!