experiment Three, Process Scheduling Simulation program Experiment
Major: Commercial Software engineering Name: Jongjie No.: 201406114236
first, the purpose of the experiment
A process scheduler is completed in a high-level language to deepen the understanding of process concepts and process scheduling algorithms.
Second, Experimental requirements and content
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.
Thinking: What is the difference between job scheduling and process scheduling?
1.2.2 experimental question A: write and debug a simulated process scheduler, using the "highest priority priority" scheduling algorithm to n (n not less than 5) process scheduling.
The basic idea of the "highest priority priority" scheduling algorithm is to allocate the CPU to the process with the highest number of priorities in the ready queue.
(1). The static precedence number is determined when the process is created and no longer changes during the entire process run.
(2). The dynamic precedence number refers to the process's priority number, which can be given an initial value when the process is created, and the priority number can be modified by a certain rule. For example, when a process obtains a CPU, it reduces its priority by 1, and the process waits longer than a certain time period (2 timecode time) to increase its priority number, and so on.
(3). (*) The priority number of the process and the running time required can be specified in advance (or can be generated by random numbers).
(4). (*) The process can be created (incremented) during the simulation scheduling process, and its arrival time is the time that the process entered.
0.
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.
(3). (*) Consider the blocking State B (Blocked) of the process to increase the blocking queue. The time at which the process is blocked and blocked is determined by the resulting "random number" (the frequency and length of the blocking is more reasonable). Note that a process can be converted to a blocking state only if it is in a running state, and the process is only ready to be converted to a running state.
1. Experimental content
According to the assigned experimental subjects: A (1), A (2), B (1) and B (2)
Complete the design, coding and commissioning work, complete the experiment report.
Note: The entry with the * * number indicates the selection.
2. Experimental environment
Turbo C can be chosen as the development environment. You can also choose a visual environment such as VB,CB under Windows, which makes it easy to use various controls. Choose the experimental environment independently.
3. Experimental principle and core algorithm reference program segment
Dynamic precedence (The priority number is reduced only):
Third, test methods, procedures and results
1. source program Name: Compressed package file (experiment Iii. rar) source program name (procnque.c)
executable program Name: ProcNQue.exe
2. principle Analysis and Flowchart
Storage structure: Linked list structure;
Main algorithm: first come first service
Key functions: void sort ()/* process to prioritize functions */
void input ()/* Build Process Control block function */
void check ()/* Displays all process state functions */
void running ()/* Run function. Determine if the completion is complete, undo it, or set the ready state and insert the ready queue */
void Cteatpdisp ()/* Displays (during operation) the process in all ready queues after adding a new process */
3. Main procedural paragraphs and their explanations:
/*
NAME:PROCNQUE.C Process Scheduling simulation experiment source storage structure chain list
Description:
Implement a multilevel feedback queue scheduling algorithm with n-level queues.
1. The program achieves a level 3
#include "stdio.h"
#include <stdlib.h>
#include <conio.h>
#define GETPCH (Type) (type*) malloc (sizeof (type))
#define N 3
struct PCB {/* Define Process Control block PCB */
Char name[10];
char status;
int prio;
int ntime;
int rtime;
struct pcb* link;
}*ready=null,*p;
typedef struct PCB PCB;
Sort ()/* process to prioritize functions */
{
PCB *first, *second;
int insert=0;
if ((ready==null) | | | ((P->prio) > (Ready->prio))) /* Priority Max, insert team First */
{
p->link=ready;
Ready=p;
}
else/* Process compare priority, insert in the appropriate location */
{
First=ready;
second=first->link;
while (Second!=null)
{
if ((P->prio) > (Second->prio))/* If the insert process is larger than the current process priority, */
{/* inserted before the current process */
p->link=second;
first->link=p;
Second=null;
Insert=1;
}
else/* Inserts a process with the lowest priority number, inserted at the end of the queue */
{
first=first->link;
second=second->link;
}
}
if (insert==0) first->link=p;
}
}
Input ()/* Build Process Control block function */
{
int i,num;
/*CLRSCR (); */* Clear screen */
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 input process name:");
scanf ("%s", p->name);
/*printf ("\ n Input process priority number:");
scanf ("%d", &p->prio); */
p->prio=n;
printf ("\ n input process run time:");
scanf ("%d", &p->ntime);
printf ("\ n");
P->rtime=0;p->status= ' R ';
p->link=null;
Sort (); /* Call the Sort function */
}
}
int space ()//function?
{
int l=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");
}
void Printbyprio (int prio)
{
pcb* PR;
Pr=ready;
The Ready process for printf ("\ n * * * * Current level%d queue (priority%d): \ n", (n+1)-prio,prio); /* Show ready queue status */
printf ("\ n QName \tstatus\t prio \tndtime\t runtime \ n");
while (Pr!=null)
{
if (Pr->prio==prio) disp (PR);
pr=pr->link;
}
}
Check ()/* Displays all process state functions */
{
pcb* PR;
int i;
printf ("\ n/\\/\\/\\/\\ currently running process is:%s", p->name); /* Displays the current running process */
printf ("\ n 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);
/*
while (Pr!=null)
{
DISP (PR);
pr=pr->link;
}
*/
}
Destroy ()/* Process undo function (Process run end, undo process) */
{
printf ("\ n process [%s] completed. \ n", p->name);
Free (p);
}
Running ()/* Run the function. Determine if the completion is complete, undo it, or set the ready state and insert the ready queue */
{
int slice,i;
The function of Slice=1;/slice is to record the scheduling algorithm that needs to run time.
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 */
}
}
void Cteatpdisp ()
/* Show (during running) the process in all ready queues after adding a new process */
{
int i;
printf ("\ n" after adding a new process, all the processes in the ready queue (no running process at this time): \ n "); /* Show ready queue status */
for (i=n;i>=1;i--)
Printbyprio (i);
}
void CREATP ()
{
char temp;
printf ("\ncreat One more Process?type Y (yes)");
scanf ("%c", &temp);
if (temp== ' Y ' | | temp== ' Y ')
{
Input ();
Cteatpdisp ();
}
}
Main ()/* primary function */
{
int len,h=0;
Char ch;
Input ();
Len=space ();
while ((len!=0) && (ready!=null))
{
Ch=getchar ();
/*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 has completed. \ n");
Ch=getchar ();
Ch=getchar ();
}
4. operation results and Analysis
This experiment is more difficult, do not know how to start, but also more difficult to complete the relevant content, process scheduling and job scheduling have some similarities, but there are many different, but also more difficult.
Experimental three-process scheduling simulation program