From a4f3f5d7ba3d60fd3b82d954b689c02cf8ef4964 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Wed, 21 Aug 2024 01:16:26 +0530 Subject: [PATCH] Added untested codes. --- Codes/FCFS.py | 65 +++++++++++++++++++++++++++++++++++++++++++ Codes/Pri_SJF.doc | Bin 0 -> 27648 bytes Codes/RR.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++ Codes/priority.cpp | 53 +++++++++++++++++++++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100755 Codes/FCFS.py create mode 100644 Codes/Pri_SJF.doc create mode 100755 Codes/RR.cpp create mode 100755 Codes/priority.cpp 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 0000000000000000000000000000000000000000..1a7e90d4357f180d11d913485eaf0566adda7c46 GIT binary patch literal 27648 zcmeI53v?CLxySbjAtVqmK%|IPBc+Id2#6Lfa1kg)q)LU06cvL!OfV#A5)mJ@)SFUQ zYb{bQbx~{4Qm?gEDdk$~1F5pmg^yAM3pC{-MQYWuqRkRbh1}nN@0rX#&m1UqwO!1~ z@67By`@O&Y?LFU_nQZw@*FE?Dyw~d{Vf(CUXAZX%m^{xv#_>6!yU3Uvj^%f_rKKhG z=J5FTFVq5ug0iX)hti4dL4Do<@}U4aLMJE$h3mrp1V98#SLg=ap%_kt9`Fh12`51> zI2lfXPr|8i8kE54&>KDlec;p37tVlwa3=JJ0Wc8Gf?eItJ^(@cRfoI8vriKR$Fa=qv4 zKYZ`QOUsIWdRM*~aKn2j`UdhUs6@GU{*zPeC45ea5OBwzn!L8 zd#t^-(XjZO*T1^vkGoJT$mFrzCSZMQPDzuS_dJ@vnY`Ju)v zFtZF<+j+R#oJL}_>tkm1F+=*qX3}m~K`C@!#WDac>|^@(iLqL|bYgq+MOZMQAh#AK zPs}rw?CW4A$5&5?<&NoZ#!l>EE}jrGv)G$CCRP-qWOK-`z)Uc6$$O3|XMVbt(yFFi z$bV-2_gk|aN3=jWCB#d#~kPGlCO6 z%sBF&M+vT{{+cHzb6f-0+V5&phTnMFNVBK5sWx^jHkIQ}W)kJD<;)~vTti9~K@A2S zK}j#0FvnbAQ|%hNAZ@Bzy%o3CJ#1sOuZw$nhPYbQXNo%{L)>9)#Jw;>+;iKA`^5}# z&ub&@tPF8K(?;AcXNWtzjksUS5cmAFamUlYG4nudJRv$H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?W%Eo9jEVKYi(){Ui2Lse8*bf z5vTn96aU`FdOcXW)qw5IQ<;@~OQk>KNXjG;G?6zC$#<+>qxkcXd|wT$5jT#AvVyA4!bNdHj1z&Bs!^bnj2s3y5;Izr6k4K_?e|_j|lQec*?W zj>E5gg5Q<+X%a=kEk*1VSMNMuB(djMTPY|^5nnprlf7QD)Q^2G`(fZi<|eSJ-N-HkTOZum{Ov3hPzMS10QwPVX>RbESM6+#(KnpImjW!6G`loq4P zOj5A_te#pmj%;k<6mH7wSrv1~)>Kv7SSs?AvRO5A^_KjwT~M_)ks(6|4Q4Z=ahr|x zV}EyOM@;9G<>eG>ZjEaroetY^v>ZX$&@XYXAlYum^8bI(B_{EI2&5{9eYuu8oh`=e z0rgtTYICW*OWXNo|J{vNRl6!Vy_+-JnDc{xG4jq>m6ywIyA9!P z;_$7~f!7i3x&-+vZ*AE`R$G0Wt08UedHPs)-s}E-{qL8HxD($!T_7Jl$)z{jr{DUK zzs9Y`Y_|EUtwu;#*e)mi8s0B5j{R&~68Cg~JKOemucN4cXOa!yTh~4*=Ob&6;YX3b a6v(!q{%zN=?GU!(gRizjv%dG_ literal 0 HcmV?d00001 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; +} +