Experimental three-process scheduling simulation Program 2.0

Source: Internet
Author: User

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, the experimental requirements

Design a process scheduling simulator with n processes executing concurrently.

1. Simulation process data generation, allows the user to choose to enter the time required for each process, the running time of the process in time slices units.

2. Function of the simulator scheduler

2.1 According to the arrival time and required running time of the simulated data, the following scheduling algorithms can be executed separately.

FCFS

Sj

Hrrn

Rr

2.2 Shows the scheduling execution sequence for each of the processes under each algorithm.

2.3 Calculate the start execution time of each process, the completion time of each job, the turnaround time and the right turnaround time (turnover factor).

Third, the experimental explanation

1) first come first service (FCFS) 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) Short job first (SJF) scheduling algorithm, priority scheduling requires the shortest running time of the job.

3) in response to high-priority (HRRN) scheduling algorithm, set a priority (response ratio) for each job, before scheduling to calculate the priority of each job, priority of the higher priority scheduling. RP (response ratio) = job turnaround time/job run time =1+ job wait time/job run time.

4) Time slice rotation (RR) scheduling algorithm: Each time the scheduler allocates the CPU to the ready queue the first process uses a single timestamp, and each process in the ready queue runs a time slice in turn. When this time slice is finished, force a process to let the processor out of the queue and wait for the next round of scheduling.

Four, the program code:

#include <stdio.h> #define TIME int#define max 24typedef struct queue{char name; int Intime;        int needtime;  int runningtime; float priority;int Waitingtime;char state;int starttime;int finishtime;float turntime;float turnnumber;} PCB; int N;int ptime;    PCB pcb[max];void addprocess (int n) {int i;for (i=0;i<n;i++) {printf ("\ n Please enter process name:");    scanf ("%s", &pcb[i].name);    printf ("\ n Please enter the priority of the process:");    scanf ("%f", &pcb[i].priority);    printf ("\ n Please enter the time required for the process:");    scanf ("%d", &pcb[i].needtime);    Pcb[i].intime=i; Pcb[i].state= ' W ';//Initialize the state of the process to the waiting state pcb[i].runningtime=0;}} void sort () {int i,j; The PCB temp;//the highest priority process by sorting to the front for (i=0;i<n-1;i++) {for (j=n-2;j>=i;j--) {if (Pcb[j+1].priori               ty>pcb[j].priority) {temp=pcb[j];              PCB[J]=PCB[J+1];       Pcb[j+1]=temp;                }}}if (pcb[0].state!= ' F ') {pcb[0].state= ' R ';       Place the highest priority state to run}}void Print () {int i; printf ("\ nthe process name priority arrival time takes time to use the time process state \ n"); for (i=0;i<n;i++) {printf ("%7s%12f%10d%10d%10d%10c\n", &pcb[i].name,pcb[i].priority, Pcb[i].intim    E,pcb[i].needtime,pcb[i].runningtime,pcb[i].state);       }}void Print1 () {int i;     printf ("\ n" time turnaround time for process name completion time \ n ");             for (i=0;i<n;i++) {printf ("%7s%10d%9d%20f%20f%\n", &pcb[i].name,pcb[i].finishtime,pcb[i].starttime,    Pcb[i].turntime,pcb[i].turnnumber);  }}void Rrattemper ()//dispatch {int i=0,j=0;       do{if (i==n) {i=0;}       if ((pcb[i].needtime-pcb[i].runningtime) >ptime) {pcb[i].runningtime=pcb[i].runningtime+ptime;  Used time to add time slices pcb[i].state= ' W '; i++;        } else {if (i==n) {i=0;}          pcb[i].runningtime=pcb[i].needtime;//has been used time equal to take time pcb[i].state= ' F '; Complete the process, set the status to complete if (i==0) {pcb[i].waitingtime=0;} else{Pcb[i].waitingtIme=pcb[i-1].needtime+pcb[i-1].waitingtime; }pcb[i].starttime=pcb[i].waitingtime;pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime;pcb[i].turntime= (        float) Pcb[i].finishtime-pcb[i].intime; Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime;i++; if (i==n) {i=0;}  if (pcb[i].state== ' F ') {j + +;}         } Print ();    }while (J<n); Print1 ();     } void Fcfsattemper () {int i=0;                Pcb[0].state= ' R '; Set the first arriving state to run do{if (pcb[i].state== ' R ') {pcb[i].runningtime=pcb[i].needtime; if (i==0) {pcb[i].waitingtime=0 ; }else{pcb[i].waitingtime=pcb[i-1].needtime+pcb[i-1].waitingtime;} pcb[i].starttime=pcb[i].waitingtime; Pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime;             Pcb[i].turntime= (float) pcb[i].finishtime-pcb[i].intime; Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime; Pcb[i].state= ' F '; i++; Pcb[i].state= ' R ';  }}while (pcb[n].state!= ' F ');     Print (); Print1 (); } void Sjattemper () {int i,j; PCB temp;//The least-time process by sorting to the front for(i=0;i<n-1;i++)               {for (j=n-2;j>=i;j--) {if (pcb[j+1].needtime<pcb[j].needtime) {temp=pcb[j];              PCB[J]=PCB[J+1];       Pcb[j+1]=temp;  }}} pcb[0].state= ' R ';  The state with the least time is set to run i=0; do{if (pcb[i].state== ' R ') {pcb[i].runningtime=pcb[i].needtime; if (i==0) {pcb[i].waitingtime=0;} else{pcb[i].waitingtime=pcb[i-1].needtime+pcb[i-1].waitingtime;} pcb[i].starttime=pcb[i].waitingtime; Pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime;             Pcb[i].turntime= (float) pcb[i].finishtime-pcb[i].intime; Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime; Pcb[i].state= ' F '; i++; Pcb[i].state= ' R '; }}while (pcb[n].state!= ' F ');         Print (); Print1 (); } void Hrrnattemper () {int i,j,k=1;         PCB temp; Sort (); Pcb[0].runningtime=pcb[0].needtime; Pcb[1].waitingtime=pcb[0].needtime; Pcb[0].state= ' F ';    for (i=1;i<n;i++) {pcb[i].runningtime=pcb[i].needtime; Pcb[i].priority=1+pcb[i].waitinGtime/pcb[i].runningtime;pcb[i+1].waitingtime=pcb[i].needtime+pcb[i].waitingtime;       } for (i=1;i<n-1;i++) {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;       }}} for (i=1;i<n;i++) {pcb[i].runningtime=0;} pcb[1].state= ' R ';  do{if (pcb[k].state== ' R ') {pcb[k].runningtime=pcb[k].needtime; Pcb[k].runningtime=pcb[k].needtime; if (k==0) {pcb[k].waitingtime=0;} else{pcb[k].waitingtime=pcb[k-1].needtime+pcb[k-1].waitingtime;} pcb[k].starttime=pcb[k].waitingtime; Pcb[k].finishtime=pcb[k].waitingtime+pcb[k].runningtime;             Pcb[k].turntime= (float) pcb[k].finishtime-pcb[k].intime; Pcb[k].turnnumber= (float) pcb[k].turntime/pcb[k].needtime; Pcb[k].state= ' F '; k++; Pcb[k].state= ' R '; }}while (pcb[n].state!= ' F '); Print ();      Print1 ();     } int Choose () {int number; printf ("\ n Select the FCFS algorithm, enter 1:\n"); printf ("\ n Select the SJ algorithm, enter 2:\n");     printf ("\ n Select the HRRN algorithm, enter 3:\n"); printf ("\ n Select the RR algorithm, enter 4:\n"); printf ("\ n End Please enter 0:\n"); scanf ("%d", &number); return number;     }main () {int chose; n=0; printf ("Please enter the number of processes:");      scanf ("%d", &n); Addprocess (n); Print (); Chose=choose (); Do{if (chose==1) {fcfsattemper (); Chose=choose ();} if (chose==2) {sjattemper (); Chose=choose ();} if (chose==3) {hrrnattemper (); Chose=choose ();}                    if (chose==4) {printf ("Please enter the value of the time slice:"); scanf ("%d", &ptime); Rrattemper (); Chose=choose ();} if (chose==0) {break;}}        while (1); }

V. Simple description of the program code

(1), define a Process control block PCB:

typedef struct queue{  char name;         int Intime; int needtime;        int runningtime;  

  

(2), the main function of the program:

Main () {int chose;         n=0; printf ("Please enter the number of processes:"); scanf ("%d", &n);         Addprocess (n); Print (); Chose=choose (); Do{if (chose==1) {fcfsattemper (); Chose=choose ();} if (chose==2) {sjattemper (); Chose=choose ();} if (chose==3) {hrrnattemper (); Chose=choose ();} if (chose==4) {printf ("Please enter the value of the time slice:");                     scanf ("%d", &ptime); Rrattemper (); Chose=choose ();} if (chose==0) {break;}} while (1);        }

  

(3), the function of the input process:

void addprocess (int n) {int i;for (i=0;i<n;i++) {        printf ("\ n Please enter process name:");    scanf ("%s", &pcb[i].name);    printf ("\ n Please enter the priority of the process:");    scanf ("%f", &pcb[i].priority);    printf ("\ n Please enter the time required for the process:");    scanf ("%d", &pcb[i].needtime);    pcb[i].intime=i;    Pcb[i].state= ' W ';//Initialize the state of the process to the waiting state    pcb[i].runningtime=0;}}

  

(4) The function of the algorithm selection:

int choose () {int number; printf ("\ n Select the FCFS algorithm, enter 1:\n");         printf ("\ n Select the SJ algorithm input 2:\n");         printf ("\ n Select the HRRN algorithm, enter 3:\n");         printf ("\ n Select the RR algorithm, enter 4:\n"); printf ("\ n End Please enter 0:\n"); scanf ("%d", &number); return number; }

  

(5), first come first service (FCFS) scheduling algorithm:

void Fcfsattemper ()  {
int i=0; Pcb[0].state= ' R '; Set the first arriving state to run do{if (pcb[i].state== ' R ') { pcb[i].runningtime=pcb[i].needtime;if (i==0) {pcb[i]. waitingtime=0;} else{Pcb[i].waitingtime=pcb[i-1].needtime+pcb[i-1].waitingtime;} Pcb[i].starttime=pcb[i].waitingtime;pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime; Pcb[i].turntime= (float) pcb[i].finishtime-pcb[i].intime; Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime;pcb[i].state= ' F '; i++;p cb[i].state= ' R ';} } while (pcb[n].state!= ' F '); Print (); Print1 (); }

  

(6), short job first (SJF) scheduling algorithm:

void Sjattemper ()  {int i,j; The PCB temp;//The least-time process by sorting to the front for (i=0;i<n-1;i++)  {for            (j=n-2;j>=i;j--)    {             if (pcb[j+1]. Needtime<pcb[j].needtime)     {         temp=pcb[j];               PCB[J]=PCB[J+1];              Pcb[j+1]=temp    ;        }}} Pcb[0].state= ' R ';  The state with the least time is set to run       i=0;  Do{if (pcb[i].state== ' R ') {        pcb[i].runningtime=pcb[i].needtime;if (i==0) {        pcb[i].waitingtime=0;} else{        Pcb[i].waitingtime=pcb[i-1].needtime+pcb[i-1].waitingtime;} Pcb[i].starttime=pcb[i].waitingtime;pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime;pcb[i].turntime= ( float) pcb[i].finishtime-pcb[i].intime;        Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime;        Pcb[i].state= ' F '; i++; Pcb[i].state= ' R '; }  }while (pcb[n].state!= ' F '); Print ();         Print1 (); }

  

(7), response to high-priority (HRRN) scheduling algorithm:

void Hrrnattemper () {int i,j,k=1;         PCB temp; Sort (); Pcb[0].runningtime=pcb[0].needtime; Pcb[1].waitingtime=pcb[0].needtime; Pcb[0].state= ' F ';        for (i=1;i<n;i++) {pcb[i].runningtime=pcb[i].needtime; Pcb[i].priority=1+pcb[i].waitingtime/pcb[i].runningtime;pcb[i+1].waitingtime=pcb[i].needtime+pcb[i]. Waitingtime; } for (i=1;i<n-1;i++) {for (j=n-2;j>=i;j--) {if (Pcb[j+1].priority>pcb[j].prior                  ity) {temp=pcb[j];                 PCB[J]=PCB[J+1];       Pcb[j+1]=temp;          }}} for (i=1;i<n;i++) {pcb[i].runningtime=0;} pcb[1].state= ' R ';     do{if (pcb[k].state== ' R ') {pcb[k].runningtime=pcb[k].needtime;     Pcb[k].runningtime=pcb[k].needtime;      if (k==0) {pcb[k].waitingtime=0; }else{Pcb[k].waitingtime=pcb[k-1].needtime+pcb[k-1].waitingtime;}        Pcb[k].starttime=pcb[k].waitingtime; Pcb[k].finishtime=pcb[k].waitingtime+pcb[k].runningtime;p cb[k].turntime= (float) pcb[k].finishtime-pcb[k].intime; Pcb[k].turnnumber= (float) pcb[k].turntime/pcb[k].needtime; Pcb[k].state= ' F '; k++; Pcb[k].state= ' R '; }}while (pcb[n].state!= ' F '); Print ();      Print1 (); }

  

(8) Time slice rotation (RR) scheduling algorithm:

void Rrattemper ()                           //Dispatch  {  
int i=0,j=0; do{ if (i==n) { i=0; } if ((pcb[i].needtime-pcb[i].runningtime) >ptime) { pcb[i].runningtime=pcb[i].runningtime+ptime; Time Chip pcb[i].state= ' W ' has been used; i++; } else { if (i==n) { i=0;
} pcb[i].runningtime=pcb[i].needtime;//has used time equal to take time pcb[i].state= ' F '; Complete the process, set the status to complete if (i==0) {pcb[i].waitingtime=0;} else{ pcb[i].waitingtime=pcb[i-1].needtime+pcb[i-1].waitingtime;} Pcb[i].starttime=pcb[i].waitingtime;pcb[i].finishtime=pcb[i].waitingtime+pcb[i].runningtime;pcb[i].turntime= ( float) pcb[i].finishtime-pcb[i].intime; Pcb[i].turnnumber= (float) pcb[i].turntime/pcb[i].needtime;i++; if (i==n) {i=0;} if (pcb[i].state== ' F ') {j + +;} } Print ();
}while (j<n); Print1 (); }

  

Vi. results of the experiment:

Figure 1, the input process

Figure 2, first come first service algorithm

Figure 3, Short job first (SJF) scheduling algorithm

Figure 4, Response high priority (HRRN) scheduling algorithm

Figure 5.1, Time slice rotation (RR) scheduling algorithm

Figure 5.2, Time slice rotation (RR) scheduling algorithm

Figure 5.3, Time slice rotation (RR) scheduling algorithm

Seven, the test summary:

Through the scheduling of the simulation process several times, the process in the operating system in the scheduling of more understanding, more profound understanding.

Experimental three-process scheduling simulation Program 2.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.