Experimental three- process Scheduling simulation program
Internet of Things 201306104112 Jianfeng
1. Purpose and Requirements
Experimental purpose
A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.
Experimental requirements
Design a process scheduling simulator with N (n not less than 5) processes executing concurrently.
Process scheduling algorithm:" time slice Rotation method " scheduling algorithm to N processes.
2. Content
The design, coding and debugging of two algorithms (Simple time slice rotation method and multistage feedback queue scheduling algorithm ) are completed, and the experimental report is completed.
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 status of each process can be ready, run R(Running), or complete F(finished ) One of three states.
3) The ready process can only run one time slice after acquiring the CPU. Represented by the elapsed CPU time plus 1 .
4) If the elapsed CPU time of the process has reached the required run time after a time slice is run, the process is undone 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 , you should insert it into the ready queue for the next schedule.
5) Each time the schedule is scheduled , the program prints a run process, the PCB of each process in the ready queue for inspection.
6) Repeat the process until the process is complete.
3. Code
#pragma COMMENT (linker, "/subsystem:console") #include <stdio.h> #include <stdlib.h> #include <conio.h > #include <dos.h> #define GETPCH (Type) (type*) malloc (sizeof (type)) #define NULL 0 struct pcb{/* Define Process Control block PCB */ Char name[10]; Char state; int super; int ntime; int rtime; struct pcb* link; } *ready=null,*p; typedef struct PCB PCB; Sort ()/* Establish prioritization functions for processes */{PCB *first, *second; int insert=0; if ((ready==null) | | | ((P->super) > (ready->super))) /* Priority Max, insert team First */{p->link=ready; Ready=p; } else/* Process Compare priority, insert the appropriate location */{first=ready; second=first->link; while (Second!=null) {if (P->super > (second->super))/* If the insert process is larger than the current process precedence, */{* * is inserted in front of the current process */p->link=second; first->link=p; Second=null; Insert=1; } else/* Inserts a process with the lowest priority number, insert to the end of the queue */{first=first->link; second=second->link; }} if (insert==0) first->link=p; }} input ()/* Establish the Process Control block function */{int i,num; printf ("\ n Please enter the process number:"); scanf ("%d", &num); num=num++; for (I=1; i<num;i++) {printf ("\ n process number no.%d:\n", i); P=GETPCH (PCB); printf ("\ n input process name:"); scanf ("%s", p->name); printf ("\ n Input process priority number:"); scanf ("%d", &p->super); printf ("\ n input process run time:"); scanf ("%d", &p->ntime); printf ("\ n"); P->rtime=0;p->state= ' W '; p->link=null; Sort (); /* Call the Sort function */}} int space () {int l=0; pcb* Pr=ready; while (pr!=null) {l++; pr=pr->link; } return (L); } disp (PCB * PR)/* Establish a process display function to display the current process */{printf ("\ QName \ t state \ t super \ ndtime \ t runtime \ n"); printf ("|%s\t", pr->name); printf ("|%c\t", pr->state); printf ("|%d\t", pr->super); printf ("|%d\t", pr->ntime); printf ("|%d\t", pr->rtime); printf ("\ n"); } check ()/* Establish process view function */{pcb* PR; printf ("\ n * * * * Currently running process is:%s", p->name); /* Displays the current running process */disp (P); Pr=ready; printf ("\ n * * * Current ready queue status: \ n"); /* Show ready queue status */while (pr!=null) {disp (PR); pr=pr->link; }} Destroy ()/* Establish the process undo function (Process run end, undo process) */{printf ("\ n process [%s] completed. \ n", p->name); Free (p); } running ()/* Establish the process-ready function (process run time to, set ready state */{(p->rtime) + +); if (P->rtime==p->ntime) destroy (); /* Call the Destroy function */else {(p->super)--; P->state= ' W '; Sort (); /* Call the Sort function */}} main ()/*/main function */{int len,h=0; Char ch; Input (); Len=space (); while ((len!=0) && (ready!=null)) {Ch=getchar (); h++; printf ("\ n the execute number:%d \ n", h); P=ready; ready=p->link; p->link=null; P->state= ' R '; Check (); Running (); printf ("\ n Press any key to continue ..."); Ch=getchar (); } printf ("\ n \ nthe process has completed. \ n"); Ch=getchar (); }
4. Summary
This test is also a multi-reference, the realization of the understanding is not too deep, but also in the class to see more to learn.
Experimental three-process scheduling simulation program