105 lines
3.0 KiB
C++
Raw Permalink Normal View History

2025-01-10 14:44:36 +10:00
#include "process_launcher.h"
2025-01-10 11:17:15 +10:00
#include "log_writer.h"
2025-01-11 02:17:31 +10:00
#include "counter_manager.h"
2025-01-11 02:35:25 +10:00
2025-01-13 19:59:39 +10:00
#include <sys/stat.h>
#include <iostream>
2025-01-11 02:35:25 +10:00
2025-01-10 11:17:15 +10:00
2025-01-11 02:17:31 +10:00
struct two_pid {
int pid_1, pid_2;
};
#ifdef _WIN32
// Для Windows
struct two_pid launch_copies() {
std::string command1 = "cmd /C main.exe 1";
std::string command2 = "cmd /C main.exe 2";
struct two_pid result;
result.pid_1 = ProcessLauncher::launch(command1);
result.pid_2 = ProcessLauncher::launch(command2);
return result;
}
#else
// Для UNIX-подобных систем
struct two_pid launch_copies() {
2025-01-10 16:59:44 +00:00
std::string command1 = "./main 1";
std::string command2 = "./main 2";
2025-01-11 02:17:31 +10:00
struct two_pid result;
result.pid_1 = ProcessLauncher::launch(command1);
result.pid_2 = ProcessLauncher::launch(command2);
return result;
}
#endif
2025-01-10 14:44:36 +10:00
2025-01-10 11:17:15 +10:00
int main(int argc, char* argv[]) {
if (argc >= 2) {
if (argv[1][0] == '1') {
2025-01-11 02:17:31 +10:00
LogWriter::log_copy_start("Copy 1");
CounterManager::set_counter(CounterManager::get_counter() + 10);
LogWriter::log_copy_finish("Copy 1");
2025-01-10 11:17:15 +10:00
return 0;
}
if (argv[1][0] == '2') {
2025-01-11 02:17:31 +10:00
LogWriter::log_copy_start("Copy 2");
CounterManager::set_counter(CounterManager::get_counter() * 2);
2025-01-11 02:35:25 +10:00
sleep(2);
2025-01-11 02:17:31 +10:00
CounterManager::set_counter(CounterManager::get_counter() / 2);
LogWriter::log_copy_finish("Copy 2");
2025-01-10 11:17:15 +10:00
return 0;
}
}
2025-01-13 19:59:39 +10:00
// create dir if it doesn't exist
struct stat info;
std::string dirpath = "queue";
if (!(stat(dirpath.c_str(), &info) == 0 && (info.st_mode & S_IFDIR))) {
mkdir(dirpath.c_str()) == 0;
}
DIR* dir;
struct dirent* entry;
std::string filename_temp;
dir = opendir(dirpath.c_str());
while ((entry = readdir(dir)) != nullptr) {
// Исключаем "." и ".."
filename_temp = std::string(entry->d_name);
if (filename_temp == "." || filename_temp == "..") {
continue;
}
remove((dirpath + "\\" + filename_temp).c_str());
}
2025-01-10 14:44:36 +10:00
LogWriter::log_start_line();
2025-01-11 02:17:31 +10:00
struct two_pid pid_of_copy_processes;
2025-01-13 19:59:39 +10:00
long long int last_counter_increment = MyTime::get_timestamp();
long long int last_counter_logging = MyTime::get_timestamp();
long long int last_copies_launching = MyTime::get_timestamp();
2025-01-10 16:59:44 +00:00
long long int current_time;
2025-01-10 14:44:36 +10:00
while (1) {
2025-01-13 19:59:39 +10:00
current_time = MyTime::get_timestamp();
2025-01-10 14:44:36 +10:00
if ((current_time - last_counter_increment) >= 300) {
last_counter_increment = current_time;
2025-01-11 02:17:31 +10:00
CounterManager::set_counter(CounterManager::get_counter() + 1);
2025-01-10 14:44:36 +10:00
}
if ((current_time - last_counter_logging) >= 1000) {
last_counter_logging = current_time;
2025-01-11 02:17:31 +10:00
LogWriter::log_counter(CounterManager::get_counter());
2025-01-10 14:44:36 +10:00
}
if ((current_time - last_copies_launching) >= 3000) {
last_copies_launching = current_time;
2025-01-11 02:17:31 +10:00
pid_of_copy_processes = launch_copies();
2025-01-10 14:44:36 +10:00
}
}
2025-01-10 11:17:15 +10:00
return 0;
}