为了正常的体验网站,请在浏览器设置里面开启Javascript功能!

操作系统进程调度算法模拟

2017-09-30 7页 doc 52KB 42阅读

用户头像

is_482581

暂无简介

举报
操作系统进程调度算法模拟操作系统进程调度算法模拟 实验一 进程调度算法模拟 一 .实验题目: 设计一个简单的进程调度算法,模拟OS中的进程调度过程 二(要求: ? 进程数不少于5个; ? 进程调度算法任选; 可以用动态优先数加时间片轮转法实现进程调度,每运行一个时间片优先数减3; ? 用C语言编程; ? 程序运行时显示进程调度过程。 三(程序中所用数据结构及说明: 进程控制块结构体: typedef struct node1 { int ID;//进程标识数 int PRIORITY;//进程优先数 int CPUTIME...
操作系统进程调度算法模拟
操作系统进程调度算法模拟 实验一 进程调度算法模拟 一 .实验目: 一个简单的进程调度算法,模拟OS中的进程调度过程 二(要求: ? 进程数不少于5个; ? 进程调度算法任选; 可以用动态优先数加时间片轮转法实现进程调度,每运行一个时间片优先数减3; ? 用C语言编程; ? 程序运行时显示进程调度过程。 三(程序中所用数据结构及说明: 进程控制块结构体: typedef struct node1 { int ID;//进程标识数 int PRIORITY;//进程优先数 int CPUTIME;//进程已占用时间片 int ALLTIME;//进程还需占用时间片 }Block,pcb; 就绪进程链节点: typedef struct node2 { pcb data; struct node2 *next; }process; 四(清单程序及描述: Procelink.h: typedef struct node1 { int ID;//进程标识数 int PRIORITY;//进程优先数 int CPUTIME;//进程已占用时间片 int ALLTIME;//进程还需占用时间片 //char STATE;//进程状态 //struct node *next;//进程队列指针 }Block,pcb; typedef struct node2 { pcb data; struct node2 *next; }process; void Initlink(process **PQ) { /*如果有内存空间,申请头结点空间并使头指针head指向头结点*/ if((*PQ = (process *)malloc(sizeof(process))) == NULL) exit(1); (*PQ)->next = NULL; /*置链尾标记NULL */ } int IsEmpty(process *PQ) { if(PQ->next == NULL) return 1; else return 0; } void EnInitlink(process *PQ,pcb p) { while(PQ->next!=NULL) PQ=PQ->next; process *temp=(process *)malloc(sizeof(Block)); temp->data.PRIORITY=p.PRIORITY; temp->data.ID=p.ID; temp->data.CPUTIME=p.CPUTIME; temp->data.ALLTIME=p.ALLTIME; temp->next=PQ->next; PQ->next=temp; }//插入 pcb DeInitlink(process *PQ) //选择优先数最小的出列 { if(IsEmpty(PQ)) { printf("所有进程已运行完!\n"); exit(0);//退出 } process *temp=(process *)malloc(sizeof(Block)); temp = PQ->next; process *te=(process *)malloc(sizeof(Block)); process *t=(process *)malloc(sizeof(Block)); te= PQ->next;//优先数最小的进程 while(temp->next != NULL) { if(te->data.PRIORITYnext->data.PRIORITY) { te=temp->next; t=temp->next->next; PQ=temp; } temp=temp->next; } PQ->next=PQ->next->next; pcb pp=te->data; // free(temp); // free(t); //free(te); return pp; }//出队列 void outid(process *PQ)//输出就绪队列函数 { printf("当前就绪队列为: "); while(!IsEmpty(PQ)) { printf("%d ",PQ->next->data.ID); PQ=PQ->next; } printf("\n"); } void dispatch(pcb p,process *PQ)//进程运行函数 { if((p.ALLTIME)!=0) { p.PRIORITY-=3; p.CPUTIME+=1; p.ALLTIME-=1; printf("进程 %d运行\n",p.ID); //printf("进程优先数:%d 进程已占用时间片:%d 进程还需占用时间 片:%d\n",p.PRIORITY,p.CPUTIME,p.ALLTIME); outid(PQ);//输出就绪队列 } if((p.ALLTIME)==0) { printf("进程 %d 运行完成!\n",p.ID); return;//完成则不加入链表 } EnInitlink(PQ,p); return;//运行之后再加入链表 } os_1.cpp: #include #include #include"procelink.h" void main() { process * PQ; int n;//n为进程数 pcb pro1; Initlink(& PQ); printf("请输入进程个数: "); scanf("%d",&n); printf("请输入各个进程的 进程标识数ID,进程优先数,进程已占用时间片,进程还需占用时间片 \n"); for(int i=1;i<=n;i++) { printf("第%d进程: ",i); scanf("%d %d %d %d",&pro1.ID,&pro1.PRIORITY,&pro1.CPUTIME,&pro1.ALLTIME); EnInitlink(PQ,pro1); } while(!IsEmpty(PQ)) { dispatch(DeInitlink(PQ),PQ);//进程运行函数调用 } if(IsEmpty(PQ)) printf("所有进程已运行完!\n"); free(PQ);//释放内存空间 } 五(执行结果: 六(实验总结: 通过这次操作系统中进程调度算法的模拟,使我对操作系统中的进程调度有了更清晰的认识和了解,程序中也有不足之处,该程序是顺序进链,出链时再选择优先数最大的进程。比如调度某个进程时若该进程大于一个时间片(即调度一次不能结束)则可以直接修改进程信息,而不必使其先出链在进链,增加了时间复杂度。
/
本文档为【操作系统进程调度算法模拟】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。 本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。 网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。

历史搜索

    清空历史搜索