Experimental three-process scheduling simulation program

Source: Internet
Author: User

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

Design a process scheduling simulator 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. 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.

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

Third, test methods, procedures and results

1. source program Name: Compressed package file (rar or zip) source program name experiment three Zy. C

executable name: Experiment three Zy.exe

2. principle Analysis and Flowchart

Storage structure: Represents a process with a struct data type, struct members include: The process arrives at the system time Arrivetime, the run time needtime, the precedence priority has occupied CPU time Usetime

3. The main Program section and its interpretation:

#include <stdio.h> #include <string.h>void arrivesort (struct PCB pcbs[10],int n); void result (struct PCB PCBs    [10],int n]; void Prioritysort (struct PCB pcbs[10],int n); struct pcb{char name[10];         int priority;      Process priority int Arrivetime;        Process arrival time int needtime;         Process requires run time int usetime; Process has consumed CPU time};    void Main () {struct PCB pcbs[10]; int n,i;    N Processes printf ("Number of input processes:", n);    scanf ("%d", &n);    printf ("Enter the process name, priority, arrival time, run time: \ n" for each process);        for (i=0;i<n;i++) {scanf ("%s", pcbs[i].name);        scanf ("%d", &pcbs[i].priority);        scanf ("%d", &pcbs[i].arrivetime);        scanf ("%d", &pcbs[i].needtime);    pcbs[i].usetime=0;         } for (i=0;i<n;i++) {prioritysort (pcbs,n);          Arrivesort (Pcbs,n);      pcbs[i].usetime++;      printf ("\ n");            printf ("Dynamic priority scheduling process run sequence: \ n");      Result (Pcbs,n);      pcbs[i].priority--;    pcbs[i].usetime++;  }}void arrivesort (struct PCB pcbs[10],int N) { int i=0;   Char temp[10];               int min; for (i=0;i<n-1;i++) {if (pcbs[i].arrivetime>pcbs[i+1].arrivetime) {min=pcbs[i].a              Rrivetime;              Pcbs[i].arrivetime=pcbs[i+1].arrivetime;              Pcbs[i+1].arrivetime=min;              Min=pcbs[i].needtime;              Pcbs[i].needtime=pcbs[i+1].needtime;                           Pcbs[i+1].needtime=min;              min=pcbs[i].priority;              pcbs[i].priority=pcbs[i+1].priority;              Pcbs[i+1].priority=min;              strcpy (Temp,pcbs[i].name);              strcpy (Pcbs[i].name,pcbs[i+1].name);          strcpy (pcbs[i+1].name,temp);    }}//Sort by process arriving at system time, first arriving at top}void result (struct PCB pcbs[10],int n) {int i;     printf ("Process name \ t-priority \ t-arrival time \ t run time \ t elapsed CPU time \ n"); for (i=0;i<n;i++) {printf ("%s\t%d\t%d\t%d\t%d\n", PCBS[I].NAME,PCBS[I].PRIORITY,PCB S[i].arrivetime,pcbs[i].needtime,pcbs[i].usetime);   }}void prioritysort (struct PCB pcbs[10],int n) {int i=0;   Char temp[10];               int min; for (i=0;i<n;i++) {if (pcbs[i].priority<pcbs[i+1].priority) {Min=pcbs[i].priorit              Y              pcbs[i].priority=pcbs[i+1].priority;              Pcbs[i+1].priority=min;              Min=pcbs[i].arrivetime;              Pcbs[i].arrivetime=pcbs[i+1].arrivetime;              Pcbs[i+1].arrivetime=min;              Min=pcbs[i].needtime;              Pcbs[i].needtime=pcbs[i+1].needtime;              Pcbs[i+1].needtime=min;              strcpy (Temp,pcbs[i].name);              strcpy (Pcbs[i].name,pcbs[i+1].name);                           strcpy (pcbs[i+1].name,temp);           pcbs[i].usetime++; }//Sort by process priority, highest in front}}

4. operation results and Analysis

Algorithm is still flawed, the main idea is to prioritize the process first, and then according to the order of arrival time to select the highest priority process to run a time slice (such as time slice is 1), the corresponding process consumes CPU time plus 1, the corresponding priority minus 1, and then priority from high to low order, Select the highest priority process scheduling, if the two processes have the same priority, then the first-come-first-serve algorithm, first-arrived scheduling.

Four, Experimental Summary

Experience: The idea of the algorithm can be clear, but the process of writing code is still encountering a lot of problems, such as the number of cycles scheduled to run will not be controlled, the CPU time per scheduling process plus 1, do not know how to express in code, and where the code should be placed in the loop body, or the loop body outside, is not familiar with the code mastery of C, either before or after priority sorting.

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.