Experiment two job scheduling simulation program
13 Internet of things Chen Zifan 201306104108
I. Purposes and requirements 1. Experimental purpose
(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, the generation of simulation 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 choose a pseudo-random number to specify the arrival time (0-30) of each job and the desired run time (1-8).
Second, the function 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.
III. analysis of simulation data results
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.
Iv. 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.
Third, the 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.
Iv. Source Code
#include <stdio.h> #include <stdlib.h> #include <conio.h> #define GETPCH (Type) (type*) malloc (sizeof ( Type)//create a spatial struct worktime{float Tb for the process; Job run time float Tc; Job completion time float Ti; Turnaround time float Wi; Turnaround time with rights}; struct JCB {char name[10]; Job name float Subtime; Job arrival time float runtime; The run time required for the job char resource; Required resource float Rp; Backup job response than Char state; Job status int worked_time; Elapsed time struct worktime wt; int need_time; Required run time int flag; Process End sign struct jcb* link; Chain Pointer}*ready=null,*p; typedef struct JCB jcb;float T=0;int N; JCB *front,*rear; Time Rotation method variable void sort () {JCB *first, *second; int insert=0; Insert number if (Ready==null) | | ((P->subtime) < (ready->subtime))) {p->link=ready; Ready=p; t=p->subtime; p->rp=1; } else { First=ready; second=first->link; while (Second!=null) {if ((P->subtime) < (second->subtime)) {P->link =second; first->link=p; Second=null; Insert=1; } else {first=first->link; second=second->link; }} if (insert==0) first->link=p; }} void Sjfget () {JCB *front,*mintime,*rear; int ipmove=0; Mintime=ready; rear=mintime->link; while (Rear!=null) {if (Rear!=null && (t>=rear->subtime) && (mintime->runtime) > (rear-& Gt;runtime)) {front=mintime; Mintime=rear; rear=rear->link; ipmove=1; } else rear=rear->link; } if (ipmove==1) {front->link=mintime->link; mintime->link=ready; } Ready=mintime;} void HrnGet () {JCB *front,*mintime,*rear; int ipmove=0; Mintime=ready; rear=mintime->link; while (Rear!=null) if ((Rear!=null) && (t>=rear->subtime) && (MINTIME->RP) < (REAR->RP)) {front=mintime; Mintime=rear; rear=rear->link; ipmove=1; } else rear=rear->link; if (ipmove==1) {front->link=mintime->link; mintime->link=ready; } Ready=mintime;} void CREATJCB ()//Create a JCB for each job and initialize it to form a loop chain queue {JCB *p,*l; int i=0; L = (JCB *) malloc (sizeof (JCB)); printf ("\ n Please enter the number of jobs:"); scanf ("%d", &n); printf ("\ n job number no.%d:\n", i); printf ("\ n Please enter the name of the job:"); scanf ("%s", l->name); printf ("\ n Please enter the time of the job:"); scanf ("%d", &l->need_time); L->state = ' R '; The initial state of the job is ready l->worked_time = 0; l->link=null; l->flag=0; Front=l; for (i =1;i<n;i++) {p = (JCB *) malloc (sizeof (JCB)); printf ("\ n job number no.%d:\n", i); printf ("\ n Please enter the name of the job:"); scanf ("%s", p->name); printf ("\ n Please enter the time of the job:"); scanf ("%d", &p->need_time); P->state= ' R '; p->worked_time=0; p->flag=0; l->link=p; l=l->link; } Rear=l;rear->link=front;} void output ()//process output function {int J; printf ("Name runtime Needtime state\n"); for (j=1;j<=n;j++) {printf ("%-4s\t%-4d\t%-4d\t%-c\n", Front->name,front->worked_time,front->need_time , front->state); front=front->link; } printf ("\ n");} int judge (JCB *p)//Judge All processes run end {int flag=1,i; for (i=0;i<n;i++) {if (p->state!= ' e ') {flag = 0; break;} p=p->link; } return flag; void input () {int i,num; printf ("\ n Please enter the number of jobs:"); scanf ("%d", &num); for (i=0;i<num;i++) {printf ("\ n job number no.%d:\n", i); P=GETPCH (JCB); printf ("\ n Enter job name:"); scanf ("%s",p->name); printf ("\ n Input job arrival time:"); scanf ("%f", &p->subtime); printf ("\ n Enter job run time:"); scanf ("%f", &p->runtime); printf ("\ n"); P->state= ' W '; p->link=null; Sort (); }} int space () {int l=0; jcb* Jr=ready; while (jr!=null) {l++; jr=jr->link; } return (l);} void disp (jcb* Jr,int Select) {if (select==3) printf ("\ n" The job arrival time service time response is at the time of completion of the runtime with right turnaround time \ n "); else printf ("Time-to-run time of the job arrival time service is completed at the moment of turn turnaround time \ n"); printf ("|%s\t", jr->name); printf ("|%.2f\t", jr->subtime); printf ("|%.2f\t", jr->runtime); if (SELECT==3&&P==JR) printf ("|%.2f", JR->RP); if (P==JR) {printf ("|%.2f\t", JR->WT. TB); printf ("|%.2f", JR->WT. TC); printf ("|%.2f\t", JR->WT. Ti); printf ("|%.2f", JR->WT. Wi); } printf ("\ n");} int Destroy () {printf ("\ n Job [%s] completed. \ n", p->name); Free (p); ReTurn (1);} void check (int select) {jcb* Jr; printf ("\ n currently running job is:%s", p->name); Disp (p,select); Jr=ready; printf ("\ nthe current Ready queue status is: \ n"); while (Jr!=null) {jr->rp= (jr->runtime+t-jr->subtime)/jr->runtime; Disp (jr,select); jr=jr->link; } Destroy ();} void running (jcb* Jr) {if (t>=jr->subtime) jr->wt. tb=t; Else JR->WT. tb=jr->subtime; JR->WT. TC=JR->WT. tb+jr->runtime; JR->WT. TI=JR->WT. tc-jr->subtime; JR->WT. WI=JR->WT. ti/jr->runtime; T=JR->WT. Tc;} int main () {int select=0,len,h=0; float sumti=0,sumwi=0; printf ("Please select the job scheduling algorithm: \ n"); printf ("\t1. FCFS 2.SJF 3.HRN \ n "); printf ("Please enter the job scheduling algorithm sequence number (1-3):"); scanf ("%d", &select); if (select==4) {CREATJCB (); } else {input (); Len=space (); while ((len!=0) && (ready!=null)) {h++; printf ("\ nthe" \ n "Operation%d jobs \ n", h); P=ready; ready=p->link; P>link=NULL; P->state= ' R '; Running (p); SUMTI+=P->WT. Ti; SUMWI+=P->WT. Wi; Check (select); if (select==2&&h<len-1) sjfget (); if (select==3&&h<len-1) hrnget (); printf ("\ n Press any key to continue ..."); GetChar (); GetChar (); } printf ("\ n \ nthe job has been completed. \ n"); printf ("\ t average turnaround time for this group of jobs:%.2f\n", sumti/h); printf ("\ t the weighted average turnaround time for this group of jobs:%.2f\n", sumwi/h); GetChar ();}}
Five, the experiment
Vi. Summary of the experiment
This experiment compared with the previous experiment, the difficulty greatly improved =. = may be my C language base is weak, leading to a lot of problems in the experimental process, these three algorithms need to write their own functions, where the CREATJCB algorithm is relatively easy to understand, so the problem is not very large, but the other two more difficult to understand, but finally in the classmate's help or completed the experiment, In the next experiment class, more practice!!!
Experimental two-Experiment report