进程调度算法实现
//数据:进程,队列结构
//处理流程:
//1 初始化--进程队列结构(包括:就绪队列,等待队列,运行队列)等必要的数据结构 init();
//2 进入无限循环,反复调度队列
#define MAX 5
#include #include int total_time=20; int time_slice=3; typedef struct process { // 进程控制块 char pname[10]; int WaitTime; int BurstTime; int priority; // 数字越小优先级越高 struct process *next; }PROCESS; //typedef struct process PROCESS; PROCESS * in_queue(PROCESS *head,PROCESS *p); //声明 PROCESS *init() //进程初始化 { int i=0; char a; PROCESS *head_new; //队列的队头 head_new=(struct process*)malloc(sizeof(struct process)); if(!head_new) exit(1); head_new=NULL; do { struct process *s; printf(\"initialize the process:\\n\"); s=(struct process *)malloc(sizeof(struct process)); if(!s) exit(1); printf(\"please input the pname:WaitTime: BurstTime: scanf(\"%c\ scanf(\"%d\ scanf(\"%d\ scanf(\"%d\ s->next=NULL; priority:\\n\"); in_queue(head_new,s); i++; printf(\"do u want to insert process more ?? 'Y'or'N'\\n\"); printf(\"----------------------------------------'\\n\"); scanf(\"%c\ scanf(\"%c\ // if(a=='Y'||a=='y') continue; // else if(a=='N'||a=='n') break; }while((i } /////////////////////////////////////////////////////////// PROCESS *in_queue(PROCESS *head,PROCESS *p) //入队函数 { if(head==NULL) { head=p; p->next=NULL; } else { p->next=head; head=p; } // printf(\"the process insert into the mothball queue :\\n\"); return head; } ///////////////////////////////////////////////////////////// /* void new_queue() //后备队列 先来先服务方式进入就绪 { return *head_new; } */ PROCESS *FCFS_process() { PROCESS *p,*q,*a; //a用来记录选中结点的前一个结点 q=p=init(); //这里是不是有个问题?? while(p->next!=NULL) { a=p; if(p->WaitTime>=q->WaitTime) { q=p; p=p->next; } } q->WaitTime--; if(q->WaitTime==0) { a->next=p->next; free(p); //如果等待时间为0则把该进程从后备队列中移除 } return q; //选择等待时间最久的) } //////////////////////就绪队列,入口函数为就绪队列的头指针///////////// int count=0; PROCESS *ready_queue(PROCESS *head) //就绪队列 优先级进入运行 4道 { PROCESS *p; while(count<4) { p=FCFS_process(); p->next=head->next; head=p; count++; printf(\"the process has inserted into the ready queue :\\n\"); } return head; } //insert_ready() // PROCESS *high_priority(PROCESS *P) //选择优先级最高的进程 { PROCESS *q,*p; //问题,入口形参中 q=p; while(p->next!=NULL) { if(q->priority>p->priority) q=p; p=p->next; } return q; } PROCESS *pick_ready(PROCESS *a) //从就绪队列中选择进程运行 { PROCESS *p=ready_queue(a); PROCESS *b=high_priority(p); return b; } void run(PROCESS *a) //运行一个时间片 { while((time_slice>0)&&(a->BurstTime>0)) //指针用-> 变量用. { printf(\"the process %c is runing\ printf(\"the process BurstTime time is %d \ printf(\"the process BurstTime time is %d \ printf(\"the process priority is %d \ a->BurstTime--; time_slice--; } a->priority--; total_time--; /* if(a->runtime>0) return a; else return NULL;*/ } void main() { PROCESS *p; PROCESS *head=init(); while(total_time!=0) { ready_queue(head); p=pick_ready(head); if(p->BurstTime==0) { p=p->next; free(p); count--; } run(p); time_slice=3; } } 因篇幅问题不能全部显示,请点此查看更多更全内容