Как предотвратить увеличение простоя после увеличения времени после разрыва последнего процессора в планировании SRTF?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Как предотвратить увеличение простоя после увеличения времени после разрыва последнего процессора в планировании SRTF?

Сообщение Anonymous »

В моей текущей моделировании планирования SRTF я сталкиваюсь с проблемой, касающейся того, как рассчитывается время простоя ЦП. Однако, поскольку последний разрыв процессора последнего процесса уже был завершен, я не хочу, чтобы это оставшееся время (во время его ввода/вывода) считалось простоя процессора - потому что в этот момент больше нет работы процессора, ожидающего какого -либо процесса.#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include

using namespace std;

struct Process {
int id;
int arrival;
vector bursts;
int burst_index = 0;
int remaining_time = 0;
int end_time = 0;
int next_ready_time = 0;
bool completed = false;

bool is_cpu_burst() const {
return burst_index % 2 == 0;
}
};

struct Compare {
bool operator()(Process* a, Process* b) const {
if (a->remaining_time != b->remaining_time)
return a->remaining_time > b->remaining_time;
if (a->next_ready_time != b->next_ready_time)
return a->next_ready_time > b->next_ready_time;
return a->id > b->id;
}
};

int main() {
FILE* fin = fopen("srtf.inp", "r");
FILE* fout = fopen("srtf.out", "w");

int n;
fscanf(fin, "%d", &n);

vector
processes(n);
for (int i = 0; i < n; ++i) {
int x;
fscanf(fin, "%d", &x);
processes.id = i;
processes.arrival = x;
processes.next_ready_time = x;
while (fscanf(fin, "%d", &x) && x != -1) {
processes.bursts.push_back(x);
}
processes.remaining_time = processes.bursts[0];
}

int time = 0;
int idle_time = 0;
int completed_count = 0;

priority_queue ready_queue;
vector io_waiting;
Process* current = nullptr;

while (completed_count < n) {
// Handle I/O completion
for (int i = 0; i < (int)io_waiting.size();) {
Process* p = io_waiting;
if (p->next_ready_time burst_index < (int)p->bursts.size()) {
p->remaining_time = p->bursts[p->burst_index];
ready_queue.push(p);
}
else {
p->end_time = time;
p->completed = true;
completed_count++;
}
io_waiting.erase(io_waiting.begin() + i);
}
else {
++i;
}
}

// Add newly arrived processes to the ready queue
for (int i = 0; i < n; ++i) {
Process& p = processes;
if (p.arrival == time && p.burst_index == 0) {
p.remaining_time = p.bursts[0];
ready_queue.push(&p);
}
}

// Handle CPU burst completion
if (current && current->remaining_time == 0) {
if (current->burst_index + 1 >= (int)current->bursts.size()) {
current->end_time = time;
current->completed = true;
completed_count++;
}
else {
current->burst_index++;
if (current->is_cpu_burst()) {
current->remaining_time = current->bursts[current->burst_index];
}
else {
current->next_ready_time = time + current->bursts[current->burst_index];
io_waiting.push_back(current);
current->burst_index++;
}
}
current = nullptr;
}

// Preemption or assign a new process
if (!ready_queue.empty()) {
if (!current || ready_queue.top()->remaining_time < current->remaining_time) {
if (current)
ready_queue.push(current);
current = ready_queue.top();
ready_queue.pop();

printf("Time=%d selectedProcess=%d idleTime=%d\n", time, current->id, idle_time);
}
}

// Execute or idle
if (current) {
current->remaining_time--;
}
else {
idle_time++;
}

time++;
printf("Final idle_time = %d\n", idle_time); // Optional debug output
}

// Output results
if (fout != nullptr) {
fprintf(fout, "%d\n", idle_time); // ✅ Total idle time
for (int i = 0; i < n; ++i) {
fprintf(fout, "%d\n", processes.end_time);
}
fclose(fout);
}
else {
fprintf(stderr, "Failed to open output file.\n");
}

return 0;
}
< /code>
srtf.inp
20
3 1 24 2 2 10 35 6 5 6 3 31 2 3 19 10 4 3 46 2 43 3 -1
5 10 33 4 11 5 3 23 6 44 5 2 25 7 4 3 4 3 9 5 --1
9 16 16 4 4 4 4 4 35 4 3 4 1 35 4 3 4 1 35 4 3 4 3 4 35 4 3 4 3 4 4 4 4 4 4 4 4 4 4 3 43 /> 16 1 15 7 7 7 10 15 3 21 4 6 8 39 4 10 15 -1
26 10 41 7 15 8 42 3 40 2 22 8 34 3 9 7 22 5 34 1 19 6 37 4 46 19 35 10 12 2 -1
33 7 47 8 49 8 18 37 5 38 10 3 24 24 3 4 24 3 4 24 3 4 24 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 37 3 4 24 3 4 24 3 4 24 37 37 37 37 37 3 4 24 3 4 24 37 37 37 37 37 37 37 37 37 2 4 24 3 4 24 37 3 4 24 3 4 24 3 4 37 3 4 24 37 3 4 37 3 4 37 3 4 24 3 4 24 3 4 24 3 4 2 4 24 3 24 3 3 4 24 33 38 10 30 14 8 10 21 21 16 9 -1
60 10 4 1 14 5 22 5 27 5 5 6 6 8 34 3 5 19 4 4 44 9 -1
67 9 15 8 40 7 22 7 11 67 9 6 9 24 15 47 15 -1
1054 5 12 8 11 6 4 7 7 7 4 4 4 40 -1
107 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 7 7 7 46 6 11 5 37 -1
1084 4 6 3 17 8 3 8 13 3 15 8 43 18 26 5 35 10 39 -1
1100 6 6 2 40 21 21 6 14 1 41 1 41 1 39 3 3 -1
1113 7 21 4 38 -1
1137 1 42 2 47 20 8 6 16 5 7 6 19 10 46 9 9 5 36 9 43 8 14 8 3 22 45 -1
1178 6 50 22 20 3 5 2 2 8 6 27 5 22 9 5 14 10 5 25 3 40 5 11 6 14 10 8 -1
1215 8 37 8 6 5 21 5 21 4 48 3 13 1 44 7 45 7 40 3 17 2 31 5 -1
1264 6 39 1 24 -1
1286 7 30 9 4 4 2 25 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 21 8 20 -1
533
657
531
470
479
663
412
666
624
585
1232
1420
1495
1397
1193
1654
1724
1617
1335
1410
1411
Это правильный вывод, который я ожидаю от симуляции. Моделирование отслеживает всплески процессора и всплески ввода -вывода для каждого процесса, включая общее время холостого хода, когда для выполнения нет процесса. В моей реализации моделирование продолжает увеличивать IDLE_TIME, даже после того, как все всплески процессора завершены, просто потому, что последний процесс все еще выполняет ввод -вывод.

Подробнее здесь: https://stackoverflow.com/questions/796 ... rtf-schedu
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»