From 2595b0b7b74176a71dc695034a9d649e8ad99da5 Mon Sep 17 00:00:00 2001 From: matv864 Date: Sat, 11 Jan 2025 02:17:31 +1000 Subject: [PATCH] update task 3, go to counter in file --- .gitignore | 7 +++++- task_3/CMakeLists.txt | 3 ++- task_3/counter.txt | 1 + task_3/counter_manager.cpp | 40 +++++++++++++++++++++++++++++++ task_3/counter_manager.h | 12 ++++++++++ task_3/log_writer.cpp | 6 +++-- task_3/log_writer.h | 4 ++-- task_3/main.cpp | 48 +++++++++++++++++++++++++++++++------- 8 files changed, 107 insertions(+), 14 deletions(-) create mode 100644 task_3/counter.txt create mode 100644 task_3/counter_manager.cpp create mode 100644 task_3/counter_manager.h diff --git a/.gitignore b/.gitignore index b1b94f2..ee9f1fa 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,9 @@ _trash/ # auto-generating trash from cmake build/ -CMakeFiles/ \ No newline at end of file +CMakeFiles/ +Makefile +CMakeCache.txt +*.cmake +*.a + diff --git a/task_3/CMakeLists.txt b/task_3/CMakeLists.txt index b327208..cf59185 100644 --- a/task_3/CMakeLists.txt +++ b/task_3/CMakeLists.txt @@ -5,7 +5,8 @@ project(hello-world-cpp-logger) add_library(log_writer STATIC log_writer.cpp) add_library(process_launcher STATIC process_launcher.cpp) +add_library(counter_manager STATIC counter_manager.cpp) add_executable(main main.cpp) -target_link_libraries(main log_writer process_launcher) \ No newline at end of file +target_link_libraries(main log_writer process_launcher counter_manager) \ No newline at end of file diff --git a/task_3/counter.txt b/task_3/counter.txt new file mode 100644 index 0000000..f937f7e --- /dev/null +++ b/task_3/counter.txt @@ -0,0 +1 @@ +233 \ No newline at end of file diff --git a/task_3/counter_manager.cpp b/task_3/counter_manager.cpp new file mode 100644 index 0000000..c8ff0f2 --- /dev/null +++ b/task_3/counter_manager.cpp @@ -0,0 +1,40 @@ +#include "counter_manager.h" + + + +long long int CounterManager::get_counter() { + char data_from_file[128]; + int num_bytes = 128; + + int fd = open( + "counter.txt", + _O_RDONLY + ); + if (fd == -1) { + return 0; // Ошибка при открытии файла + } + + read(fd, data_from_file, num_bytes); + + close(fd); + + return atoi(data_from_file); +} + + +int CounterManager::set_counter(long long int new_counter) { + std::string new_counter_string = std::to_string(new_counter); + int fd = open( + "counter.txt", + O_WRONLY|O_TRUNC|O_APPEND|O_CREAT, + 0777 + ); + if (fd == -1) { + return -1; // Ошибка при открытии файла + } + + write(fd, new_counter_string.c_str(), new_counter_string.length()); + close(fd); + + return 0; +} \ No newline at end of file diff --git a/task_3/counter_manager.h b/task_3/counter_manager.h new file mode 100644 index 0000000..ff5cd8c --- /dev/null +++ b/task_3/counter_manager.h @@ -0,0 +1,12 @@ +#include +#include +#include +#include + + +class CounterManager { +public: + static long long int get_counter(); + static int set_counter(long long int new_counter); + +}; diff --git a/task_3/log_writer.cpp b/task_3/log_writer.cpp index 5cdc4e5..94268f8 100644 --- a/task_3/log_writer.cpp +++ b/task_3/log_writer.cpp @@ -13,6 +13,8 @@ int LogWriter::write_log(const std::string& log_line) { write(fd, log_line.c_str(), log_line.length()); write(fd, new_line.c_str(), new_line.length()); + close(fd); + return 0; } @@ -36,7 +38,7 @@ int LogWriter::log_start_line(){ } -int LogWriter::log_counter(int counter){ +int LogWriter::log_counter(long long int counter){ std::string current_time = "00:00:00"; std::string log_line = ( "counter at " + @@ -51,7 +53,7 @@ int LogWriter::log_counter(int counter){ } -int LogWriter::log_copy_start(int counter, std::string copy_name) { +int LogWriter::log_copy_start(std::string copy_name) { std::string current_time = "00:00:00"; std::string log_line = ( copy_name + diff --git a/task_3/log_writer.h b/task_3/log_writer.h index 1bf5532..d5f5d96 100644 --- a/task_3/log_writer.h +++ b/task_3/log_writer.h @@ -9,8 +9,8 @@ class LogWriter { public: static int log_start_line(); - static int log_counter(int counter); - static int log_copy_start(int counter, std::string copy_name); + static int log_counter(long long int counter); + static int log_copy_start(std::string copy_name); static int log_copy_finish(std::string copy_name); static int log_copy_error(std::string copy_name); diff --git a/task_3/main.cpp b/task_3/main.cpp index d34d737..7dfa1e0 100644 --- a/task_3/main.cpp +++ b/task_3/main.cpp @@ -1,8 +1,35 @@ #include "process_launcher.h" #include "log_writer.h" +#include "counter_manager.h" #include // for ms in timestamp #include // for debugging +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 + uint64_t get_timestamp_ms() { using namespace std::chrono; @@ -12,19 +39,26 @@ uint64_t get_timestamp_ms() { int main(int argc, char* argv[]) { if (argc >= 2) { if (argv[1][0] == '1') { - std::cout << "start copy 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') { - std::cout << "start copy 2"; + LogWriter::log_copy_start("Copy 2"); + CounterManager::set_counter(CounterManager::get_counter() * 2); + // sleep 2s + CounterManager::set_counter(CounterManager::get_counter() / 2); + LogWriter::log_copy_finish("Copy 2"); return 0; } } LogWriter::log_start_line(); - long long int counter = 0; + struct two_pid pid_of_copy_processes; + uint64_t last_counter_increment = get_timestamp_ms(); uint64_t last_counter_logging = get_timestamp_ms(); uint64_t last_copies_launching = get_timestamp_ms(); @@ -35,18 +69,16 @@ int main(int argc, char* argv[]) { if ((current_time - last_counter_increment) >= 300) { last_counter_increment = current_time; - counter++; + CounterManager::set_counter(CounterManager::get_counter() + 1); } if ((current_time - last_counter_logging) >= 1000) { last_counter_logging = current_time; - LogWriter::log_counter(counter); + 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;