您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 薪酬管理 > 操作系统处理机调度算法的实现c语言源代码
第1页共5页处理机调度算法的实现处理机调度算法的实现1.设定系统中有五个进程,每一个进程用一个进程控制块表示。2.输入每个进程的“优先数”和“要求运行时间”,3.为了调度方便,将五个进程按给定的优先数从大到小连成就绪队列。用一单元指出队列首进程,用指针指出队列的连接情况。4.处理机调度总是选队首进程运行。采用动态优先数算法,进程每运行一次优先数就减“1”,同时将运行时间减“1”。5.若要求运行时间为零,则将其状态置为“结束”,且退出队列。6.运行所设计程序,显示或打印逐次被选中进程的进程名以及进程控制块的动态变化过程。#includestdio.h#includestdlib.hstructPCB{charname[10];intpriority,time;structPCB*next;}*k;structLinkQueue{PCB*front;PCB*rear;};//队列初始化LinkQueueinit(){LinkQueueQ;PCB*p;p=(PCB*)malloc(sizeof(PCB));if(p){Q.front=Q.rear=p;Q.front-next=NULL;returnQ;}else{printf(队列初始化失败,程序运行终止!\n);exit(0);}}//插入新进程,使优先数从大到小排列LinkQueuesort(LinkQueueQ,PCB*p){PCB*temp1;第2页共5页PCB*temp2;if(Q.rear==Q.front){Q.front-next=p;Q.rear=p;}else{temp1=Q.front;temp2=temp1-next;while(temp2-priority=p-priority&&temp2-next!=NULL){temp1=temp2;temp2=temp1-next;}if(temp2-next==NULL&&temp2-priority=p-priority){temp2-next=p;Q.rear=p;}else{p-next=temp1-next;temp1-next=p;}}returnQ;}LinkQueueinput(LinkQueueQ)/*建立进程控制块函数*/{inti;for(i=1;i=5;i++){printf(\n进程号No.%d:\n,i);k=(PCB*)malloc(sizeof(PCB));printf(\n输入进程名:);scanf(%s,k-name);printf(\n输入进程优先数:);scanf(%d,&k-priority);printf(\n输入进程运行时间:);scanf(%d,&k-time);printf(\n);k-next=NULL;Q=sort(Q,k);/*调用sort函数*/}returnQ;}LinkQueuerunning(LinkQueueQ)/*建立进程就绪函数(进程运行时间到,置就绪状态*/{if(k-time==0){第3页共5页printf(运行后进程[%s]已完成状态为\结束\.\n,k-name);free(k);}else{(k-priority)--;(k-time)--;printf(运行后优先数:%d需要运行时间:%d\n,k-priority,k-time);Q=sort(Q,k);/*调用sort函数*/}returnQ;}voidcheck(LinkQueueQ)/*建立进程查看函数*/{PCB*pr;pr=(PCB*)malloc(sizeof(PCB));pr=Q.front-next;printf(\n****输入的五个过程为:\n);while(pr!=NULL){printf(\n进程名:%s状态:就绪优先数:%d需要运行时间:%d\n,pr-name,pr-priority,pr-time);pr=pr-next;}}voidmain(){inth=0;LinkQueueP;P=init();P=input(P);check(P);while((P.front-next)!=NULL){h++;k=P.front-next;P.front-next=k-next;k-next=NULL;printf(\n第%d次运行,被选中进程是:%s,h,k-name);P=running(P);}printf(\n\n进程已经完成.\n);}第4页共5页第5页共5页
本文标题:操作系统处理机调度算法的实现c语言源代码
链接地址:https://www.777doc.com/doc-5153636 .html