资源预览内容
第1页 / 共19页
第2页 / 共19页
第3页 / 共19页
第4页 / 共19页
第5页 / 共19页
第6页 / 共19页
第7页 / 共19页
第8页 / 共19页
第9页 / 共19页
第10页 / 共19页
亲,该文档总共19页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
天津大学仁爱学院计算机系操作系统原理实验报告指导老师: 张磊 殷妍 姓名:徐亮亮学号:600920375班级:网络(2)班完成日期: 2012年1 月 1 日操作系统课程实验(一)生产者消费者同步问题的算法实现一)问题描述:一组生产者向一组消费者提供消息,它们共享一个有界缓冲池,生产者向其中投放消息,消费者从中取得消息。假定这些生产者和消费者互相等效,只要缓冲池未满,生产者可将消息送入缓冲池;只要缓冲池未空,消费者可从缓冲池取走一个消息。二)整体功能设计采用多线程方式实现,有3个生产者,4个消费者,生产者当有空池时放入数据,消费者当有数据时取出并打印三)代码:/by 徐亮亮/2012 1 1#include #include #include #include #define p(a) sem_wait(&a)#define v(a) sem_post(&a)/定义生产者,消费者线程个数,缓冲区大小#define PRD 3#define CSM 4#define PSZ 15using namespace std;/定义剩余缓冲区,已有数据项,访问限制锁sem_t pool,iterms,mutex;/定义缓冲区queue pdt;/生产者线程void* prod(void*argv) while(1) p(pool);p(mutex);pdt.push(*(char*)argv);cout#includeusing namespace std;struct procstring name,state;/PRIO/ROUNDint pr;unsigned long cputime,needtime;int count;proc* next;proc *run(NULL),*ready(NULL),*finish(NULL),*tail(NULL);string algo;/打印函数void print()proc*tem;for(tem=ready;tem!=NULL;tem=tem-next)coutnamestatenext)coutnamestatenext!=NULL;tem=tem-next);tem-next=new proc;tem=tem-next;cintem-name;cintem-pr; cintem-needtime;tem-next=NULL;tem-count=0;tem-state=W;tail=tem;/循环调度void roundsch()run-cputime+=run-pr;run-needtime-=run-pr;+run-count;if(run-needtime0)run-state=W;tail-next=run;tail=run;ready=ready-next;else run-state=O;if(ready=run)ready=run-next;elseproc*tem;for(tem=ready;tem-next!=run;tem=tem-next);tem-next=run-next;if(finish=NULL)finish=run;elsefor(proc*tem=finish;tem-next!=NULL;tem=tem-next);tem-next=run;tail-next=run;tail=run;ready=ready-next;/优先级调度void prisch()int maxm(0);proc* maxp;run-pr-=3;+run-cputime; -run-needtime;+run-count;for(proc*tem=ready;tem!=NULL;tem=tem-next)if(tem-prmaxm)maxm=tem-pr;maxp=tem;if(run-needtime0)run-state=W;elserun-state=O;if(ready=run)ready=run-next;elsefor(proc*tem=ready;tem-next!=run;tem=tem-next);tem-next=run-next;if(finish=NULL)finish=run;elsefor(proc*tem=finish;tem-next!=NULL;tem=tem-next);tem-next=run;run=maxp;print();void firstin()run=ready;run-state=R;if(algo=PRIORITY)prisch();else if(algo=ROUNDROBIN)roundsch();int main()for(int i=0;ialgo; firstin();(4)使用说明 按提示输入信息,最后输入调度方式(5)结果分析数据量太少看不出两种调度的区别(6)设计体会无操作系统课程实验(三)内存管理和分配算法模拟1、 问题概述目的:页面置换算法的好坏直接关系到系统运行的效率。本设计要求用高级语言编写和调试一个简单的内存页面调度程序。通过本实验可以加深理解有关最佳置换算法、先进先出置换算法和最近最久未使用置换算法的概念,并体会和了解这三种页面置换算法的具体实施办法。问题:1、页面流如下:7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,12 首先利用最佳置换算法求出理想状态下的置换率。再将先进先出算法和最近最久未使用算法与最佳置换算法做比较,说明哪个缺页率较低。3 假设所用的物理块个数分别为3和4。二、整体功能及设计三、编程实现(代码)/by 徐亮亮/2012 1 1#includeusing namespace std;void algorithm3_2() int cache3 = -1, -1, -1 ;int count = 0;int i, j, k = 0;for (i = 0; i = 0; j-) if (flag1 * flag2 * flag3) != 0) goto N;if (cache0 = pagej) if (flag1 != 0) goto M;flag1 = 1;index = 0;else if (cache1 = pagej) if (flag2 != 0) goto M;flag2 = 1;index = 1;else if (cache2 = pagej) if (flag3 != 0) goto M;flag3 = 1;index = 2;M:continue;N:cacheindex = pagei;L:for (int m = 0; m ph_ch;L:int ch_al;switch (ph_ch) case 3:coutch_al;switch (ch_al) case 1:algorithm3_1();break;case 2:algorithm3_2();break;case 3:algorithm3_3();break;default :coutch_al;switch (ch_al) case 1:algorithm4_1();break;case 2:algorithm4_2();break;case 3:algorithm4_3();break;default :cout#include#includeusing namespace std;int TR9 = 55, 58, 39, 18, 90, 160, 150, 38, 184;int main() int mark,num,number,numb;int i,j,k,m;int a10, b10;float sum1=0,sum2=0;float avg1,avg2;cout=abs(num-TRj) numb=abs(num-TRj); number=TRj;mark=j;TRmark=1000;printf(%d %dn,number,abs(num-number);sum1=sum1+(abs(num-number);num=number;avg1=sum1/9;printf(最短寻道时间算法平均长度为:%4.2fn,avg1);printf(扫描算法n);printf(从100号磁道开始)n);num=100;number=100;TR0=55;TR1=58;TR2=39;TR3=18;TR4=90;TR5=160; TR6=150;TR7=38;TR8=184;k=0;m=0;for( i=0; inum) ak=TRi;k+; for( i=0; i=abs(num-aj) numb=abs(num-aj);number=aj;mark=j;amark=1000;printf(%d %dn,number,abs(num-number);sum2=sum2+(abs(num-number);num=number;for(i=0; i=abs(num-bj) numb=abs(num-bj);number=bj;mark=j; bmark=1000;printf(%d %dn,number,abs(num-number);sum2=sum2+(abs(num-number);num=number;avg2=sum2/9;printf(扫描算法平均长度为:%4.2fn,avg2);while(1);结果分析
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号