ما هي طريقة عمل كود للSJF non preemptive؟
أريد طريقة عمل كود للـSJF non preemptive algorithm بحيث تقرأ من ملف وتضع الـoutput في ملف آخر.
كتبت هذا الكود لكنه يعمل بشكل خاطئ.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Process
{
string name;
int arrival;
int processing;
public:
Process(string name, int arrival, int processing): name(name), arrival(arrival), processing(processing) {};
string get_name()
{
return this->name;
}
int get_arrival()
{
return this->arrival;
}
int get_processing()
{
return this->processing;
}
};
static int current_time = 0;
bool compare_arrival_times(Process a, Process b)
{
if (a.get_arrival() - current_time < 0 && b.get_arrival() - current_time < 0) //to check if the process has arrived(in the ready Q)
{
return a.get_processing() > b.get_processing();
}
else if (a.get_arrival() - current_time < 0)
{
return false;
}
else if (b.get_arrival() - current_time < 0)
{
return true;
}
return a.get_arrival() - current_time > b.get_arrival() - current_time;
}
int main()
{
fstream input;
string line;
vector<Process> processes {};
input.open("input.txt", ios:: in);
while (getline(input, line))
{
vector<string> parts {};
string current = "";
int processes_num;
for (size_t i = 0; i < line.size(); i++)
{
if (line[i] == ' ')
{
parts.push_back(string(current));
current = "";
}
else if (!line[i + 1])
{
current.push_back(line[i]);
parts.push_back(string(current));
current = "";
}
else
{
current.push_back(line[i]);
}
}
processes.push_back(Process(parts.at(0), stoi(string(parts.at(1))), stoi(string(parts.at(2)))));
}
input.close();
sort(processes.begin(), processes.end(), compare_arrival_times);
fstream out;
out.open("out.txt", ios::trunc | ios::out);
int temp;
//string ready_q[];
while (processes.size() > 0)
{
Process *now = &processes[processes.size() - 1];
if (now->get_arrival() > current_time)
{
current_time = now->get_arrival();
temp = current_time;
}
else
{
temp = current_time;
current_time += now->get_processing();
}
out << now->get_name() << " (response=" << temp - now->get_arrival() << ", turnaround=" << current_time - now->get_arrival() << ", delay=" << current_time - now->get_arrival() - now->get_processing() << ")" << endl;
processes.pop_back();
sort(processes.begin(), processes.end(), compare_arrival_times);
now = nullptr;
delete now;
}
out.close();
}
//turnaround=end-arrival
//delay=turnaround-processing time
//response=once 1st-arrival
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class Process
{
string name;
int arrival;
int processing;
public:
Process(string name, int arrival, int processing): name(name), arrival(arrival), processing(processing) {};
string get_name()
{
return this->name;
}
int get_arrival()
{
return this->arrival;
}
int get_processing()
{
return this->processing;
}
};
static int current_time = 0;
bool compare_arrival_times(Process a, Process b)
{
if (a.get_arrival() - current_time < 0 && b.get_arrival() - current_time < 0) //to check if the process has arrived(in the ready Q)
{
return a.get_processing() > b.get_processing();
}
else if (a.get_arrival() - current_time < 0)
{
return false;
}
else if (b.get_arrival() - current_time < 0)
{
return true;
}
return a.get_arrival() - current_time > b.get_arrival() - current_time;
}
int main()
{
fstream input;
string line;
vector<Process> processes {};
input.open("input.txt", ios:: in);
while (getline(input, line))
{
vector<string> parts {};
string current = "";
int processes_num;
for (size_t i = 0; i < line.size(); i++)
{
if (line[i] == ' ')
{
parts.push_back(string(current));
current = "";
}
else if (!line[i + 1])
{
current.push_back(line[i]);
parts.push_back(string(current));
current = "";
}
else
{
current.push_back(line[i]);
}
}
processes.push_back(Process(parts.at(0), stoi(string(parts.at(1))), stoi(string(parts.at(2)))));
}
input.close();
sort(processes.begin(), processes.end(), compare_arrival_times);
fstream out;
out.open("out.txt", ios::trunc | ios::out);
int temp;
//string ready_q[];
while (processes.size() > 0)
{
Process *now = &processes[processes.size() - 1];
if (now->get_arrival() > current_time)
{
current_time = now->get_arrival();
temp = current_time;
}
else
{
temp = current_time;
current_time += now->get_processing();
}
out << now->get_name() << " (response=" << temp - now->get_arrival() << ", turnaround=" << current_time - now->get_arrival() << ", delay=" << current_time - now->get_arrival() - now->get_processing() << ")" << endl;
processes.pop_back();
sort(processes.begin(), processes.end(), compare_arrival_times);
now = nullptr;
delete now;
}
out.close();
}
//turnaround=end-arrival
//delay=turnaround-processing time
//response=once 1st-arrival