2
1

Added untested codes.

This commit is contained in:
K 2024-08-21 01:16:26 +05:30
parent b5f96daec6
commit a4f3f5d7ba
Signed by: notkshitij
GPG Key ID: C5B8BC7530F8F43F
4 changed files with 185 additions and 0 deletions

65
Codes/FCFS.py Executable file
View File

@ -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()

BIN
Codes/Pri_SJF.doc Normal file

Binary file not shown.

67
Codes/RR.cpp Executable file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Process {
int id;
int burstTime;
int remainingTime;
int waitingTime;
int turnaroundTime;
};
void roundRobinScheduling(vector<Process> &processes, int quantum) {
int n = processes.size();
queue<int> readyQueue;
vector<int> 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<Process> 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;
}

53
Codes/priority.cpp Executable file
View File

@ -0,0 +1,53 @@
#include <iostream>
#include <vector>
#include <algorithm>
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<Process> &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<Process> 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;
}