Experimental three-process scheduling simulation Program 2.0

Source: Internet
Author: User

Experiment Three Process Scheduling Simulation program 2.0

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 contents and requirements of the experiment

Design a process scheduling simulator with n processes executing concurrently.

1. Simulation of process data generation

Allows the user to specify the number of jobs (2-24) and the default value is 5.

Allows the user to choose to enter the time of arrival of each process, the elapsed time, and the elapsed time of the process in time slices.

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

2.4 Simulation Data Results analysis: Comparing the average turnaround time and turnover coefficient of each algorithm in the same set of simulation data.

Test methods, steps and Results

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.

#include <stdio.h>
#include <time.h>
#include <windows.h>

struct job
{
Char name[10]; Program Name
char status;
int id;
int ARRT; Arrival time
int RunT; Run time
int StarT; Start time
int Finisht; End time
int best; Priority level
float Tatime,tawtime;
float RP; Response ratio
}JOB[24];
int Pseudo_random_number ()
{
int i,n;
Srand ((unsigned) time (0));
The parameter seed is the seed of Rand (), which is used to initialize the starting value of rand ().
Enter the number of jobs
N=rand ()%23+5;
for (i=0; i<n; i++)
{
job[i].id=i+1;
Job Arrival time
Job[i].arrt=rand ()%29+1;
Job run time
Job[i].runt=rand ()%7+1;
}
printf ("\ n" the time required for the job to run when the ID job arrives);
for (i=0; i<n; i++)
{
printf ("\n%3d%12d%15d", Job[i].id,job[i].arrt,job[i].runt);
}
return n;

}

Void sort (struct job Temp[24],int num)
{
    int i;
    Int J;
    struct Job k;
    for (i=0;i<num-1;i++)
    {
         for (j=i+1;j<num;j++)
        {
            if (TEMP[J].ARRT<TEMP[I].ARRT)
             {
                k = Temp[j];
               temp[j] = temp[i];
               temp[i] = k;
           }
       }
   }
}

void FCFS (struct job temp[24],int num)
{
int i=0;
float sumtatime=0;
float avetatime=0;
printf ("┬┬┬┬┬┬┬┬┬ first come first service algorithm fcfs┬┬┬┬┬┬┬┬┬\n");
Sort (temp,num);
Temp[i].start = TEMP[I].ARRT;
Temp[i].finisht = Temp[i].start + temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
for (i=1;i<num;i++)
{
Temp[i].start = Temp[i-1].finisht;
Temp[i].finisht = Temp[i].start +temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Temp[i].rp=temp[i]. Tatime/temp[i].runt;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
}


printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", TEMP[I].NAME,TEMP[I].ARRT,
Temp[i].runt,temp[i].start,temp[i].finisht,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);

}

void SJF (struct job temp[24],int num)
{

int i=0;
Int J;
struct Job k;
float sumtatime=0;
float avetatime=0;
printf ("┬┬┬┬┬┬┬┬┬ Shortest job Priority algorithm sjf┬┬┬┬┬┬┬┬┬\n");
Sort (temp,num);
Temp[i].start = TEMP[I].ARRT;
Temp[i].finisht = Temp[i].start + temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
for (i=1;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (Temp[j].runt<temp[i].runt)
{
K=TEMP[J];
Temp[j]=temp[i];
Temp[i]=k;
}
}
}
for (i=1;i<num;i++)
{
Temp[i].start = Temp[i-1].finisht;
Temp[i].finisht = Temp[i].start +temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
}


printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", TEMP[I].NAME,TEMP[I].ARRT,
Temp[i].runt,temp[i].start,temp[i].finisht,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);

}

void Hrrn (struct job temp[24],int num)
{
int i=0;
Int J;
struct Job k;
float sumtatime=0;
float avetatime=0;
printf ("┬┬┬┬┬┬┬┬┬ Shortest job Priority algorithm hrrf┬┬┬┬┬┬┬┬┬\n");
Sort (temp,num);

Temp[i].start = TEMP[I].ARRT;
Temp[i].finisht = Temp[i].start + temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
for (i=1;i<num;i++)
{
Temp[i].start = Temp[i-1].finisht;
Temp[i].finisht = Temp[i].start +temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Temp[i].rp=temp[i]. Tatime/temp[i].runt;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
}
for (i=1;i<num-1;i++)
{
for (j=i+1;j<num;j++)
{
if (TEMP[J].RP&LT;TEMP[I].RP)
{
K=TEMP[J];
Temp[j]=temp[i];
Temp[i]=k;
}
}
}
for (i=1;i<num;i++)
{
Temp[i].start = Temp[i-1].finisht;
Temp[i].finisht = Temp[i].start +temp[i].runt;
Temp[i]. Tatime = TEMP[I].FINISHT-TEMP[I].ARRT;
Temp[i].rp=temp[i]. Tatime/temp[i].runt;
Sumtatime+=temp[i]. Tatime;
Avetatime+=temp[i]. Tatime/temp[i].runt;
}

printf ("Job name arrival time CPU time start time end time turnaround time \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t%d\t%d\t%d\t%f\n", TEMP[I].NAME,TEMP[I].ARRT,
Temp[i].runt,temp[i].start,temp[i].finisht,temp[i]. Tatime);
}
printf ("Average turnaround time =%f\n", sumtatime/num);
printf ("Average turnaround time =%f\n", avetatime/num);
}

int main ()
{
int x;
int num;
int i;
printf ("┏----------------------┓\n");
printf ("┣1. Random numbers Generate data------┫\n");
printf ("┣2. Self-input analog data----┫\n");
printf ("┗----------------------┛\n");
printf ("Please select menu item:");
scanf ("%d", &x);
if (x==1)
{
Num=pseudo_random_number ();
}
else if (x==2)
{
printf ("Number of programs:");
scanf ("%d", &num);
printf ("\ n");
for (i = 0;i<num;i++)
{
printf ("%d programs: \ n", i+1);
printf ("Input program Name:");
scanf ("%s", &job[i].name);
printf ("Arrival time:");
scanf ("%d", &AMP;JOB[I].ARRT);
printf ("Run Time:");
scanf ("%d", &job[i].runt);
printf ("Priority:");
scanf ("%d", &job[i].best);
printf ("\ n");
}
printf ("The queue is not reached \ n" After sorting by arrival time);
printf ("Id\t arrival time \ t run time \ t priority \ n");
for (i=0;i<num;i++)
{
printf ("%s\t%d\t\t%d\t\t%d\n", job[i].name,job[i].arrt,job[i].runt,job[i].best);
}

}
while (1)
{
printf ("\ n");
printf ("┏-----------------------------┓\n");
printf ("┣0. Exit algorithm scheduling---------------┫\n");
printf ("┣1. FCFS algorithm scheduling---------------┫\n ");
printf ("┣2. SJF algorithm scheduling----------------┫\n ");
printf ("┣3. HRRF algorithm scheduling---------------┫\n ");

printf ("┣4. RR algorithm scheduling---------------┫\n ");

printf ("┗-----------------------------┛\n");
printf ("Please enter menu item:");
scanf ("%d", &x);
if (x==1)
{
FCFS (Job,num);
}
else if (x==2)
{
SJF (Job,num);
}
else if (x==3)
{
Hrrn (Job,num);
}
else if (x==0)
{
Exit (0);
}

Return0;

}

}

Test results:

Iv. Summary of the experiment

    1. In the operating system, system scheduling is very important, learning process scheduling for our future study has a great help.
    2. Among the various scheduling algorithms, FCFS is the simplest scheduling algorithm, but it only considers the job waiting time to neglect the job calculation time, which is advantageous to the long job and not to the short job.
    3. The FJS short job first algorithm improves the FCFS algorithm, improves the average turnaround time and the average turnaround time, but it is very unfavorable to the long operation, and it may not be executed for a long time.

HRRF algorithm is between the FJF and FCFS algorithm between the compromise algorithm, both to consider the left job running time, not only to take care of short jobs and not long-term job waiting time too long, its disadvantage is that each time the calculation of the response of the job than there is a certain time overhead, the time is worse than the SJF.

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.