这是我关于操作系统调度算法的一些分享。
算法一:先来先服务算法
实现代码:
#include
float t,d; /*定义两个全局变量*/
struct /*定义一个结构体数组,包括进程的信息*/
{
int id;
float ArriveTime;
float RequestTime;
float StartTime;
float EndTime;
float RunTime;
float DQRunTime;
int Status;
}arrayTask[4]; /*定义初始化的结构体数组*/
GetTask()/*给结构体数组赋值,输入到达,服务时间*/
{
int i;
float a;
for(i=0;i<4;i++)
{arrayTask[i].id=i+1;
printf("input thenumber");
printf("input thethe ArriveTime of arrayTask[%d]:",i); /*用户输入进程的时间,初始为零 */
scanf("%f",&a);
arrayTask[i].ArriveTime=a;
printf("input theRequestTime of arrayTask[%d]:",i);
scanf("%f",&a);
arrayTask[i].RequestTime=a;
arrayTask[i].StartTime=0;
arrayTask[i].EndTime=0;
arrayTask[i].RunTime=0;
arrayTask[i].Status=0; /*开始默认的标志位零*/
}
}
int fcfs() /*定义 FCFS 中寻找未执行的进程的最先到达时间*/
{
int i,j,w=0; /*在结构体数组中找到一个未执行的进程*/
for(i=0;i<4;i++)
{
if(arrayTask[i].Status==0)
{
t=arrayTask[i].ArriveTime;
w=1;
}
if(w==1)
break;
}
for(i=0;i<4;i++) /*查找数组中到达时间最小未执行的进程*/
{
if(arrayTask[i].ArriveTime t=arrayTask[i].ArriveTime; } /*返回最小到达时间的数组的下标*/ for(i=0;i<4;i++) { if(arrayTask[i].ArriveTime==t) return i; } } int sjf() /*定义 FCFS 中寻找未执行的进程的最先到达时间*/ { int i,x=0,a=0,b=0; /*判断是不是第一个执行的进程*/ float g; for(i=0;i<4;i++) { if(arrayTask[i].Status==1) { g=arrayTask[i].EndTime; x=1; } } if(x==0) /*第一个执行的进程按 FCFS*/ { t=arrayTask[0].ArriveTime; for(i=0;i<4;i++) { if(arrayTask[i].ArriveTime {t=arrayTask[i].ArriveTime; a=i; } } return a; } else { for(i=0;i<4;i++) {if(arrayTask[i].EndTime>g) g=arrayTask[i].EndTime; } for(i=0;i<4;i++) {if(arrayTask[i].Status==0&&arrayTask[i].ArriveTime<=g) {t=arrayTask[i].RequestTime; a=i; b=1;}/*判断有没有进程在前个进程完成前到达*/ } if(b!=0) /*有进程到达则按 SJF*/ {for(i=0;i<4;i++) { if(arrayTask[i].Status==0&&arrayTask[i].ArriveTime<=g&&arrayTask [i].RequestTime {t=arrayTask[i].RequestTime; a=i;} } return a; } else{ /*否则按 FCFS*/ for(i=0;i<4;i++) {if(arrayTask[i].Status==0) t=arrayTask[i].ArriveTime; } for(i=0;i<4;i++) { if(arrayTask[i].Status==0&&arrayTask[i].ArriveTime {t=arrayTask[i].ArriveTime; a=i; } } return a;} } } new(int s) /*定义执行进程后相关数据的修改*/ { int i,g=0; for(i=0;i<4;i++) { if(arrayTask[i].Status==0) continue; else { g=1; break; } } if(g==0) /*当处理的是第一个未执行的进程时执行*/ { arrayTask[s].StartTime=arrayTask[s].ArriveTime; arrayTask[s].EndTime=arrayTask[s].RequestTime+arrayTask[s].ArriveTime; arrayTask[s].RunTime=arrayTask[s].RequestTime; arrayTask[s].Status=1; g=2; } if(g==1) /*当处理的不是第一个未执行的进程时执行*/ { arrayTask[s].Status=1; for(i=0;i<4;i++) { if(arrayTask[i].Status==1) d=arrayTask[i].EndTime; } for(i=0;i<4;i++) /*查找最后执行的进程的完成时间*/ { if(arrayTask[i].EndTime>d&&arrayTask[i].Status==1) d=arrayTask[i].EndTime; } if(arrayTask[s].ArriveTime arrayTask[s].StartTime=d; else arrayTask[s].StartTime=arrayTask[s].ArriveTime; arrayTask[s].EndTime=arrayTask[s].StartTime+arrayTask[s].RequestTime; arrayTask[s].RunTime=arrayTask[s].EndTime-arrayTask[s].ArriveTime; } arrayTask[s].DQRunTime=arrayTask[s].RunTime/arrayTask[s].RequestTime; } Printresult(int j) /*定义打印函数*/ { printf("%d\t",arrayTask[j].id); printf("%5.2f\t",arrayTask[j].ArriveTime); printf("%5.2f\t",arrayTask[j].RequestTime); printf("%5.2f\t",arrayTask[j].StartTime); printf("%5.2f\t",arrayTask[j].EndTime); printf("%5.2f\t",arrayTask[j].RunTime); printf("%5.2f\n",arrayTask[j].DQRunTime); } main() { int i,b,k,a,c=0; int d[4]; system("clear"); printf("\t 1. EXIT \n"); printf("\t 2. FCFS \n"); printf("\t 3. SFJ \n"); for(i=0;;i++) {if(c) break; printf("please input the number a:\n"); scanf("%d",&a); switch(a) { case 1: c=1; break; case 2:printf("please input the different-ArriveTime ofarrayTasks\n"); GetTask(); printf("*****************************the result of fcfs\n"); printf("Number\tArrive\tServer\tStart\tFinish\tTurnover\tTakepower turnover time\n"); for(b=0;b<4;b++) /*调用两个函数改变结构体数的值*/ { k=fcfs(); d[b]=k; new(k); } for(b=0;b<4;b++) Printresult(d[b]);/*调用打印函数打出结果*/ continue; case 3: printf("please input the different-RequestTime ofarrayTasks\n"); GetTask(); printf("******************************the result of sjf\n"); printf("Number\tArrive\tRequest\tStart\tEnd\tRun\tDQRuntime\n"); for(b=0;b<4;b++) { k=sjf(); d[b]=k; new(k); } for(b=0;b<4;b++) Printresult(d[b]); continue; default:printf("the number Error.please input anothernumber!\n"); } } } 运行结果: FCFS调度算法: SFJ调度算法: 分析: 当在进程调度中采用FCFS算法时,每次调度是从就绪的进程队列中选择一个最先进入该队列的进程,为之分配处理机,使之投入运行。该进程一直运行到完成或发某事件而阻塞后,进程调度程序才将处理机分配给其它程序。 算法二:时间片轮转算法 #include #include #define NULL 0 typedef struct quen /*定义结构*/ { char pname[8]; int time1; int time2; char state; struct quen *next; }QUEN; main()/*主程序*/ { QUEN *q,*p,*head,*m; char str[8],f; int t,d,n; system("clear"); printf("Enter the maxnumber of nodes(n):\n");/*输入进程数*/ scanf("%d",&n); d=n; if(d>0) { printf("enter thepname:"); scanf("%s",str); printf("enter the need time:"); scanf("%d",&t); head=p=(QUEN *)malloc(sizeof(QUEN)); strcpy(p->pname,str); p->time1=t; p->time2=0; p->state='R'; p->next=NULL; head=p; getchar(); --d;} while(d>0) {/*构建队列表*/ printf("enter the pname:"); scanf("%s",str); printf("enter need time:"); scanf("%d",&t); q=(QUEN *)malloc(sizeof(QUEN)); strcpy(q->pname,str); q->time1=t; q->time2=0; q->state='R'; q->next=NULL; p->next=q; p=q; --d; p->next=head; q=head;} printf("process name need time runned static\n"); do{ printf(" %s%d %d%c\n",q->pname,q->time1,q->time2,q->state); q=q->next; }while(q!=head); printf("\n"); do{ if(head->time2 {head->time2++; if(head->time2==head->time1) { head->state='E'; q=head; printf("The running process is %s\n",q->pname); printf("process name left time runned static\n"); do{ /*输入队列表*/ printf(" %s %d %d%c\n",q->pname,q->time1,q->time2,q->state); q=q->next;} while(q!=head); printf("\n"); head=head->next; q=head; p->next=head; } else{ printf("The running process is %s\n",q->pname); printf("process name left time runned static\n"); do { printf("%s%d%d%c\n",q->pname,q->time1,q->time2,q->state); q=q->next;}while(q!=head); printf("\n"); head=head->next; q=head; p=p->next;} printf("Is it needing new process?(y or n)\n");/*是否加入新的进程*/ getchar(); scanf("%c",&f); if(f=='Y'||f=='y'){ getchar(); printf("Enter the new pname:"); scanf("%s",str); printf("Enter the new neededtime:"); scanf("%d",&t); m=(QUEN *)malloc(sizeof(QUEN)); strcpy(m->pname,str); m->time1=t; m->time2=0; m->state='R'; m->next=NULL; if(q->next->state=='E') {p=m; head=m; p->next=head; q=head;} else{p->next=m; m->next=head; p=m;}} }}while(q->next->state!='E'); printf("The processes are finished\n"); } 运行结果: 分析: 时间片轮转算法中,系统会根据FCFS算法,将其排入一个就绪进程队列。并且可以设置一个时间间隔产生一次中断,激活调度程序,完成调度,将cpu分配给队首进程。在时间耗尽或者运行完毕时,分配给新的队首进程。这样就可以达到每隔一段时间所有进程都能完成一次调度。 算法三:优先级调度算法 #include typedef struct pcb/*定义结构*/ {char name[5]; struct pcb *next; int needtime; int priority; char state[5]; }NODE; NODE *create_process(int n)/*创建队列*/ {NODE *head,*s,*t; int time,i=0,j; char pname[5]; head=(NODE *)malloc(sizeof(NODE)); printf("please input processname:"); scanf("%s",&pname); strcpy(head->name,pname); printf("please input need time:"); scanf("%d",&time); head->needtime=time; printf("please input priority:"); scanf("%d",&j); head->priority=j; strcpy(head->state,"ready"); head->next=NULL; t=head; for(i=1;i { s=(NODE *)malloc(sizeof(NODE)); printf("please input processname:"); getchar(); gets(pname); strcpy(s->name,pname); printf("please input need time:"); scanf("%d",&time); s->needtime=time; printf("please input priority:"); scanf("%d",&j); s->priority=j; strcpy(s->state,"ready"); s->next=NULL; t->next=s; t=s; } return head; } pri_process(NODE *p)/*输出进程队列*/ {int i; NODE *q; q=p->next; printf("\n name\tneedtime\tpriority \tstate\n"); while(q!=NULL) {printf("%5s\t %2d \t %2d \t %5s\n",q->name,q->needtime,q->priority,q->state); q=q->next; } } NODE *order(NODE *head_sort)/*对进程的优先级进行排序*/ {NODE *p,*s,*q,*head,*r,*t; head=(NODE *)malloc(sizeof(NODE)); p=(NODE *)malloc(sizeof(NODE)); q=(NODE *)malloc(sizeof(NODE)); int m,pr; char name[5]; head = head_sort; p=head->next; r=p; t=p; q=p->next; while(r!=NULL) { while(q!=NULL) {if(p->priority {m=p->priority; p->priority=q->priority; q->priority=m; strcmp(name,p->name); strcmp(p->name,q->name); strcmp(q->name,name); pr=p->needtime; p->needtime=q->needtime; q->needtime=pr; } p=q; q=q->next; } r=r->next; p=t; q=p->next; } return(head_sort); } main()/*主程序*/ { NODE*p=NULL,*head=NULL,*m=NULL,*z=NULL,*n=NULL; head=(NODE *)malloc(sizeof(NODE)); m=(NODE *)malloc(sizeof(NODE)); int j,time,x=0; char c,pname[5]; system("clear"); printf("please input processnumber!"); scanf("%d",&x); p=create_process(x); head->next=p; pri_process(head); getchar(); while(x>0) { order(head); m=head->next; strcpy(m->state,"run"); if(m->priority>=2) m->priority--; m->needtime--; if(head->next!=NULL) pri_process(head); if(m->needtime==0) { head->next=m->next; printf("%s hasfinished\n",m->name); free(m); x--; } getchar(); } printf("over!"); getchar(); } 运行结果: 分析: 在优先级算法中,则是基于作业的紧迫程度,由外部赋予作业相应的优先级,调度算法是根据该优先级进行调度的。这样就可一保证紧迫性作业优先运行。我们这里输入的优先级数字越大优先级越高。