using System; using System.Collections.Generic; using System.Threading; using System.Text; using System.Diagnostics; namespace Lab4 { // First-Come, First-Serverd class FCFS { private List procs = new List(); // list of processes private List queue = new List(); // list for current amount of idling processes Thread serve; // thread for emulating serve bool isServing = false, isEnd = false; // some flags Int32 totalIdlingProcs = 0; // indocate total idling procs Stopwatch[] idleSW; // array of stopwatches for each process Process current; public FCFS(Int32 totalProcs) { idleSW = new Stopwatch[totalProcs]; // init for (Int32 i = 0; i < totalProcs; i++) idleSW[i] = new Stopwatch(); } public void AddProcess(Process proc) // add new process to queue { Console.WriteLine("FCFS: add proc with id " + proc.PID.ToString()); procs.Add(proc); // to list if (!isServing) // processes not serving, there were no processes { isServing = true; // serving flag serve = new Thread(this.ServeProcess); serve.Start(); // start new serving thread } else { queue.Add(procs.Count); // was added new process, store total queued processes totalIdlingProcs++; // inc number of idling procs idleSW[proc.PID].Start(); // start SW by procs PID } } public void CheckQueue() // check processes queue { if (procs.Count > 0) // there is some procs in queue { serve = new Thread(this.ServeProcess); // start another thread to serve serve.Start(); } else { isServing = false; // no procs, stop serving if (isEnd) // it's end of emulation, show info Console.WriteLine("FCFS: emulation end, press any key to view results."); } } void ServeProcess() // serve process { idleSW[procs[0].PID].Stop(); // stop idling proc SW current = procs[0]; procs.RemoveAt(0); // remove first proc cause it's serving //Console.WriteLine("FCFS: remove proc with id " + current.PID.ToString()); Console.WriteLine("FCFS: start serving proc with id " + current.PID.ToString()); Thread.Sleep(current.executionTime * 100); // emulate serving Console.WriteLine("FCFS: served proc with id " + current.PID.ToString()); CheckQueue(); //RemoveProcess(); // remove process } public void EndEmulation() { isEnd = true; } public Int32 maxQueue // get max queue count { get { Int32 res = 0; foreach(Int32 i in queue) if(i > res) res = i; return res; } } public Double avgIdleTime // get average idle time { get { Int64 res = 0; for (Int32 i = 0; i < idleSW.Length; i++) // sum all res += idleSW[i].ElapsedMilliseconds; return 1.0 * res / totalIdlingProcs / 100; // divide by number of total idling procs } } } }