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