Noam_15

אלגוריתמים של תזמון תהליכים במבני נתונים

Feb 8th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 38.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. // ננסה לכתוב תוכית שפותרת שאלות במבחן
  8.  
  9. namespace ConsoleApplication75
  10. {
  11.     // UsefullTools
  12.     class UT
  13.     {
  14.         public static String GetTextFromUser(String massage, int minimum = 1)
  15.         {
  16.             String text = "";
  17.             do
  18.             {
  19.                 Console.Write(massage);
  20.                 text = Console.ReadLine();
  21.  
  22.                 // קליטה בטוחה למקרה שהמשתמש ילחץ אנטר בלי להזין נתיב
  23.                 if (text.Length < minimum)
  24.                 {
  25.                     continue;
  26.                 }
  27.                 break;
  28.             } while (true);
  29.             return text;
  30.         }
  31.  
  32.  
  33.         public static int GetINTFromUser(String massage, int max = 99999999, int min = 0)
  34.         {
  35.             int number;
  36.            
  37.             do
  38.             {
  39.                 Console.Write(massage);
  40.                 if (!Int32.TryParse(Console.ReadLine(), out number)) continue;
  41.  
  42.                 if (number > max) continue;
  43.  
  44.                 if (number < min) continue;
  45.  
  46.                 break;
  47.             } while (true);
  48.             return number;
  49.         }
  50.  
  51.         public static bool IsYes(String str)
  52.         {
  53.             if (str == null) return false;
  54.             if (str[0] == 'y' || str[0] == 'Y') return true;
  55.             return false;
  56.         }
  57.  
  58.         public static bool IsYesOrNo(String str)
  59.         {
  60.             if (str == null) return false;
  61.             if (str[0] == 'y' || str[0] == 'Y' || str[0] == 'n' || str[0] == 'N') return true;
  62.             return false;
  63.         }
  64.     }
  65.  
  66.    
  67.  
  68.  
  69.  
  70.  
  71.     // ייצוג של תהליך בודד
  72.     class aProcess
  73.     {
  74.         private String Name;
  75.         private int BurstTime; // זמן ביצוע
  76.         private int ArrivalTime; // זמן הגעה
  77.         private int Priority; // עדיפות
  78.         public int HelpNumber;
  79.  
  80.         public aProcess CloneP()
  81.         {
  82.             return new aProcess(Name, BurstTime, ArrivalTime, Priority, HelpNumber);
  83.         }
  84.  
  85.         // פונקציה בונה ריקה עם ברירות המחדל
  86.         public aProcess()
  87.         {
  88.             Name = "No";
  89.             BurstTime = 0;
  90.             ArrivalTime = 0;
  91.             Priority = 1;
  92.             HelpNumber = 0;
  93.         }
  94.  
  95.         // פונקציה בונה מלאה
  96.         public aProcess(String name, int burst, int arrival, int priority, int help)
  97.         {
  98.             Name = name;
  99.             BurstTime = burst;
  100.             ArrivalTime = arrival;
  101.             Priority = priority;
  102.             HelpNumber = help;
  103.         }
  104.  
  105.         // פונקציה בונה כמעט מלאה
  106.         public aProcess(String name, int burst, int arrival, int priority)
  107.             : this(name, burst, arrival, 1, 0)
  108.         {
  109.         }
  110.  
  111.         // פונקציה בונה שלא מקבלת שדה עדיפות. עושה שימוש בפונקציה בונה מלאה
  112.         public aProcess(String name, int burst, int arrival)
  113.             : this(name, burst, arrival, 1, 0)
  114.         {
  115.         }
  116.  
  117.         public String GetName() { return Name; }
  118.         public int GetBurstTime() { return BurstTime; }
  119.         public int GetArrivalTime() { return ArrivalTime; }
  120.         public int GetPriority() { return Priority; }
  121.  
  122.         public void SetBurstTime(int b) { BurstTime = b; }
  123.         public void SetArrivalTime(int a) { ArrivalTime = a; }
  124.  
  125.  
  126.  
  127.  
  128.         public void Set_AllFields_FromUser()
  129.         {
  130.             START:
  131.             Name = UT.GetTextFromUser("Enter Process Name: ");
  132.             BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
  133.             if (BurstTime == -1) goto START;
  134.             ArrivalTime = UT.GetINTFromUser("Enter ArrivalTime: ", 9999999, -1);
  135.             if (ArrivalTime == -1) goto START;
  136.             Priority = UT.GetINTFromUser("Enter Priority: ", 9999999, -1);
  137.             if (Priority == -1) goto START;
  138.         }
  139.         public void Set_NameAndBurstAndArrival_FromUser()
  140.         {
  141.             START:
  142.             Name = UT.GetTextFromUser("Enter Process Name: ");
  143.             BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
  144.             if (BurstTime == -1) goto START;
  145.             ArrivalTime = UT.GetINTFromUser("Enter ArrivalTime: ", 9999999, -1);
  146.             if (ArrivalTime == -1) goto START;
  147.         }
  148.         public void Set_NameAndBurst_FromUser()
  149.         {
  150.             START:
  151.             Name = UT.GetTextFromUser("Enter Process Name: ");
  152.             BurstTime = UT.GetINTFromUser("Enter BurstTime: ", 9999999, -1);
  153.             if (BurstTime == -1) goto START;
  154.         }
  155.  
  156.  
  157.  
  158.         public override string ToString()
  159.         {
  160.             return "Name: " + Name + ", Burst: " +
  161.                 BurstTime + ", Arrival: " + ArrivalTime + ", Priority: " + Priority;
  162.         }
  163.  
  164.  
  165.     }
  166.  
  167.  
  168.  
  169.  
  170.  
  171.     class ProcessNode // "המחלקה "צומת
  172.     {
  173.         public aProcess infoObject;
  174.         private ProcessNode next; // הוא כתובת האיבר הבא ברשימה next האובייקט
  175.  
  176.  
  177.         // פונקציה בונה שלא מקבלת שום פרמטר
  178.         public ProcessNode()
  179.         {
  180.             this.infoObject = new aProcess();
  181.             this.next = null;
  182.         }
  183.  
  184.  
  185.         // פונקציה בונה שמקבלת רק תוכן מלא ולא מצביע
  186.         public ProcessNode(String name, int burst, int arrival, int priority)
  187.         {
  188.             infoObject = new aProcess(name, burst, arrival, priority);
  189.             this.next = null;
  190.         }
  191.  
  192.  
  193.         // פונקציה בונה שמקבלת רק תוכן ולא מצביע ולא שדה עדיפות
  194.         // פונקציה בונה שמקבלת רק תוכן מלא ולא מצביע
  195.         public ProcessNode(String name, int burst, int arrival)
  196.         {
  197.             infoObject = new aProcess(name, burst, arrival);
  198.             this.next = null;
  199.         }
  200.  
  201.  
  202.  
  203.         // פונקציה בונה שמקבלת תוכן, ומצביע
  204.         public ProcessNode(String name, int burst, int arrival, int priority, ProcessNode next)
  205.             : this(name, burst, arrival, priority)
  206.         {
  207.             this.next = next;
  208.         }
  209.  
  210.         // פונקציה בונה שמקבלת תוכן בלי שדה עדיפות, ומצביע
  211.         public ProcessNode(String name, int burst, int arrival, ProcessNode next)
  212.             : this(name, burst, arrival)
  213.         {
  214.             this.next = next;
  215.         }
  216.  
  217.  
  218.  
  219.         public ProcessNode(aProcess newInfoObject, ProcessNode newNext)
  220.         {
  221.             infoObject = newInfoObject;
  222.             next = newNext;
  223.         }
  224.  
  225.  
  226.  
  227.  
  228.         // Set -ו Get  פונקציות
  229.  
  230.         public ProcessNode GetNext()
  231.         {
  232.             return next;
  233.         }
  234.  
  235.         public void SetNext(ProcessNode next)
  236.         {
  237.             this.next = next;
  238.         }
  239.  
  240.  
  241.         public static int Compare(ProcessNode p1, ProcessNode p2)
  242.         {
  243.             if (p1.infoObject.GetBurstTime() > p2.infoObject.GetBurstTime()) return 1;
  244.             if (p1.infoObject.GetBurstTime() < p2.infoObject.GetBurstTime()) return -1;
  245.             return 0;
  246.         }
  247.     }
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.     class ProcessesCircularList // רשימה מעגלית של תהליכים - נועד להיות תור המוכנים
  255.     {
  256.  
  257.         private ProcessNode Head;
  258.         private ProcessNode Last;
  259.         private int length;
  260.         public int Length { get { return length; } }
  261.  
  262.  
  263.         public ProcessesCircularList()
  264.         {
  265.             //Head = new ProcessNode();
  266.             Head = null;
  267.             Last = null;
  268.             length = 0;
  269.         }
  270.  
  271.         public ProcessesCircularList(bool isFromUser)
  272.         {
  273.             if (isFromUser == false)
  274.             {
  275.                 Head = null;
  276.                 Last = null;
  277.                 length = 0;
  278.             }
  279.             else
  280.             {
  281.                 CreateListFromUser(out Head, out Last, out length);
  282.             }
  283.         }
  284.  
  285.  
  286.         // פונקציה בונה שיוצרת העתק של רשימה שהיא מקבלת
  287.         public ProcessesCircularList(ProcessesCircularList oldList)
  288.         {
  289.             if (oldList == null)
  290.             {
  291.                 Head = null;
  292.                 Last = null;
  293.                 length = 0;
  294.                 return;
  295.             }
  296.  
  297.  
  298.             // (אתחול ה"ראש" צריך להיות לפני הלולאה)
  299.             ProcessNode p = new ProcessNode(oldList.Head.infoObject.CloneP(), null);
  300.             ProcessNode oldP = oldList.Head.GetNext();
  301.             Head = Last = p;
  302.             length = oldList.Length;
  303.  
  304.             for (int i = 1; i < length; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
  305.             {
  306.                 p = new ProcessNode(oldP.infoObject.CloneP(), null);
  307.                 Last.SetNext(p);
  308.                 Last = p;
  309.  
  310.                 oldP = oldP.GetNext();
  311.             }
  312.             Last.SetNext(Head); // רשימה מעגלית
  313.         }
  314.  
  315.  
  316.  
  317.         public void CreateListFromUser(out ProcessNode head, out ProcessNode last, out int thisLength)
  318.         {
  319.             ProcessNode p;
  320.             head = null;
  321.             last = null;
  322.  
  323.             thisLength = UT.GetINTFromUser("Enter amount of Processes: ", 1000, 1);
  324.             Console.WriteLine("\nMode:\n----");
  325.             Console.WriteLine("1 = Process Name, BurstTime, ArrivalTime, Priority");
  326.             Console.WriteLine("2 = Process Name, BurstTime, ArrivalTime");
  327.             Console.WriteLine("3 = Process Name, BurstTime");
  328.             Console.WriteLine();
  329.  
  330.             int mode = UT.GetINTFromUser("Enter User Mode: ", 3, 1);
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.             switch (mode)
  340.             {
  341.                 case 1:
  342.                     {
  343.                         // (אתחול ה"ראש" צריך להיות לפני הלולאה)
  344.                         Console.WriteLine("\n1.");
  345.                         p = new ProcessNode();
  346.                         p.infoObject.Set_AllFields_FromUser();
  347.                         head = last = p;
  348.  
  349.  
  350.                         for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
  351.                         {
  352.                             Console.WriteLine("\n" + (i + 1) + ".");
  353.                             p = new ProcessNode();
  354.                             p.infoObject.Set_AllFields_FromUser();
  355.                             last.SetNext(p);
  356.                             last = p;
  357.                         }
  358.                         last.SetNext(head); // רשימה מעגלית
  359.  
  360.                         break;
  361.                     }
  362.  
  363.                 case 2:
  364.                     {
  365.                         // (אתחול ה"ראש" צריך להיות לפני הלולאה)
  366.                         Console.WriteLine("\n1.");
  367.                         p = new ProcessNode();
  368.                         p.infoObject.Set_NameAndBurstAndArrival_FromUser();
  369.                         head = last = p;
  370.  
  371.  
  372.                         for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
  373.                         {
  374.                             Console.WriteLine("\n" + (i + 1) + ".");
  375.                             p = new ProcessNode();
  376.                             p.infoObject.Set_NameAndBurstAndArrival_FromUser();
  377.                             last.SetNext(p);
  378.                             last = p;
  379.                         }
  380.                         last.SetNext(head); // רשימה מעגלית
  381.  
  382.                         break;
  383.                     }
  384.  
  385.                 case 3:
  386.                     {
  387.                         // (אתחול ה"ראש" צריך להיות לפני הלולאה)
  388.                         Console.WriteLine("\n1.");
  389.                         p = new ProcessNode();
  390.                         p.infoObject.Set_NameAndBurst_FromUser();
  391.                         head = last = p;
  392.  
  393.  
  394.                         for (int i = 1; i < thisLength; i++) // מתחילים מ-1 כי עם ה"ראש" כבר סיימנו
  395.                         {
  396.                             Console.WriteLine("\n" + (i + 1) + ".");
  397.                             p = new ProcessNode();
  398.                             p.infoObject.Set_NameAndBurst_FromUser();
  399.                             last.SetNext(p);
  400.                             last = p;
  401.                         }
  402.                         last.SetNext(head); // רשימה מעגלית
  403.  
  404.                         break;
  405.                     }
  406.             }
  407.         }
  408.  
  409.  
  410.  
  411.         public ProcessesCircularList Clone()
  412.         {
  413.             return new ProcessesCircularList(this);
  414.         }
  415.  
  416.         public void AddToEnd(aProcess newProcess)
  417.         {
  418.             if (length == 0)
  419.             {
  420.                 Head = new ProcessNode(newProcess, Head);
  421.                 Last = Head;
  422.                 length = 1;
  423.             }
  424.             else
  425.             {
  426.                 Last.SetNext(new ProcessNode(newProcess, Head));
  427.                 Last = Last.GetNext();
  428.                 length++;
  429.             }
  430.  
  431.         }
  432.  
  433.         // הוספה של איבר לרשימה
  434.         // זהירות! משנה את המצביע של האיבר המקורי
  435.         public void AddToEnd(ProcessNode newProcessNode)
  436.         {
  437.             if (length == 0)
  438.             {
  439.                 Head = newProcessNode;
  440.                 Last = newProcessNode;
  441.                 newProcessNode.SetNext(newProcessNode);
  442.                 length = 1;
  443.             }
  444.             else
  445.             {
  446.                 Last.SetNext(newProcessNode);
  447.                 Last = newProcessNode;
  448.                 Last.SetNext(Head);
  449.                 length++;
  450.             }
  451.  
  452.         }
  453.  
  454.  
  455.  
  456.  
  457.  
  458.            
  459.         public void Show()
  460.         {
  461.             int i = 0;
  462.             ProcessNode p = Head;
  463.             if(length == 0) return;
  464.             do
  465.             {
  466.                 Console.WriteLine((++i) + ".");
  467.                 Console.WriteLine(p.infoObject);
  468.                 p = p.GetNext();
  469.             } while (p != Head);
  470.         }
  471.  
  472.  
  473.         public aProcess GetFirst()
  474.         {
  475.             if (length == 0) return null;
  476.             return Head.infoObject;
  477.         }
  478.         public aProcess GetLast()
  479.         {
  480.             if (length == 0) return null;
  481.             return Last.infoObject;
  482.         }
  483.  
  484.  
  485.         public void RemoveFirst()
  486.         {
  487.             if (length == 0) return; // אם זו רשימה ריקה
  488.             if (length == 1) // אם יש רק איבר אחד
  489.             {
  490.                 Head = null;
  491.                 Last = null;
  492.                 length = 0;
  493.                 return;
  494.             }
  495.  
  496.             Head = Head.GetNext();
  497.             length--;
  498.         }
  499.  
  500.         public aProcess TakeFirstProcess() // מחזיר את התהליך הראשון ומוחק אותו
  501.         {
  502.             if (length == 0) return null;
  503.  
  504.             aProcess tmp = Head.infoObject;
  505.  
  506.             if (length == 1) // אם יש רק איבר אחד
  507.             {
  508.                 Head = null;
  509.                 Last = null;
  510.                 length = 0;
  511.                 return tmp;
  512.             }
  513.  
  514.             Head = Head.GetNext();
  515.             length--;
  516.             return tmp;
  517.         }
  518.  
  519.  
  520.  
  521.  
  522.         public ProcessNode TakeFirstNode() // מחזיר את התהליך הראשון ומוחק אותו
  523.         {
  524.             if (length == 0) return null;
  525.  
  526.             ProcessNode tmp = Head;
  527.  
  528.             if (length == 1) // אם יש רק איבר אחד
  529.             {
  530.                 Head = null;
  531.                 Last = null;
  532.                 length = 0;
  533.                 return tmp;
  534.             }
  535.  
  536.             Head = Head.GetNext();
  537.             length--;
  538.             return tmp;
  539.         }
  540.  
  541.  
  542.  
  543.  
  544.         public aProcess Get(int index)
  545.         {
  546.             if (length == 0) return null;
  547.             if (index >= length) return null;
  548.             if (index < 0) return null;
  549.             int i = 0;
  550.             ProcessNode p = Head;
  551.            
  552.             do
  553.             {
  554.                 if (i == index)
  555.                 {
  556.                     return p.infoObject;
  557.                 }
  558.                 i++;
  559.                 p = p.GetNext();
  560.             } while (p != Head);
  561.             return null; // אם לא מצא
  562.         }
  563.  
  564.  
  565.  
  566.         // מיון ששם את הקצר ביותר בהתחלה
  567.         // לא מיון חכם, אבל עובד
  568.         public void Sort()
  569.         {
  570.             ProcessNode tmp;
  571.             ProcessNode p0;
  572.             ProcessNode p1;
  573.             ProcessNode p2;
  574.             bool isReplace;
  575.  
  576.             if (Head == Head.GetNext()) return;
  577.             if (length == 2) // משום מה זה צריך תנאי מיוחד
  578.             {
  579.                 if (ProcessNode.Compare(Head, Last) == 1)
  580.                 {
  581.                     tmp = Head;
  582.                     Head = Last;
  583.                     Last = tmp;
  584.                 }
  585.                 return;
  586.             }
  587.  
  588.             do{
  589.                 isReplace = false;
  590.                 p0 = Last;
  591.                 p1 = Head;
  592.                 p2 = Head.GetNext();
  593.  
  594.                 for (int i = 0; i < Length - 1; i++)
  595.                 {
  596.                     // if p1 bigger ->  replace!
  597.                     if (ProcessNode.Compare(p1, p2) == 1)
  598.                     {
  599.                         isReplace = true;
  600.                         if (p2 == Last) Last = p1;
  601.  
  602.                         p1.SetNext(p2.GetNext());
  603.                         p2.SetNext(p1);
  604.                         p0.SetNext(p2);
  605.  
  606.                         tmp = p2;
  607.                         p2 = p1;
  608.                         p1 = tmp;
  609.  
  610.                         if (p2 == Head) Head = p1;
  611.                     }
  612.  
  613.  
  614.                     p0 = p0.GetNext();
  615.                     p1 = p1.GetNext();
  616.                     p2 = p2.GetNext();
  617.                 }
  618.             } while (isReplace == true);
  619.         }
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.         ///////  Scheduling Algorithms:   ///////
  627.  
  628.  
  629.         public void SimpleFIFO(out double WaitingTime_AVG, out double Turnaround_AVG)
  630.         {
  631.             int WaitingTime_SUM = 0;     // סכום זמני המתנה
  632.             int Turnaround_SUM = 0;      // סכום זמני סבב
  633.  
  634.             WaitingTime_AVG = 0;  // זמן המתנה ממוצע
  635.             Turnaround_AVG = 0;   // זמן סבב ממוצע
  636.  
  637.  
  638.             // בדיקת זמן המתנה
  639.  
  640.             ProcessNode currentProcess = Head;
  641.             ProcessNode tmp;
  642.             for (int i = 0; i < length; i++)
  643.             {
  644.                 tmp = Head;
  645.                 for (int j = 0; j < i; j++) // "סיכום התהליכים שהיו לפני ה"תהליך-הנוכחי
  646.                 {
  647.                     WaitingTime_SUM += tmp.infoObject.GetBurstTime();
  648.  
  649.  
  650.                     tmp = tmp.GetNext();
  651.                 }
  652.                 WaitingTime_SUM -= currentProcess.infoObject.GetArrivalTime();
  653.                 currentProcess = currentProcess.GetNext();
  654.             }
  655.             WaitingTime_AVG = (double)WaitingTime_SUM / length;
  656.  
  657.  
  658.  
  659.  
  660.             // בדיקת זמן סבב
  661.  
  662.             currentProcess = Head;
  663.             for (int i = 0; i < length; i++)
  664.             {
  665.                 tmp = Head;
  666.                 for (int j = 0; j <= i; j++) // "סיכום התהליכים שהיו לפני ה"תהליך-הנוכחי
  667.                 {
  668.                     Turnaround_SUM += tmp.infoObject.GetBurstTime();
  669.  
  670.  
  671.                     tmp = tmp.GetNext();
  672.                 }
  673.                 Turnaround_SUM -= currentProcess.infoObject.GetArrivalTime();
  674.                 currentProcess = currentProcess.GetNext();
  675.             }
  676.             Turnaround_AVG = (double)Turnaround_SUM / length;
  677.  
  678.             // Show:
  679.  
  680.             tmp = Head;
  681.             do{
  682.                 Console.Write("{0}({1}), ", tmp.infoObject.GetName(), tmp.infoObject.GetBurstTime());
  683.                 tmp = tmp.GetNext();
  684.             } while (tmp != Head);
  685.             Console.WriteLine("\n");
  686.         }
  687.  
  688.         public void SJF_NoPreemptive(out double WaitingTime_AVG, out double Turnaround_AVG)
  689.         {
  690.             int WaitingTime_SUM = 0;     // סכום זמני המתנה
  691.             int Turnaround_SUM = 0;      // סכום זמני סבב
  692.  
  693.             WaitingTime_AVG = 0;  // זמן המתנה ממוצע
  694.             Turnaround_AVG = 0;   // זמן סבב ממוצע
  695.             ProcessNode tmp;
  696.  
  697.             // מעתיק את הרשימה לרשימה חדשה שמותר לשנות
  698.             ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
  699.  
  700.             // מערך ההיסטוריה
  701.             aProcess[] history = new aProcess[2000];
  702.             int index = 0;
  703.  
  704.  
  705.             for (int i = 0; i < newReadyQueue.Length; i++)
  706.             {
  707.  
  708.  
  709.                 // רשימה ריקה שעתידה להכיל ולמיין את כל התהליכים שכבר הגיעו
  710.                 ProcessesCircularList ArrivedQueue = new ProcessesCircularList();
  711.  
  712.  
  713.                 // מילוי רשימת התהליכים שכבר הגיעו
  714.                 tmp = newReadyQueue.Head;
  715.                 do
  716.                 {
  717.                     // אם התהליך כבר הגיע ועוד לא התבצע
  718.                     if (tmp.infoObject.GetArrivalTime() <= 0 && tmp.infoObject.HelpNumber == 0)
  719.                     {
  720.                         ArrivedQueue.AddToEnd(tmp.infoObject);
  721.                     }
  722.  
  723.                     tmp = tmp.GetNext();
  724.                 } while (tmp != newReadyQueue.Head);
  725.  
  726.  
  727.                 if (ArrivedQueue.Length == 0)
  728.                 {
  729.                     Console.WriteLine("*Error1");
  730.                     return;
  731.                 }
  732.                 if (index == 2000)
  733.                 {
  734.                     Console.WriteLine("*Error2");
  735.                     return;
  736.                 }
  737.  
  738.  
  739.                 // מיון הרשימה מהקטן לגדול
  740.                 ArrivedQueue.Sort();
  741.  
  742.                 ArrivedQueue.GetFirst().HelpNumber = 1; // newReadyQueue זה משפיע גם על האיבר שברשימה
  743.  
  744.                 // הוספה של הראשון שעכשיו התבצע בשלמותו אל מערך ההיסטוריה
  745.                 history[index++] = ArrivedQueue.GetFirst();
  746.  
  747.  
  748.  
  749.                 // להחסיר מזמן ההגעה של כל הנותרים שלא התבצעו את זמן הביצוע של הנוכחי
  750.                 tmp = newReadyQueue.Head;
  751.                 do
  752.                 {
  753.                     // אם התהליך עוד לא התבצע
  754.                     if (tmp.infoObject.HelpNumber == 0)
  755.                     {
  756.                         tmp.infoObject.SetArrivalTime( tmp.infoObject.GetArrivalTime() -
  757.                             ArrivedQueue.GetFirst().GetBurstTime() );
  758.                     }
  759.  
  760.                     tmp = tmp.GetNext();
  761.                 } while (tmp != newReadyQueue.Head);
  762.             }
  763.  
  764.  
  765.             // בירור זמן סבב וזמן המתנה
  766.             tmp = newReadyQueue.Head;
  767.             do
  768.             {
  769.                 WaitingTime_SUM += (tmp.infoObject.GetArrivalTime() * -1);
  770.                 // (זמן סבב הוא זמן המתנה פלוס זמן ביצוע)
  771.                 Turnaround_SUM += (tmp.infoObject.GetArrivalTime() * -1) + tmp.infoObject.GetBurstTime();
  772.  
  773.                 tmp = tmp.GetNext();
  774.             } while (tmp != newReadyQueue.Head);
  775.  
  776.             WaitingTime_AVG = (double)WaitingTime_SUM / length;
  777.             Turnaround_AVG = (double)Turnaround_SUM / length;
  778.  
  779.  
  780.  
  781.  
  782.             // Show:
  783.             for (int i = 0; i < index; i++)
  784.             {
  785.                 Console.Write("{0}({1}), ", history[i].GetName(), history[i].GetBurstTime());
  786.             }
  787.             Console.WriteLine("\n");
  788.         }
  789.  
  790.  
  791.  
  792.  
  793.  
  794.  
  795.         public void SJF_Preemptive(out double WaitingTime_AVG, out double Turnaround_AVG)
  796.         {
  797.             int WaitingTime_SUM = 0;     // סכום זמני המתנה
  798.             int Turnaround_SUM = 0;      // סכום זמני סבב
  799.  
  800.             WaitingTime_AVG = 0;  // זמן המתנה ממוצע
  801.             Turnaround_AVG = 0;   // זמן סבב ממוצע
  802.             ProcessNode tmp;
  803.  
  804.             // מעתיק את הרשימה לרשימה חדשה שמותר לשנות
  805.             ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
  806.  
  807.             // מערך ההיסטוריה
  808.             aProcess[] history = new aProcess[2000];
  809.             int index = 0;
  810.  
  811.  
  812.             int leftProcesses_Number = length;
  813.             while (leftProcesses_Number > 0)
  814.             {
  815.                 // רשימה ריקה שעתידה להכיל ולמיין את כל התהליכים שכבר הגיעו
  816.                 ProcessesCircularList ArrivedQueue = new ProcessesCircularList();
  817.  
  818.                 // (newReadyQueue משפיע על ArrivedQueue) .מילוי רשימת התהליכים שכבר הגיעו
  819.                 tmp = newReadyQueue.Head;
  820.                 do
  821.                 {
  822.                     // אם התהליך כבר הגיע ועוד לא נגמר
  823.                     if (tmp.infoObject.GetArrivalTime() <= 0 && tmp.infoObject.GetBurstTime() > 0)
  824.                     {
  825.                         ArrivedQueue.AddToEnd(tmp.infoObject);
  826.                     }
  827.  
  828.                     tmp = tmp.GetNext();
  829.                 } while (tmp != newReadyQueue.Head);
  830.  
  831.                 // בדיקת שגיאות
  832.  
  833.                 if (ArrivedQueue.Length == 0)
  834.                 {
  835.                     Console.WriteLine("*Error3");
  836.                     return;
  837.                 }
  838.                 if (index == 2000)
  839.                 {
  840.                     Console.WriteLine("*Error4");
  841.                     return;
  842.                 }
  843.  
  844.  
  845.                 // מיון הרשימה מהקטן לגדול
  846.                 ArrivedQueue.Sort();
  847.  
  848.                 // הוספה של הראשון ברשימה שהוא הקצר ביותר אל מערך ההיסטוריה
  849.                 if (index == 0) // אם זו הפעם הראשונה - אסור לצאת מהמערך אל אינדקס מינוס אחד
  850.                 {
  851.                     history[index] = ArrivedQueue.GetFirst().CloneP();
  852.                     history[index++].SetBurstTime(0);
  853.                 }
  854.                 else
  855.                 {
  856.                     // אם מדובר באיבר שלא היה שם כבר
  857.                     // (לאיבר שכבר נמצא שם פשוט נוסיף)
  858.                     if (history[index-1].GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
  859.                     {
  860.                         history[index] = ArrivedQueue.GetFirst().CloneP();
  861.                         history[index++].SetBurstTime(0);
  862.                     }
  863.                 }
  864.  
  865.  
  866.                 // כל עוד "זמן ביצוע" של תהליך נוכחי לא שווה ל-אפס
  867.                 while (true)
  868.                 {
  869.  
  870.                     // לחסר 1 מזמן ההגעה של כל התהליכים שלא נגמרו וגם אינם התהליך הנוכחי
  871.                     ProcessNode tmp2 = newReadyQueue.Head;
  872.                     do
  873.                     {
  874.                         if (tmp2.infoObject.GetBurstTime() > 0 &&
  875.                             tmp2.infoObject.GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
  876.                         {
  877.                             tmp2.infoObject.SetArrivalTime(tmp2.infoObject.GetArrivalTime()-1);
  878.                         }
  879.  
  880.                         tmp2 = tmp2.GetNext();
  881.                     } while (tmp2 != newReadyQueue.Head);
  882.  
  883.                     // newReadyQueue לחסר 1 מ"זמן ביצוע" של התהליך הנוכחי. משפיע גם על הרשימה
  884.                     ArrivedQueue.GetFirst().SetBurstTime( ArrivedQueue.GetFirst().GetBurstTime()-1 );
  885.  
  886.                     // להוסיף 1 לאיבר האחרון במערך ההיסטוריה
  887.                     history[index-1].SetBurstTime( history[index-1].GetBurstTime()+1 );
  888.  
  889.  
  890.                     // אם התהליך נגמר - לצאת
  891.                     if(ArrivedQueue.GetFirst().GetBurstTime() == 0) {
  892.                         leftProcesses_Number--;
  893.                         break;
  894.                     }
  895.  
  896.                     // newReadyQueue לסרוק את כל הרשימה
  897.                     // אם יש תהליך שגם "זמן ההגעה" שלו שווה ל-אפס
  898.                     // וגם הוא אינו התהליך הנוכחי וגם הוא לא גמור
  899.                     //זמן הביצוע שלו קטן מזמן הביצוע שנותר לתהליך הנוכחי - לצאת מהלולאה
  900.                     tmp2 = newReadyQueue.Head;
  901.                     bool needToStop = false;
  902.                     do
  903.                     {
  904.                         if (tmp2.infoObject.GetArrivalTime() == 0 && tmp2.infoObject.GetBurstTime() != 0 &&
  905.                             tmp2.infoObject.GetName().Equals(ArrivedQueue.GetFirst().GetName()) == false)
  906.                         {
  907.                             if (tmp2.infoObject.GetBurstTime() < ArrivedQueue.GetFirst().GetBurstTime())
  908.                             {
  909.                                 needToStop = true;
  910.                                 break;
  911.                             }
  912.  
  913.                         }
  914.  
  915.                         tmp2 = tmp2.GetNext();
  916.                     } while (tmp2 != newReadyQueue.Head);
  917.                     if (needToStop == true) break;
  918.                    
  919.                 }
  920.  
  921.             }
  922.  
  923.             // Show:
  924.             for (int i = 0; i < index; i++)
  925.             {
  926.                 Console.Write("{0}({1}), ", history[i].GetName(), history[i].GetBurstTime());
  927.             }
  928.             Console.WriteLine("\n");
  929.  
  930.  
  931.             // חישוב סכום כל זמני ההמתנה
  932.             ProcessNode tmp3 = newReadyQueue.Head;
  933.             do{
  934.                 WaitingTime_SUM += tmp3.infoObject.GetArrivalTime() * -1;
  935.                 tmp3 = tmp3.GetNext();
  936.             } while (tmp3 != newReadyQueue.Head);
  937.  
  938.  
  939.             // חישוב סכום כל זמני הסבב
  940.             // (זמן סבב הוא זמן המתנה פלוס זמן ביצוע)
  941.             Turnaround_SUM += WaitingTime_SUM;
  942.             tmp3 = Head;
  943.             do
  944.             {
  945.                 Turnaround_SUM += tmp3.infoObject.GetBurstTime();
  946.                 tmp3 = tmp3.GetNext();
  947.             } while (tmp3 != Head);
  948.  
  949.             // :סוף סוף מסקנות
  950.             WaitingTime_AVG = (double)WaitingTime_SUM / length;
  951.             Turnaround_AVG = (double)Turnaround_SUM / length;
  952.         }
  953.  
  954.  
  955.  
  956.  
  957.         private class OnlyNameAndTimeOfProcess
  958.         {
  959.             public String Name;
  960.             public int Time;
  961.             public bool IsLast;
  962.             public OnlyNameAndTimeOfProcess(String Name, int Time)
  963.             {
  964.                 this.Name = Name;
  965.                 this.Time = Time;
  966.                 IsLast = false;
  967.             }
  968.             public OnlyNameAndTimeOfProcess()
  969.             {
  970.                 this.Name = "No";
  971.                 this.Time = -1;
  972.                 IsLast = false;
  973.             }
  974.  
  975.             public void show()
  976.             {
  977.                 Console.Write("{0}({1}), ", Name, Time);
  978.             }
  979.         }
  980.         private int BurstMinusQuantum(int Burst, int quantum)
  981.         {
  982.             int x = Burst - quantum;
  983.             if (x >= 0) return x;
  984.             return 0;
  985.         }
  986.         public void RR_FIFO(out double WaitingTime_AVG, out double Turnaround_AVG, int quantum )
  987.         {
  988.             // העתקה של המקור אל רשימת עזר שמותר לבצע בה שינויים
  989.             // (אם היינו לומדים DEEP CLONE היה עדיף להשתמש ב)
  990.             ProcessesCircularList newReadyQueue = new ProcessesCircularList(this);
  991.             ProcessNode currentProcessNode = newReadyQueue.Head;
  992.  
  993.             // מערך ההיסטוריה בעצם מציג את כל מה שקרה
  994.             //                  A20 B15 C10 D7 Q10 :עבור
  995.             //A(10), B(10), C10, D(7), A(10), B(5) :יכיל
  996.             OnlyNameAndTimeOfProcess[] history = new OnlyNameAndTimeOfProcess[2000];
  997.  
  998.  
  999.  
  1000.             int index = 0;
  1001.  
  1002.  
  1003.             // מספר התהליכים שנותרו לא גמורים
  1004.             int leftProcesses_Number = newReadyQueue.Length;
  1005.             while (leftProcesses_Number > 0)
  1006.             {
  1007.                 aProcess currentProcess = currentProcessNode.infoObject;
  1008.  
  1009.                 // מדלגים על תהליכים שנגמרו
  1010.                 if (currentProcess.GetBurstTime() == 0) goto INCREASE;
  1011.  
  1012.                 // (עושים כאן ניו משום שיצירת המערך לא הפעילה את הפונקציה הבונה של איבריו)
  1013.                 history[index] = new OnlyNameAndTimeOfProcess();
  1014.  
  1015.  
  1016.  
  1017.                 //  :החסרה של הקוונטום מהתהליך הנוכחי
  1018.  
  1019.  
  1020.                 // אם התהליך הנוכחי לא יגמר בעת ביצוע הקוונטום
  1021.                 if (currentProcess.GetBurstTime() > quantum)
  1022.                 {
  1023.                     history[index].Time = quantum;
  1024.                     history[index].Name = currentProcess.GetName();
  1025.                        // הפחתת הקוונטום מהזמן שנותר לתהליך זה. כתיבה ארוכה משום שהוא פרייבט
  1026.                     currentProcess.SetBurstTime(currentProcess.GetBurstTime() - quantum);
  1027.                 }
  1028.  
  1029.                 // אם התהליך הנוכחי כן יגמר בעת ביצוע הקוונטום
  1030.                 else
  1031.                 {
  1032.                     history[index].Time = currentProcess.GetBurstTime();
  1033.                     history[index].Name = currentProcess.GetName();
  1034.                     history[index].IsLast = true;
  1035.                     // הפחתת הקוונטום מהזמן שנותר לתהליך זה. כתיבה ארוכה משום שהוא פרייבט
  1036.                     currentProcess.SetBurstTime(0);
  1037.                     leftProcesses_Number--;
  1038.                 }
  1039.                
  1040.                 index++;
  1041.             INCREASE:
  1042.                 currentProcessNode = currentProcessNode.GetNext();
  1043.             }
  1044.  
  1045.  
  1046.             // show:
  1047.             for (int i = 0; i < index; i++)
  1048.             {
  1049.                 history[i].show();
  1050.             }
  1051.             Console.WriteLine("\n");
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.  
  1059.  
  1060.             int WaitingTime_SUM = 0;     // סכום זמני המתנה
  1061.             int Turnaround_SUM = 0;      // סכום זמני סבב
  1062.  
  1063.             WaitingTime_AVG = 0;  // זמן המתנה ממוצע
  1064.             Turnaround_AVG = 0;   // זמן סבב ממוצע
  1065.  
  1066.             ProcessNode pInOriginal = Head;
  1067.             //currentProcessNode = newReadyQueue.Head;
  1068.             do{
  1069.                 String currentProcessName = pInOriginal.infoObject.GetName();
  1070.  
  1071.                 for (int i = 0; i < history.Length; i++)
  1072.                 {
  1073.                     if (history[i].Name.Equals(currentProcessName) &&
  1074.                         history[i].IsLast == true) break;
  1075.                     if (history[i].Name.Equals(currentProcessName)) continue;
  1076.  
  1077.                     WaitingTime_SUM += history[i].Time;
  1078.                     Turnaround_SUM += history[i].Time;
  1079.                 }
  1080.  
  1081.                 // זמן סבב של תהליך הוא זמן ההמתנה שלו פלוס זמן הביצוע שלו
  1082.                 Turnaround_SUM += pInOriginal.infoObject.GetBurstTime();
  1083.  
  1084.                 // מחסרים זמן הגעה מזמן המתנה ומזמן סבב
  1085.                 WaitingTime_SUM -= pInOriginal.infoObject.GetArrivalTime();
  1086.                 Turnaround_SUM -= pInOriginal.infoObject.GetArrivalTime();
  1087.  
  1088.  
  1089.  
  1090.  
  1091.                 pInOriginal = pInOriginal.GetNext();
  1092.                 //currentProcessNode = currentProcessNode.GetNext();
  1093.             } while (pInOriginal != Head); // ריצה על רשימה מקושרת מעגלית
  1094.  
  1095.             WaitingTime_AVG = (double)WaitingTime_SUM / length;
  1096.             Turnaround_AVG = (double)Turnaround_SUM / length;
  1097.         }
  1098.  
  1099.  
  1100.     }
  1101.  
  1102.  
  1103.     class Program
  1104.     {
  1105.         static void Main(string[] args)
  1106.         {
  1107.             START:
  1108.             String UserWantsAnotherAlgorithm = "";
  1109.            
  1110.             double WaitingTime_AVG=-1, Turnaround_AVG=-1;
  1111.             int quantum=1;
  1112.             ProcessesCircularList ReadyQueue = new ProcessesCircularList(true); // ממלא מידע מהמשתמש
  1113.  
  1114.             Console.WriteLine("\n\n\nYour Processes:\n--------------");
  1115.             //ReadyQueue.Show();
  1116.  
  1117.  
  1118.             ALGORITHMS:
  1119.             ReadyQueue.Show(); // זה אמור להיות איפה שההערה, אבל בנתיים בשביל הבדיקות
  1120.             Console.WriteLine("\n\n\nAlgorithms:\n----------");
  1121.             Console.WriteLine("1 = FIFO, NON PREEMPTIVE");
  1122.             Console.WriteLine("2 = SJF, NON PREEMPTIVE");
  1123.             Console.WriteLine("3 = SJF, PREEMPTIVE");
  1124.             Console.WriteLine("4 = RR (algorithm with \"quantum\"), FIFO, NON PREEMPTIVE");
  1125.             Console.WriteLine();
  1126.             int mode = UT.GetINTFromUser("Choose Algorithm: ", 4, 1);
  1127.             if(mode == 4) {quantum = UT.GetINTFromUser("Enter quantum: ", 1000000, 1); }
  1128.  
  1129.             Console.WriteLine("Results:\n-------\n\n\n");
  1130.             switch (mode)
  1131.             {
  1132.                 case 1:
  1133.                     {
  1134.                         ReadyQueue.SimpleFIFO(out WaitingTime_AVG, out Turnaround_AVG);
  1135.                         break;
  1136.                     }
  1137.  
  1138.                 case 2:
  1139.                     {
  1140.                         ReadyQueue.SJF_NoPreemptive(out WaitingTime_AVG, out Turnaround_AVG);
  1141.                         break;
  1142.                     }
  1143.  
  1144.                 case 3:
  1145.                     {
  1146.                         ReadyQueue.SJF_Preemptive(out WaitingTime_AVG, out Turnaround_AVG);
  1147.                         break;
  1148.                     }
  1149.  
  1150.                 case 4:
  1151.                     {
  1152.                         ReadyQueue.RR_FIFO(out WaitingTime_AVG, out Turnaround_AVG, quantum);
  1153.                         break;
  1154.                     }
  1155.             }
  1156.             Console.WriteLine("WaitingTime AVG: " + WaitingTime_AVG);
  1157.             Console.WriteLine("Turnaround AVG: " + Turnaround_AVG);
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.             do{
  1164.             UserWantsAnotherAlgorithm =
  1165.                 UT.GetTextFromUser("\n\nDo you want Another Algorithm? (Y/N): ");
  1166.             } while (!UT.IsYesOrNo(UserWantsAnotherAlgorithm));
  1167.             if (UT.IsYes(UserWantsAnotherAlgorithm)) goto ALGORITHMS;
  1168.             else {
  1169.                 Console.Clear();
  1170.                 goto START;
  1171.             }
  1172.  
  1173.         }
  1174.     }
  1175. }
Add Comment
Please, Sign In to add comment