Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:24:04+00:00 2026-05-17T17:24:04+00:00

I have to simulate a process scheduler using SRTN algorithm and im having trouble

  • 0

I have to simulate a process scheduler using SRTN algorithm and im having trouble within a certain part.

I have a queue of a custom class called ‘Process’ I need to sort it based on a a field called ‘last_prediction’. My code works most of the time, but if you look at time:19 of my output, the output in the ready queue is wrong (it should be: 1004(1.5) 1002(2) 1003(2)).

Here is my code:

 int count = ReadyQueue.Count;

        // Copy Queue into Vector
        ArrayList temp = new ArrayList();
        for (int i = 0; i < count; i++)
        {
            Process p = (Process)ReadyQueue.Dequeue();
            temp.Add(p);
        }

        // Sort Vector
        for (int i = 0; i < count; i++)
        {
            double min = ((Process)temp[i]).last_prediction;
            for (int j=i+1; j<count; j++)
            {
                if ( ((Process)temp[j]).last_prediction < min )
                {
                    min = ((Process)temp[j]).last_prediction;
                    Process dummy = (Process)temp[j];
                    temp[j] = temp[i];
                    temp[i] = dummy; 
                }
            }
        }

        // Copy Vector back into Queue
        for (int i = 0; i < count; i++)
        {
            Process p = (Process)temp[i];
            ReadyQueue.Enqueue(p);
        }

EDIT: ok, im trying to use ICompare, similar to what you gave hughdbrown.Now i get a different error:

public class Process
    {
        public int process_id;
        public int arrival_time;
        public int total_time;
        public int avg_burst;
        public int actual_burst;
        public int last_burst; // SRTN
        public double last_prediction; // SRTN
        public int io_delay;
        public int context_switch_delay;

        public class ProcessSort : IComparer
        {
            public int Compare(object x, object y)
            {
                var a = x as Process;
                var b = y as Process;
                double aNum = a.last_prediction;
                double bNum = b.last_prediction;
                return Compare(aNum, bNum);
            }
        }
    }

this is the error i get now:

Unhandled Exception: System.InvalidOperationException: Failed to compare two elements in the array. ---> System.NullReferenceException: Object reference not set to an instance of an object. 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:24:05+00:00Added an answer on May 17, 2026 at 5:24 pm

    I would use a real sorting routine on this array, not a hand-crafted insertion/bubble sort. Add a comparison function to your object.

    I’d also use a templatized data collection, not ArrayList. You might be interested in using this C# PriorityQueue code from my website. That has Queue semantics and maintains items in a sorted order.


    Later: Your IComparable code would be something like this:

    public class Process : IComparable 
    {
        int last_prediction;
        public int CompareTo(object obj)
        {
                Process right = obj as Process;
                return this.last_prediction.CompareTo(right.last_prediction);
        }
    }
    

    Later still: here is a complete test program that has a sortable Process. Tested in Mono on ubuntu.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Comparer
    {
        public class Process : IComparable 
        {
            int last_prediction;
            public Process(int p)
            {
                this.last_prediction = p;
            }
            public int CompareTo(object obj)
            {
                Process right = obj as Process;
                return this.last_prediction.CompareTo(right.last_prediction);
            }
            public int Prediction { get { return this.last_prediction; } }
        }
    
        class MainClass
        {
            public static void Main (string[] args)
            {
                List<Process> list = new List<Process>();
                for (int i = 0; i < 10; i++)
                    list.Add(new Process(10 - i));
    
                System.Console.WriteLine("Current values:");
                foreach (Process p in list)
                    System.Console.WriteLine("Process {0}", p.Prediction);
    
                list.Sort();
    
                System.Console.WriteLine("Sorted values:");
                foreach (Process p in list)
                    System.Console.WriteLine("Process {0}", p.Prediction);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have started testing my UI using qUnit, so I need to simulate some
I have a number of tables that use the trigger/sequence column to simulate auto_increment
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
Have just started using Google Chrome , and noticed in parts of our site,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
We have an application that generates simulated data for one of our services for
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.