ImageVerifierCode 换一换
格式:DOCX , 页数:33 ,大小:20.40KB ,
资源ID:12633608      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/12633608.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(操作系统实验源代码.docx)为本站会员(b****2)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

操作系统实验源代码.docx

1、操作系统实验源代码操作系统实验源代码C+控制台程序(win32 console application)实验一:处理机调度算法的实现#include stdafx.h#include stdio.h #include #include #define getpch(type) (type*)malloc(sizeof(type) #define NULL 0 struct pcb /* 定义进程控制块PCB */ char name10; char state; int super; int ntime; int rtime; struct pcb* link; *ready=NULL,*p;

2、 typedef struct pcb PCB; void sort() /* 建立对进程进行优先级排列函数*/ PCB *first, *second; int insert=0; if(ready=NULL)|(p-super)(ready-super) /*优先级最大者,插入队首*/ p-link=ready; ready=p; else /* 进程比较优先级,插入适当的位置中*/ first=ready; second=first-link; while(second!=NULL) if(p-super)(second-super) /*若插入进程比当前进程优先数大,*/ /*插入到当

3、前进程前面*/ p-link=second; first-link=p; second=NULL; insert=1; else /* 插入进程优先数最低,则插入到队尾*/ first=first-link; second=second-link; if(insert=0) first-link=p; void input() /* 建立进程控制块函数*/ int i,num; system(cls); /*清屏*/ printf(n 请输入进程数: ); scanf(%d,&num); for(i=1;iname); printf(n 输入进程优先数:); scanf(%d,&p-super

4、); printf(n 输入进程运行时间:); scanf(%d,&p-ntime); printf(n); p-rtime=0;p-state=W; p-link=NULL; sort(); /* 调用sort函数*/ int space() int l=0; PCB* pr=ready; while(pr!=NULL) l+; pr=pr-link; return(l); void disp(PCB * pr) /*建立进程显示函数,用于显示当前进程*/ printf(n 进程名t 状态t 优先数t 需要运行时间t 已经运行时间n); printf(|%st,pr-name); print

5、f(|%ct,pr-state); printf(|%dt,pr-super); printf(|%dtt,pr-ntime); printf(|%dt,pr-rtime); printf(n); void check() /* 建立进程查看函数*/ PCB* pr; printf(n * 当前正在运行的进程是:n); /*显示当前运行进程*/ disp(p); pr=ready; printf(n * 当前就绪队列状态为:n); /*显示就绪队列状态*/ while(pr!=NULL) disp(pr); pr=pr-link; void destroy() /*建立进程撤消函数(进程运行结

6、束,撤消进程)*/ printf(n 进程%s 已完成.n,p-name); free(p); void running() /* 建立进程就绪函数(进程运行时间到,置就绪状态*/ (p-rtime)+; if(p-rtime=p-ntime) destroy(); /* 调用destroy函数*/ else (p-super)-; p-state=W; sort(); /*调用sort函数*/ void main() /*主函数*/ int len,h=0; char ch; input(); len=space(); while(len!=0)&(ready!=NULL) ch=getch

7、ar(); h+; printf(-); printf(n 现在是第%d次运行:n,h); p=ready; ready=p-link; p-link=NULL; p-state=R; check(); running(); printf(n 按任意键继续.n); printf(nn 进程已经完成.n); 实验二:存储器的分配与回收算法实现#include stdafx.h/Memory allocation and recovery algorithm#include #include #include using namespace std;typedef struct FreeLink/

8、定义自由链 struct FreeLink *prior; char name; int start; int size; bool flag; struct FreeLink *next;* ptr,*head;head top;ptr p;void print()/将内存分配情况打印到屏幕上 p=top; cout*内存分配情况表*endl; cout区号tt起始位置t区间长度t区间状态tendl; do coutnamettstartttsizeflag=false)cout空闲endl; elsecout已占用next; while(p!=NULL);void clear()/结束操作

9、时清空“内存”以备其他操作 do p=top; top=top-next; free(p); while(top!=NULL);void asc(ptr &p)/最佳适应法的内存分配函数 int min; ptr op; FreeLink *fl=(FreeLink *)malloc(sizeof(FreeLink); cout请输入要分配内存的进程名fl-name; cout请输入要分配内存的大小fl-size; min=256; fl-flag=true; do if(p-flag=false&p-sizesize=fl-size) min=p-size; op=p; p=p-next;

10、while(p!=NULL); if(op-sizefl-size) fl-start=op-start; op-start=fl-start+fl-size; op-size=op-size-fl-size; fl-next=op; fl-prior=op-prior; op-prior-next=fl; op-prior=fl; goto flag1; if(op-size=fl-size) op-flag=fl-flag; op-name=fl-name; free(fl); goto flag1; cout内存过小,分配失败!endl;goto flag2;flag1: cout分配成

11、功!endl;flag2: ;void dec(ptr &p)/最坏适应法的内存分配函数 int max; ptr op; FreeLink *fl=(FreeLink *)malloc(sizeof(FreeLink); cout请输入要分配内存的进程名fl-name; cout请输入要分配内存的大小fl-size; max=fl-size; fl-flag=true; do if(p-flag=false&p-size=max) max=p-size; op=p; p=p-next; while(p!=NULL); if(op-sizefl-size) fl-start=op-start;

12、 op-start=fl-start+fl-size; op-size=op-size-fl-size; fl-next=op; fl-prior=op-prior; op-prior-next=fl; op-prior=fl; goto flag3; if(op-size=fl-size) op-flag=fl-flag; op-name=fl-name; free(fl); goto flag3; cout内存过小,分配失败!endl;goto flag4;flag3: cout分配成功!prior-flag=false&p-next-flag=false)x=1; if(p-prior-

13、flag=false&p-next-flag=true)|(p-prior-flag=false&p-next=NULL)x=2; if(p-prior-flag=true&p-next-flag=false)|(p-prior=NULL&p-next-flag=false)x=3; if(p-prior-flag=true&p-next-flag=true)|(p-prior=NULL&p-next-flag=true)|(p-prior-flag=true&p-next=NULL)x=4; switch(x) case 1:p-next-prior=p-prior; p-prior-nex

14、t=p-next; p-prior-size=p-prior-size+p-size+p-next-size; p-prior-next=p-next-next; if(p-next-next!=NULL)p-next-next-prior=p-next-prior; free(p-next); free(p); break; case 2:if(p-next=NULL) p-prior-next=p-next; else p-next-prior=p-prior; p-prior-next=p-next; p-prior-size=p-prior-size+p-size; free(p);

15、break; case 3:if(p-prior=NULL) top=p-next; p-next-prior=NULL; p-next-start=p-start; p-next-size=p-next-size+p-size; else p-next-prior=p-prior; p-prior-next=p-next; p-next-start=p-start; p-next-size=p-next-size+p-size; free(p); break; case 4:p-name=; p-flag=false; break; void allocate(ptr &p)/最先适应法的内

16、存分配函数 FreeLink *fl=(FreeLink *)malloc(sizeof(FreeLink); cout请输入要分配内存的进程名fl-name; cout请输入要分配内存的大小fl-size; fl-flag=true; do if(p-flag=false&p-sizefl-size) fl-start=p-start; p-start=fl-start+fl-size; p-size=p-size-fl-size; fl-next=p; fl-prior=p-prior; p-prior-next=fl; p-prior=fl; goto a; if(p-flag=fals

17、e&p-size=fl-size) p-flag=fl-flag; p-name=fl-name; free(fl); goto a; p=p-next; while(p!=NULL); cout内存过小,分配失败!endl;goto b;a: cout分配成功!endl;b: ;void recover(ptr &p)/内存回收函数 char n = ; coutn; do if(p-flag=true&p-name=n) splice(p); goto c; p=p-next; while(p!=NULL); cout内存并未分配给对应进程,回收失败!endl;goto d;c: cout

18、内存回收成功!next=top; pcb-prior=top-prior; top-prior=pcb; pcb-start=top-start; cout请输入要为系统分配的内存块名pcb-name; cout请输入要分配内存的大小endl; goto f;e: cout超过内存最大容量请重新输入要分配内存的大小pcb-size; if(pcb-size256) goto e; top-size=top-size-pcb-size; top=pcb; top-flag=true; top-next-start+=top-size; print(); while(true) do p=top-

19、next; cout请从下列选项中进行选择endl; cout1.分配内存endl; cout2.回收内存endl; cout3.结束操作endl; coutchoice; while(choice!=1&choice!=2&choice!=3); switch(choice) case 1:allocate(p);print();break; case 2:recover(p);print();break; case 3:clear();return 0;break; int bfa()/最佳适应法 char choice= ; print(); ptr pcb=(FreeLink *)ma

20、lloc(sizeof(FreeLink); pcb-next=top; pcb-prior=top-prior; top-prior=pcb; pcb-start=top-start; cout请输入要为系统分配的内存块名pcb-name; cout请输入要分配内存的大小endl; goto h;g: cout超过内存最大容量请重新输入要分配内存的大小pcb-size; if(pcb-size256) goto g; top-size=top-size-pcb-size; top=pcb; top-flag=true; top-next-start+=top-size; print(); w

21、hile(true) do p=top-next; cout请从下列选项中进行选择endl; cout1.分配内存endl; cout2.回收内存endl; cout3.结束操作endl; coutchoice; while(choice!=1&choice!=2&choice!=3); switch(choice) case 1:asc(p);print();break; case 2:recover(p);print();break; case 3:clear();return 0;break; int wfa()/最坏适应法 char choice= ; print(); ptr pcb

22、=(FreeLink *)malloc(sizeof(FreeLink); pcb-next=top; pcb-prior=top-prior; top-prior=pcb; pcb-start=top-start; cout请输入要为系统分配的内存块名pcb-name; cout请输入要分配内存的大小endl; goto j;i: cout超过内存最大容量请重新输入要分配内存的大小pcb-size; if(pcb-size256) goto i; top-size=top-size-pcb-size; top=pcb; top-flag=true; top-next-start+=top-s

23、ize; print(); while(true) do p=top-next; cout请从下列选项中进行选择endl; cout1.分配内存endl; cout2.回收内存endl; cout3.结束操作endl; coutchoice; while(choice!=1&choice!=2&choice!=3); switch(choice) case 1:dec(p);print();break; case 2:recover(p);print();break; case 3:clear();return 0;break; int main()/主函数 char choice = ; ptr free=(FreeLink *)malloc(sizeof(FreeLink); top=free; top-name=; top-start=0; top-size=256; top-flag=false; top-prior=NULL; top-next=NULL; cout*Memory allocation and recovery algorithm*endl; cout*存储器的分配与回收算法*

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1