First, Experimental Purpose
A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.
Second, experimental content and requirements
Design a process scheduling simulator with n processes executing concurrently.
Process scheduling algorithm: Using the highest priority scheduling algorithm (that is, the processor is assigned to the highest priority process) and first-come first service (if the same priority) algorithm.
(1). Each process has a Process Control block (PCB) representation. The Process Control block contains the following information: Process name, priority, arrival time, required run time, elapsed CPU time, process state, and so on.
(2). The priority of the process and the desired run time can be specified in advance, and the running time of the process is calculated in time slices.
(3). The state of each process can be either ready, running R (Running), or completing one of the three states of F (finished).
(4). The ready process can only run one time slice after the CPU is acquired. Represented by the elapsed CPU time plus.
(5). If the elapsed CPU time of a process has reached the desired run time after a time slice is run, the process is undone, and if the elapsed CPU time of the process after running a time slice has not reached the required run time, that is, the process needs to continue running, the priority number of the process should be reduced by 1 (that is, lowering the first , and then insert it into the ready queue for scheduling.
(6). Each time the scheduler is run, the PCB for each process in the running process and ready queue is printed for inspection.
(7). Repeat the process until the process is complete.
main program sections and their explanations:
#include <stdio.h>#include<stdlib.h>#defineM 24structpcb{CharName//Job name intR//Priority Level intCt//CPU (Run) time required intHt//CPU Time Consumed CharS//process Status}p[m];//output FunctionvoidOutput (inta) { inti; printf ("\ n The CPU time consumed by the job name priority required CPU time \ n"); for(i=0; i<a;i++) {printf ("%c\t", P[i].name); printf ("%d\t\t", P[I].R); printf ("%d\t\t", P[I].CT); printf ("%d\t\t", p[i].ht); printf ("%c\t\n", P[I].S); }}//Input FunctionintInput () {inti,n,b=0; intinit,k; intj=1; structPCB F; Do{printf ("Please enter n jobs (n>=5):"); scanf ("%d",&N); if(n>=5) Break; } while(j=1); for(i=0; i<n;i++) {printf ("Please enter name:"); scanf ("%s",&p[i].name); printf ("Please enter CT:"); scanf ("%d",&p[i].ct); printf ("Please enter R:"); scanf ("%d",&P[I].R); P[i].s='R'; P[i].ht=0; } for(i=0; i<n;i++)//sort by priority from highest to lowest{init=P[B].R; F=P[b]; for(k=b;k<n;k++) { if(p[k].r>init) {f=P[k]; P[K]=P[b]; P[B]=F; Init=P[B].R; }} B++; }printf ("---------------jobs sort output by priority from high to low------------"); Output (n);//sort by priority from highest to lowest returnN;}CharSelect (structPCB A[m],intTintN) { inti,j,init,k=0; Chars; structPCB F; Init=a[0].R; F=a[0]; for(i=0; i<n;i++) { if(a[k].r>init) {f=A[k]; A[K]=a[0]; a[0]=F; Init=a[0].R; } } returna[0].name;}voidStart (intt) { inti,j; intk=0, m=0, n=0, q=0, x=0; Charl; intnow=4, y=0, z=0; structPCB a[m],b[m],c[m];//a is ready queue, B is completed job queue, C is waiting queue J=Input (); for(i=0; i<j;i++) A[i]=P[i]; while(now>0) { if(a[0].name==a[1].name) { for(i=0; i<j;i++) A[i]=C[i]; X=0; } l=Select (A,NOW,J); for(i=0; i<j;i++) { if(a[i].name==l) { for(y=i;y<j;y++) A[y]=a[y+1]; Break; } } for(i=0; i<j;i++) { if(p[i].name==l) {p[i].s='R'; printf ("---------------in--------------job scheduling"); Output (j); P[i].ht=p[i].ht+4; if(p[i].ht>p[i].ct) p[i].ht=P[i].ct; if(p[i].ht>=p[i].ct) {P[I].S='F'; printf ("--------------A job is completed---------------"); Output (j); B[n]=P[i]; N++; } Else{P[I].R=p[i].r-1; P[i].s='R'; C[X]=P[i]; X++; } Break; }} now=now+4; if(b[j-1].s=='F') Break; } }intMain () {intI=1; CharA; inttp=4; Do{Start (TP); printf ("want to continue? (Y or N):"); scanf ("%c",&a); if(a=='N') Break; } while(i=1); printf ("The end!\n"); return 0;}
Summary: Just beginning to write code when I do not know where to start, but clear the idea behind there is no problem.
Experimental three-process scheduling simulation program Experiment