一.实验名称:进程调度功能的设计与实现
二.实验目的:
通过该实验,理解进程调度的概念;深入了解进程控制块的功能、进程的创建、删除以及进程各个状态间的转换过程。
三.实验内容:
1)实现进程相关数据结构(如进程控制块task_struct)的创建和查看功能。
2)实现多种进程调度算法:先来先服务算法、优先级调度算法、时间片轮转法、多级反馈轮转法(选做)等。
3)实现相关队列在进程调度中的动态变化过程。
#include #include #define N 20 typedef struct pcb { char pname[N]; /*进程名*/ int priority; /*进程优先级,用整数表示,值越小,优先级越高*/ int arrivetime; /*进程到达时间*/ int runtime; /*运行进程所需时间*/ char state; /*进程状态*/ struct pcb *next; /*链表指针*/ }PCB; static char R = 'r' , E = 'e' , B = 'b' , F = 'f'; /*进程状态:就绪、执行、阻塞、完成*/ PCB ready_head; /*就绪进程队列*/ PCB execute_head; /*执行进程队列*/ PCB block_head; /*阻塞进程队列*/ PCB finish_head; /*完成进程队列*/ int currenttime; /*记录系统当前时间*/ void createProcess(); /*建立进程队列,所有进程初始状态为就绪态*/ void showProcess(); /*显示所有队列中的进程情况*/ int isProcessReady(); /*就绪队列中的进程进入执行队列*/ void executeProcess1(); /*按先来先服务调度算法执行进程*/ int runProcess1(); /*运行执行队列中的进程*/ void executeProcess2(); /*按优先级调度算法执行进程*/ int runProcess2(); /*运行执行队列中的进程*/ void executeProcess3(); /*按时间片轮转调度算法执行进程*/ int runProcess3(); /*运行执行队列中的进程*/ void sortByArrivetime(); /*将就绪队列中的进程按到达时间排序*/ void sortByPriority(); /*将就绪队列中的进程按优先级排序*/ /*建立进程队列,所有进程初始状态为就绪态*/ void createProcess() { PCB *p = &ready_head; int num; /*进程数*/ printf("How many processes do you want to run : "); scanf("%d",&num); for(int i = 0;i < num;i ++) { printf("/n"); p->next = new PCB; p = p->next; printf("No.%3d process input pname: ",i + 1); scanf("%s",p->pname); printf(" priority: "); scanf("%d",&p->priority); printf(" arrivetime: "); scanf("%d",&p->arrivetime); printf(" runtime: "); scanf("%d",&p->runtime); p->state = R; } p->next = NULL; } /*显示所有队列中的进程情况*/ void showProcess() { PCB *p; p = &ready_head; p = p->next; printf("/n就绪队列中的进程:/n"); while(p != NULL) { printf("pname:%s/t/tpriority:%d/tarrivetime:%d/truntime:%d/tState:%c/n",p->pname, p->priority,p->arrivetime,p->runtime,p->state); p = p->next; } p = &execute_head; p = p->next; printf("/n执行队列中的进程:/n"); while(p != NULL) { printf("pname:%s/t/tpriority:%d/tarrivetime:%d/truntime:%d/tState:%c/n",p->pname, p->priority,p->arrivetime,p->runtime,p->state); p = p->next; } p = &block_head; p = p->next; printf("/n阻塞队列中的进程:/n"); while(p != NULL) { printf("pname:%s/t/tpriority:%d/tarrivetime:%d/truntime:%d/tState:%c/n",p->pname, p->priority,p->arrivetime,p->runtime,p->state); p = p->next; } p = &finish_head; p = p->next; printf("/n完成队列中的进程:/n"); while(p != NULL) { printf("pname:%s/t/tpriority:%d/tarrivetime:%d/truntime:%d/tState:%c/n",p->pname, p->priority,p->arrivetime,p->runtime,p->state); p = p->next; } } /*就绪队列中的进程进入执行队列*/ int isProcessReady() { if(ready_head.next == NULL) { if(execute_head.next == NULL) return 0; else return 1; } PCB *p1 , *p2 , *p3; p3 = &execute_head; while(p3->next != NULL) { p3 = p3->next; } p2 = &ready_head; p1 = p2->next; while(p1 != NULL) { if((p1->arrivetime <= currenttime)&&(p1->state == R)) { printf("Time slice is %2d ; Process %s start./n",currenttime,p1->pname); p1->state = E; p2->next = p1->next; p1->next = p3->next; p3->next = p1; p3 = p3->next; p1 = p2; } p2 = p1; p1 = p2->next; } return 1; } /*按先来先服务调度算法执行进程*/ void executeProcess1() { sortByArrivetime(); while(1) { if(isProcessReady() == 0) return; else runProcess1(); } } /*运行执行队列中的进程*/ int runProcess1() { PCB *p1 , *p2 , *p3; if(execute_head.next == NULL) { currenttime ++; return 1; } else { p2 = &execute_head; p1 = p2->next; p3 = &finish_head; while(p3->next != NULL) { p3 = p3->next; } while(p1 != NULL) { while( p1->runtime > 0) { p1->runtime --; currenttime ++; } if(p1->runtime == 0) { printf("Time slice is %2d ; Process %s end./n",currenttime,p1->pname); p1->state = F; p2->next = p1->next; p1->next = p3->next; p3->next = p1; p3 = p3->next; p1 = p2; } p2 = p1; p1 = p2->next; } return 1; } } /*按优先级调度算法执行进程*/ void executeProcess2() { while(1) { if(isProcessReady() == 0) return; else runProcess2(); } } /*运行执行队列中的进程*/ int runProcess2() { PCB *p1 , *p2 , *p3; sortByPriority(); if(execute_head.next == NULL) { currenttime ++; return 1; } else { p2 = &execute_head; p1 = p2->next; p3 = &finish_head; while(p3->next != NULL) { p3 = p3->next; } p1->runtime --; currenttime ++; if(p1->runtime == 0) { printf("Time slice is %2d ; Process %s end./n",currenttime,p1->pname); p1->state = F; p2->next = p1->next; p1->next = p3->next; p3->next = p1; } return 1; } } /*按时间片轮转调度算法执行进程*/ void executeProcess3() { while(1) { if(isProcessReady() == 0) return; else runProcess3(); } } /*运行执行队列中的进程*/ int runProcess3() { PCB *p1 , *p2 , *p3; if(execute_head.next == NULL) { currenttime ++; return 1; } else { p2 = &execute_head; p1 = p2->next; p3 = &finish_head; while(p3->next != NULL) { p3 = p3->next; } while(p1 != NULL) { p1->runtime --; currenttime ++; if(p1->runtime <= 0) { printf("Time slice is %2d ; Process %s end./n",currenttime,p1->pname); p2->next = p1->next; p1->state = F; p2->next = p1->next; p1->next = p3->next; p3->next = p1; p3 = p3->next; p1 = p2->next; } else { p2 = p1; p1 = p2->next; } } return 1; } } /*将就绪队列中的进程按到达时间排序*/ void sortByArrivetime() { PCB temp; PCB *p1 , *p2 , *p3 , *p4; p2 = &ready_head; p1 = p2->next; temp.next = NULL; while(p1 != NULL) { p4 = &temp; p3 = p4->next; while((p3 != NULL)&&(p3->arrivetime <= p1->arrivetime)) { p4 = p3; p3 = p3->next; } p2->next = p1->next; p1->next = p3; p4->next = p1; p1 = p2->next; } ready_head = temp; } /*将就绪队列中的进程按优先级排序*/ void sortByPriority() { PCB temp; PCB *p1 , *p2 , *p3 , *p4; p2 = &execute_head; p1 = p2->next; temp.next = NULL; while(p1 != NULL) { p4 = &temp; p3 = p4->next; while((p3 != NULL)&&(p3->priority >= p1->priority)) { p4 = p3; p3 = p3->next; } p2->next = p1->next; p1->next = p3; p4->next = p1; p1 = p2->next; } execute_head = temp; } void main() { int select; currenttime = 0; createProcess(); printf("/n/n"); printf("选择调度算法(1表示先来先服务调度算法,2表示优先级调度算法,3表示时间片轮转算法。默认选择为先来先服务调度算法):"); scanf("%d",&select); printf("/n/n"); switch(select) { case 1: executeProcess1(); break; case 2: executeProcess2(); break; case 3: executeProcess3(); break; default: executeProcess1(); } printf("/n/n/n/n/n/n/n/n/n"); }