Experiment two, Process scheduling simulation Program 1.0

Source: Internet
Author: User

Experiment two, Process scheduling simulation Program 1.0

First, Experimental Purpose

C Language Simulation Process Scheduler, in order to deepen the concept of process and process scheduling algorithm understanding.

Second, experimental content and requirements

Design a process scheduling simulator with n processes executing concurrently.

Process scheduling algorithm:

(1) First come first service scheduling algorithm, that is, according to the order of operation arrived scheduling. The job that waits the longest in the system is always dispatched first.

(2) Priority scheduling algorithm, set a priority for each job, before scheduling compare the priority of each job, priority is higher priority scheduling, if the same priority is the first to first service 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 required run time can be specified in advance, and the running time of the process is calculated in time slices.

3) The status 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 acquiring the CPU. Represented by the elapsed CPU time plus.

5) If the elapsed CPU time of the process has reached the required 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, lower one level) Then insert it into the ready queue for scheduling.

6) Each time the scheduler is run, the PCB of each process in the ready queue is printed for inspection.

7) Repeat the process until the process is complete.

Third, experimental methods and results test

(1) First come first service scheduling algorithm:

#include <stdio.h>

#include <stdlib.h>

typedef struct PCB {

Char name[30];//process Name

Float arrivetime;//Arrival Time

Float servetime;//Service Hours

Float finishtime;//Completion Time

} FCFS;

struct PCB a[100];

struct PCB *sortarrivetime (struct PCB a[],int n);//Arrival time Bubble sort

void Fcfs (struct PCB a[],int n);//FCFS algorithm

Bubble Sort by arrival time

struct PCB *sortarrivetime (struct PCB a[],int N) {

int i,j;

struct PCB t;

int flag;

for (I=1; i<n; i++) {

flag=0;

for (j=0; j<n-i; J + +) {

if (a[j].arrivetime>a[j+1].arrivetime) {//will arrive at a short time to the front of the interchange

T=A[J];

A[J]=A[J+1];

a[j+1]=t;

flag=1;//Exchange

}

}

if (flag=0) {//If no interchange occurs in a trip, the sort ends

Break

}

}

Return a;//returns the sorted process array

}

First come first service algorithm

void Fcfs (struct PCB a[],int N) {

int i;

a[0].finishtime=a[0].arrivetime+a[0].servetime;//Completion time = arrival Time + Service time

for (I=1; i<n; i++) {

if (a[i].arrivetime<a[i-1].finishtime) {//The current arrival time is before the last job end time

a[i].finishtime=a[i-1].finishtime+a[i].servetime;//completion time = Last finish time + service time

} else {

A[i].finishtime=a[i].arrivetime+a[i].servetime;

}

}

printf ("Process name \ t arrival time \ t service time \ t completion time \t\n");

for (i=0; i<n; i++) {

printf ("%s\t", a[i].name);

printf ("\t%f", a[i].arrivetime);

printf ("\t%f", a[i].servetime);

printf ("\t%f", a[i].finishtime);

printf ("\ n");

}

}

Main () {

int n,i;

printf ("\ n ************* process scheduling algorithm (first come first service algorithm) ************\n\n");

printf ("Please enter the number of processes:");

scanf ("%d", &n);

printf ("\ n");

for (i=0; i<n; i++) {

printf ("The name of the%d process:", i+1);

scanf ("%s", &a[i].name);

printf ("The arrival time of the%d process:", i+1);

scanf ("%f", &a[i].arrivetime);

printf ("Service Hours for%d processes:", i+1);

scanf ("%f", &a[i].servetime);

printf ("\ n");

}

Sortarrivetime (a,n);//Bubble sort

FCFS (a,n);//First come first service algorithm

}

Test results:

(2) priority scheduling algorithm:

#include <stdio.h>

#include <stdlib.h>

#define MAX 100

/* Define Process structure body */

typedef struct PCB {

Char name[30];//process Name

int priority;//Process precedence number

int servetime;//Process Arrival time

int cputime;//process consumes CPU time

Char state;//process State

} PCB;

Input (PCB p[max],int N) {

int i;

for (i=0; i<n; i++) {

printf ("The name of the%d process:", i+1);

scanf ("%s", &p[i].name);

printf ("Number of priority%d processes:", i+1);

scanf ("%d", &p[i].priority);

printf ("Service Hours for%d processes:", i+1);

scanf ("%d", &p[i].servetime);

P[i].state= ' W ';

p[i].cputime=0;

printf ("\ n");

}

}

Output (PCB p[max],int N) {

int i;

printf ("Process name \ t Precedence series \ t service time \ t current time \ t \ process state \ n");

for (i=0; i<n; i++) {

printf ("%s\t", p[i].name);

printf ("\t%d\t", p[i].priority);

printf ("\t%d\t", p[i].servetime);

printf ("\t%d\t", p[i].cputime);

printf ("\t%c\t", p[i].state);

printf ("\ n");

}

}

Prioritysort (PCB p[max],int N) {

int m=0,i=0,j;

PCB temp;

m = n;

for (i = 1; i < n; i++) {

m = m-1;

for (j = 0; J < m; J + +) {

if (P[j].priority < p[j+1].priority) {

temp = P[j];

P[J] = p[j+1];

P[J+1] = temp;

}

}

}

}

Run (PCB P[max], int n) {

int I, J;

int m=0;

for (i = 0; i < n; i + +) {

m = p[i].servetime+m;

}

for (i = 0; i < n; i + +) {

for (j = 0; J < m; J + +) {

printf ("Press ENTER to continue running ...! \ n ");

GetChar ();

p[i].priority--;

p[i].cputime++;

p[i].servetime--;

P[i].state= ' R ';

Prioritysort (P,n);

Output (P,n);

if (p[i].servetime!=0) {

P[i].state= ' R ';

} else {

P[i].state= ' F ';

printf ("******** of%d processes completed!********\n", i+1);

Break

}

}

}

}

End (PCB P[max], int n) {

int I, J;

for (i = 0; i < n; i + +) {

if (p[i].servetime==0) {

P[i].state= ' F ';

}

}

Output (P,n);

printf ("******** Process dispatch End!********\n", i+1);

}

Main () {

PCB P[max];

int n,i;

printf ("\ n ************* process scheduling algorithm (priority scheduling algorithm) ************\n\n");

printf ("Please enter the number of processes:");

scanf ("%d", &n);

printf ("\ n");

Input (P,n);

Prioritysort (P,n);

Output (P,n);

GetChar ();

Run (P,n);

End (P,n);

}

Test results:

Four, Experimental Summary

1, in general, process scheduling is not difficult to understand, but quite simple, but the program language simulation of its process is a bit difficult;

2, although these two process scheduling algorithm can be regarded as a simple simulation of the effect, but there are many needs to improve.

Experiment two, Process scheduling simulation Program 1.0

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.