[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

Preemptive Shortest Job First

Asked by microcoop in C++ Programming Language

Tags: job, shortest, first, preemptive

I am trying to change this to preemptive shortest job first, but I honestly just do not understand what to do. I know that I need to remove the quantum and determine the scheduling based on the next burst length..I think. But I really do not understand it. Can anyone begin to point me in the right direction here. Below is the same code formulated in Preemptive Shortest Job First, except it prompts for a quantum instead of using a formula to figure it out.

#include <iostream>
#include <iomanip>

using namespace std;

// Struct to represent process

struct process {
  int idno;
  int ready;            // start time
  int run;              // run time
  int remaining;        // time remaining
  int active;           // is process active?
  int finish;           // finish time
};

// Primitive class to represent system clock

class myclock
{
  protected:
    int clock99;        // represent time on clock
                        // clock itself is a predefined function name
  public:
    myclock()  {  clock99 = 0; }
    void setclock( int w ) { clock99 = w; }
    void tick() { clock99++; }
    int gettime() { return clock99; }
};

//  This class represents the processor and the list of jobs waiting
//  for the processor

class processor
{
  protected:
    int maxjobs;        // maximum number of jobs
    int jobsub;         // location of next job to be added, also # of jobs
    int current_id;     // subscript of job currently running
    int previous_id;
    int quantum;        // maximum length of time a job may run
    int elapsed;        // time current job has been running
    process * list;     // list of processes that want to use processor

  public:

    // constructor

    processor( int q, int n)
    {
      maxjobs = n;
      jobsub = 0;
      elapsed = 0;
      current_id = -1;
      previous_id = -1;
      quantum = q;
      list = new process[n];
    }

    // place a single job into a queue

    int insertjob( int id, int start, int run )
    {
      if( jobsub < maxjobs )
        {
          list[jobsub].idno = id;
          list[jobsub].ready = start;
          list[jobsub].run = run;
          list[jobsub].remaining = run;
          list[jobsub].active = 0;
          list[jobsub].finish = -1;
          jobsub++;
          return 1;
        }
      else
        return 0;
    }

    //  display all jobs in the queue

    void printlist()
    {
      if( jobsub > 0 )
       for( int i = 0; i < jobsub; i++ )
         {
            cout << list[i].idno << setw(10) << list[i].ready << setw(10)
                 << list[i].run << setw(10) << list[i].finish;
            if( list[i].finish != -1 )
              cout << setw(10) << list[i].finish - list[i].ready << endl;
            else
              cout << endl;
         }


    }

    //  determine if any jobs are left to run

    int jobsleft()
    {
       int temp = 0;
       for( int i=0; i < jobsub ; i++)
         if( list[i].remaining != 0 )
           temp = 1;
       return temp;
     }

     //  make all processes that become ready at the current time active

     void fixready( int t)
     {
       for( int i=0; i < jobsub; i++)
         if( list[i].ready == t)
           list[i].active = 1;
     }

     //  are there any active processes?

     int anyactive()
     {
        int temp = 0;
        for( int i = 0; i < jobsub; i++)
          if( list[i].active )
            temp = 1;
        return temp;
      }


    //  actual work of the simulation, checks quantum, performs context
    //  switch if necessary, and updates statistics


void check( int t )
{
  if( current_id != -1 )
    {
        if( list[current_id].remaining == 0 )
          {
                list[current_id].finish = t;
                list[current_id].active = 0;
                previous_id = -1;
                current_id = -1;
                elapsed = 0;
          }
        if( elapsed == quantum)
          {
             previous_id = current_id;
             current_id = -1;
             elapsed = 0;
          }
      }
      if( current_id == -1 && anyactive() )
      {
         current_id = findmin();
      }
      if( current_id != -1 )
      {
        list[current_id].remaining -= 1;
        elapsed++;
      }
 }



     //  returns the id of the current process

     int getcurrent() { return list[current_id].idno; }

     //  determines the position of the next process to get the processor
     //  this is the only function that needs modification if the
     //  scheduling discipline is changed from SRT

     int findmin()
     {
       int found = 0, smallsub;
       for( int i = 0; i < jobsub; i++)
         if( list[i].remaining != 0 && list[i].active )
           if( found == 0 )
             {
               smallsub = i;
               found = 1;
             }
           else
             if( list[i].active && (list[i].remaining <
                                    list[smallsub].remaining ))
                 smallsub = i;

    //  This code makes sure that a job different from the current one is
    //  chosen in case of a tie.

    if( smallsub == previous_id )
       {
         int countties = 0;
         for( int i = 0; i < jobsub; i++)
           if( list[i].active && list[i].remaining == list[smallsub].remaining )
             countties++;
         if( countties > 1 )
           {
              int j = 0;
              int found = 0;
              while( j < jobsub && !found )
                if( list[j].active && list[j].remaining == list[smallsub].remaining
                                && j != smallsub )
                  found = 1;
                else
                  j++;
              smallsub = j;
           }
       }
   return smallsub;
}


//  Sets finishing time of the last job to finish
//  Not a very pleasing fix to allow the main program to run but it works.

void endsim( int t )
{
  list[current_id].finish = t;

}

};

int main()
{
  myclock timer;                        // clock
  int quantum, t;
  int newid,newstart,newruntime;
  char ch;
  int n;

  cout << "How Many Jobs? ";
  cin >> n;                             // Number of Jobs
  cout << endl << "What Is The Quantum? ";
  cin >> quantum;                       // Quantum

  processor cpu( quantum, n);           // Set up processor

  cout << endl << "Enter ID Number, Start Time, and Run Time For Each Job ";
  cout << endl << "-------------------------------------------------------";
  cout << endl;
  for( int i = 0; i < n; i++)
    {
      cin >> newid >> newstart >> newruntime;
      if( cpu.insertjob( newid, newstart, newruntime ) )
        cout << "Job Inserted Successfully " << endl;
      else
        cout << "Processor Queue Size Limit Exceeded " << endl;
      cout << "-------------------------------------------------------"
           << endl;
    }

  cout << endl << "-------------------------------------------------------"
       << endl;
  cout << "Summary of Data Entered " << endl;
  cout << "-------------------------------------------------------" << endl;

  cpu.printlist();
  cout << "-------------------------------------------------------" << endl;

  cout << endl << endl << "-------------------------------------------------------" << endl;
  cout << "Gantt Chart" << endl;
  cout << "-------------------------------------------------------" << endl;


  while( cpu.jobsleft() )               // continue until time remaining = 0
  {                                     // for all jobs

    t = timer.gettime();                // read clock
    cpu.fixready( t );                  // make processes ready at time t active
    cpu.check( t );                     // do the work of the simulation
    if( cpu.anyactive() )               // display the number of the active
      cout << "|" << cpu.getcurrent();  // processes for the purpose of a trac
    else
      cout << "|---";
    timer.tick();                       // update the clock
  }

  t = timer.gettime();                  // read clock

  cout << "|";
  cpu.endsim(t);                        // update last finish time, a crude fix
  cout << endl << "-------------------------------------------------------" << endl;
  cout << endl << endl;
  cout << "-------------------------------------------------------" << endl;
  cout << "Simulation Results " << endl;
  cout << "-------------------------------------------------------" << endl;
  cpu.printlist();  
    int ll;
  cin >> ll; // display final results
  cout << "-------------------------------------------------------" << endl;


}
[+][-]12/15/04 07:14 PM, ID: 12836984Accepted Solution

Your question has an Asker Certified™ answer! microcoop verified that this solution worked for them--which means it will likely work for you, too. Click to view the solution free for 30-days now.

About this solution

Zone: C++ Programming Language
Tags: job, shortest, first, preemptive
Sign Up Now!
Solution Provided By: babuno5
Participating Experts: 1
Solution Grade: A
 
 
Related Solutions
Keywords: Preemptive Shortest Job First
 
Loading Advertisement...
 
 
Loading Advertisement...
20100315-EE-VQP-143