进程调度之动态高优先权优先(实验二)
来源:互联网 发布:c语言结构体数组赋值 编辑:程序博客网 时间:2024/05/16 12:04
进程:程序在一个数据集合上的动态执行,是系统进行资源分配和调度的独立单位。
动态高优先权优先:就绪进程在时间片上根据优先级的高低轮流执行,当进程结束或时间片结束,进程的优先级减小(相当于提高就绪队列中其他进程的优先级)重新回到就绪队列等待处理机。
模拟动态高优先权优先的C代码:
#include <stdio.h>#include <string.h>#include <stdlib.h>//#define OUTALLINFO#define Max 256#define time 1#define subb 1enum pathread_attr{ //进程的状态信息 runing = 1, wait, finsih};struct Pthread{ //描述进程信息 char *name; int come_time; int need_time; int runing_time; int priority; double zz_time; double dqzz_time; enum pathread_attr attr; struct Pthread *next; Pthread() //默认构造函数 { name = NULL; come_time = 0; runing_time = need_time = 0; zz_time = 0; dqzz_time = 12.66; priority = 0; attr = wait; next = NULL; } void get_info(int end_time) //计算周转时间,带权周转时间 { this->zz_time = (end_time - this->come_time) * 1.0; dqzz_time = zz_time / need_time; } void copy_queue(struct Pthread *temp) //拷贝进程信息 { name = (char*)malloc(sizeof(char) * strlen(temp->name)); strcpy(name,temp->name); come_time = temp->come_time; need_time = temp->need_time; runing_time = temp->runing_time; priority = temp->priority; attr = temp->attr; }};int run_sum = 0; //统计进程的个数void init_pthread(struct Pthread *head,const char *name,const int come_time,const int need_time,const int priority) //初始化进程信息{ struct Pthread *temp = new Pthread(); temp->name = (char *) malloc(sizeof(char) * strlen(name)); strcpy(temp->name,name); temp->come_time = come_time; temp->need_time = need_time; temp->runing_time = need_time; temp->priority = priority; temp->attr = wait; temp->next = head->next; head ->next = temp; return;}void out_one(struct Pthread *temp) //输出一个进程信息{ printf("%s pathread info:\n",temp->name);#ifdef OUTALLINFO printf("come_time: %d\n",temp ->come_time); printf("need_time: %d\n",temp ->need_time); switch(temp->attr){ case 1: printf("attr: waiting \n"); break; case 2: printf("attr: runing \n"); break; case 3: printf("attr: finsih \n"); break; } printf("priority: %d\n",temp->priority);#endif // OUTALLINFO printf("周转时间: %0.2f\n",temp->zz_time); printf("带权周转时间: %0.2f\n",temp->dqzz_time); return;}void out_all(struct Pthread *head) //输出所有进程信息{ for(struct Pthread *p = head->next; p; p = p->next) out_one(p); return;}void add_queue(struct Pthread *head_temp,struct Pthread *head_queue) //添加进程到就绪队列{ struct Pthread *temp = new Pthread(); temp->copy_queue(head_temp); temp->attr = wait; struct Pthread *p = NULL; for(p = head_queue; p ->next; p = p->next); p->next = temp; return;}void sqrt_queue(struct Pthread *head_queue) //根据进程优先级排序{ struct Pthread *p = head_queue ->next; struct Pthread *q = NULL; (head_queue) ->next = NULL; for(struct Pthread *pp = p; p; ){ pp = p; p = p ->next; pp ->next = NULL; for(q = head_queue; q ->next; q = q ->next){ if(q ->next->priority < pp ->priority){ pp->next = q->next; q ->next = pp; break; } if(q ->next->priority == pp ->priority){ if(q->next->come_time > pp->come_time){ pp->next = q->next; q ->next = pp; break; } } } if(!q ->next) q ->next = pp; } head_queue->attr = runing; //进程处于执行状态 return;}void queue_del(struct Pthread *head_queue) //从就绪队列删除一个进程{ if(!(head_queue ->next)) return; head_queue ->next->attr = finsih; out_one(head_queue ->next); struct Pthread *p = head_queue ->next; head_queue->next = p->next; delete p; return;}void mo_ni(struct Pthread *head,struct Pthread *head_queue) //模拟动态高优先权优先处理过程{ int run_i = 0,run_over = 0,time_runing = 0; while(time_runing >= 0){ for(struct Pthread *p = head ->next; p; p = p->next) //检查此时刻是否有进程到达 if(time_runing == p->come_time) add_queue(p,head_queue); if(head_queue ->next){ //如果就绪队列非空 if(run_i == time && head_queue->next->runing_time){ //时间片结束&&进程为未完成 if(head_queue->next->priority){ //优先级非0 head_queue->next->priority -= subb; //降低该进程的优先级 if(head_queue->next->priority < 0) //如果小于0,则置为0 head_queue->next->priority = 0; } sqrt_queue(head_queue); //根据进程的优先级排序 run_i = 0; //时间片置0 } if(!head_queue->next->runing_time){ //如果进程执行结束 if(head_queue->next->priority){ head_queue->next->priority -= subb; if(head_queue->next->priority < 0) head_queue->next->priority = 0; } head_queue->next->get_info(time_runing); queue_del(head_queue); //从就绪队列中删除该进程 sqrt_queue(head_queue); //根据进程优先级排队 run_i = 0; //时间片置0 run_over++; //统计已经结束的进程个数 } if(head_queue ->next) //如果有进程正在执行,服务时间减1 head_queue->next->runing_time--; run_i++; //时间片加1 } time_runing++; if(run_over == run_sum) //如果处理完所有进程则结束模拟 return; } return;}int main(int argc,char *argv[]){ struct Pthread *head = new Pthread(); //进程链表 struct Pthread *head_queue = new Pthread(); //就绪态进程链表队列 char name[Max]; int come_time,need_time,priority; FILE *input_file = fopen("C:\\Users\\chenhongyu\\Desktop\\test.txt","r"); //读取测试信息 printf("plase input name come_time need_time priority,if over,plase input [Ctrl + Z]\n\n"); while(fscanf(input_file,"%s%d%d%d",name,&come_time,&need_time,&priority) != EOF){ init_pthread(head,name,come_time,need_time,priority); //初始化进程链表 run_sum++; //统计总共多少个进程 } mo_ni(head,head_queue); //模拟动态高优先权处理机的进程调度 return 0;}
0 0
- 进程调度之动态高优先权优先(实验二)
- 动态优先权进程调度算法
- 操作系统——动态高优先权优先调度算法(无到达时间)
- OS 使用动态优先权的进程调度
- FCFS,高优先权算法进程调度程序模拟
- 使用动态优先权的进程调度算法的模拟
- 使用动态优先权的进程调度算法的模拟
- 进程调度实验(最高优先数优先算法)的java实现
- 作业调度之短作业优先(实验一)
- 操作系统——使用动态优先权的进程调度算法的模拟
- 操作系统-使用动态优先权的进程的调度算法的模拟
- java按优先权调度进程算法实现
- java按优先权调度进程算法实现
- OS之进程调度(二)
- 进程调度(二)
- 操作系统实验二(调度算法模拟-先进先出-时间片轮转法-优先服务调度算法)
- 操作系统实验三之进程调度算法
- 操作系统——进程调度之短进程优先
- ios JSPatch热更新技术详解
- 二分查找
- 10.30 E - 汉诺塔III
- B哥传
- linux中各种删除
- 进程调度之动态高优先权优先(实验二)
- 实现点击通知栏发送广播控制音乐的播放、暂停、上一曲、下一曲
- 关于HTTP的一些自我归纳
- 简单c-1
- 数据结构--KMP应用
- 【DFS】CODE[VS] 2066 三角恋(刷题记录)
- 169. Majority Element (python)
- JUNIPER路由器中常用命令总结
- 使用Spring Cloud Config搭建配置中心