Experimental three-process scheduling simulation program

Source: Internet
Author: User
Tags function prototype

Experiment two, Operation scheduling simulation program experiment

Professional Internet of things name Xinyi No. 201306104135

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. Experimental 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 either ready, running R (Running), or completing one of the three states of F (finished).

3) The ready process can only run one time slice after acquiring the CPU. Represented by the elapsed CPU time plus.

4) If the elapsed CPU time of the 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, it should be inserted into the ready queue for the next dispatch.

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. Experimental principles and core algorithms

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:

All ready processes are queued as FCFS, always assigning the processor to the team's first process, with the same length of time slices for each process that consumes 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.

(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.

#include <stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>/*Process Control block data structure*/typedefstructNode {Charname[Ten];/*Process Name*/ intPrio/*Process Priority*/  intRound/*time slices for process allocation*/  intCPUTime;/*CPU time consumed by the process*/ intNeedtime;/*the cup time required by the process*/ intCount/*Process Run Time*/ CharState/*status of the process: ' R ': Run, ' W ': Wait, ' F ': End*/ structNode *next;/*pointer to next process*/}PCB; PCB*finish,*ready,*tail,*run;/*A pointer to the team head of the three queue, tail the tail pointer of the ready queue*/intN/*define the number of processes*//*function function: First put in ready queue function prototype in the process ready queue: void Firstin (void) function parameter: void function return value: void*/voidFirstin (void){    if(ready!=NULL) {Run=Ready ; Ready=ready->Next; Run->state='R'; Run->next=NULL; }   Else{Run=NULL; }   } /*function function : function prototype for outputting single process information: void Prt2 (char a,pcb *p) function parameter: Char a:a== ' P ' is priority, = = ' R ' for time slice rotation PCB *p for pointer to process control block to be output function return value: void*/voidPrt2 (CharA,PCB *p) {printf ("%-10s,%-10d,%-10d,%-10d,%-10d,%-5c\n",p->name,p->cputime,p->needtime,p->count,p->round,p->State );}/*function function : function prototype for outputting all process information: void prt (char algo) function parameter: Char a:a== ' P ' as priority, = = ' R ' for time slice rotation function return value: void*/voidPrtCharalgo) {PCB*p; if(run!=NULL)  {Prt2 (Algo,run); } P=Ready ;  while(p!=NULL)   {Prt2 (algo,p); P=p->Next; } P=finish;  while(p!=NULL)  {Prt2 (algo,p); P=p->Next; } getchar ();}/*function function: Time slice rotation algorithm scheduling inserts a process into the Ready queue algorithm function prototype: void Insert2 (PCB *q) function parameter: PCB *q queue Process Control block function return value to be inserted: void*/ voidInsert2 (PCB *q) {tail->next=q; Tail=q; q->next=NULL;}/*function function: Using time slice rotation process scheduling method, process initialization function function prototype: void Rcreate_task (char algo) function parameter: Char algo:r function return value: void*/voidRcreate_task (Charalgo) {PCB*p;intI,time;CharMaxTen]; Ready=NULL; Finish=NULL; Run=NULL; for(i=0; i<n;i++) {p= (pcb*) malloc (sizeof(PCB)); printf ("\nenter the name of process\n"); scanf ("%s", NA); printf ("Enter the time of process\n"); scanf ("%d",&Time ); strcpy (P-Name,na); P->cputime=0; P->needtime=Time ; P->count=0; P->state='W'; P->round=2; if(ready!=NULL)  {Insert2 (P); }  Else{p->next=Ready ; Ready=p; Tail=p; } printf ("Output The waiting processes information\n");  PRT (ALGO); } Run=Ready ;=ready->Next; Run->state='R'; }/*function function: Using time slice rotation process scheduling method, process scheduling function function prototype: void Roundrun (char algo) function parameter: Char algo:r function return value: void*/voidRoundrun (Charalgo) {   while(run!=NULL) {Run->cputime=run->cputime+1; Run->needtime=run->needtime-1; Run->count=run->count+1; if(run->needtime==0) {Run->next=finish; Finish=run; Run->state='F'; Run=NULL; if(ready!=NULL)    {Firstin (); }            }   Else   {     if(run->count==run->round) {Run->count=0; if(ready!=NULL) {Run->state='W';      Insert2 (run);   Firstin ();  }}} prt (Algo); }  }/*Main function*/voidMain () {Charalgo='R'; printf ("Please enter the number of processes n:\n"); scanf ("%d",&N);  Rcreate_task (ALGO); Roundrun (algo);}

Summary: The time Slice Algorithm program idea is the reference on the net, looked the online procedure also is not very can understand. Multilevel feedback scheduling algorithm is still not understand.

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.