操作系统c语言版的处理机调度

来源:互联网 发布:固定网国内数据传送 编辑:程序博客网 时间:2024/06/16 00:44

前一段时间进行的操作系统的处理机控制程序

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <signal.h>#include <time.h>//定义进程结构信息typedef struct pcbstruct{int pid;char name[10];int status;int type;int resTime;int totalTime;int runTime;int countTime;int prio;struct pcbstruct *next;}PCB;//定义资源结构typedef struct resStruct{int pid;int free;}Resource;Resource resource;PCB * finish,*ready,*tail,*run,*wait,*head;int N;int timeSlice = 2;int hodeUpTime = 5;int changePrio = 0;void sort();void changeReadyPrio();void changeRunPrio();int randomPrio(double from,double to) {return 1+(int)((to)*rand()/(RAND_MAX+from));}void runIn() {run = ready;run->status = 1;ready = ready->next;}void readyIn() {wait->status = 2;tail->next = wait;wait = wait->next;}void printl() {printf("====================================================================\n");printf("pid name statuds type prio res totalTime count runTime\n");}void print2(PCB *q) {printf("%d|%-4s|%-4d|%-6d|%-4d|%-4d|%-8d|%-5d|%-d\n",q->pid,q->name,q->status,q->type,q->prio,q->resTime,q->totalTime,q->countTime,q->runTime);}void print() {PCB *p;if(ready!=NULL) sort();if(run != NULL){printf("Running .....\n");print2(run);}p=ready;if(p!=NULL) printf("Ready.....\n");while(p!=NULL){print2(p);p=p->next;}p=wait;if(p!=NULL) printf("Waiting .......\n");while(p!=NULL){print2(p);p=p->next;}p=finish;if(p!=NULL) printf("Finished.....\n");while(p!=NULL){print2(p);p=p->next;}printl();}void insertReady(PCB *p2) {tail->next = p2;tail=p2;p2->next=NULL;}void insertWait(PCB *p2) {head->next = p2;head = p2;p2->next = NULL;}void creat(){PCB *p;int i,time;char na[10];ready=NULL;finish=NULL;run=NULL;wait=NULL;printf("Enter name nad run time of each process :(eg.pid1[press ENTER] 100)\n");for(i=1;i<=N;i++){p=(PCB*)malloc(sizeof(PCB));p->pid=100+i;scanf("%s",na);scanf("%d",&time);strcpy(p->name,na);p->status=2;if(i%2==0){p->type=0;p->prio=randomPrio(1.0,9.0);}else {p->type=1;p->prio=randomPrio(11.0,19.0);}p->resTime = time /2;p->totalTime=time;p->countTime=0;p->runTime=0;if(ready!=NULL)insertReady(p);else{p->next=ready;ready=p;tail=p;}}printf("********************调度开始**********************\n");printl();print();run=ready;ready=ready->next;run->status=1;} void prioChangerun()  {while(run!=NULL){if(run->resTime==run->runTime){if(resource.free==1){resource.pid=run->pid;resource.free=0;} else {run->countTime=0;run->status=3;PCB *p=run;if(wait!=NULL)insertWait(p);else {p->next=wait;wait=p;head = p;}runIn();}}run->runTime=run->runTime+1;run->countTime=run->countTime+1;//sleep(1);changePrio++;if(changePrio%2==0){changeRunPrio();changeReadyPrio();}if((run->runTime-run->resTime)>=hodeUpTime)resource.free=1;if(run->runTime>=run->totalTime){if( run->pid==resource.pid)resource.free=1;run->next=finish;finish = run;run->status=0;run = NULL;if(ready!=NULL)runIn();} else if ( run->countTime==timeSlice){run->countTime=0;if(ready!=NULL){run->status=2;insertReady(run);runIn();}}if(ready!=NULL) {if(run->prio > ready->prio){run->countTime=0;if(ready!=NULL){run->status=2;insertReady(run);runIn();}}}if(resource.free==1){if(wait!=NULL){wait->status=2;PCB *tem = wait->next;if(ready!=NULL)insertReady(wait);else{wait->next = ready;ready = wait;tail = wait;}wait= tem;}if(run == NULL && ready != NULL){runIn();}}print();}}void changeRunPrio() {if(run->prio<20)run->prio+=1;}void changeReadyPrio() {PCB *p;p=ready;if(p!=NULL){do{if(p->type==0){if(p->prio>-20)p->prio-=2;}else{if(p->prio>0)p->prio-=2;}p=p->next;}while(p!=NULL);}}void sort(){PCB *p,*min;min=ready;p=ready;while(p->next!=NULL){if(min->prio>p->next->prio){min=p->next;p->next=p->next->next;min->next=ready;ready=min;} else {if(p->next!=NULL)p=p->next;}}p=ready;while(p->next!=NULL){p=p->next;}tail=p;}int main() {resource.free=1;printf("Enter process number\n");scanf("%d",&N);creat();prioChangerun();return 1;}