1203 experimental three-process scheduling simulation experiment

Source: Internet
Author: User


Experimental three-process scheduling simulation experiment

Internet of Things engineering Zhang Xuiling 201306104140

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

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.

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.

Third, test methods, procedures and results

1. source program Name: Compressed package file (RAR or Zip) source program name: zxl3.c

Executable Program Name: Zxl3.exe

2. principle Analysis and Flowchart

Major overall design issues (including storage structure, main algorithms, implementation of key functions, etc.)

1. Set up a Process control block (PCB) to hold the process information, including the process name, priority, arrival time, required run time, elapsed time, and process status;

2. The function input () used to enter the process, when the input process is less than 5, will require the continuation of input;

3. The function sort () that is used to order the process of the input completion is sorted by the priority of the process;

4. The function print () for the printing process, prints the process that has been sorted out;

5. The function attemper () for processing the process, the higher priority process first processes a time slice, and then proceeds in the order of execution;

6. Function Select () for selecting a function;

7.main () function.

3. Main procedural paragraphs and their explanations:

To implement the main function of the program section, it is important that the program annotation interpretation.

#include <stdio.h>
#define MAX 50

typedef struct JOB
{
Char Name[max]; Process Name
int priority; Priority level
int arrtime; Arrival time
int needtime; Run time required
int usedtime; Used Time
Char state; Process status
}PCB;

int n=0;
PCB Pcb[max];
int ptime; Time slices

void Input ()
{
Char ch;

do{
do{
printf ("\n*** Please enter the process of%d process ***\n", n);
printf ("\ n process name");
scanf ("%s", pcb[n].name);
printf ("process priority");
scanf ("%d", &pcb[n].priority);
printf ("The time required by the process");
scanf ("%d", &pcb[n].needtime);
Pcb[n].arrtime=n;
pcb[n].usedtime=0;
Pcb[n].state= ' W ';
n++;
printf ("Does want to continue?" Y ' es or ' N ' o: ");
do{
Ch=getchar ();
}while (ch!= ' y ' &&ch!= ' n ' &&ch!= ' y ' &&ch!= ' n ');
}while (ch== ' Y ' | | ch== ' y ');
}while (n<5);
}

void Sort ()
{
int i,j;
PCB temp;
for (i=0;i<n-1;i++)//Sort by arrival time
{
for (j=n-2;j>=i;j--)
{
if (pcb[j+1].arrtime<pcb[j].arrtime)
{
TEMP=PCB[J];
PCB[J]=PCB[J+1];
Pcb[j+1]=temp;
}
}
}

for (i=0;i<n-1;i++)//Sort by priority
{
for (j=n-2;j>=i;j--)
{
if (pcb[j+1].priority>pcb[j].priority)
{
TEMP=PCB[J];
PCB[J]=PCB[J+1];
Pcb[j+1]=temp;
}
}
}
if (pcb[0].state!= ' F ')
{
Pcb[0].state= ' R ';
}
}
void Print ()
{
int i;
Sort ();
printf ("\n* Process name * Priority * Arrival time * takes time * used time * process status *\n");
for (i=0;i<n;i++)
{
printf ("%8s%8d%8d%10d%10d%10c\n", Pcb[i].name,pcb[i].priority,pcb[i].arrtime,pcb[i].needtime,pcb[i].usedtime, Pcb[i].state);
}
}
void Attemper ()
{
do{
if ((pcb[0].needtime-pcb[0].usedtime) >ptime)//Determine if the remaining run time of the process is greater than the time slice
{
Pcb[0].usedtime+=ptime;
pcb[0].priority--;
Pcb[0].state= ' W ';
}
else//Completed process
{
Pcb[0].usedtime=pcb[0].needtime;
Pcb[0].priority=-1;
Pcb[0].state= ' F ';
}
Print ();
}while (pcb[0].state!= ' F ');
}
Char Select ()
{
Char Select;
printf ("Key functions:");
printf ("\n0: End Current Task");
printf ("\n1: Add and dispatch process");
printf ("\n2: Printing Process");
printf ("\ n Please select:");
do{
Select=getchar ();
}while (select!= ' 0 ' &&select!= ' 1 ' &&select!= ' 2 ');
return select;
}

void Main ()
{
Char choose;
n=0;
printf ("\n*******************************************************\n");
printf ("\t\t*** Welcome to use process scheduling simulation ***\n\n");

Choose=select ();
do{

if (choose== ' 0 ')
{
Return
}
if (choose== ' 1 ')
{
printf ("Please set the size of the time slice:");
scanf ("%d", &ptime);
Input ();
printf ("\n*****************************\n");
Print ();
printf ("\n*****************************\n");
Attemper ();
}
if (choose== ' 2 ')
{
Print ();
}
Choose=select ();
}while (1);
}

4. operation results and Analysis

It must be fitted with running results, and the results are consistent with expectations and analysis.

Automatically continues to require input when there are less than 5 processes:

After the process has reached 5, you can exit or continue typing:

Sort:

Process processing:

Four, Experimental Summary

During the experiment, I wanted to use a certain value to loop through the data about the process, but it didn't show the process name and state, and the process was displayed in error, so use Do-while to enter the edge to determine the value. This program is to learn from other programs to complete, the most important thing is to understand the simple time slice rotation method, multi-level feedback queue scheduling algorithm ideas.

1203 experimental three-process scheduling simulation experiment

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.