Short job priority scheduling algorithm

Experiment content:

The simulation realizes the dynamic high priority priority (if the value is larger and the priority is higher, the priority of each time unit is - N; if the value is smaller and the priority is higher, the priority of each time unit is not + n), as follows:

Set process body: process name, process arrival time, service time, initial priority, process status (W-wait, R-run, F-finish), inter process link pointer

Process initialization: the user enters the process name, service time and initial priority for initialization. At the same time, the status of the initialization process is W.

Display function: display before, during and after process scheduling.

Sort function: sort the processes in ready state by priority. The process with the same priority and earlier waiting time is ahead. Pay attention to the arrival time

Scheduling function: scheduling the process with the highest priority from the waiting queue, and the status changes. And after executing a time unit, the priority changes, the service time changes and the status changes. When the service time is 0, the status changes to F.

Delete function: undoes the process with state F.

Experimental requirements:

  1. Test data can then be entered or read from a file.
  2. The arrival time of the process must be taken into account
  3. Finally, the turnover time of each process can be calculated.

 

Operating system experiment. C + + priority queue implementation, turnaround time or other sum can be their own home, the results are calculated~~~

/*
Implementation of short job priority scheduling algorithm
*/
#include<bits/stdc++.h>
using namespace std;

struct job
{
    int id;//Job ID
    int StartTime;//start time
    int ServerTime;//service time
    int runtime;//Running time
    int finsihTime;//Completion time
} j[100001];

bool operator <(const job& a,const job& b)//Overloaded priority queue, the one with short service time is first, if equal, the one with first arrival is first
{
    if(a.ServerTime == b.ServerTime)
        return a.StartTime>b.StartTime;

    return a.ServerTime>b.ServerTime;
}

bool cmp(job a,job b)
{
    return a.id<b.id;
}

priority_queue<job> readQueue;//Preparation queue

void updateFinishTime(int id,int time)
{
    for(int i=0; ; i++)
    {
        if(j[i].id == id)
        {
            j[i].finsihTime = time;
            break;
        }
    }
}
int main()
{
    int n;
    // int time = 0;
    printf("Enter the number of processes:");
    scanf("%d",&n);
    printf("Enter the id,Arrival time, service time\n");
    for(int i=0; i<n; i++)
    {
        scanf("%d %d %d",&j[i].id,&j[i].StartTime,&j[i].ServerTime);
        j[i].runtime = 0;
        //time += j[i].ServerTime; / / time required for all jobs to complete
    }
    int cpu = 0;//CPU status, 0 for idle, 1 for running
    sort(j,j+n,cmp);
    int nowJob = 0;//Number of jobs arrived
    job text;//Jobs at the top of the queue
    for(int i=0; ; i++)//From 0 to finish
    {
        if(i == j[nowJob].StartTime)//Press in the preparation queue when a job arrives
        {
            readQueue.push(j[nowJob]);
            nowJob++;
        }
        if(cpu == 1)//Processor is running
        {
            text.runtime++;
            if(text.runtime == text.ServerTime)
            {
                cpu = 0;//CPU set idle
                updateFinishTime(text.id,i);
            }
        }
        if(cpu == 0)//Processor idle
        {
            cpu = 1;//Processor in working state
            if(!readQueue.empty())
            {
                text = readQueue.top();//Get the job with the shortest service time from the preparation queue
                readQueue.pop();
            }
        }
        if(readQueue.empty() && nowJob == n)
        {
            updateFinishTime(text.id,i+text.ServerTime);
            break;
        }
    }
    for(int i=0; i<n; i++)
    {
        printf("The first%d End time of processes:%d\n",i, j[i].finsihTime);
    }
    return 0;
}

 

Posted on Fri, 29 Nov 2019 22:03:42 -0800 by parijat_php