public void sort(int sel, int siz, process p) { process tmp = p; int n = siz; int x, y; for (x = 0; x < n; x++) { process tmp2 = tmp.get_next(); for (y = 0; y < n-x- 1; y++) { if (sel == 0) { int c = tmp.get_aT()-tmp.get_timePassed(); int nn = tmp2.get_aT()-tmp.get_timePassed(); if (c > nn) { swap(tmp, tmp2); } } else if (sel == 1) { int c = tmp.get_bT()-tmp.get_bTimePassed(); int nn = tmp2.get_bT()-tmp2.get_bTimePassed(); if (c > nn) { swap(tmp, tmp2); } } else if (sel == 2) { int c = tmp.get_priority(); int nn = tmp2.get_priority(); if (c > nn) { swap(tmp, tmp2); } } tmp2 =tmp2.get_next(); } tmp=tmp.get_next(); } }
public void swap(process tmp,process tmp2) { string tmpStr = tmp2.get_processName(); int tmpAT = tmp2.get_aT(); int tmpBT = tmp2.get_bT(); int tmpPir = tmp2.get_priority(); int tmpatp = tmp2.get_timePassed(); int tmpbtp = tmp2.get_bTimePassed(); tmp2.set_aT(tmp.get_aT()); tmp2.set_bT(tmp.get_bT()); tmp2.set_processName(tmp.get_processName()); tmp2.set_priority(tmp.get_priority()); tmp2.set_timePassed(tmp.get_timePassed()); tmp2.set_bTimePassed(tmp.get_bTimePassed()); tmp.set_aT(tmpAT); tmp.set_bT(tmpBT); tmp.set_processName(tmpStr); tmp.set_priority(tmpPir); tmp.set_timePassed(tmpatp); tmp.set_bTimePassed(tmpbtp); }
private void button2_Click(object sender, EventArgs e) { int size = ls.size(); if (size == 0) { Form2 m = new Form2(); m.Show(); return; } listBox2.Items.Clear(); float waiting = 0; float turna = 0; string gantt = ""; if (comboBox1.Text == "FCFS") { ls.sort(0, size, ls.get_process()); process p = ls.get_process(); int begin = p.get_aT(); for (int i = 0; i < ls.size(); i++) { if (i == 0) { listBox2.Items.Add(p.get_processName() + " " + "waiting=0" + " " + "Departure=" + (p.get_aT() + p.get_bT())); gantt = gantt + "| " + p.get_processName() + " |"; begin = p.get_bT() + p.get_aT(); } else { p = p.get_next(); int d; if (begin >= p.get_aT()) { d = (begin + p.get_bT()); waiting = waiting + (begin - p.get_aT()); listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - p.get_aT()) + " " + "Departure=" + d); gantt = gantt + " " + p.get_processName() + " |"; } else { d = (p.get_aT() + p.get_bT()); listBox2.Items.Add(p.get_processName() + " " + "waiting=0" + " " + "Departure=" + d); gantt = gantt + " " + "| " + p.get_processName() + " |"; } begin = d; } } } else if (comboBox1.Text == "SJF") { bool first = true; if (radioButton2.Checked) { ls.sort(0, size, ls.get_process()); process p = ls.get_process(); int begin = p.get_aT(); while (p != null) { if (p.get_next() != null) { int ctp = p.get_timePassed(); int ntp = p.get_next().get_timePassed(); int c = p.get_aT() - ctp; int n = p.get_next().get_aT() - ntp; int loop = 1; if (c == n) { loop = ls.check(c, p); } if (loop > 1) { ls.sort(1, loop, p); } } int b = p.get_bT(); int d; if (begin >= p.get_aT() && !first) { d = begin + b; waiting = waiting + (begin - p.get_aT()); listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - p.get_aT()) + " Departure=" + d); gantt = gantt + " " + p.get_processName() + " |"; } else { d = p.get_aT() + b; listBox2.Items.Add(p.get_processName() + " " + "waiting=0" + " Departure=" + d); gantt = gantt + " " + "| " + p.get_processName() + " |"; first = false; } begin = d; p = p.get_next(); ls.elmiTDeff(b, p); } ls.restPassedT(); } else { ls.sort(0, size, ls.get_process()); process p = ls.get_process(); int begin = p.get_aT(); while (begin < ls.tBT()) { if (p == null) { p = ls.get_process(); } if (p.get_next() != null) { int ctp = p.get_timePassed(); int ntp = p.get_next().get_timePassed(); int c = p.get_aT() - ctp; int n = p.get_next().get_aT() - ntp; int loop = 1; if (c == n) { loop = ls.check(c, p); } if (loop > 1) { ls.sort(1, loop, p); } } int bt = p.get_bT(); int btP = p.get_bTimePassed(); while (p != null) { if (bt == btP) { p = p.get_next(); bt = p.get_bT(); btP = p.get_bTimePassed(); } else { break; } } bt = p.get_bT(); btP = p.get_bTimePassed(); if (bt != btP) { if (bt - btP > 1) { p.set_bTimePassed(btP + 1); begin++; } else { int b = p.get_bT(); begin++; waiting = waiting + (begin - (p.get_aT() + b)); listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - (p.get_aT() + b)) + " Departure=" + begin); p.set_bTimePassed(btP + 1); } if (!first) { gantt = gantt + " " + p.get_processName() + " |"; } else { gantt = gantt + "| " + p.get_processName() + " |"; first = false; } ls.elmiTDeff(1, p); } } ls.restPassedT(); } } else if (comboBox1.Text == "Priority") { bool first = true; if (radioButton2.Checked) { ls.sort(0, size, ls.get_process()); process p = ls.get_process(); int begin = 0; while (p != null) { if (p.get_next() != null) { int ctp = p.get_timePassed(); int ntp = p.get_next().get_timePassed(); int c = p.get_aT() - ctp; int n = p.get_next().get_aT() - ntp; int loop = 1; if (c == n) { loop = ls.check(c, p); } if (loop > 1) { ls.sort(2, loop, p); } } int b = p.get_bT(); int d; if (begin >= p.get_aT() && !first) { d = begin + b; waiting = waiting + (begin - p.get_aT()); listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - p.get_aT()) + " Departure=" + d); gantt = gantt + " " + p.get_processName() + " |"; begin = d; } else { d = p.get_aT() + b; listBox2.Items.Add(p.get_processName() + " " + "waiting=0" + " Departure=" + d); gantt = gantt + " " + "| " + p.get_processName() + " |"; first = false; begin = d; } p = p.get_next(); ls.elmiTDeff(b, p); } ls.restPassedT(); } else { ls.sort(0, size, ls.get_process()); process p = ls.get_process(); int begin = p.get_aT(); while (begin < ls.tBT()) { if (p == null) { p = ls.get_process(); } if (p.get_next() != null) { int ctp = p.get_timePassed(); int ntp = p.get_next().get_timePassed(); int c = p.get_aT() - ctp; int n = p.get_next().get_aT() - ntp; int loop = 1; if (c == n) { loop = ls.check(c, p); } if (loop > 1) { ls.sort(2, loop, p); } } int bt = p.get_bT(); int btP = p.get_bTimePassed(); while (p != null) { if (bt == btP) { p = p.get_next(); bt = p.get_bT(); btP = p.get_bTimePassed(); } else { break; } } bt = p.get_bT(); btP = p.get_bTimePassed(); if (bt != btP) { if (bt - btP > 1) { p.set_bTimePassed(btP + 1); begin++; } else { int b = p.get_bT(); waiting = waiting + (begin - (p.get_aT() + b)); begin++; listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - (p.get_aT() + b)) + " Departure=" + begin); p.set_bTimePassed(btP + 1); } if (!first) { gantt = gantt + " " + p.get_processName() + " |"; } else { gantt = gantt + "| " + p.get_processName() + " |"; first = false; } ls.elmiTDeff(1, p); } } ls.restPassedT(); } } else if (comboBox1.Text == "Round Roben") { bool first = true; int q = (int)numericUpDown4.Value; lsrr.sort(0, size, lsrr.get_process()); process p = lsrr.get_process(); int begin = p.get_aT(); while (begin < lsrr.tBT()) { if (p == null) { p = lsrr.get_process(); } int loop = check(p); for (int i = 0; i < loop; i++) { int cpbt = p.get_bTimePassed(); int bt = p.get_bT(); if (cpbt != bt) { if (bt - cpbt > q) { p.set_bTimePassed(cpbt + q); begin = begin + q; } else { p.set_bTimePassed(bt); begin = begin + (bt - cpbt); waiting = waiting + (begin - (p.get_bT() + p.get_aT())); listBox2.Items.Add(p.get_processName() + " " + "waiting=" + (begin - (p.get_bT() + p.get_aT())) + " Departure=" + begin); } if (!first) { gantt = gantt + " " + p.get_processName() + " |"; } else { gantt = gantt + "| " + p.get_processName() + " |"; first = false; } lsrr.elmiTDeff(q, p); check(p); } if (p.get_next() != null) { if (p.get_next().get_aT() == p.get_next().get_timePassed()) { p = p.get_next(); } } else { p = null; } } } } label7.Text = "" + (waiting / size); listBox3.Items.Clear(); listBox3.Items.Add(gantt); lsrr.restPassedT(); }