Skip to content

Instantly share code, notes, and snippets.

@penut85420
Created August 11, 2018 01:21
Show Gist options
  • Save penut85420/8e8bdbfd15e4473517d7e84e09c2891f to your computer and use it in GitHub Desktop.
Save penut85420/8e8bdbfd15e4473517d7e84e09c2891f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
using namespace std;
char id2ch(int);
typedef struct process {
int pid;
int burst;
int arrt;
int prt;
int left;
// fcfs
int fcfs_waiting;
int fcfs_begin;
int fcfs_turn;
// rr
int rr_begin;
int rr_left;
int rr_end;
} process;
void process_sort(process[], int);
void process_sort_by_id(process[], int);
void fcfs(process[], int);
void rr(process[], int, int);
int main() {
char fin_path[64] = "input.txt";
int method, slice;
process p[100];
int n = 0;
// cout << "File: ";
// cin >> fin_path;
ifstream fin(fin_path);
fin >> method >> slice;
char s[64];
for (int i = 0; i < 6; i++)
fin >> s;
while (fin) {
fin >> p[n].pid >> p[n].burst >> p[n].arrt >> p[n].prt;
p[n].fcfs_begin = 0;
p[n].rr_left = p[n].burst;
n++;
}
n--;
process_sort(p, n);
// for (int i = 0; i < n; i++) {
// cout << p[i].pid << '\t' << p[i].burst << '\t'
// << p[i].arrt << '\t' << p[i].prt << endl;
// }
fin.close();
switch (method) {
case 1:
fcfs(p, n);
break;
case 6:
fcfs(p, n);
rr(p, n, slice);
break;
}
process_sort_by_id(p, n);
cout << "\n==================\n"
<< "Waiting Time\nPID\tFCFS\tRR\n";
for (int i = 0; i < n; i++)
cout << p[i].pid << "\t" << p[i].fcfs_waiting << endl;
cout << "Turnaround Time\nPID\tFCFS\tRR\n";
for (int i = 0; i < n; i++)
cout << p[i].pid << "\t" << p[i].fcfs_turn << endl;
return 0;
}
char id2ch(int id) {
if (id < 10)
return '0' + id;
return 'A' + id - 10;
}
void process_init(process p[], int n) {
for (int i = 0; i < n; i++)
p[i].left = p[i].burst;
}
void process_sort(process p[], int n) {
for (int i = 0; i < n - 1; i++) {
int maxi = i;
for (int j = i + 1; j < n; j++) {
if (p[j].arrt < p[maxi].arrt)
maxi = j;
else if (p[j].arrt == p[maxi].arrt && p[j].pid < p[maxi].pid)
maxi = j;
else if (p[j].pid == p[maxi].pid && p[j].prt < p[maxi].prt)
maxi = j;
}
process t = p[i];
p[i] = p[maxi];
p[maxi] = t;
}
}
void process_sort_by_id(process p[], int n) {
for (int i = 0; i < n - 1; i++) {
int maxi = i;
for (int j = i + 1; j < n; j++) {
if (p[j].pid < p[maxi].pid)
maxi = j;
}
process t = p[i];
p[i] = p[maxi];
p[maxi] = t;
}
}
void fcfs(process p[], int n) {
process_init(p, n);
cout << "===== FCFS =====\n";
int now = 0, t = 0;
process *pq[128];
process tmp;
tmp.left = 0;
pq[0] = &tmp;
int pf = 0, pe = 0;
while (now < n || pf < pe) {
while (p[now].arrt == t)
pq[pe++] = &p[now++];
if (pq[pf]->left == 0)
cout << "-";
else {
cout << id2ch(pq[pf]->pid);
if (pq[pf]->burst == pq[pf]->left) {
pq[pf]->fcfs_begin = t;
pq[pf]->fcfs_waiting = pq[pf]->fcfs_begin - pq[pf]->arrt;
pq[pf]->fcfs_turn = pq[pf]->fcfs_waiting + pq[pf]->burst;
}
pq[pf]->left--;
if (!pq[pf]->left)
pf++;
}
t++;
}
cout << endl;
}
void rr(process p[], int n, int slice) {
return;
process_init(p, n);
cout << "===== RR =====\n";
int now = 0, t = 0;
process *pq[128];
process tmp;
tmp.left = 0;
pq[0] = &tmp;
int pf = 0, pe = 0;
while (now < n || pf < pe) {
while (p[now].arrt == t)
pq[pe++] = &p[now++];
if (pq[pf]->left == 0)
cout << "-";
else {
int i;
for (i = 0; i < slice; i++, t++) {
cout << id2ch(pq[pf]->pid);
if (pq[pf]->burst == pq[pf]->left)
pq[pf]->rr_begin = t;
pq[pf]->left--;
if (!pq[pf]->left) {
pq[pf]->rr_end = t;
pf++;
break;
}
}
}
t++;
}
cout << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment