105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
#include "process_launcher.h"
|
|
#include "log_writer.h"
|
|
#include "counter_manager.h"
|
|
|
|
#include <sys/stat.h>
|
|
#include <iostream>
|
|
|
|
|
|
|
|
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() {
|
|
std::string command1 = "./main 1";
|
|
std::string command2 = "./main 2";
|
|
struct two_pid result;
|
|
result.pid_1 = ProcessLauncher::launch(command1);
|
|
result.pid_2 = ProcessLauncher::launch(command2);
|
|
return result;
|
|
}
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
if (argc >= 2) {
|
|
if (argv[1][0] == '1') {
|
|
LogWriter::log_copy_start("Copy 1");
|
|
CounterManager::set_counter(CounterManager::get_counter() + 10);
|
|
LogWriter::log_copy_finish("Copy 1");
|
|
return 0;
|
|
}
|
|
|
|
if (argv[1][0] == '2') {
|
|
LogWriter::log_copy_start("Copy 2");
|
|
CounterManager::set_counter(CounterManager::get_counter() * 2);
|
|
sleep(2);
|
|
CounterManager::set_counter(CounterManager::get_counter() / 2);
|
|
LogWriter::log_copy_finish("Copy 2");
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
// 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());
|
|
}
|
|
|
|
LogWriter::log_start_line();
|
|
|
|
struct two_pid pid_of_copy_processes;
|
|
|
|
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();
|
|
long long int current_time;
|
|
|
|
while (1) {
|
|
current_time = MyTime::get_timestamp();
|
|
|
|
if ((current_time - last_counter_increment) >= 300) {
|
|
last_counter_increment = current_time;
|
|
CounterManager::set_counter(CounterManager::get_counter() + 1);
|
|
}
|
|
if ((current_time - last_counter_logging) >= 1000) {
|
|
last_counter_logging = current_time;
|
|
LogWriter::log_counter(CounterManager::get_counter());
|
|
}
|
|
if ((current_time - last_copies_launching) >= 3000) {
|
|
last_copies_launching = current_time;
|
|
pid_of_copy_processes = launch_copies();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
} |