Experimental three-process simulation scheduling

Source: Internet
Author: User
Tags define null

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.

4. Experimental environment

Choose the experimental environment independently. Turbo C can be chosen as the development environment. You can also choose a visual environment under Windows to make it easier to take advantage of a variety of controls.

5.  Experiment Code
#include <stdio.h> #include <stdlib.h> #define GETPCH (Type) (type*) malloc (sizeof (type)) #define NULL   0struct pcb{//define Process Control block PCB char name[10];   Process name Char state;   status int priority;   priority int Needtime;   Time required to run int rtime; Arrival time struct pcb* link;}    *ready=null,*p;typedef struct PCB Pcb;sort ()//Establish priority sequencing of processes {PCB *first,*second;    int insert=0; if ((ready==null) | | |  ((p->priority) > (ready->priority)))        Top priority, insert team first {p->link=ready;    Ready=p;        } else//process comparison priority, insert the appropriate location in {First=ready;        second=first->link;            while (Second!=null) {if (P->priority > (second->priority))//If the insert process is larger than the current process priority, it is inserted in front of the current process                {p->link=second;                first->link=p;                Second=null;            Insert=1;                } else//insert process has the lowest priority, insert to end of queue {first=first->link;       second=second->link;     }} if (insert==0) first->link=p;    }}input ()//Build Process Control block function {int i,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);        printf ("\ n input process name:");        scanf ("%s", p->name);        printf ("\ n Input process priority number:");        scanf ("%d", &p->priority);        printf ("\ n input process run time:");        scanf ("%d", &p->needtime);        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 for displaying the current process {printf ("\ n name\t state\t priority\t needtime\t runtime \ n");    printf ("%s\t", pr->name);    printf ("%c\t", pr->state);    printf ("%d\t\t", pr->priority);    printf ("%d\t\t", pr->needtime);    printf ("%d\t", pr->rtime); printf ("\ n");}   Check ()Build Process View function {PCB *pr;   printf ("\ n currently running process is:%s", p->name);    Displays the current running Process disp (p);    Pr=ready;   printf ("\ nthe current Ready queue status is: \ n");        Show ready queue status while (Pr!=null) {disp (PR);    pr=pr->link; }}destroy ()//build process Undo function (Process run end, undo process) {printf ("\ n process [%s] completed.    \ n ", p->name); Free (p);}    Running ()//build process-ready function (process run time to, set ready state) {(p->rtime) + +;   if (P->rtime==p->needtime) destroy ();        Call the Destroy function else {(p->priority)--;        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 been completed.    \ n "); Ch=getchar ();}

Six experiments

Seven experimental experiences

Experiment three and the last job simulation scheduling has a certain similarity, do it more simple than the last time, the familiarity is also increased,

But the idea is not clear enough, but also refer to other people's procedures.

Experimental three-process simulation scheduling

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.