#include <stdio.h>#include<string.h>structjob{Charname[Ten]; intStartTime//job arrives system time intNeedtime;//Job Run Time intRuntime//Job Turnaround Time intEndtime//Job End Time DoubleDqzztime;//turnaround time with weight DoubleXiangyingbi;};voidFcfsstructJob jobs[ -],intn);voidSjfstructJob jobs[ -],intn);voidHRRF (structJob jobs[ -],intn);voidResultstructJob jobs[ -],intN); voidMain () {structJob jobs[ -]; intN,i;//N Jobsprintf ("Enter the number of jobs:", N); scanf ("%d",&N); printf ("Enter the job name, arrival time, and run time for each job: \ n"); for(i=0; i<n;i++) {scanf ("%s", Jobs[i].name); scanf ("%d",&jobs[i].starttime); scanf ("%d",&jobs[i].needtime); } printf ("\ n"); printf ("job name \ t reaches system time \ t run time \ n"); for(i=0; i<n;i++) {printf ("%s\t%d\t%d\n", Jobs[i].name,jobs[i].starttime,jobs[i].needtime); } FCFS (Jobs,n); printf ("First come first service algorithm run result: \ n"); Result (Jobs,n); SJF (Jobs,n); printf ("minimum job first algorithm run result: \ n");//HRRF (jobs,n);//printf ("Maximum response ratio priority algorithm run result: \ n");result (jobs,n);}voidFcfsstructJob jobs[ -],intN) { inti; for(i=0; i<n;i++) { if(i==0) {//First ProcessJobs[i].runtime=Jobs[i].needtime; Jobs[i].endtime=jobs[i].starttime+Jobs[i].needtime; } Else if(jobs[i].starttime>jobs[i-1].endtime) {//when the first process arrives at the system, the first i-1 process is completedJobs[i].runtime=Jobs[i].needtime; Jobs[i].endtime=jobs[i].starttime+Jobs[i].needtime; } Else{//when the first process arrives at the system, the first i-1 process is not finished runningjobs[i].runtime=jobs[i].needtime+jobs[i-1].endtime-Jobs[i].starttime; Jobs[i].endtime=jobs[i].starttime+Jobs[i].runtime; } jobs[i].dqzztime=jobs[i].runtime*1.0/Jobs[i].needtime; }}voidResultstructJob jobs[ -],intN) { inti; DoubleAverruntime; DoubleAverdqzztime; intSum_runtime=0; DoubleSum_dqzztime=0.00; printf ("job name \ t reaches system time \ t run time \ t finish time \ t turnaround time with right turnaround time \ n"); for(i=0; i<n;i++) {printf ("%s\t%d\t%d\t%d\t%d%.2f\n", Jobs[i].name,jobs[i].starttime,jobs[i].needtime,jobs[i].endtime,jobs[i].runtime,jobs[i].dqzztime); Sum_runtime=sum_runtime+Jobs[i].runtime; Sum_dqzztime=sum_dqzztime+Jobs[i].dqzztime; } averruntime=sum_runtime*1.0/N; Averdqzztime=sum_dqzztime*1.000/N; printf ("average turnaround time:%.2f \ n", Averruntime); printf ("average take right turnaround time:%.3f \ n", Averdqzztime); printf ("\ n");}voidSjfstructJob jobs[ -],intN) { intI=0, b=0; Chartemp[Ten]; intmin; for(i=0; i<n-1; i++) { if(jobs[i].starttime>jobs[i+1].starttime) {min=Jobs[i].starttime; Jobs[i].starttime=jobs[i+1].starttime; Jobs[i+1].starttime=min; Min=Jobs[i].needtime; Jobs[i].needtime=jobs[i+1].needtime; Jobs[i+1].needtime=min; strcpy (Temp,jobs[i].name); strcpy (Jobs[i].name,jobs[i+1].name); strcpy (Jobs[i+1].name,temp); } } //sort by job arrival system time, first arriving at the topjobs[0].endtime=jobs[0].starttime+jobs[0].needtime; jobs[0].runtime=jobs[0].needtime; jobs[0].dqzztime=jobs[0].runtime*1.0/jobs[0].needtime; for(i=1; i<n;i++) { if(jobs[i].starttime>jobs[0].endtime); Elseb=b+1;//when the job arrives at the system, the No. 0 job is still running the number of jobs that need to wait for job 0 to run with B statistics } for(i=1; i<=b-1; i++) { if(jobs[i].needtime>jobs[i+1].needtime) {min=Jobs[i].starttime; Jobs[i].starttime=jobs[i+1].starttime; Jobs[i+1].starttime=min; Min=Jobs[i].needtime; Jobs[i].needtime=jobs[i+1].needtime; Jobs[i+1].needtime=min; strcpy (Temp,jobs[i].name); //copies the value of the second parameter to the first argument, returns the first argumentstrcpy (jobs[i].name,jobs[i+1].name); strcpy (Jobs[i+1].name,temp); } //Sort by shortest run time } for(i=1; i<n;i++) { if(jobs[i].starttime>jobs[i-1].endtime) {//when the first process arrives at the system, the first i-1 process is completedjobs[i].endtime=jobs[i].starttime+Jobs[i].needtime; Jobs[i].runtime=Jobs[i].needtime; } Else{jobs[i].endtime=jobs[i-1].endtime+Jobs[i].needtime; Jobs[i].runtime=jobs[i].endtime-Jobs[i].starttime; } jobs[i].dqzztime=jobs[i].runtime*1.0/Jobs[i].needtime; }}
This is the FCFS SJF algorithm code and running results, the HRRF algorithm is still in the finished.
Experiment two: Job scheduling simulation Program (FCFS SJF algorithm)