experiment two, job scheduling simulation program
Major: Business Soft one class name: Li Yunjia No.: 201406114143
I. Purpose and Requirements
1. Purpose of the experiment
(1) Deepen the understanding of the job scheduling algorithm;
(2) Training in program design.
2 . Experimental requirements
A simulation program that writes one or more job schedules in a high-level language.
Job scheduler for single-channel batch processing systems. When the job is put into operation, it occupies all the resources of the computer until the job is completed, so it is not necessary to schedule the job to consider whether the resources it needs are met, the time it runs, and so on.
Job scheduling algorithm:
1) The 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
Each job is represented by a job control block, JCB can include the following information: Job name, Submission (arrival) time, required run time, required resources, job status, chain pointers, and so on.
The status of the job can be one of waiting for W (wait), running R (run), and completing F (finish) three. The initial state of each job is to wait for W.
First, generation of simulated data
1. Allows the user to specify the number of jobs (2-24) and the default value is 5.
2. Allows the user to choose to enter the arrival time and the desired run time for each job.
3. (* *) read the above data from the file.
4. (* *) also allows the user to select a pseudo-random number to specify the arrival time (0-30) and the desired run time (1-8) for each job.
Second, functions of the simulation program
1. According to the arrival time and required running time of the simulated data, the FCFS, SJF and HRRN scheduling algorithms are executed, the program calculates the start execution time of each job, the completion time of each job, the turnaround time and the turnaround time (turnover factor).
2. Dynamic demonstration of each scheduling, update the current system time, in the running state and waiting for the corresponding information of each job (job name, arrival time, the desired run time, etc.) for the HRRN algorithm, can show each job response than R in each schedule.
3. (*) allows users to submit new jobs during the impersonation process.
4. (* *) to write and dispatch a multi-channel program system job scheduling simulation program. Only the job scheduling algorithm is required: the first-come-first service-based scheduling algorithm is used. For a multi-channel program system, it is assumed that the resource requirements for each job must be taken into account when scheduling jobs with various resources and quantities in the system.
Third, Simulation Data Results Analysis
1. The average turnaround time of each algorithm of the same simulation data is compared with the turnover coefficient.
2. (* *) using a graph or column chart to represent the above data, analysis of the advantages and disadvantages of the algorithm.
Four, Experiment Preparation
Serial number |
Prepare content |
Complete situation |
1 |
What is a job? |
A job is a separate task that a user submits to the operating system calculation. |
2 |
What information does a job have? |
Each job is represented by a job control block, JCB can include the following information: Job name, Submission (arrival) time, required run time, required resources, job status, chain pointers, and so on. |
3 |
In order to facilitate the simulation of the scheduling process, what kind of data structure is used to store and represent the job? JCB |
A single job uses structs, and multiple jobs use queues. |
4 |
What are the commonly used job scheduling algorithms in the operating system? |
First come first service (FCFS) algorithm, the shortest job first (SJF) algorithm, the shortest remaining time priority algorithm, the highest response ratio priority (HRRN) algorithm. |
5 |
How to implement the job scheduling algorithm programmatically? |
First come first service algorithm. |
6 |
How is the input of the simulator easier to design and how does the output of the results render better? |
Input: Read file Output: Calculates and prints the average turnaround time for this set of jobs and the time of the weighted turnaround. |
Five, Other requirements
1. Complete report, complete content, specification.
2. The experiment must be examined to answer questions about the experiment.
Note: The entry with the * * number indicates the selection.
Ii. contents of the experiment
Complete the design, coding and commissioning work according to the assigned experimental project and complete the experiment report.
three
, experimental environment
You can use TC, or you can choose to use a variety of controls in Windows VB,VC and other visual environment. It is also possible to choose other experimental environments independently.
Four, the experiment
principle and core algorithm reference program segment
Pipettes FCFS algorithm:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
struct jcb{
Char name[10]; Job name
char status; Job Status
Double arrtime; Job Arrival time
Double reqtime; Job request time
Double Startime; Job start time
Double finitime; Job completion time
Double turntime; Job turnaround Time
float Tatime,tawtime;
float Prio;
}jobarr[24],jobfin[24],job[24],cunfang;
int systime=0;
int intarr,intfin,intjob; Number of jobs reached, number of finishes, number of jobs not reached
int i;
int R;
Sort
void rank (int flag) {
Int J;
for (r=0;r<flag;r++)
{
for (j=r+1;j<flag;j++)
if (jobarr[r].arrtime>jobarr[j].arrtime)
{
CUNFANG=JOBARR[R];
JOBARR[R]=JOBARR[J];
Jobarr[j]=cunfang;
}
}
}
First come first service algorithm
void FCFS (int flag)
{
int R;
Double attime,wattime;
jobarr[0].startime=0;
Jobarr[0].finitime=jobarr[0].startime+jobarr[0].reqtime;
Jobarr[0].turntime=jobarr[0].reqtime;
Attime=jobarr[0].turntime;
Wattime=jobarr[0].finitime;
for (r=1;r<flag;r++)
{
Jobarr[r].startime=jobarr[r-1].finitime;
Jobarr[r].finitime=jobarr[r].startime+jobarr[r].reqtime;
Jobarr[r].turntime=jobarr[r-1].finitime-jobarr[r].arrtime+jobarr[r].reqtime;
Attime+=jobarr[r].turntime;
Wattime=jobarr[r].finitime-jobarr[r].reqtime+wattime;
}
attime=attime/(double) flag);
wattime=wattime/(double) flag);
printf ("\NFCFS algorithm job sequence table \ n");
printf ("--------------------------------------------------------------------------------\ n");
printf ("Job name \ t time required to reach system time \TCPU \ t start time \ t end time \ t turnaround time");
for (r=1;r<flag;r++) {
printf ("\ n");
printf ("%s", jobarr[r].name);
printf ("%11.2lf", jobarr[r].arrtime);
printf ("%16.2lf", jobarr[r].reqtime);
printf ("%16.2lf", jobarr[r].startime);
printf ("%16.2lf", jobarr[r].finitime);
printf ("%16.2lf", jobarr[r].turntime);
}
printf ("\ n" average turnaround time:%.2lf ", attime);
printf ("\ n \ nthe average right turnaround time:%.2lf\n", wattime);
printf ("--------------------------------------------------------------------------------\ n");
}
Insert
void Inser ()
{
printf ("%d jobs: \ n", i+1);
printf ("Enter job Name:");
scanf ("%s", jobarr[i].name);
printf ("Job Arrival time:");
scanf ("%d", &jobarr[i].arrtime);
printf ("Job requires service time:");
scanf ("%d", &jobarr[i].reqtime);
printf ("\ n");
i++;
Rank (i);
}
Delete
void Delet ()
{
int q;
Char remove[10];
printf ("Please enter the job name to delete:");
scanf ("%s", remove);
for (r=0;r<i;r++)
if (strcmp (jobarr[r].name,remove) ==0) {
for (q=r;q<i;q++) {
JOBARR[Q]=JOBARR[Q+1];
}
i--;
}
}
Enter job information
void input ()
{
printf ("Number of jobs:");
scanf ("%d", &i);
printf ("\ n");
for (r=0;r<i;r++)
{
printf ("%d jobs: \ n", r+1);
printf ("Enter job Name:");
scanf ("%s", jobarr[r].name);
printf ("Arrival time:");
scanf ("%lf", &jobarr[r].arrtime);
printf ("Request Service Time:");
scanf ("%lf", &jobarr[r].reqtime);
printf ("\ n");
}
}
Output Job Information
void Output ()
{
int num,m;
while (1) {
printf ("The queue is not reached \ n" After sorting by arrival time);
printf ("\tname\tartime\trqtime\n");
for (r=0;r<i;r++)
{
printf ("N%d", r+1);
printf ("\t%s", jobarr[r].name);
printf ("\t%.2lf", jobarr[r].arrtime);
printf ("\t%.2lf", jobarr[r].reqtime);
printf ("\ n");
}
printf ("\n\t\t\t Now system Time 0:\n");
printf ("Please select algorithm: 1. First come first service (FCFS) scheduling algorithm 2. Short job first (SJF) scheduling algorithm \ n");
scanf ("%d", &m);
Switch (m)
{
Case 1:
FCFS (i);
Break
Case 2:
Break
}
printf ("\n1." Insert 2. Delete 3. Exit? \ n ");
printf ("Please select:");
scanf ("%d", &num);
if (num==1)
Inser ();
else if (num==2)
Delet ();
else if (num==3)
Return
Else
Continue
}
}
Reading data from a file
void ReadFile ()
{
int m;
int i=1;
int flag=0;
FILE *FP; Defining file Pointers
Fp=fopen ("3.txt", "R"); Open File
if (fp==null)
{
printf ("File open Error!\n");
Exit (0);
}
printf ("\n\t Job name \ t ' time the job arrives \ \ \ \ \ \ \ \ \ \ \ \");
while (!feof (FP))
{
FSCANF (FP, "%s%lf%lf", &jobarr[i].name,&jobarr[i].arrtime,&jobarr[i].reqtime); The FSCANF () function reads the data into the
i++;
Flag=i;
};
Rank (i);
for (i=1;i<flag;i++)
printf ("\n\t%s\t\t%.2lf\t\t%.2lf", jobarr[i].name,jobarr[i].arrtime,jobarr[i].reqtime); Output to screen
printf ("\ n");
printf ("\ n");
if (fclose (FP))//close file
{
printf ("Can not close the file!\n");
Exit (0);
}
printf ("Please select algorithm: 1. First come first service (FCFS) scheduling algorithm 2. Short job first (SJF) scheduling algorithm \ n");
scanf ("%d", &m);
Switch (m)
{
Case 1:
FCFS (i);
Break
Case 2:
Break
}
printf ("\ n");
printf ("");
}
Pseudo-random number generator
void Pseudo_random_number ()
{
int i,n,m;
Srand ((unsigned) time (0)); The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand ().
Enter the number of jobs
N=rand ()%13+5;
for (I=1; i<=n; i++)
{
Jobarr[i].name=i;
Job Arrival time
Jobarr[i].arrtime=rand ()%29+1;
Job run time
Jobarr[i].reqtime=rand ()%7+1;
}
Rank (i);
printf ("\ n" required time for job to run when job name job arrives);
for (I=1; i<=n; i++)
{
printf ("\njob%2d%11.2lf%.2lf", i,jobarr[i].arrtime,jobarr[i].reqtime);
}
printf ("\ n");
printf ("Please select algorithm: 1. First come first service (FCFS) scheduling algorithm 2. Short job first (SJF) scheduling algorithm \ n");
scanf ("%d", &m);
Switch (m)
{
Case 1:
FCFS (i);
Break
Case 2:
Break
}
printf ("\ n");
}
void Main ()
{
int num;
while (1)
{
printf ("-----------------------------------------------------------------------------\ n");
printf ("Choose how to read the job: 1. File read 2. Manually enter 3. Pseudo random number randomly generated \ n");
printf ("-----------------------------------------------------------------------------\ n");
printf ("Please select:");
scanf ("%d", &num);
Switch (NUM)
{
Case 1:
ReadFile ();
Break
Case 2:
Input ();
Rank (i);
Output ();
Break
Case 3:
Pseudo_random_number ();
Break
}
}
}
Operation Result:
Lab 2 Job scheduling