Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I'm new to using lists and I've tried creating a list of objects that represent processes. In each object, the processname, arrivaltime, bursttime is stored.This is represented by a class called PCB.

Now ive created a list in which to add a number of processes:
C#
public List<pcb> list = new List<pcb>();


//For loop runs in which above 3 parameters are assigned values & then they're added to list:

C#
PCB pcb = new PCB(processname1, arrivaltime1, bursttime1);
list.Add(pcb);


Now how do i access each value of the list in order to manipulate it? Say i want to access bursttime1 of processname="P1" and decrement it by 4?how to do tht?
Additional Info tht might help understand my Question:
The burst time is a value of a process's total time to run. Ive taken the minimum as 0 and maximum value is input from user. I then randomly generate numbers within this range. I sort each process according to their arrival time. Now i have to apply the round robin(rr)..the one tht arrived first runs for a fixed time quantum(say 4) and then its burst time is decremented by tht amount and it goes to the end of the list until its turn comes again or entire burst time is expired..I need to output this info to a textbox on a form in each iteration.
Posted
Updated 5-Dec-11 2:59am
v4
Comments
BillWoodruff 5-Dec-11 3:29am    
A suggestion: explain exactly what 'bursttime' is: is it equivalent to a .NET DateTime, or, TimeSpan ? Is it completely independent of "arrivaltime" ?

By the way, I can see that the intermittent bug in the CP editor is working here tonight, and has changed what should be "PCB" in your List creating code to "pcb."

Also, may I suggest that the information you disclosed in a response to one solution here: that you want to sort the List on "arrival time" is important information; that kind of information is really good to include in your first post: that requirement is an important factor in focusing on what data structures to use.

The "experimental" solution (#4) I added here does implement sorting on arrival time (Dictionary does not expose any sorting facility).
Member 8412210 5-Dec-11 8:44am    
Thank you alot for the suggestions and solution response. The burst time is a value of a process's total time to run. Ive taken the minimum as 0 and maximum value is input from user. I then randomly generate numbers within this range. I sort each process according to their arrival time. Now i have to apply the round robin(rr)..the one tht arrived first runs for a fixed time quantum(say 4) and then its burst time is decremented by tht amount and it goes to the end of the list until its turn comes again or entire burst time is expired..I need to output this info to a textbox on a form in each iteration. The code sample example i found, its rr logic doesnt make sense to me, so im going to try myself again and see what happens (Assignment due in one more day, wish me luck :S), i think i'll copy this info to the Questions as well. Thanks again. :)

If you always access it via the name, I would strongly recommend a Dictionary instead of a List:
C#
public Dictionary<string, PCB> list = new Dictionary<string,PCB>();

PCB pcb = new PCB(processname1, arrivaltime1, bursttime1);
list.Add(pcb.ProcessName, pcb);

PCB x = list["MyProcess"];
 
Share this answer
 
Comments
Wonde Tadesse 4-Dec-11 13:14pm    
5+
Monjurul Habib 4-Dec-11 13:52pm    
my 5!
How about this:
C#
totaltime += list.get(i).bursttime; // From the sample code


Translates to:
C#
totaltime += list[i].bursttime; // C# (any version :))
 
Share this answer
 
v2
Comments
Member 8412210 5-Dec-11 8:23am    
Yeah, i found out after i started experimenting randomly with the code, i thought i had to use some enumeration method but what you posted was all tht was needed. Now i need to figure out the logic used in tht sample code, but im thinking to make my own logic because i just cant get my head around the flow of the program in tht one. Thank you for your help. :)
There are other, and in my opinion better ways, of modeling your required data structure and required sort behavior than the Dictionary examples shown here.

The primary reason I would not use a Dictionary is that there is no sorting functionality. While you could use a SortedList, which is a list of KeyValuePairs, the sorting that will be performed automatically (or by using the custom IComparer implementation you provide), will sort on the Keys only: I doubt you want to have Keys be the arrival time !

Well, yes, you could write your own custom sort routine to re-arrange the indexes of all the KeyValuePairs in a Dictionary, perhaps sub-classing Dictionary (?): and good luck with that :)

Since I am currently studying the use of Tuples[^] in .NET 4.0, your scenario seemed like an interesting one to try to solve using Tuples: so, I wrote an experimental solution using Tuples that I'll share with you: but be aware:

-2. I was made aware by a CP member of a good article on Tuples in .NET 4: "C# 4 - Tuples" by fmsalmeida"[^].

-1. this is experimental code: there are a lot of opinions that using Tuples in the way shown here is not a good idea: just Google on: "Mutable Tuple"[^]

0. I've implemented this as a static class, another reason I call this code "experimental." There was no particular reason I did that, it just seemed somehow appropriate in the light of the fact that Tuple is, inherently, a static object, a kind of "factory class" (which is why we have to use Tuple.Create).

1. the hack that I do here to get around the fact that Tuple items are immutable (read-only): is not pretty ! adding to the complexity here is that I am using a nested Tuple: it is a Tuple with two Items: the first Item is a DateTime (arrivalTime); the second Item is a Tuple with two Items: its first Item is a string (processName); its second Item is a TimeSpan (burstTime).

2. Tuples in a generic List, with a DateTime as the first Item, can be sorted by the DateTime by the generic List basic Sort method.

The code: this assumes "bursttime" is a TimeSpan
C#
public static class PCB
{
    // static resources
    public static Tuple<DateTime, Tuple<string, TimeSpan>> CurrentProcess;

    public static List<Tuple<DateTime, Tuple<string, TimeSpan>>> ProcessList = new List<Tuple<DateTime, Tuple<string, TimeSpan>>>();

    // static constructor: no instances here ...
    static PCB()
    {
    }

    // add a new PCB datum
    public static void add(DateTime arrivalTime, string procName, TimeSpan burstTime)
    {
        CurrentProcess = Tuple.Create(arrivalTime, Tuple.Create(procName, burstTime));
        ProcessList.Add(CurrentProcess);
    }

    // sort on arrival time
    public static void SortTupleList()
    {
        if (ProcessList.Count >= 2) ProcessList.Sort();
    }

    // here's where it gets weird
    public static void ChangeBurstTime(string procName, TimeSpan timeToAdd)
    {
        // we have to supply our own custom delegate to the List.Find method
        Tuple<DateTime, Tuple<string, TimeSpan>> targetInstance = ProcessList.Find
        (
            delegate(Tuple<DateTime, Tuple<string, TimeSpan>> pcbMatch)
            {
                return pcbMatch.Item2.Item1 == procName;
            }
        );

        // here's where we go into yogic contortions
        // to work around the read-only limitations of Tuples
        if (targetInstance != null)
        {
            int ndx = ProcessList.IndexOf(targetInstance);
            targetInstance = Tuple.Create(targetInstance.Item1, Tuple.Create(targetInstance.Item2.Item1, targetInstance.Item2.Item2.Add(timeToAdd)));
            ProcessList.RemoveAt(ndx);
            ProcessList.Insert(ndx, targetInstance);
        }
    }
}
Using the code:
C#
PCB.add(DateTime.Now.AddHours(2.3), "arrival two", TimeSpan.FromHours(3.4));
PCB.add(DateTime.Now.AddHours(1), "arrival one", TimeSpan.FromHours(0.0));
PCB.SortTupleList();
PCB.ChangeBurstTime("arrival two", TimeSpan.FromHours(4));
Once you've built the List of Tuples, constructing lists sorted on various criteria is relatively easy: suppose you want a List of all Processes sorted by bursttime that includes processname:
List<Tuple<TimeSpan, string>> SortedBurstTimeArrivalList = 
(
  from tpl in PCB.ProcessList
    select
      new Tuple<TimeSpan, string>(tpl.Item2.Item2, tpl.Item2.Item1)
).ToList();

// interesting that applying the Sort method had to be deferred like this ...
SortedBurstTimeArrivalList.Sort();
Suggest you try this out, and set breakpoints on each call into the static class PCB, and then examine the internal contents of 'ProcessList: verify that its contents match what you expect to happen every step of the way, as entries are added, as they are sorted, and, finally, as the "bursttime" of one "entry" is modified.

Or, add some code like this, and examine the output:
foreach(var process in PCB.ProcessList){ Console.WriteLine(process);}
Last, I hope someone will come along and show a (much simpler) solution here that uses a custom collection class, and does some real magic with Linq !
 
Share this answer
 
v14
I don't think you can acces the list the way you want.

First try this:
C#
    List<pcb> pcbList = new List<pcb>();
    PCB pcb = new PCB(processname1, arrivaltime1, bursttime1);
    pcbList.Add(pcb);
    //And now acces pcb by index
    (pcbList[0]==pcb) //would return true;
</pcb></pcb>


However what i think you want is a dictionary which could be used like this:
C#
Dictionary<string,PCB> dictionary = new Dictionary<string,PCB>();
PCB pcb = (processname1, arrivaltime1, bursttime1);
dictionary.Add("processname1",pcb);
(dictionary["processname1"] == pcb) //would return true
 
Share this answer
 
v2
Comments
Member 8412210 4-Dec-11 11:19am    
Basically i want to order the list by arrival times so i can apply a round robin scheduling algorithm on it.
Member 8412210 4-Dec-11 11:39am    
i found this code for it in java
http://stackoverflow.com/questions/7544452/round-robin-cpu-scheduling-java-threads
so i decided to implement it in C#
however i cant seem to access the list object like how its done in ArrayList's. Any ideas on how i should go abt it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900