Experiment Three Process Scheduling Simulation program 2.0
First, the purpose of the experiment
A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.
Second, the experimental requirements
Design a process scheduling simulator with n processes executing concurrently.
1. Simulation of process data generation
Allows the user to specify the number of jobs (2-24) and the default value is 5.
Allows the user to choose to enter the time of arrival of each process, the elapsed time, and the elapsed time of the process in time slices.
2. Function of the simulator scheduler
2.1 According to the arrival time and required running time of the simulated data, the following scheduling algorithms can be executed separately.
FCFS
Sj
Hrrn
Rr
2.2 Shows the scheduling execution sequence for each of the processes under each algorithm.
2.3 Calculate the start execution time of each process, the completion time of each job, the turnaround time and the right turnaround time (turnover factor).
2.4 Simulation Data Results analysis: Comparing the average turnaround time and turnover coefficient of each algorithm in the same set of simulation data.
Third, the experimental explanation
1) first come first service (FCFS) scheduling algorithm, that is, according to the order of operation arrived scheduling. The job that waits the longest in the system is always dispatched first.
2) Short job first (SJF) scheduling algorithm, priority scheduling requires the shortest running time of the job.
3) in response to high-priority (HRRN) scheduling algorithm, set a priority (response ratio) for each job, before scheduling to calculate the priority of each job, priority of the higher priority scheduling. RP (response ratio) = job turnaround time/job run time =1+ job wait time/job run time.
4) Time slice rotation (RR) scheduling algorithm: Each time the scheduler allocates the CPU to the ready queue the first process uses a single timestamp, and each process in the ready queue runs a time slice in turn. When this time slice is finished, force a process to let the processor out of the queue and wait for the next round of scheduling.
Four, the experimental environment
Turbo C can be chosen as the development environment. You can also choose a visual environment such as VB,CB under Windows, which makes it easy to use various controls. Choose the experimental environment independently.
Five, the experimental code
#include <stdio.h>
#include <time.h>
#include <windows.h>
struct job
{
Char name[10];
char status;
int id;
int arrtime;
int reqtime;
int startime;
int finitime;
float Tatime,tawtime;
float RP;
}JOB[24];
int Pseudo_random_number ()
{
int i,n;
Srand ((unsigned) time (0));
N=rand ()%23+5;
for (i=0; i<n; i++)
{
job[i].id=i+1;
Job Arrival time
Job[i].arrtime=rand ()%29+1;
Job run time
Job[i].reqtime=rand ()%7+1;
}
printf ("\ n" the time required for the job to run when the ID job arrives);
for (i=0; i<n; i++)
{
printf ("\n%3d%12d%15d", job[i].id,job[i].arrtime,job[i].reqtime);
}
return n;
}
void sort (struct job temp[24],int num)
{
int i;
Int J;
struct Job k;
for (i=0;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (temp[j].arrtime<temp[i].arrtime)
{
k = Temp[j];
TEMP[J] = Temp[i];
Temp[i] = k;
}
}
}
}
void FCFS (struct job temp[24],int num)
{
int i=0;
printf ("First come first service algorithm fcfs\n");
float sumtatime=0;
float avetatime=0;
Sort (temp,num);
Temp[i].startime = Temp[i].arrtime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
for (i=1;i<num;i++)
{
Temp[i].startime = Temp[i-1].finitime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Temp[i].rp=temp[i]. Tatime/temp[i].reqtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
}
printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", Temp[i].name,temp[i].arrtime,
Temp[i].reqtime,temp[i].startime,temp[i].finitime,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);
}
void SJF (struct job temp[24],int num)
{
printf ("Shortest job priority algorithm sjf\n");
int i=0;
Int J;
struct Job k;
float sumtatime=0;
float avetatime=0;
Sort (temp,num);
Temp[i].startime = Temp[i].arrtime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
for (i=1;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (temp[j].reqtime<temp[i].reqtime)
{
K=TEMP[J];
Temp[j]=temp[i];
Temp[i]=k;
}
}
}
for (i=1;i<num;i++)
{
Temp[i].startime = Temp[i-1].finitime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
}
printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", Temp[i].name,temp[i].arrtime,
Temp[i].reqtime,temp[i].startime,temp[i].finitime,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);
}
void Hrrn (struct job temp[24],int num)
{
int i=0;
Int J;
struct Job k;
printf ("Shortest job priority algorithm hrrf\n");
Sort (temp,num);
float sumtatime=0;
float avetatime=0;
Temp[i].startime = Temp[i].arrtime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
for (i=1;i<num;i++)
{
Temp[i].startime = Temp[i-1].finitime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Temp[i].rp=temp[i]. Tatime/temp[i].reqtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
}
for (i=1;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (TEMP[J].RP<TEMP[I].RP)
{
K=TEMP[J];
Temp[j]=temp[i];
Temp[i]=k;
}
}
}
for (i=1;i<num;i++)
{
Temp[i].startime = Temp[i-1].finitime;
Temp[i].finitime = Temp[i].startime + temp[i].reqtime;
Temp[i]. Tatime = Temp[i].finitime-temp[i].arrtime;
Temp[i].rp=temp[i]. Tatime/temp[i].reqtime;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].reqtime;
}
printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", Temp[i].name,temp[i].arrtime,
Temp[i].reqtime,temp[i].startime,temp[i].finitime,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);
}
int main ()
{
int x;
int num;
int i;
printf ("1. Random number generating data \ n");
printf ("2. Input analog data yourself \ n");
printf ("Please select menu item:");
scanf ("%d", &x);
if (x==1)
{
Num=pseudo_random_number ();
}
else if (x==2)
{
printf ("Number of jobs:");
scanf ("%d", &num);
printf ("\ n");
for (i = 0;i<num;i++)
{
printf ("%d jobs: \ n", i+1);
printf ("Enter job Name:");
scanf ("%s", &job[i].name);
printf ("Arrival time:");
scanf ("%d", &job[i].arrtime);
printf ("Request Service Time:");
scanf ("%d", &job[i].reqtime);
printf ("\ n");
}
printf ("The queue is not reached \ n" After sorting by arrival time);
printf ("The time required for the id\t job to run \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t\t%d\n", job[i].name,job[i].arrtime,job[i].reqtime);
}
}
while (1)
{
printf ("\ n");
printf ("1.FCFS algorithm dispatch \ n");
printf ("2.SJF algorithm dispatch \ n");
printf ("3.HRRF algorithm dispatch \ n");
printf ("0. Exit algorithm dispatch \ n");
printf ("Please enter menu item:");
scanf ("%d", &x);
if (x==1)
{
FCFS (Job,num);
}
else if (x==2)
{
SJF (Job,num);
}
else if (x==3)
{
Hrrn (Job,num);
}
else if (x==0)
{
Exit (0);
}
return 0;
}
}
Vi. Results of the experiment
Experimental three-process scheduling simulation program