Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- // ננסה לכתוב תוכית שפותרת שאלות במבחן
- namespace ConsoleApplication75
- {
- // UsefullTools
- class UT
- {
- public static String GetTextFromUser(String massage, int minimum = 1)
- {
- String text = "";
- do
- {
- Console.Write(massage);
- text = Console.ReadLine();
- // קליטה בטוחה למקרה שהמשתמש ילחץ אנטר בלי להזין נתיב
- if (text.Length < minimum)
- {
- continue;
- }
- break;
- } while (true);
- return text;
- }
- public static int GetINTFromUser(String massage, int max = 99999999, int min = 0)
- {
- int number;
- do
- {
- Console.Write(massage);
- if (!Int32.TryParse(Console.ReadLine(), out number)) continue;
- if (number > max) continue;
- if (number < min) continue;
- break;
- } while (true);
- return number;
- }
- public static bool IsYes(String str)
- {
- if (str == null) return false;
- if (str[0] == 'y' || str[0] == 'Y') return true;
- return false;
- }
- public static bool IsYesOrNo(String str)
- {
- if (str == null) return false;
- if (str[0] == 'y' || str[0] == 'Y' || str[0] == 'n' || str[0] == 'N') return true;
- return false;
- }
- }
- // ייצוג של תהליך בודד
- class aProcess
- {
- private String Name;
- private int BurstTime; // זמן ביצוע
- private int ArrivalTime; // זמן הגעה
- private int Priority; // עדיפות
- public int HelpNumber;
- public aProcess CloneP()
- {
- return new aProcess(Name, BurstTime, ArrivalTime, Priority, HelpNumber);
- }
- // פונקציה בונה ריקה עם ברירות המחדל
- public aProcess()
- {
- Name = "No";
- BurstTime = 0;
- ArrivalTime = 0;
- Priority = 1;
- HelpNumber = 0;
- }
- // פונקציה בונה מלאה
- public aProcess(String name, int burst, int arrival, int priority, int help)
- {
- Name = name;
- BurstTime = burst;
- ArrivalTime = arrival;
- Priority = priority;
- HelpNumber = help;
- }
- // פונקציה בונה כמעט מלאה
- public aProcess(String name, int burst, int arrival, int priority)
- : this(name, burst, arrival, 1, 0)
- {
- }
- // פונקציה בונה שלא מקבלת שדה עדיפות. עושה שימוש בפונקציה בונה מלאה
- public aProcess(String name, int burst, int arrival)
- : this(name, burst, arrival, 1, 0)
- {
- }
- public String GetName() { return Name; }
- public int GetBurstTime() { return BurstTime; }
- public int GetArrivalTime() { return ArrivalTime; }
- public int GetPriority() { return Priority; }
- public void SetBurstTime(int b) { BurstTime = b; }
- public void SetArrivalTime(int a) { ArrivalTime = a; }
- public void Set_AllFields_FromUser()
- {
- START:
- Name = UT.GetTextFromUser("Enter Process Name: ");
- BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
- if (BurstTime == -1) goto START;
- ArrivalTime = UT.GetINTFromUser("Enter ArrivalTime: ", 9999999, -1);
- if (ArrivalTime == -1) goto START;
- Priority = UT.GetINTFromUser("Enter Priority: ", 9999999, -1);
- if (Priority == -1) goto START;
- }
- public void Set_NameAndBurstAndArrival_FromUser()
- {
- START:
- Name = UT.GetTextFromUser("Enter Process Name: ");
- BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
- if (BurstTime == -1) goto START;
- ArrivalTime = UT.GetINTFromUser("Enter ArrivalTime: ", 9999999, -1);
- if (ArrivalTime == -1) goto START;
- }
- public void Set_NameAndBurst_FromUser()
- {
- START:
- Name = UT.GetTextFromUser("Enter Process Name: ");
- BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
- if (BurstTime == -1) goto START;
- }
- public override string ToString()
- {
- return "Name: " + Name + ", Burst: " +
- BurstTime + ", Arrival: " + ArrivalTime + ", Priority: " + Priority;
- }
- }
- class ProcessNode // "המחלקה "צומת
- {
- public aProcess infoObject;
- private ProcessNode next; // הוא כתובת האיבר הבא ברשימה next האובייקט
- // פונקציה בונה שלא מקבלת שום פרמטר
- public ProcessNode()
- {
- this.infoObject = new aProcess();
- this.next = null;
- }
- // פונקציה בונה שמקבלת רק תוכן מלא ולא מצביע
- public ProcessNode(String name, int burst, int arrival, int priority)
- {
- infoObject = new aProcess(name, burst, arrival, priority);
- this.next = null;
- }
- // פונקציה בונה שמקבלת רק תוכן ולא מצביע ולא שדה עדיפות
- // פונקציה בונה שמקבלת רק תוכן מלא ולא מצביע
- public ProcessNode(String name, int burst, int arrival)
- {
- infoObject = new aProcess(name, burst, arrival);
- this.next = null;
- }
- // פונקציה בונה שמקבלת תוכן, ומצביע
- public ProcessNode(String name, int burst, int arrival, int priority, ProcessNode next)
- : this(name, burst, arrival, priority)
- {
- this.next = next;
- }
- // פונקציה בונה שמקבלת תוכן בלי שדה עדיפות, ומצביע
- public ProcessNode(String name, int burst, int arrival, ProcessNode next)
- : this(name, burst, arrival)
- {
- this.next = next;
- }
- public ProcessNode(aProcess newInfoObject, ProcessNode newNext)
- {
- infoObject = newInfoObject;
- next = newNext;
- }
- // Set -ו Get פונקציות
- public ProcessNode GetNext()
- {
- return next;
- }
- public void SetNext(ProcessNode next)
- {
- this.next = next;
- }
- public static int Compare(ProcessNode p1, ProcessNode p2)
- {
- if (p1.infoObject.GetBurstTime() > p2.infoObject.GetBurstTime()) return 1;
- if (p1.infoObject.GetBurstTime() < p2.infoObject.GetBurstTime()) return -1;
- return 0;
- }
- }
- class ProcessesCircularList // רשימה מעגלית של תהליכים - נועד להיות תור המוכנים
- {
- private ProcessNode Head;
- private ProcessNode Last;
- private int length;
- public int Length { get { return length; } }
- public ProcessesCircularList()
- {
- //Head = new ProcessNode();
- Head = null;
- Last = null;
- length = 0;
- }
- public ProcessesCircularList(bool isFromUser)
- {
- if (isFromUser == false)
- {
- Head = null;
- Last = null;
- length = 0;
- }
- else
- {
- CreateListFromUser(out Head, out Last, out length);
- }
- }
- // פונקציה בונה שיוצרת העתק של רשימה שהיא מקבלת
- public ProcessesCircularList(ProcessesCircularList oldList)
- {
- if (oldList == null)
- {
- Head = null;
- Last = null;
- length = 0;
- return;
- }
- // (אתחול ה"ראש" צריך להיות לפני הלולאה)
- ProcessNode p = new ProcessNode(oldList.Head.infoObject.CloneP(), null);
- ProcessNode oldP = oldList.Head.GetNext();
- Head = Last = p;
- length = oldList.Length;
- for (int i = 1; i < length; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
- {
- p = new ProcessNode(oldP.infoObject.CloneP(), null);
- Last.SetNext(p);
- Last = p;
- oldP = oldP.GetNext();
- }
- Last.SetNext(Head); // רשימה מעגלית
- }
- public void CreateListFromUser(out ProcessNode head, out ProcessNode last, out int thisLength)
- {
- ProcessNode p;
- head = null;
- last = null;
- thisLength = UT.GetINTFromUser("Enter amount of Processes: ", 1000, 1);
- Console.WriteLine("\nMode:\n----");
- Console.WriteLine("1 = Process Name, BurstTime, ArrivalTime, Priority");
- Console.WriteLine("2 = Process Name, BurstTime, ArrivalTime");
- Console.WriteLine("3 = Process Name, BurstTime");
- Console.WriteLine();
- int mode = UT.GetINTFromUser("Enter User Mode: ", 3, 1);
- switch (mode)
- {
- case 1:
- {
- // (אתחול ה"ראש" צריך להיות לפני הלולאה)
- Console.WriteLine("\n1.");
- p = new ProcessNode();
- p.infoObject.Set_AllFields_FromUser();
- head = last = p;
- for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
- {
- Console.WriteLine("\n" + (i + 1) + ".");
- p = new ProcessNode();
- p.infoObject.Set_AllFields_FromUser();
- last.SetNext(p);
- last = p;
- }
- last.SetNext(head); // רשימה מעגלית
- break;
- }
- case 2:
- {
- // (אתחול ה"ראש" צריך להיות לפני הלולאה)
- Console.WriteLine("\n1.");
- p = new ProcessNode();
- p.infoObject.Set_NameAndBurstAndArrival_FromUser();
- head = last = p;
- for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
- {
- Console.WriteLine("\n" + (i + 1) + ".");
- p = new ProcessNode();
- p.infoObject.Set_NameAndBurstAndArrival_FromUser();
- last.SetNext(p);
- last = p;
- }
- last.SetNext(head); // רשימה מעגלית
- break;
- }
- case 3:
- {
- // (אתחול ה"ראש" צריך להיות לפני הלולאה)
- Console.WriteLine("\n1.");
- p = new ProcessNode();
- p.infoObject.Set_NameAndBurst_FromUser();
- head = last = p;
- for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
- {
- Console.WriteLine("\n" + (i + 1) + ".");
- p = new ProcessNode();
- p.infoObject.Set_NameAndBurst_FromUser();
- last.SetNext(p);
- last = p;
- }
- last.SetNext(head); // רשימה מעגלית
- break;
- }
- }
- }
- public ProcessesCircularList Clone()
- {
- return new ProcessesCircularList(this);
- }
- public void AddToEnd(aProcess newProcess)
- {
- if (length == 0)
- {
- Head = new ProcessNode(newProcess, Head);
- Last = Head;
- length = 1;
- }
- else
- {
- Last.SetNext(new ProcessNode(newProcess, Head));
- Last = Last.GetNext();
- length++;
- }
- }
- // הוספה של איבר לרשימה
- // זהירות! משנה את המצביע של האיבר המקורי
- public void AddToEnd(ProcessNode newProcessNode)
- {
- if (length == 0)
- {
- Head = newProcessNode;
- Last = newProcessNode;
- newProcessNode.SetNext(newProcessNode);
- length = 1;
- }
- else
- {
- Last.SetNext(newProcessNode);
- Last = newProcessNode;
- Last.SetNext(Head);
- length++;
- }
- }
- public void Show()
- {
- int i = 0;
- ProcessNode p = Head;
- if(length == 0) return;
- do
- {
- Console.WriteLine((++i) + ".");
- Console.WriteLine(p.infoObject);
- p = p.GetNext();
- } while (p != Head);
- }
- public aProcess GetFirst()
- {
- if (length == 0) return null;
- return Head.infoObject;
- }
- public aProcess GetLast()
- {
- if (length == 0) return null;
- return Last.infoObject;
- }
- public void RemoveFirst()
- {
- if (length == 0) return; // אם זו רשימה ריקה
- if (length == 1) // אם יש רק איבר אחד
- {
- Head = null;
- Last = null;
- length = 0;
- return;
- }
- Head = Head.GetNext();
- length--;
- }
- public aProcess TakeFirstProcess() // מחזיר את התהליך הראשון ומוחק אותו
- {
- if (length == 0) return null;
- aProcess tmp = Head.infoObject;
- if (length == 1) // אם יש רק איבר אחד
- {
- Head = null;
- Last = null;
- length = 0;
- return tmp;
- }
- Head = Head.GetNext();
- length--;
- return tmp;
- }
- public ProcessNode TakeFirstNode() // מחזיר את התהליך הראשון ומוחק אותו
- {
- if (length == 0) return null;
- ProcessNode tmp = Head;
- if (length == 1) // אם יש רק איבר אחד
- {
- Head = null;
- Last = null;
- length = 0;
- return tmp;
- }
- Head = Head.GetNext();
- length--;
- return tmp;
- }
- public aProcess Get(int index)
- {
- if (length == 0) return null;
- if (index >= length) return null;
- if (index < 0) return null;
- int i = 0;
- ProcessNode p = Head;
- do
- {
- if (i == index)
- {
- return p.infoObject;
- }
- i++;
- p = p.GetNext();
- } while (p != Head);
- return null; // אם לא מצא
- }
- // מיון ששם את הקצר ביותר בהתחלה
- // לא מיון חכם, אבל עובד
- public void Sort()
- {
- ProcessNode tmp;
- ProcessNode p0;
- ProcessNode p1;
- ProcessNode p2;
- bool isReplace;
- if (Head == Head.GetNext()) return;
- if (length == 2) // משום מה זה צריך תנאי מיוחד
- {
- if (ProcessNode.Compare(Head, Last) == 1)
- {
- tmp = Head;
- Head = Last;
- Last = tmp;
- }
- return;
- }
- do{
- isReplace = false;
- p0 = Last;
- p1 = Head;
- p2 = Head.GetNext();
- for (int i = 0; i < Length - 1; i++)
- {
- // if p1 bigger -> replace!
- if (ProcessNode.Compare(p1, p2) == 1)
- {
- isReplace = true;
- if (p2 == Last) Last = p1;
- p1.SetNext(p2.GetNext());
- p2.SetNext(p1);
- p0.SetNext(p2);
- tmp = p2;
- p2 = p1;
- p1 = tmp;
- if (p2 == Head) Head = p1;
- }
- p0 = p0.GetNext();
- p1 = p1.GetNext();
- p2 = p2.GetNext();
- }
- } while (isReplace == true);
- }
- /////// Scheduling Algorithms: ///////
- public void SimpleFIFO(out double WaitingTime_AVG, out double Turnaround_AVG)
- {
- int WaitingTime_SUM = 0; // סכום זמני המתנה
- int Turnaround_SUM = 0; // סכום זמני סבב
- WaitingTime_AVG = 0; // זמן המתנה ממוצע
- Turnaround_AVG = 0; // זמן סבב ממוצע
- // בדיקת זמן המתנה
- ProcessNode currentProcess = Head;
- ProcessNode tmp;
- for (int i = 0; i < length; i++)
- {
- tmp = Head;
- for (int j = 0; j < i; j++) // "סיכום התהליכים שהיו לפני ה"תהליך-הנוכחי
- {
- WaitingTime_SUM += tmp.infoObject.GetBurstTime();
- tmp = tmp.GetNext();
- }
- WaitingTime_SUM -= currentProcess.infoObject.GetArrivalTime();
- currentProcess = currentProcess.GetNext();
- }
- WaitingTime_AVG = (double)WaitingTime_SUM / length;
- // בדיקת זמן סבב
- currentProcess = Head;
- for (int i = 0; i < length; i++)
- {
- tmp = Head;
- for (int j = 0; j <= i; j++) // "סיכום התהליכים שהיו לפני ה"תהליך-הנוכחי
- {
- Turnaround_SUM += tmp.infoObject.GetBurstTime();
- tmp = tmp.GetNext();
- }
- Turnaround_SUM -= currentProcess.infoObject.GetArrivalTime();
- currentProcess = currentProcess.GetNext();
- }
- Turnaround_AVG = (double)Turnaround_SUM / length;
- // Show:
- tmp = Head;
- do{
- Console.Write("{0}({1}), ", tmp.infoObject.GetName(), tmp.infoObject.GetBurstTime());
- tmp = tmp.GetNext();
- } while (tmp != Head);
- Console.WriteLine("\n");
- }
- public void SJF_NoPreemptive(out double WaitingTime_AVG, out double Turnaround_AVG)
- {
- int WaitingTime_SUM = 0; // סכום זמני המתנה
- int Turnaround_SUM = 0; // סכום זמני סבב
- WaitingTime_AVG = 0; // זמן המתנה ממוצע
- Turnaround_AVG = 0; // זמן סבב ממוצע
- ProcessNode tmp;
- // מעתיק את הרשימה לרשימה חדשה שמותר לשנות
- ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
- // מערך ההיסטוריה
- aProcess[] history = new aProcess[2000];
- int index = 0;
- for (int i = 0; i < newReadyQueue.Length; i++)
- {
- // רשימה ריקה שעתידה להכיל ולמיין את כל התהליכים שכבר הגיעו
- ProcessesCircularList ArrivedQueue = new ProcessesCircularList();
- // מילוי רשימת התהליכים שכבר הגיעו
- tmp = newReadyQueue.Head;
- do
- {
- // אם התהליך כבר הגיע ועוד לא התבצע
- if (tmp.infoObject.GetArrivalTime() <= 0 && tmp.infoObject.HelpNumber == 0)
- {
- ArrivedQueue.AddToEnd(tmp.infoObject);
- }
- tmp = tmp.GetNext();
- } while (tmp != newReadyQueue.Head);
- if (ArrivedQueue.Length == 0)
- {
- Console.WriteLine("*Error1");
- return;
- }
- if (index == 2000)
- {
- Console.WriteLine("*Error2");
- return;
- }
- // מיון הרשימה מהקטן לגדול
- ArrivedQueue.Sort();
- ArrivedQueue.GetFirst().HelpNumber = 1; // newReadyQueue זה משפיע גם על האיבר שברשימה
- // הוספה של הראשון שעכשיו התבצע בשלמותו אל מערך ההיסטוריה
- history[index++] = ArrivedQueue.GetFirst();
- // להחסיר מזמן ההגעה של כל הנותרים שלא התבצעו את זמן הביצוע של הנוכחי
- tmp = newReadyQueue.Head;
- do
- {
- // אם התהליך עוד לא התבצע
- if (tmp.infoObject.HelpNumber == 0)
- {
- tmp.infoObject.SetArrivalTime( tmp.infoObject.GetArrivalTime() -
- ArrivedQueue.GetFirst().GetBurstTime() );
- }
- tmp = tmp.GetNext();
- } while (tmp != newReadyQueue.Head);
- }
- // בירור זמן סבב וזמן המתנה
- tmp = newReadyQueue.Head;
- do
- {
- WaitingTime_SUM += (tmp.infoObject.GetArrivalTime() * -1);
- // (זמן סבב הוא זמן המתנה פלוס זמן ביצוע)
- Turnaround_SUM += (tmp.infoObject.GetArrivalTime() * -1) + tmp.infoObject.GetBurstTime();
- tmp = tmp.GetNext();
- } while (tmp != newReadyQueue.Head);
- WaitingTime_AVG = (double)WaitingTime_SUM / length;
- Turnaround_AVG = (double)Turnaround_SUM / length;
- // Show:
- for (int i = 0; i < index; i++)
- {
- Console.Write("{0}({1}), ", history[i].GetName(), history[i].GetBurstTime());
- }
- Console.WriteLine("\n");
- }
- public void SJF_Preemptive(out double WaitingTime_AVG, out double Turnaround_AVG)
- {
- int WaitingTime_SUM = 0; // סכום זמני המתנה
- int Turnaround_SUM = 0; // סכום זמני סבב
- WaitingTime_AVG = 0; // זמן המתנה ממוצע
- Turnaround_AVG = 0; // זמן סבב ממוצע
- ProcessNode tmp;
- // מעתיק את הרשימה לרשימה חדשה שמותר לשנות
- ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
- // מערך ההיסטוריה
- aProcess[] history = new aProcess[2000];
- int index = 0;
- int leftProcesses_Number = length;
- while (leftProcesses_Number > 0)
- {
- // רשימה ריקה שעתידה להכיל ולמיין את כל התהליכים שכבר הגיעו
- ProcessesCircularList ArrivedQueue = new ProcessesCircularList();
- // (newReadyQueue משפיע על ArrivedQueue) .מילוי רשימת התהליכים שכבר הגיעו
- tmp = newReadyQueue.Head;
- do
- {
- // אם התהליך כבר הגיע ועוד לא נגמר
- if (tmp.infoObject.GetArrivalTime() <= 0 && tmp.infoObject.GetBurstTime() > 0)
- {
- ArrivedQueue.AddToEnd(tmp.infoObject);
- }
- tmp = tmp.GetNext();
- } while (tmp != newReadyQueue.Head);
- // בדיקת שגיאות
- if (ArrivedQueue.Length == 0)
- {
- Console.WriteLine("*Error3");
- return;
- }
- if (index == 2000)
- {
- Console.WriteLine("*Error4");
- return;
- }
- // מיון הרשימה מהקטן לגדול
- ArrivedQueue.Sort();
- // הוספה של הראשון ברשימה שהוא הקצר ביותר אל מערך ההיסטוריה
- if (index == 0) // אם זו הפעם הראשונה - אסור לצאת מהמערך אל אינדקס מינוס אחד
- {
- history[index] = ArrivedQueue.GetFirst().CloneP();
- history[index++].SetBurstTime(0);
- }
- else
- {
- // אם מדובר באיבר שלא היה שם כבר
- // (לאיבר שכבר נמצא שם פשוט נוסיף)
- if (history[index-1].GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
- {
- history[index] = ArrivedQueue.GetFirst().CloneP();
- history[index++].SetBurstTime(0);
- }
- }
- // כל עוד "זמן ביצוע" של תהליך נוכחי לא שווה ל-אפס
- while (true)
- {
- // לחסר 1 מזמן ההגעה של כל התהליכים שלא נגמרו וגם אינם התהליך הנוכחי
- ProcessNode tmp2 = newReadyQueue.Head;
- do
- {
- if (tmp2.infoObject.GetBurstTime() > 0 &&
- tmp2.infoObject.GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
- {
- tmp2.infoObject.SetArrivalTime(tmp2.infoObject.GetArrivalTime()-1);
- }
- tmp2 = tmp2.GetNext();
- } while (tmp2 != newReadyQueue.Head);
- // newReadyQueue לחסר 1 מ"זמן ביצוע" של התהליך הנוכחי. משפיע גם על הרשימה
- ArrivedQueue.GetFirst().SetBurstTime( ArrivedQueue.GetFirst().GetBurstTime()-1 );
- // להוסיף 1 לאיבר האחרון במערך ההיסטוריה
- history[index-1].SetBurstTime( history[index-1].GetBurstTime()+1 );
- // אם התהליך נגמר - לצאת
- if(ArrivedQueue.GetFirst().GetBurstTime() == 0) {
- leftProcesses_Number--;
- break;
- }
- // newReadyQueue לסרוק את כל הרשימה
- // אם יש תהליך שגם "זמן ההגעה" שלו שווה ל-אפס
- // וגם הוא אינו התהליך הנוכחי וגם הוא לא גמור
- //זמן הביצוע שלו קטן מזמן הביצוע שנותר לתהליך הנוכחי - לצאת מהלולאה
- tmp2 = newReadyQueue.Head;
- bool needToStop = false;
- do
- {
- if (tmp2.infoObject.GetArrivalTime() == 0 && tmp2.infoObject.GetBurstTime() != 0 &&
- tmp2.infoObject.GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
- {
- if (tmp2.infoObject.GetBurstTime() < ArrivedQueue.GetFirst().GetBurstTime())
- {
- needToStop = true;
- break;
- }
- }
- tmp2 = tmp2.GetNext();
- } while (tmp2 != newReadyQueue.Head);
- if (needToStop == true) break;
- }
- }
- // Show:
- for (int i = 0; i < index; i++)
- {
- Console.Write("{0}({1}), ", history[i].GetName(), history[i].GetBurstTime());
- }
- Console.WriteLine("\n");
- // חישוב סכום כל זמני ההמתנה
- ProcessNode tmp3 = newReadyQueue.Head;
- do{
- WaitingTime_SUM += tmp3.infoObject.GetArrivalTime() * -1;
- tmp3 = tmp3.GetNext();
- } while (tmp3 != newReadyQueue.Head);
- // חישוב סכום כל זמני הסבב
- // (זמן סבב הוא זמן המתנה פלוס זמן ביצוע)
- Turnaround_SUM += WaitingTime_SUM;
- tmp3 = Head;
- do
- {
- Turnaround_SUM += tmp3.infoObject.GetBurstTime();
- tmp3 = tmp3.GetNext();
- } while (tmp3 != Head);
- // :סוף סוף מסקנות
- WaitingTime_AVG = (double)WaitingTime_SUM / length;
- Turnaround_AVG = (double)Turnaround_SUM / length;
- }
- private class OnlyNameAndTimeOfProcess
- {
- public String Name;
- public int Time;
- public bool IsLast;
- public OnlyNameAndTimeOfProcess(String Name, int Time)
- {
- this.Name = Name;
- this.Time = Time;
- IsLast = false;
- }
- public OnlyNameAndTimeOfProcess()
- {
- this.Name = "No";
- this.Time = -1;
- IsLast = false;
- }
- public void show()
- {
- Console.Write("{0}({1}), ", Name, Time);
- }
- }
- private int BurstMinusQuantum(int Burst, int quantum)
- {
- int x = Burst - quantum;
- if (x >= 0) return x;
- return 0;
- }
- public void RR_FIFO(out double WaitingTime_AVG, out double Turnaround_AVG, int quantum )
- {
- // העתקה של המקור אל רשימת עזר שמותר לבצע בה שינויים
- // (אם היינו לומדים DEEP CLONE היה עדיף להשתמש ב)
- ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
- ProcessNode currentProcessNode = newReadyQueue.Head;
- // מערך ההיסטוריה בעצם מציג את כל מה שקרה
- // A20 B15 C10 D7 Q10 :עבור
- //A(10), B(10), C10, D(7), A(10), B(5) :יכיל
- OnlyNameAndTimeOfProcess[] history = new OnlyNameAndTimeOfProcess[2000];
- int index = 0;
- // מספר התהליכים שנותרו לא גמורים
- int leftProcesses_Number = newReadyQueue.Length;
- while (leftProcesses_Number > 0)
- {
- aProcess currentProcess = currentProcessNode.infoObject;
- // מדלגים על תהליכים שנגמרו
- if (currentProcess.GetBurstTime() == 0) goto INCREASE;
- // (עושים כאן ניו משום שיצירת המערך לא הפעילה את הפונקציה הבונה של איבריו)
- history[index] = new OnlyNameAndTimeOfProcess();
- // :החסרה של הקוונטום מהתהליך הנוכחי
- // אם התהליך הנוכחי לא יגמר בעת ביצוע הקוונטום
- if (currentProcess.GetBurstTime() > quantum)
- {
- history[index].Time = quantum;
- history[index].Name = currentProcess.GetName();
- // הפחתת הקוונטום מהזמן שנותר לתהליך זה. כתיבה ארוכה משום שהוא פרייבט
- currentProcess.SetBurstTime(currentProcess.GetBurstTime() - quantum);
- }
- // אם התהליך הנוכחי כן יגמר בעת ביצוע הקוונטום
- else
- {
- history[index].Time = currentProcess.GetBurstTime();
- history[index].Name = currentProcess.GetName();
- history[index].IsLast = true;
- // הפחתת הקוונטום מהזמן שנותר לתהליך זה. כתיבה ארוכה משום שהוא פרייבט
- currentProcess.SetBurstTime(0);
- leftProcesses_Number--;
- }
- index++;
- INCREASE:
- currentProcessNode = currentProcessNode.GetNext();
- }
- // show:
- for (int i = 0; i < index; i++)
- {
- history[i].show();
- }
- Console.WriteLine("\n");
- int WaitingTime_SUM = 0; // סכום זמני המתנה
- int Turnaround_SUM = 0; // סכום זמני סבב
- WaitingTime_AVG = 0; // זמן המתנה ממוצע
- Turnaround_AVG = 0; // זמן סבב ממוצע
- ProcessNode pInOriginal = Head;
- //currentProcessNode = newReadyQueue.Head;
- do{
- String currentProcessName = pInOriginal.infoObject.GetName();
- for (int i = 0; i < history.Length; i++)
- {
- if (history[i].Name.Equals(currentProcessName) &&
- history[i].IsLast == true) break;
- if (history[i].Name.Equals(currentProcessName)) continue;
- WaitingTime_SUM += history[i].Time;
- Turnaround_SUM += history[i].Time;
- }
- // זמן סבב של תהליך הוא זמן ההמתנה שלו פלוס זמן הביצוע שלו
- Turnaround_SUM += pInOriginal.infoObject.GetBurstTime();
- // מחסרים זמן הגעה מזמן המתנה ומזמן סבב
- WaitingTime_SUM -= pInOriginal.infoObject.GetArrivalTime();
- Turnaround_SUM -= pInOriginal.infoObject.GetArrivalTime();
- pInOriginal = pInOriginal.GetNext();
- //currentProcessNode = currentProcessNode.GetNext();
- } while (pInOriginal != Head); // ריצה על רשימה מקושרת מעגלית
- WaitingTime_AVG = (double)WaitingTime_SUM / length;
- Turnaround_AVG = (double)Turnaround_SUM / length;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- START:
- String UserWantsAnotherAlgorithm = "";
- double WaitingTime_AVG=-1, Turnaround_AVG=-1;
- int quantum=1;
- ProcessesCircularList ReadyQueue = new ProcessesCircularList(true); // ממלא מידע מהמשתמש
- Console.WriteLine("\n\n\nYour Processes:\n--------------");
- //ReadyQueue.Show();
- ALGORITHMS:
- ReadyQueue.Show(); // זה אמור להיות איפה שההערה, אבל בנתיים בשביל הבדיקות
- Console.WriteLine("\n\n\nAlgorithms:\n----------");
- Console.WriteLine("1 = FIFO, NON PREEMPTIVE");
- Console.WriteLine("2 = SJF, NON PREEMPTIVE");
- Console.WriteLine("3 = SJF, PREEMPTIVE");
- Console.WriteLine("4 = RR (algorithm with \"quantum\"), FIFO, NON PREEMPTIVE");
- Console.WriteLine();
- int mode = UT.GetINTFromUser("Choose Algorithm: ", 4, 1);
- if(mode == 4) {quantum = UT.GetINTFromUser("Enter quantum: ", 1000000, 1); }
- Console.WriteLine("Results:\n-------\n\n\n");
- switch (mode)
- {
- case 1:
- {
- ReadyQueue.SimpleFIFO(out WaitingTime_AVG, out Turnaround_AVG);
- break;
- }
- case 2:
- {
- ReadyQueue.SJF_NoPreemptive(out WaitingTime_AVG, out Turnaround_AVG);
- break;
- }
- case 3:
- {
- ReadyQueue.SJF_Preemptive(out WaitingTime_AVG, out Turnaround_AVG);
- break;
- }
- case 4:
- {
- ReadyQueue.RR_FIFO(out WaitingTime_AVG, out Turnaround_AVG, quantum);
- break;
- }
- }
- Console.WriteLine("WaitingTime AVG: " + WaitingTime_AVG);
- Console.WriteLine("Turnaround AVG: " + Turnaround_AVG);
- do{
- UserWantsAnotherAlgorithm =
- UT.GetTextFromUser("\n\nDo you want Another Algorithm? (Y/N): ");
- } while (!UT.IsYesOrNo(UserWantsAnotherAlgorithm));
- if (UT.IsYes(UserWantsAnotherAlgorithm)) goto ALGORITHMS;
- else {
- Console.Clear();
- goto START;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment