Experimental three-process scheduling simulation program

Source: Internet
Author: User

experiment Three, Process Scheduling Simulation program

Major: Shang Soft 2 class name: Guo Mingyin No.: 201406114204

First, Experimental Purpose

A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.

Second, experimental content and requirements

1.2.1 Example: design a process scheduling simulation program with n processes executing concurrently.

Process scheduling algorithm: Using the highest priority scheduling algorithm (that is, the processor is assigned to the highest priority process) and first-come first service (if the same priority) algorithm.

(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 priority of the process and the desired run time can be specified in advance, and the running time of the process is calculated in time slices.

(3). The state of each process can be either ready, running R (Running), or completing one of the three states of F (finished).

(4). The ready process can only run one time slice after the CPU is acquired. Represented by the elapsed CPU time plus.

(5). If the elapsed CPU time of a process has reached the desired run time after a time slice is run, the process is undone, and 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, the priority number of the process should be reduced by 1 (that is, lowering the first , and then insert it into the ready queue for scheduling.

(6). Each time the scheduler is run, the PCB for each process in the running process and ready queue is printed for inspection.

(7). Repeat the process until the process is complete.

1.2.3 experimental question B: write and debug a simulated process scheduler, using "time-slice rotation" scheduling algorithm to n (n not less than 5) process scheduling. The "Rotation method" has the simple rotation method and the multilevel feedback queue scheduling algorithm.

(1). The basic idea of the simple rotation method is that all ready processes FCFS into a queue, always assigning the processor to the team's first process, and each process takes on the same length of time slices of the CPU. If the running process has not finished using its time slice, send it back to the end of the ready queue and reassign the processor to the team's first process. Until all processes have finished running. (Is there a priority for this scheduling algorithm?) )

(2). The basic idea of the multilevel feedback queue scheduling algorithm is:

The ready queue is divided into N-level (N=3~5), each ready queue has different precedence and is assigned to different time slices: the higher the queue level, the lower the priority number, the longer the time slice, and the smaller the priority, the shorter the time slice.

The system is scheduled from the first level, when the first level is empty, the system turns to the second level queue, ... When the running process runs out of time slices, it discards the CPU and enters the next level queue if it is not completed.

When the process is first ready, it enters the first-level queue.

Third, test methods, procedures and results

1. principle Analysis and Flowchart

2. main procedural paragraphs and their explanations:

#include <stdio.h>#include<stdlib.h>#include<conio.h>#defineGETPCH (Type) (type*) malloc (sizeof (type))#defineN 3structPCB {/*define Process Control block PCB*/        Charname[Ten]; Charstatus; intPrio; intNtime; intRtime; structpcb*link;}*ready=null,*p; typedefstructPCB PCB; Sort ()/*Process priority permutation function*/{PCB*first, *second; intinsert=0; if((ready==null) | | ((P->prio) > (Ready->prio)))/*highest priority, insert team head*/{p->link=Ready ; Ready=p; }   Else /*process comparison priority, inserted in the appropriate location*/{ First=Ready ; Second=first->link;  while(second!=NULL) {       if((P->prio) > (Second->prio))/*If the insert process has a higher precedence than the current process,*/       { /*insert in front of current process*/P->link=second; First->link=p; Second=NULL; Insert=1; }       Else /*insert process with lowest priority number, insert to end of queue*/{ First=first->link; Second=second->link; }     }     if(insert==0) first->link=p; }} input ()/*Building Process Control block functions*/ {   intI,num; printf ("\ n Please enter the number of processes:"); scanf ("%d",&num);  for(i=0; i<num;i++) {printf ("\ n Process number no.%d:\n", i); P=GETPCH (PCB);/*macro (type*) malloc (sizeof (type))*/printf ("\ n Enter the process name:"); scanf ("%s",p->name); P->prio=N; printf ("\ n Enter the process run time:"); scanf ("%d",&p->ntime); printf ("\ n"); P->rtime=0;p->status='R'; P->link=NULL; Sort (); /*Call the Sort function*/   } } intSpace ()//number of calculation processes{   intL=0; pcb* pr=Ready ;  while(pr!=NULL) {L++; PR=pr->link; }   return(l);} Disp (PCB* PR)/*Single Process display function*/{printf ("%s\t",pr->name); printf ("%c\t",pr->status); printf ("%d\t",pr->prio); printf ("%d\t",pr->ntime); printf ("%d\t",pr->rtime); printf ("\ n"); } voidPrintbyprio (intPrio) {PCB*PR; PR=Ready ; printf ("\ n * * * The ready process for the current level%d queue (with priority%d) is: \ n", (n+1)-prio,prio);/*Show ready queue status*/printf ("\ QName \tstatus\t prio \tndtime\t runtime \ n");  while(pr!=NULL) {     if(pr->prio==prio) disp (PR); PR=pr->link; }}check ()/*Show All process state functions*/{PCB*PR; inti; printf ("\ n------------The process currently running is:%s------------", p->name);/*Show current running process*/printf ("\ QName \tstatus\t prio \tndtime\t runtime \ n");     DISP (p); printf ("\ nthe current ready queue status is: \ n");/*Show ready queue status*/    for(i=n;i>=1; i--) Printbyprio (i);} Destroy ()/*process undo function (Process run end, undo process)*/{printf ("\ nthe process [%s] is complete. \ n",p->name);  Free(P);} Running ()/*Run the function. Decide whether to complete, undo if done, otherwise set the ready state and insert the ready queue*/ {   intSlice,i; Slice=1;  for(i=1;i< ((n+1)-p->prio); i++) Slice=slice*2;  for(i=1; i<=slice;i++) {(P->rtime) + +; if(p->rtime==p->ntime) Break; }  if(p->rtime==p->Ntime) Destroy (); /*Call the Destroy function*/   Else   {     if(p->prio>1) (P->prio)--; P->status='R'; Sort (); /*Call the Sort function*/   } } voidCteatpdisp ()/*Show (during run) the process in all ready queues after adding a new process*/{    inti; printf ("\ n When a new process is added, all processes in the ready queue (no running process at this time): \ n");/*Show ready queue status*/    for(i=n;i>=1; i--) Printbyprio (i);}voidCREATP () {Chartemp; printf ("\ncreat One more process?type Y (yes)"); scanf ("%c",&temp); if(temp=='y'|| temp=='Y') {input ();     Cteatpdisp (); }} main ()/*Main function*/ {       CharK; A:intLen,h=0; Charch;   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->status='R';     Check ();     Running ();    CREATP (); printf ("\ n Press any key to continue ..."); CH=GetChar (); } printf ("\ n \ nthe process is complete. \ n"); printf ("\ n"); printf ("whether to re-enter the process (Y or N):"); scanf ("%c",&k); if(k=='Y') {System ("CLS"); GotoA; }   ElseExit (0);} 

3. operation results and Analysis

Four, Experimental Summary

At first not very clear the difference between process scheduling and job scheduling, through this experiment, through the teacher-given code, more clearly the process scheduling and job scheduling differences. But there are still some places do not know very well, I will continue to work hard.

Experimental three-process scheduling simulation program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.