#include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> typedef struct node{ char name[10];//进程名字 int first; //优先数 int round; // int arrive_time; int need_run_time; int use_time;//已用cpu时间 char state; //状态 int count; struct node *next; }PCB; int algo; PCB *finish,*ready,*tail,*run;//队列指针 int progress;//进程数 void firstin(){ run=ready; run->state='R'; ready=ready->next; } //标题输出 void printtile(int a){ switch(a){ case '1'://优先算法 printf("名字 进程占用时间 进程完成需要时间 状态\n"); break; case '2'://时间片算法 printf("名字 进程到达时间 时间片 状态\n"); break; default: break; } } //PCB输出 void printPCB(int a,PCB *p){ switch(a){ case 1: printf("%s %d %d %c\n",p->name,p->use_time,p->need_run_time,p->state); break; case 2: printf("%s %d %d %c\n",p->name,p->arrive_time,p->round,p->state); break; default: break; } } void print(char tile){ PCB *p; printtile(tile); if(run!=NULL) printPCB(tile,run); p=ready; while(p!=NULL){ printPCB(tile,p); p=p->next; } p=finish; while(p!=NULL){ printPCB(tile,p); p=p->next; } getch(); } //先来先服务 void firstserve(PCB *p){ PCB *p1,*s,*r; int b; s=p; p1=ready; r=p1; b=1; while((p1!=NULL)&&b){ if(p1->arrive_time<s->arrive_time){ r=p1; p1=p1->next; }else{ b=0; } if(r!=p1){ r->next=s; s->next=p1; }else{ s->next=p1; ready=s; } } } //轮转算法 void robin(PCB *p){ tail->next=p; tail=p; p->next=NULL; } void create_robin(char tile){ PCB *p; int i,time; char name1[10]; ready=NULL; finish=NULL; run=NULL; printf("输入进程名字和运行需要时间\n"); for(i=0;i<progress;i++){ p=(PCB*)malloc(sizeof(PCB)); scanf("%s",&name1); scanf("%d",&time); strcpy(p->name,name1); p->need_run_time=time; p->state='W'; p->round=2; if(ready!=NULL){ robin(p); }else{ p->next=ready; ready=p; tail=p; } } } //时间轮转法输出 void print_robin(void){ printf("时间片轮转模拟输出结果:\n"); printf("******************************************************"); printtile(algo); run=ready; ready=ready->next; run->state='R'; } //先来先服务创建PCB void print_first(char alg){ PCB *p; int i; ready=NULL; run=NULL; finish=NULL; printf("输入进程名字和到达时间和运行需要时间\n"); for(i=0;i<progress;i++){ p=(PCB*)malloc(sizeof(PCB)); scanf("%s",&p->name); scanf("%d",&p->arrive_time); scanf("%d",&p->need_run_time); p->state='W'; if(ready!=NULL) firstserve(p); else{ p->next=ready; ready=p; } } } //先来先服务输出 void print_firstserve(void){ printf("***************************************\n"); printtile(algo); run=ready; ready=ready->next; run->state='R'; } //时间片算法 void robin_run(char alg){ while(run!=NULL){ run->need_run_time-=1; run->count+=1; if(run->need_run_time==0){ run->next=finish; run->state='F'; run=NULL; if(ready!=NULL){ firstin(); }else{ if(run->count==run->round){ run->count=0; if(ready!=NULL){ run->state='W'; robin(run); firstin(); } } print(alg); } } } } //先来先服务算法 void FIFO(char alg){ int time; while(run!=NULL){ time=run->arrive_time>time?run->arrive_time:time; time+=run->use_time; run->next=finish; finish=run; run->state='F'; run=NULL; if(ready!=NULL){ run=ready; run->state='R'; ready=ready->next; } print(alg); } } int main(){ char mainmenu; do{ system("cls"); printf("\n输入你的选择1时间片算法2先来先服务算法\n"); scanf("%d",&algo); switch(algo){ case 1: printf("请输入进程数\n"); scanf("%d",&progress); create_robin(algo); robin_run(algo); break; case 2: printf("输入进程数\n"); scanf("%d",&progress); print_first(algo); FIFO(algo); break; default: printf("你的输入有错误!!\n"); break; } mainmenu=getchar(); }while(mainmenu=='y'||mainmenu=='Y'); return 0; }
操作系统实习代码
猜你喜欢
转载自blog.csdn.net/qq_40955914/article/details/80260815
今日推荐
周排行