diff --git a/Codes/FCFS.py b/Codes/FCFS.py new file mode 100755 index 0000000..a39a4d6 --- /dev/null +++ b/Codes/FCFS.py @@ -0,0 +1,65 @@ +''' +Every process is an obj with it's own wt,bt,at etc. +the table is an obj of table class. +Our table is just a list of process objects +wt,tat,ct are calculated by waitCalculator() +createTable() displays table neatly +''' + + + + +class ProcessClass: + def __init__(self): #constructor in Py + self.name =input("Enter Process Name: ") + self.at = int(input("Enter Arrival Time: ")) + self.bt = int(input("Enter Burst Time: ")) + self.wt = 0 + self.tat = 0 + self.ct = 0 + def display(self): + print(f"{self.name}:\t{self.at}\t{self.bt}\t{self.wt}\t{self.ct}\t\t{self.tat}\n") + +class Table_class: + + def __init__(self): + self.table = [] + self.table1 = [] + print("Enter Processes:\n") + while True: + ch = int(input("\n\nAdd a new process?\t")) + if ch: + p = ProcessClass() + self.table.append(p) + else: break + def fcfs(self): + time = 0 + self.table1 = sorted(self.table, key= lambda p: p.at) #sorts array based on arrival time + for cp in self.table1: + cp.ct = cp.bt + time + cp.wt = time - cp.at + cp.tat = cp.wt + cp.bt + time+= cp.bt + def createTable(self): + print(f"\n\nThe Table is:") + print(f"Process\tArrival\tBurst\tWaiting\tCompletedAt\tT.A.T\n") + for p in self.table1: + p.display() + + def sjf(self): + time = 0 + self.table1 = sorted(self.table,key= lambda p: p.bt) #sorts array based on arrival time + for cp in self.table1: + cp.ct = cp.bt + time + if time: + cp.wt = time - cp.at + else: + cp.wt = 0 + cp.tat = cp.wt + cp.bt + time+= cp.bt + +# Code by Afan Shaikh. +tab = Table_class() +print("Using sjf!!\n\n") +tab.sjf() +tab.createTable() \ No newline at end of file diff --git a/Codes/Pri_SJF.doc b/Codes/Pri_SJF.doc new file mode 100644 index 0000000..1a7e90d Binary files /dev/null and b/Codes/Pri_SJF.doc differ diff --git a/Codes/RR.cpp b/Codes/RR.cpp new file mode 100755 index 0000000..df2cda5 --- /dev/null +++ b/Codes/RR.cpp @@ -0,0 +1,67 @@ +#include +#include +#include + +using namespace std; + +struct Process { + int id; + int burstTime; + int remainingTime; + int waitingTime; + int turnaroundTime; +}; + +void roundRobinScheduling(vector &processes, int quantum) { + int n = processes.size(); + queue readyQueue; + vector completionTimes(n, 0); + + int currentTime = 0; + + for (int i = 0; i < n; ++i) { + readyQueue.push(i); + processes[i].remainingTime = processes[i].burstTime; + } + + while (!readyQueue.empty()) { + int currentProcessId = readyQueue.front(); + readyQueue.pop(); + + if (processes[currentProcessId].remainingTime <= quantum) { + currentTime += processes[currentProcessId].remainingTime; + processes[currentProcessId].remainingTime = 0; + processes[currentProcessId].turnaroundTime = currentTime; + processes[currentProcessId].waitingTime = processes[currentProcessId].turnaroundTime - processes[currentProcessId].burstTime; + completionTimes[currentProcessId] = currentTime; + } else { + currentTime += quantum; + processes[currentProcessId].remainingTime -= quantum; + readyQueue.push(currentProcessId); + } + } + + + cout<<"Time Quantum = 4"; + cout << "Process ID\tBurst Time\tWaiting Time\tTurnaround Time\n"; + for (int i = 0; i < n; ++i) { + cout << processes[i].id << "\t\t" << processes[i].burstTime << "\t\t" << processes[i].waitingTime + << "\t\t" << processes[i].turnaroundTime << '\n'; + } +} + +int main() { + vector processes = { + {1, 6, 0, 0, 0}, + {2, 8, 0, 0, 0}, + {3, 7, 0, 0, 0}, + {4, 3, 0, 0, 0} + }; + + int timeQuantum = 4; + + roundRobinScheduling(processes, timeQuantum); + + return 0; +} + diff --git a/Codes/priority.cpp b/Codes/priority.cpp new file mode 100755 index 0000000..a961144 --- /dev/null +++ b/Codes/priority.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +using namespace std; + +struct Process { + int id; + int burstTime; + int priority; + int waitingTime; + int turnaroundTime; +}; + +bool comparePriority(Process a, Process b) { + return a.priority > b.priority; +} + +void priorityScheduling(vector &processes) { + int n = processes.size(); + + sort(processes.begin(), processes.end(), comparePriority); + + processes[0].waitingTime = 0; + processes[0].turnaroundTime = processes[0].burstTime; +for (int i = 1; i < n; ++i) { + processes[i].waitingTime = 0; + for (int j = 0; j < i; ++j) { + processes[i].waitingTime += processes[j].burstTime; + } + processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime; + } + + cout << "Process ID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n"; + for (const auto &p : processes) { + cout << p.id << "\t\t" << p.burstTime << "\t\t" << p.priority + << "\t\t" << p.waitingTime << "\t\t" << p.turnaroundTime << '\n'; + } +} + +int main() { + vector processes = { + {1, 6, 2, 0, 0}, + {2, 8, 1, 0, 0}, + {3, 7, 3, 0, 0}, + {4, 3, 4, 0, 0} + }; + + priorityScheduling(processes); + + return 0; +} +