Experimental three-process scheduling simulation program

Source: Internet
Author: User

1.Purpose and requirements

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

1.2. Experimental 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.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.

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.

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

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

4. Experiment principle and core algorithm reference program segment

Dynamic precedence (The priority number is reduced only):

SOURCE program:

#include "stdio.h"
#include "Stdlib.h"
#include "string.h"
typedef struct NODE
{
Char name[10]; Process identifier
int prio; Process priority number
int CPUTime; Process consumes CPU time
int needtime; The time it takes for the process to complete
Char state; Status of the process
struct node *next; Chain Pointers
}PCB;
PCB *finish,*ready,*tail,*run; Queue pointers
int N; Number of processes
Put the first process of the ready queue into operation
Firstin ()
{
Run=ready; The ready queue header pointer is assigned to the running head pointer
Run->state= ' R '; Process state changes to run mode
ready=ready->next; Ready column header pointer moves back to the next process
}//Title output function
void Prt1 (char a)
{
printf ("Process number CPU time takes precedence state \ n");

}
Process PCB Output
void Prt2 (char a,pcb *q)
{//Priority number algorithm output
printf ("% -10s% -10d% -10d% -10d%c\n", q->name,q->cputime,q->needtime,q->prio,q->state);
}
Output function
void prt (char algo)
{
PCB *p;
Prt1 (ALGO); Output title
if (run!=null)//If the run header pointer is not empty
Prt2 (Algo,run); Output the currently running PCB
P=ready; Output Ready Queue PCB
while (P!=null)
{
Prt2 (ALGO,P);
p=p->next;
}
P=finish; PCB for Output Completion queue
while (P!=null)
{
Prt2 (ALGO,P);
p=p->next;
}
GetChar (); Press any key to continue
}
Algorithm insertion algorithm for precedence number
Insert1 (PCB *q)
{
PCB *p1,*s,*r;
int b;
s=q; PCB pointer to be inserted
P1=ready; Ready Queue Header pointer
R=P1; R the precursor of the P1.
B=1;
while ((p1!=null) &&b)//Determine the insertion position based on the priority number
if (P1->prio>=s->prio)
{
R=P1;
p1=p1->next;
}
Else
b=0;
if (R!=P1)//If the conditions of incorporation are inserted between R and P1
{
r->next=s;
s->next=p1;
}
Else
{
s->next=p1; Otherwise, insert the header in the ready queue
Ready=s;
}
}
Priority number Create initial PCB information
void Create1 (char alg)
{
PCB *p;
int i,time;
Char na[10];
Ready=null; Ready Queue Header File
Finish=null; Complete the queue header file
Run=null; Run the queue header file
printf ("Enter the process number and run time: \ n"); Enter process flag and time required to create PCB
for (i=1;i<=n;i++)
{
p= (PCB *) malloc (sizeof (PCB));
scanf ("%s", NA);
scanf ("%d", &time);
strcpy (P->name,na);
p->cputime=0;
p->needtime=time;
P->state= ' W ';
p->prio=50-time;
if (ready!=null)//Ready queue is not empty, call insert function to insert
Insert1 (P);
Else
{
p->next=ready; Create a ready queue for the first PCB
Ready=p;
}
}
CLRSCR ();
printf ("Priority number algorithm output information: \ n");
printf ("***********************************************\n");
PRT (ALG); Output process PCB Information
Run=ready; Put the first process of the ready queue into operation
ready=ready->next;
Run->state= ' R ';
}
Priority number scheduling algorithm
void priority (char ALG)
{
while (Run!=null)//When the running queue is not empty, a process is running
{
run->cputime=run->cputime+1;
run->needtime=run->needtime-1;
run->prio=run->prio-3; Reduce the number of 3 units per run priority
if (run->needtime==0)//If the required time is 0 insert it into the completion queue
{
run->next=finish;
Finish=run;
Run->state= ' F '; State is complete
Run=null; Run queue header pointer is empty
if (ready!=null)//If the ready queue is not empty
Firstin (); Put the first process of the ready queue into operation
}
else//does not run out and the priority number is not the maximum, then it is inserted into the ready queue
if ((ready!=null) && (Run->prio<ready->prio))
{
Run->state= ' W ';
Insert1 (run);
Firstin (); Put the first process of the ready queue into operation
}
PRT (ALG); Output process PCB Information
}
}
Main function
void Main ()
{
Char Algo; Algorithm tags
CLRSCR ();

printf ("Number of input processes: \ n");
scanf ("%d", &n); Number of input processes
Create1 (ALGO); Priority number algorithm
Priority (ALGO);
}

Experience: Because there is a previous experiment, so the difficulty of doing this experiment is not very big, after all, or less practice, strengthen the practice is essential

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.