1、进程管理和存储管理算法#include #include #include#include#define N 100 / 共有100个内存块int processNN+1; / 存放每个进程的页表int blockN; / 内存块状态标志数组,0:空闲,1:使用int blockCount; / 记录当前内存剩余空间int processCount; / 记录当前进程数bool flag = true;int M;typedef struct node int pid; int round; int needtime; int cputime; int count; int state; st
2、ruct node *next;PCB;PCB *finish,*ready,*tail,*run;/void init();void output();/bool createProcess();/bool endProcess();void init() int i, j; / 初始化内存状态标志数组 for (i=0; iN; i+) blocki = 0; for (i=0; i80; i+) blockrand()%(N-1) = 1; blockCount = 0; for (i=0; iN; i+) if (blocki = 0) blockCount+; / 初始化存放进程的数
3、组 for (i=0; iN; i+) processi0 = 0; for (j=1; jN; j+) processij = -1; processCount = 0; printf(初始化结果如下:); output(); flag = false;void output() printf(n内存总量:%d 块, 已用空间:%d 块, 剩余空间:%d 块, 进程总数:%d 个n, N, N-blockCount, blockCount, processCount); if (flag & blockCount N) printf(已使用的内存块(%d):n, N-blockCount);
4、 for (int k=0,count=0; k 0) int id; printf(请输入要查看的进程号: ); scanf(%d,&id); printf(内存详细使用情况如下:n); / for (int i=0; i 0) printf(进程号:%d 占用内存块(%2d):n, id, processid0); printf(|-|n); printf(| 页号| 块号|n); printf(|-|n); for (int j=1,count=0; jstate=R; ready=ready-next; void prt1(PCB *q) printf(* %-10d %-10d %-
5、10d %-10d %-10d %-10c*n, q-pid,q-cputime,q-needtime,q-count,q-round,q-state);void prt() PCB *q; printf(* 进程号 cpu时间 所需时间 记数 时间片 状态 *n); if(run!=NULL) prt1(run); q=ready; while(q!=NULL&q!=run) prt1(q); if(q-next = run) break; else q = q-next; q=finish; while(q!=NULL) prt1(q); q=q-next; getchar();void
6、insert(PCB *q) tail-next =q; tail=tail-next;bool createProcess() int na; int pages, k = 0; PCB *p; int time; /char na10; ready=NULL; finish=NULL; run=NULL; printf( 输入进程数:); scanf(%d,&M); for(int j=1;j 99) printf(错误!进程号过大!n); goto loop; if (pages blockCount) return false; blockCount -= pages; process
7、na0 = pages; for (int i=1; i=pages; i+) while (blockk=1 & kpid,pid); p-pid=na; p-cputime=0; p-needtime=time; p-state=W; p-round =0; p-count =0; if(ready!=NULL) insert(p); else p-next=ready; ready=p; tail=ready; processCount+; printf(创建新进程成功!nn); return true; Roundrun() int timeSlice; /if(processCoun
8、tnext; tail-next =run; run-state=R; prt(); while(run != NULL) run-cputime = run-cputime + timeSlice; run-needtime = run-needtime - timeSlice; run-round+=timeSlice; run-count+; if(run-needtime needtime = 0; run-next = finish; finish = run; if(run != tail) tail-next = ready; else ready = NULL; run-sta
9、te = F; run = NULL; if(ready != NULL) firstin(); else if(ready != NULL) run-state = W; tail = run; run = ready; run - state = R; ready = ready - next; prt(); printf(*n); printf( 输出结束n);bool endProcess() int ID, pages; if (processCount 1) printf(当前内存没有进程!nn); return false; printf(当前内存中的进程有 %d 个, 进程号为
10、:, processCount); for (int i=0; i 0) printf(%2d , i); putchar(n); printf(请输入您要结束的进程号(小于%d):, N); scanf(%d, &ID); PCB *p1,*p2; p1=ready; while(ID!=p1-pid&p1-next!=NULL) p2=p1; p1=p1-next; if(ID=p1-pid) if(p1=ready) ready=p1-next; else p2-next=p1-next; printf(已删除进程%d n,ID); else printf(对不起!您所要结束的进程%d不
11、存在,请重新输入nn,ID); /* pages = processID0; if (pages = 0) printf(对不起!该进程不存在!n); return false; */ for (int j=1; j 创建进程n 2 - 结束进程n 3 - 查看内存n 4 - 运行程序n 0 - 退出程序n); printf(请输入您要进行的操作:); scanf(%d, &choice); switch (choice) case 1: if (createProcess() /else / printf(抱歉!内存空间不足,创建新进程失败!nn); continue; case 2: if (endProcess() / printf(进程已结束!nn); /else / printf(进程结束失败!nn); break; case 3: output(); break; case 4: Roundrun(); break; case 0: return ; default: printf(对不起!您的选择有误!请重新选择!nn); void main() init(); menu();
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1