Загрузка данных
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>
using namespace std;
const int NUM_THREADS = 3;
struct ProcessInfo {
int id;
int burst_time;
int remaining_time;
int arrival_time;
int counter;
int finish_time;
int waiting_time;
int turnaround_time;
};
mutex mtx;
condition_variable cv_scheduler;
condition_variable cv_worker[NUM_THREADS];
bool run_token[NUM_THREADS] = { false };
bool worker_finished_step = false;
bool stop_all = false;
int current_time = 0;
ProcessInfo processes[NUM_THREADS] = {
{0, 6, 6, 0, 0, 0, 0, 0},
{1, 2, 2, 2, 0, 0, 0, 0},
{2, 4, 4, 5, 0, 0, 0, 0}
};
void worker_thread(int id) {
while (true) {
unique_lock<mutex> lock(mtx);
cv_worker[id].wait(lock, [&]() {
return run_token[id] || stop_all;
});
if (stop_all) break;
processes[id].counter += 10;
processes[id].remaining_time--;
current_time++;
cout << "Время: " << current_time << " | Поток P" << id
<< " | Счетчик: " << processes[id].counter
<< " | Осталось: " << processes[id].remaining_time << endl;
if (processes[id].remaining_time == 0) {
processes[id].finish_time = current_time;
}
run_token[id] = false;
worker_finished_step = true;
cv_scheduler.notify_one();
lock.unlock();
this_thread::sleep_for(chrono::milliseconds(100));
}
}
int main() {
setlocale(LC_ALL, "Russian");
vector<thread> threads;
for (int i = 0; i < NUM_THREADS; ++i) {
threads.emplace_back(worker_thread, i);
}
int total_remaining = 0;
for (int i = 0; i < NUM_THREADS; ++i) {
total_remaining += processes[i].burst_time;
}
while (total_remaining > 0) {
int best_id = -1;
int min_rem = 1000;
for (int i = 0; i < NUM_THREADS; ++i) {
if (processes[i].arrival_time <= current_time && processes[i].remaining_time > 0) {
if (processes[i].remaining_time < min_rem) {
min_rem = processes[i].remaining_time;
best_id = i;
}
}
}
{
unique_lock<mutex> lock(mtx);
worker_finished_step = false;
run_token[best_id] = true;
cv_worker[best_id].notify_one();
}
{
unique_lock<mutex> lock(mtx);
cv_scheduler.wait(lock, []() { return worker_finished_step; });
}
total_remaining--;
}
{
unique_lock<mutex> lock(mtx);
stop_all = true;
for (int i = 0; i < NUM_THREADS; ++i) {
cv_worker[i].notify_one();
}
}
for (auto& th : threads) {
if (th.joinable()) {
th.join();
}
}
double avg_waiting = 0;
double avg_turnaround = 0;
cout << endl << "Результаты:" << endl;
cout << "Процесс\tТприх\tТисп\tТкон\tТполн\tТож\tСчетчик" << endl;
for (int i = 0; i < NUM_THREADS; ++i) {
processes[i].turnaround_time = processes[i].finish_time - processes[i].arrival_time;
processes[i].waiting_time = processes[i].turnaround_time - processes[i].burst_time;
avg_waiting += processes[i].waiting_time;
avg_turnaround += processes[i].turnaround_time;
cout << "P" << processes[i].id << "\t"
<< processes[i].arrival_time << "\t"
<< processes[i].burst_time << "\t"
<< processes[i].finish_time << "\t"
<< processes[i].turnaround_time << "\t"
<< processes[i].waiting_time << "\t"
<< processes[i].counter << endl;
}
avg_waiting /= NUM_THREADS;
avg_turnaround /= NUM_THREADS;
cout << endl;
cout << "Среднее полное время: " << avg_turnaround << endl;
cout << "Среднее время ожидания: " << avg_waiting << endl;
cout << endl << "Нажмите Enter для выхода...";
cin.get();
return 0;
}