[C / C ] 模拟战略协调过程
单独对于协调算法不会在这篇文章中的进程中所提出, 你可以参考,如果需要在网络上阅读 (也许我会在某个美丽的一天写他们). 在本文中,我只是想向大家介绍 (分享) 你计算出码, 单独模拟这些策略.
计划将包括 2 文件是文件 input.txt中 和程序文件 dieuphoitientrinh.cpp.
在INPUT.TXT文件包括:
– 第一行是工序数 (ñ)
– 第一线 2 含 2 值是战略协作和量子
1: FIFO
2: 轮循
3: 独家优先
4: 非排他性优先权
5: 水井坊独家
6: 水井坊非排他性
– n中的下一行, 每一行包括 4 值分别进程名, 时间列出准备, CPU处理时间和优先级 (公约优先权 1 > 2 > 3 > … ).
示例文件INPUT.TXT
4 5 2 P1 0 20 3 P2 3 6 2 P3 3 5 1 P4 7 4 0
然后, 数据包括: 4 过程, 战略协调,被选为独家水井坊, 量子值= 2. Tiến trình P1 có thời gian vào danh sách sẵn sàng (timeRL) 是 0, CPU处理时间 (timeCPU) 是 20, độ ưu tiên là 3, …
Kết quả chạy chương trình:
Dưới đây là code chương trình hoặc bạn có thể 这里下载. 所有查询, 建议请 连接的 qua Email hoặc facebook với mình.
/* * --------------- @author: nguyenvanquan7826 --------------- * ------------ cachhoc.net ------------- */ #include <cstdlib> #include <cstdio> #include <fstream> #include <iostream> #include <iomanip> #include <queue> #define INP "input.txt" using namespace std; struct process { char name[10]; int timeRL, timeCPU, priority; int timeOUT, timeIN, timewait, timesave; int index; //the index of pr[i] }; typedef process *ListP; int quantum; int set; void input(ListP &pr, int &n, int &timeOUT); void output_input(ListP pr, int n); void output_FIFO(ListP pr, int n, int timeOUT); void output_RR(ListP pr, ListP RL, int n, int m, int timeOUT); void output_PRIORITY_preemptive(ListP pr, int n, int timeOUT); void output_PRIORITY_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT); void output_SJF_preemptive(ListP pr, int n, int timeOUT); void output_SJF_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT); void OUTPUT(ListP pr, ListP RL, int n, int m, int timeOUT, int set); void process_FIFO(ListP &pr, int n, int timeOUT); void process_RR(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int quantum); void process_PRIORITY_preemptive(ListP &pr, int n, int timeOUT); void process_PRIORITY_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT); void process_SJF_preemptive(ListP &pr, int n, int timeOUT); void process_SJF_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT); void PROCESS(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int set); void input(ListP &pr, int &n, int &timeOUT) { timeOUT = 0; ifstream in(INP); if (in == NULL) { cout << "Not input file !"; return; } in >> n; in >> set; in >> quantum; pr = new process[n]; for (int i = 0; i < n; i++) { in >> pr[i].name; in >> pr[i].timeRL; in >> pr[i].timeCPU; in >> pr[i].priority; if (timeOUT < pr[i].timeRL) timeOUT = pr[i].timeRL + pr[i].timeCPU; else timeOUT += pr[i].timeCPU; pr[i].index = i; } } void output_input(ListP pr, int n) { cout << endl << "---------------INPUT---------------" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeRL << setw(10) << pr[i].timeCPU << setw(10) << pr[i].priority << endl; cout << "quantum = " << quantum << endl; cout << endl << "---------------OUTPUT---------------" << endl << endl; } void output_FIFO(ListP pr, int n, int timeOUT) { cout << "FIFO" << endl << endl << "PROCESS" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeRL << setw(10) << pr[i].timeCPU << setw(10) << pr[i].priority << setw(10) << pr[i].timeIN << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; } void output_RR(ListP pr, ListP RL, int n, int m, int timeOUT) { cout << "ROUND ROBIN" << endl << endl << "OUTPUT" << endl << endl; cout << "Name" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; cout << endl << endl << "---PROCESS---" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << endl; for (int i = 0; i < m; i++) cout << RL[i].name << setw(10) << RL[i].timeRL << setw(10) << RL[i].timeCPU << setw(10) << RL[i].priority << setw(10) << RL[i].timeIN << setw(10) << RL[i].timeOUT << endl; } void output_PRIORITY_preemptive(ListP pr, int n, int timeOUT) { cout << "PRIORITY preemptive" << endl << endl << "PROCESS" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeRL << setw(10) << pr[i].timeCPU << setw(10) << pr[i].priority << setw(10) << pr[i].timeIN << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; } void output_SJF_preemptive(ListP pr, int n, int timeOUT) { cout << "SJF preemptive" << endl << endl << "PROCESS" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeRL << setw(10) << pr[i].timeCPU << setw(10) << pr[i].priority << setw(10) << pr[i].timeIN << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; } void output_PRIORITY_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT) { cout << "PRIORITY nopreemptive" << endl << endl << "OUTPUT" << endl << endl; cout << "Name" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; cout << endl << endl << "---PROCESS---" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << endl; for (int i = 0; i < m; i++) cout << RL[i].name << setw(10) << RL[i].timeRL << setw(10) << RL[i].timeCPU << setw(10) << RL[i].priority << setw(10) << RL[i].timeIN << setw(10) << RL[i].timeOUT << endl; } void output_SJF_nopreemptive(ListP pr, ListP RL, int n, int m, int timeOUT) { cout << "SJF nopreemptive" << endl << endl << "OUTPUT" << endl << endl; cout << "Name" << setw(10) << "TimeOUT" << setw(10) << "Timewait" << setw(10) << "Timesave" << endl; for (int i = 0; i < n; i++) cout << pr[i].name << setw(10) << pr[i].timeOUT << setw(10) << pr[i].timewait << setw(10) << pr[i].timesave << endl; cout << endl << endl << "---PROCESS---" << endl << endl; cout << "Name" << setw(10) << "TimeRL" << setw(10) << "TimeCPU" << setw(10) << "Priority" << setw(10) << "TimeIN" << setw(10) << "TimeOUT" << endl; for (int i = 0; i < m; i++) cout << RL[i].name << setw(10) << RL[i].timeRL << setw(10) << RL[i].timeCPU << setw(10) << RL[i].priority << setw(10) << RL[i].timeIN << setw(10) << RL[i].timeOUT << endl; } void process_FIFO(ListP &pr, int n, int timeOUT) { ListP RL = new process[n]; int m = -1; for (int t = 0; t < timeOUT; t++) { for (int i = 0; i < n; i++) if (t == pr[i].timeRL) RL[++m] = pr[i]; } timeOUT = 0; for (int i = 0; i <= m; i++) { if (timeOUT <= RL[i].timeRL) { timeOUT = RL[i].timeRL + RL[i].timeCPU; RL[i].timeIN = RL[i].timeRL; } else { timeOUT += RL[i].timeCPU; RL[i].timeIN = RL[i - 1].timeOUT; } RL[i].timeOUT = timeOUT; RL[i].timewait = RL[i].timeOUT - (RL[i].timeRL + RL[i].timeCPU); RL[i].timesave = RL[i].timeOUT - RL[i].timeRL; } pr = RL; } void process_RR(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int quantum) //Round Robin { RL = new process; ListP pr1 = pr; //list temp of pr m = 0; // the number of element in RL int count = 0; //count time quantum int j = 0; int temptime = 0; for (int t = 0; t <= timeOUT; t++) { if (m > 0 && j < m) { count++; if (count <= quantum && RL[j].timeCPU - temptime > 0) temptime++; if (count == quantum && RL[j].timeCPU - temptime > 0) { m++; RL = (process *) realloc(RL, m * sizeof(process)); RL[m - 1] = RL[j]; RL[m - 1].timeCPU -= temptime; } if (RL[j].timeCPU - temptime == 0) { pr1[RL[j].index].timeOUT = t; pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU); pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL; } if (count == quantum || RL[j].timeCPU - temptime == 0) { RL[j].timeOUT = t; RL[j].timeCPU = temptime; RL[j].timeIN = t - RL[j].timeCPU; j++; temptime = 0; count = 0; } } for (int i = 0; i < n; i++) if (t == pr1[i].timeRL) { m++; RL = (process *) realloc(RL, m * sizeof(process)); RL[m - 1] = pr1[i]; } } } void process_PRIORITY_preemptive(ListP &pr, int n, int timeOUT) { ListP RL = new process[n]; ListP pr1 = pr; //list temp of pr int j = 0, m = 0; int temptime = 0; for (int t = 0; t <= timeOUT; t++) { if (m > 0 && j < m) { if (temptime < RL[j].timeCPU) temptime++; if (temptime == RL[j].timeCPU) { RL[j].timeIN = t - RL[j].timeCPU; RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU; RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU); RL[j].timesave = RL[j].timeOUT - RL[j].timeRL; temptime = 0; j++; } } for (int i = 0; i < n; i++) if (t == pr1[i].timeRL) { int k = m; while (k > j + 1 && pr1[i].priority < RL[k - 1].priority) { RL[k] = RL[k - 1]; k--; } RL[k] = pr1[i]; m++; } } pr = RL; } void process_PRIORITY_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT) { RL = new process; ListP pr1 = pr; //list temp of pr process temp; int j = 0; m = 0; int temptime = 0; for (int t = 0; t <= timeOUT; t++) { if (m > 0 && j < m) { if (temptime < RL[j].timeCPU) temptime++; if (temptime == RL[j].timeCPU) { RL[j].timeIN = t - RL[j].timeCPU; RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU; RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU); RL[j].timesave = RL[j].timeOUT - RL[j].timeRL; pr1[RL[j].index].timeOUT = t; pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU); pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL; temptime = 0; j++; } } for (int i = 0; i < n; i++) if (t == pr1[i].timeRL) { m++; int k = m - 1; RL = (process *) realloc(RL, m * sizeof(process)); if (temptime > 0 && pr1[i].priority < RL[j].priority) { m++; k = m - 1; RL = (process *) realloc(RL, m * sizeof(process)); for (k = m - 1; k > j + 1; k--) RL[k] = RL[k - 2]; RL[j + 1] = pr1[i]; RL[j + 2] = RL[j]; RL[j + 2].timeCPU -= temptime; RL[j].timeIN = t - temptime; RL[j].timeOUT = t; RL[j].timeCPU = temptime; temptime = 0; j++; } else { while (k > j && pr1[i].priority < RL[k - 1].priority) { RL[k] = RL[k - 1]; k--; } RL[k] = pr1[i]; } } } } void process_SJF_preemptive(ListP &pr, int n, int timeOUT) { ListP RL = new process[n]; ListP pr1 = pr; //list temp of pr int j = 0, m = 0; int temptime = 0; for (int t = 0; t <= timeOUT; t++) { if (m > 0 && j < m) { if (temptime < RL[j].timeCPU) temptime++; if (temptime == RL[j].timeCPU) { RL[j].timeIN = t - RL[j].timeCPU; RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU; RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU); RL[j].timesave = RL[j].timeOUT - RL[j].timeRL; temptime = 0; j++; } } for (int i = 0; i < n; i++) if (t == pr1[i].timeRL) { int k = m; while (k > j + 1 && pr1[i].timeCPU < RL[k - 1].timeCPU) { RL[k] = RL[k - 1]; k--; } RL[k] = pr1[i]; m++; } } pr = RL; } void process_SJF_nopreemptive(ListP &pr, ListP &RL, int n, int &m, int timeOUT) { RL = new process; ListP pr1 = pr; //list temp of pr process temp; int j = 0; m = 0; int temptime = 0; for (int t = 0; t <= timeOUT; t++) { if (m > 0 && j < m) { if (temptime < RL[j].timeCPU) temptime++; if (temptime == RL[j].timeCPU) { RL[j].timeIN = t - RL[j].timeCPU; RL[j].timeOUT = RL[j].timeIN + RL[j].timeCPU; RL[j].timewait = RL[j].timeOUT - (RL[j].timeRL + RL[j].timeCPU); RL[j].timesave = RL[j].timeOUT - RL[j].timeRL; pr1[RL[j].index].timeOUT = t; pr1[RL[j].index].timewait = pr1[RL[j].index].timeOUT - (pr1[RL[j].index].timeRL + pr1[RL[j].index].timeCPU); pr1[RL[j].index].timesave = pr1[RL[j].index].timeOUT - pr1[RL[j].index].timeRL; temptime = 0; j++; } } for (int i = 0; i < n; i++) if (t == pr1[i].timeRL) { m++; int k = m - 1; RL = (process *) realloc(RL, m * sizeof(process)); if (temptime > 0 && pr1[i].timeCPU < RL[j].timeCPU - temptime) { m++; k = m - 1; RL = (process *) realloc(RL, m * sizeof(process)); for (k = m - 1; k > j + 1; k--) RL[k] = RL[k - 2]; RL[j + 1] = pr1[i]; RL[j + 2] = RL[j]; RL[j + 2].timeCPU -= temptime; RL[j].timeIN = t - temptime; RL[j].timeOUT = t; RL[j].timeCPU = temptime; temptime = 0; j++; } else { while (k > j + 1 && pr1[i].timeCPU < RL[k - 1].timeCPU) { RL[k] = RL[k - 1]; k--; if (k == j + 1 && pr1[i].timeCPU < RL[k - 1].timeCPU - temptime) { RL[k] = RL[k - 1]; k--; } } RL[k] = pr1[i]; } } } } void PROCESS(ListP &pr, ListP &RL, int n, int &m, int timeOUT, int select) { switch (select) { case 1: process_FIFO(pr, n, timeOUT); break; case 2: process_RR(pr, RL, n, m, timeOUT, quantum); break; case 3: process_PRIORITY_preemptive(pr, n, timeOUT); break; case 4: process_PRIORITY_nopreemptive(pr, RL, n, m, timeOUT); break; case 5: process_SJF_preemptive(pr, n, timeOUT); break; case 6: process_SJF_nopreemptive(pr, RL, n, m, timeOUT); break; } } void OUTPUT(ListP pr, ListP RL, int n, int m, int timeOUT, int select) { switch (select) { case 1: output_FIFO(pr, n, timeOUT); break; case 2: output_RR(pr, RL, n, m, timeOUT); break; case 3: output_PRIORITY_preemptive(pr, n, timeOUT); break; case 4: output_PRIORITY_nopreemptive(pr, RL, n, m, timeOUT); break; case 5: output_SJF_preemptive(pr, n, timeOUT); break; case 6: output_SJF_nopreemptive(pr, RL, n, m, timeOUT); break; } cout << endl << "-----------------END-----------------" << endl << endl; } int main() { ListP pr, RL; int n, m, timeOUT; input(pr, n, timeOUT); output_input(pr, n); PROCESS(pr, RL, n, m, timeOUT, set); OUTPUT(pr, RL, n, m, timeOUT, set); return 0; }
阅读更多:
模拟战略协调独家水井坊
轮循模拟战略管理过程
Cảm ơn anh rất nhiều về bài rất hay. Em xin được phép tải về để tham khảo.
Bạn cứ download thoải mái 🙂
.Bác cho em hỏi hôm trước em gõ lại thuật toán FIFO nhưng khi chạy nó lại báo lỗi debug , nếu bác có thời gian hướng dẫn em được không ạ , 谢谢 ,
Giờ mình cũng không nhớ code từ mấy năm trước nữa. Chắc bạn phải tự mò thôi. 😉
em tải về sao nó không chạy được vậy anh. em chạy trên dev c++
Chu y file input va output nha
提供, bài viết của a rất hay, 但不知道一个可能的移动这个代码到帕斯卡尔一直ķ这么先生. 非常感谢ê.