进程调度算法——优先级调度
算法流程

代码实现
/* 非抢占式优先级进程调度 */ #include<stdio.h> #include<stdlib.h> typedef struct PCB { int pid; int need_time;//进程运行所需时间 int priority;//进程优先级 char state;//进程状态 struct PCB *next; }*Queue,Q; Queue createProcess(int n) { Queue head,s,t; int time,i=0,j; head=(Queue)malloc(sizeof(Q)); printf("请输入进程所需时间:"); scanf("%d",&time); head->need_time=time; head->pid=1; printf("请输入进程优先级:"); scanf("%d",&j); head->priority=j; head->state='R'; t=head; for(i=1;i<n;i++) { s=(Queue)malloc(sizeof(Q)); printf("请输入进程所需时间:"); scanf("%d",&time); s->need_time=time; s->pid=i+1; printf("请输入进程优先级:"); scanf("%d",&j); s->state='R'; s->priority=j; s->next=NULL; t->next=s; t=s; } return head; } void printProcess(Queue p) { int i; Queue q; q=p;//->next printf("进程标识符\t运行时间\t优先级\t\t状态\n"); while(q!=NULL) { printf("%d\t\t %d \t\t%d \t\t%c\n",q->pid,q->need_time,q->priority,q->state); q=q->next; } } Queue sort(Queue head)//根据优先级进行冒泡排序 { Queue p,h,s,q,r,t; int m,pr; int pid; char st; h=head; p=head;//->next r=p; t=p; q=p->next; while(r!=NULL) { while(q!=NULL) { if(p->priority<q->priority) { m=p->priority; p->priority=q->priority; q->priority=m; pid=p->pid; p->pid=q->pid; q->pid=m; pr=p->need_time; p->need_time=q->need_time; q->need_time=pr; st=p->state; p->state=q->state; q->state=st; } p=q; q=q->next; } r=r->next; p=t; q=p->next; } return head; } int main() { Queue p=NULL,head=NULL,m=NULL,z=NULL; int j,time,x=0; char c,pid; printf("请输入进程数:"); scanf("%d",&x); p=createProcess(x); head=p;//->next z=head; printf("*********************创建的进程********************\n"); printProcess(head); printf("*********************进程调度过程******************\n"); while(x>0) { sort(z); m=z;//->next m->state='E'; if(m!=NULL)//head->next { printProcess(head); } z=m->next;//->next //free(m); printf("进程%d已完成!\n",m->pid); x--; } printf("所有进程运行结束!\n"); return 0; }
效果


浙公网安备 33010602011771号