操作系统调度算法练习

您所在的位置:网站首页 作业调度大题 操作系统调度算法练习

操作系统调度算法练习

#操作系统调度算法练习| 来源: 网络整理| 查看: 265

#include #include #include #define MAX_PROCESS 20 //最大进程数量

typedef struct PCB{     char name[10];     int prio;     int round;     int cputime;     int needtime;     int count;     char state;     struct PCB *next; } PCB;

typedef struct queue{     PCB *front;     PCB *rear;     int size; } Queue;

Queue *initQueue(){     Queue *q = (Queue *)malloc(sizeof(Queue));     q->front = q->rear = NULL;     q->size = 0;     return q; }

void enque(Queue *q, PCB *p){     if(q->size == 0){         q->front = q->rear = p;         p->next = NULL;     }     else{         q->rear->next = p;         q->rear = p;         p->next = NULL;     }     q->size++; }

PCB *deque(Queue *q){     if(q->size == 0){         return NULL;     }     else{         PCB *p = q->front;         q->front = q->front->next;         q->size--;         return p;     } }

typedef struct rrqueue{     PCB *data[MAX_PROCESS];     int front, rear, size; } RRQueue;

RRQueue *initRRQueue(){     RRQueue *q = (RRQueue *)malloc(sizeof(RRQueue));     q->front = q->rear = 0;     q->size = 0;     return q; }

void rrEnque(RRQueue *q, PCB *p){     if(q->size >= MAX_PROCESS){         printf("RR队列已满\n");         exit(0);     }     q->data[q->rear] = p;     q->rear = (q->rear + 1) % MAX_PROCESS;     q->size++; }

PCB *rrDeque(RRQueue *q){     if(q->size == 0){         return NULL;     }     else{         PCB *p = q->data[q->front];         q->front = (q->front + 1) % MAX_PROCESS;         q->size--;         return p;     } }

void FCFS(Queue *q){     printf("========== FCFS调度模拟 ==========\n");     int clock = 0;     int i, total_time = 0, total_count = 0, total_wait = 0;     PCB *p = NULL;     printf("进程号 进入时间 运行时间 开始时间 结束时间 周转时间 带权周转时间 等待时间\n");     while(q->size > 0){         p = deque(q);         total_wait += clock - p->round;         if(p->round > clock){             clock = p->round;         }         p->state = 'R';//ready状态         p->count = clock + p->needtime;//计算结束时间         total_time += p->needtime;         total_count += p->count - p->round;         int m=clock - p->round;         if(m     printf("========== SJF调度模拟 ==========\n");     int clock = 0, i, total_time = 0, total_count = 0, total_wait = 0;     PCB *p = NULL, *tmp = NULL;     Queue *tmpq = initQueue();     printf("进程号 进入时间 运行时间 开始时间 结束时间 周转时间 带权周转时间 等待时间\n");     while(q->size > 0 || tmpq->size > 0){         while(q->size > 0 && q->front->round                     m1=0;                 }                 printf("%s\t %d\t %d\t %d\t  %d\t   %d\t    %.2f\t %d\n", tmp->name, tmp->round, tmp->needtime, clock, tmp->count, tmp->count - tmp->round, (float)(tmp->count - tmp->round) / tmp->needtime, m1);                 clock = tmp->count;                 free(tmp);                 continue;             }             //寻找最短的进程         tmp = tmpq->front;         PCB *p = tmpq->front->next;         while(p != NULL){             if(p->needtime < tmp->needtime){                 tmp = p;             }             p = p->next;         }         tmp->state = 'R';         tmp->count = clock + tmp->needtime;         total_time += tmp->needtime;         total_count += tmp->count - tmp->round;         total_wait += clock - tmp->round;         int m2=clock - tmp->round;         if(m2             m3=0;         }         printf("%s\t %d\t %d\t %d\t  %d\t   %d\t    %.2f\t %d\n\n", p->name, p->round, p->needtime, clock, p->count, p->count - p->round, (float)(p->count - p->round) / p->needtime, m3);         clock = p->count;         free(p);         continue;     }

    clock++;//队列为空,时间片加1     } } void Priority(Queue *q){     printf("========== 优先级调度模拟 ==========\n");     int clock = 0, i, total_time = 0, total_count = 0, total_wait = 0;     PCB *p = NULL, *tmp = NULL;     Queue *tmpq = initQueue();     printf("进程号 进入时间 优先级 运行时间 开始时间 结束时间 周转时间 带权周转时间 等待时间\n");     while(q->size > 0 || tmpq->size > 0){         while(q->size > 0 && q->front->round                 m4=0;             }             printf("%s\t %d\t %d\t %d\t %d\t %d\t %d\t %.2f\t %d\n", tmp->name, tmp->round, tmp->prio, tmp->needtime, clock, tmp->count, tmp->count - tmp->round, (float)(tmp->count - tmp->round) / tmp->needtime,m4);             clock = tmp->count;             free(tmp);             continue;         }          //寻找最高优先级的进程         tmp = tmpq->front;         PCB *p = tmpq->front->next;         while(p != NULL){             if(p->prio < tmp->prio){                 tmp = p;             }             p = p->next;         }         tmp->state = 'R';         tmp->count = clock + tmp->needtime;         total_time += tmp->needtime;         total_count += tmp->count - tmp->round;         total_wait += clock - tmp->round;         int m5=clock - tmp->round;         if(m5             m6=0;         }         printf("%s\t %d\t %d\t %d\t %d\t %d\t %d\t %.2f\t %d\n", p->name, p->round, p->prio, p->needtime, clock, p->count, p->count - p->round, (float)(p->count - p->round) / p->needtime, m6);         clock = p->count;         free(p);         continue;     }

    clock++;//队列为空,时间片加1     } } //RR void RR(RRQueue *q, int slice){         printf("========== RR调度模拟 ==========\n");     int clock = 0, i, total_time = 0, total_count = 0, total_wait = 0;     int tmpTime = 0;     PCB *p = NULL;     printf("进程号 进入时间 运行时间 开始时间 结束时间 周转时间 带权周转时间 等待时间\n");     while(q->size > 0){         p = rrDeque(q);         p->state = 'R';         if(p->needtime > slice){//剩余时间大于时间片,分多次处理             total_wait += clock - p->round;             total_time += slice;             printf("%s\t %d\t %d\t %d\t  -\t   -\n", p->name, p->round, slice, clock);             tmpTime = clock + slice;             p->needtime -= slice;             clock = tmpTime;                 rrEnque(q, p);//放回队列尾部,下次继续执行         }         else{//剩余时间小于等于时间片,完成处理             total_wait += clock - p->round;             total_time += p->needtime;             p->count = clock + p->needtime;             total_count += p->count - p->round;             int m7=clock - p->round;             if(m7     int i, algo, time_slice;     char name[10];     int prio, rund, needt;     PCB *p;     Queue *q = initQueue();     RRQueue *rq = initRRQueue();         printf("请输入要模拟的算法(1:FCFS(先来先服务);2:SJF(短作业优先);3:优先级调度;4:RR(时间片轮转))");     scanf("%d", &algo);

    if(algo == 4){         printf("请输入时间片大小:");         scanf("%d", &time_slice);     }

    printf("请输入进程数:");     scanf("%d", &i);

    while(i--){         printf("请依次输入进程名、进入时间、运行时间、优先级:\n");            scanf("%s%d%d%d", name, &rund, &needt, &prio);            p = (PCB *)malloc(sizeof(PCB));            strcpy(p->name, name);            p->prio = prio;           p->round = rund;           p->cputime = 0;           p->needtime = needt;           p->count = 0;            p->state = 'W';//wait状态            p->next = NULL;         if(algo == 4){                 rrEnque(rq, p);         }         else{                enque(q, p);         }     }

    switch(algo){         case 1://FCFS算法             FCFS(q);            break;         case 2://SJF算法                   SJF(q);             break;         case 3://优先级调度算法             Priority(q);             break;         case 4://RR算法             RR(rq, time_slice);             break;         default:             printf("不支持的算法\n");             break;     }     return 0; }  



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3