asfsffs

 avatar
unknown
plain_text
a month ago
4.7 kB
2
Indexable
#include <iostream>
#include <fstream>
#include <sstream>
#include <queue>
#include <string>
using namespace std;
struct Task {
int taskID;
int arrivalTime;
int cpuBurstTime;
int remainingTime; // For RR scheduling
int priorityLevel;
int completionTime;
int turnaroundTime;
int waitingTime;
};
int readTasks(const string& filename, Task tasks[], int maxTasks) {
ifstream file(filename);
string line;
int count = 0;
getline(file, line); // Skip header
while (getline(file, line) && count < maxTasks) {
stringstream ss(line);
string id, arrival, burst, priority;
getline(ss, id, ',');
getline(ss, arrival, ',');
getline(ss, burst, ',');
getline(ss, priority, ',');
tasks[count].taskID = stoi(id);
tasks[count].arrivalTime = stoi(arrival);
tasks[count].cpuBurstTime = stoi(burst);
tasks[count].remainingTime = stoi(burst);
tasks[count].priorityLevel = stoi(priority);
tasks[count].completionTime = 0;
count++;
}
return count;
}
void roundRobin(Task tasks[], int n, int quantum, int& currentTime, queue<int>&
executionOrder) {
bool done = false;
cout << "Level 1 (Round Robin) Scheduling:\n";
while (!done) {
done = true;
for (int i = 0; i < n; i++) {
if (tasks[i].priorityLevel == 1 && tasks[i].remainingTime > 0 &&
tasks[i].arrivalTime <= currentTime) {
done = false;

int executeTime = min(quantum, tasks[i].remainingTime);

tasks[i].remainingTime -= executeTime;

currentTime += executeTime;
executionOrder.push(tasks[i].taskID);
cout << "Task " << tasks[i].taskID << " executed (remaining burst time: "
<< tasks[i].remainingTime << ")\n";
if (tasks[i].remainingTime == 0) {
tasks[i].completionTime = currentTime;
}
}

}
}
}
void fcfs(Task tasks[], int n, int& currentTime, queue<int>& executionOrder) {
bool visited[100] = {false};
cout << "Level 2 (FCFS) Scheduling:\n";
for (int count = 0; count < n; count++) {
int earliest = -1;
for (int i = 0; i < n; i++) {
if (tasks[i].priorityLevel == 2 && !visited[i] && tasks[i].arrivalTime <=
currentTime) {
if (earliest == -1 || tasks[i].arrivalTime <
tasks[earliest].arrivalTime) {
earliest = i;
}
}
}
if (earliest != -1) {
currentTime += tasks[earliest].cpuBurstTime;
tasks[earliest].completionTime = currentTime;
visited[earliest] = true;
executionOrder.push(tasks[earliest].taskID);
cout << "Task " << tasks[earliest].taskID << " executed\n";
} else {
bool allDone = true;
for (int i = 0; i < n; i++) {
if (tasks[i].priorityLevel == 2 && !visited[i]) {
allDone = false;

break;
}
}
if (allDone) break;
currentTime++;
}
}
}
void sjf(Task tasks[], int n, int& currentTime, queue<int>& executionOrder) {
bool visited[100] = {false};
cout << "Level 3 (SJF) Scheduling:\n";
for (int count = 0; count < n; count++) {
int shortest = -1;
for (int i = 0; i < n; i++) {
if (tasks[i].priorityLevel == 3 && !visited[i] && tasks[i].arrivalTime <=
currentTime) {
if (shortest == -1 || tasks[i].cpuBurstTime <
tasks[shortest].cpuBurstTime) {
shortest = i;
}
}
}
if (shortest != -1) {
currentTime += tasks[shortest].cpuBurstTime;
tasks[shortest].completionTime = currentTime;
visited[shortest] = true;
executionOrder.push(tasks[shortest].taskID);
cout << "Task " << tasks[shortest].taskID << " executed\n";
} else {
bool allDone = true;
for (int i = 0; i < n; i++) {
if (tasks[i].priorityLevel == 3 && !visited[i]) {
allDone = false;

break;
}
}

if (allDone) break;
currentTime++;
}
}
}
int main() {
Task tasks[100];
int n = readTasks("tasks.csv", tasks, 100);
int currentTime = 0;
queue<int> executionOrder;
// Execute all three levels
roundRobin(tasks, n, 2, currentTime, executionOrder);
fcfs(tasks, n, currentTime, executionOrder);
sjf(tasks, n, currentTime, executionOrder);
// Calculate times
for (int i = 0; i < n; i++) {
tasks[i].turnaroundTime = tasks[i].completionTime - tasks[i].arrivalTime;
tasks[i].waitingTime = tasks[i].turnaroundTime - tasks[i].cpuBurstTime;
}
// Print Final Task Execution Order
cout << "\nFinal Task Execution Order:\n";
queue<int> tempQueue = executionOrder;
bool first = true;
while (!tempQueue.empty()) {
if (!first) cout << " -> ";
cout << "Task " << tempQueue.front();
tempQueue.pop();
first = false;
}
cout << "\n\n";
// Print Completion Times
cout << "Task Completion Times:\n";
for (int i = 0; i < n; i++) {
cout << "Task " << tasks[i].taskID << ": Completion Time = "
<< tasks[i].completionTime << "\n";
}
cout << "\n";
// Print Turnaround Times
cout << "Turnaround Times:\n";
for (int i = 0; i < n; i++) {
cout << "Task " << tasks[i].taskID << ": Turnaround Time = "
<< tasks[i].turnaroundTime << "\n";
}
cout << "\n";
// Print Waiting Times
cout << "Waiting Times:\n";
for (int i = 0; i < n; i++) {
cout << "Task " << tasks[i].taskID << ": Waiting Time = "
<< tasks[i].waitingTime << "\n";
}
return 0;
}
Leave a Comment