时间片轮转——进程调度
程序员文章站
2022-06-23 23:26:59
原创 上一篇博客写了最高优先级算法——进程调度:http://www.cnblogs.com/chiweiming/p/9028002.html 此篇介绍时间片轮转调度,时间片轮转调度比最高优先级调度更为简单,每次都从PCB(进程存在的唯一标识)队列中将 首进程调入CPU,增加其已用CPU时间,改变 ......
原创
上一篇博客写了最高优先级算法——进程调度:http://www.cnblogs.com/chiweiming/p/9028002.html
此篇介绍时间片轮转调度,时间片轮转调度比最高优先级调度更为简单,每次都从PCB(进程存在的唯一标识)队列中将
首进程调入CPU,增加其已用CPU时间,改变其进程状态;然后判断其已用CPU时间是否大于等于需要运行时间,大于将
其进程状态置为完成状态,否则将此PCB插入队列尾部,再次在队列中寻找优先级最高的PCB...两篇博客的代码大同小异。
/* 时间片轮转算法 */ #include<stdio.h> #include<stdlib.h> #include<time.h> #define N 3 #define Time_film 2 //时间片 int count = 0; void print(struct PCB *head); struct PCB{ int process_name; //进程名 int priority_number; //优先数,全置0 int arrive_time; //到达时间,为进程的创建时间 int need_time; //需要运行的时间,随机产生 int used_time; //已用CPU的时间,初始值为0 int process_state; //进程状态,1表示运行,0表示完成,-1表示就绪,初始值为-1 struct PCB *cre; //前驱指针域 struct PCB *next; //后驱指针域 }; void Process_scheduling(struct PCB *head){ /* 扫描队列,将进程按到底顺序调入CPU运行; 如果 use_CPU == need_time 撤销此PCB; 否则用完一个时间片后放回队列尾部,继续扫描; */ //**************************** struct PCB *Move=head; struct PCB *Max_Pri=head->next; struct PCB *Tail; //尾指针 //**************************** Move->next = Max_Pri->next; //调出队列首进程 if(Move->next != NULL){ Move = Max_Pri->next; Move->cre = Max_Pri->cre; } //**************************** printf(" 进程 %d 被调度: \n",Max_Pri->process_name); Max_Pri->used_time += Time_film; //增加CPU占用时间 if(Max_Pri->used_time >= Max_Pri->need_time){ Max_Pri->used_time = Max_Pri->need_time; //进程状态改变 Max_Pri->process_state = 0; count++; } else{ Max_Pri->process_state = 1; } printf(" %d %d %d %d %d %d \n\n",Max_Pri->process_name,Max_Pri->priority_number,Max_Pri->arrive_time,Max_Pri->need_time,Max_Pri->used_time,Max_Pri->process_state); if(count == N){ //所有进程执行完毕 printf(" 所有进程执行完毕!"); return; } printf(" 就绪队列:\n"); print(head); //输出就绪队列 printf("\n"); //**************************** if(Max_Pri->process_state !=0){ Move = head; while( Move->next!=NULL ){ //当被调出进程未完成时将其插入就绪队列尾部 Move = Move->next; } Tail = Move; Max_Pri->cre = Tail; Max_Pri->next = NULL; Tail->next = Max_Pri; Max_Pri->process_state = -1; } //**************************** Process_scheduling(head); } void print(struct PCB *head){ //输出队列函数 if(head->next == NULL){ printf("就绪队列已空\n"); return; } printf("name priority arr_time need_time use_CPU pro_state\n"); struct PCB *fry = head->next; while(fry != NULL){ printf(" %d ",fry->process_name); printf("%d ",fry->priority_number); printf("%d ",fry->arrive_time); printf("%d ",fry->need_time); printf("%d ",fry->used_time); printf("%d ",fry->process_state); printf("\n"); fry = fry->next; } printf("\n"); } int main(){ struct PCB *head; //头指针 struct PCB Pro[N+1]; //创建 N+1 个进程 head = &Pro[0]; srand(time(0)); //**************************** //设置进程参数 Pro[0].process_name = 0; Pro[0].cre = NULL; Pro[0].next = &Pro[1]; Pro[0].priority_number = 0; int i=0; for(i=1;i<=N;i++){ Pro[i].process_name = i; Pro[i].priority_number = 0; Pro[i].arrive_time = i; Pro[i].need_time = rand()%7; while(Pro[i].need_time == 0){ Pro[i].need_time = rand()%7; } Pro[i].used_time = 0; Pro[i].process_state = -1; } for(i=1;i<=N;i++){ //形成双向队列 if( i == N ){ Pro[i].cre = &Pro[i-1]; Pro[i].next = NULL; break; } Pro[i].cre = &Pro[i-1]; Pro[i].next = &Pro[i+1]; } //**************************** printf(" 进程初始状态: \n"); print(head); //输出初始队列状态 Process_scheduling(head); //调用进程调度函数(时间片轮转) return 0; }
(运行结果部分截图)
15:52:07
2018-05-13