make ready for linux

This commit is contained in:
Ivanov Matvey 2025-01-10 16:59:44 +00:00
parent 3ef5493892
commit 07d5d32cbd
5 changed files with 23 additions and 23 deletions

View File

@ -8,7 +8,8 @@ long long int CounterManager::get_counter() {
int fd = open( int fd = open(
"counter.txt", "counter.txt",
_O_RDONLY O_RDONLY,
0777
); );
if (fd == -1) { if (fd == -1) {
return 0; // Ошибка при открытии файла return 0; // Ошибка при открытии файла

View File

@ -1,5 +1,6 @@
#include <string> #include <string>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h>
class CounterManager { class CounterManager {

View File

@ -4,7 +4,8 @@ int LogWriter::write_log(const std::string& log_line) {
std::string new_line = "\n"; std::string new_line = "\n";
int fd = open( int fd = open(
"main.log", "main.log",
O_WRONLY|O_APPEND|O_CREAT O_WRONLY|O_APPEND|O_CREAT,
0777
); );
if (fd == -1) { if (fd == -1) {
return -1; // Ошибка при открытии файла return -1; // Ошибка при открытии файла
@ -30,7 +31,7 @@ std::string LogWriter::get_current_time() {
} }
int LogWriter::log_start_line(){ void LogWriter::log_start_line(){
std::string log_line = ( std::string log_line = (
"start programm with pid " + "start programm with pid " +
std::to_string(getpid()) + std::to_string(getpid()) +
@ -38,11 +39,10 @@ int LogWriter::log_start_line(){
get_current_time() get_current_time()
); );
write_log(log_line); write_log(log_line);
return 0;
} }
int LogWriter::log_counter(long long int counter){ void LogWriter::log_counter(long long int counter){
std::string log_line = ( std::string log_line = (
"counter at " + "counter at " +
get_current_time() + get_current_time() +
@ -52,11 +52,10 @@ int LogWriter::log_counter(long long int counter){
std::to_string(counter) std::to_string(counter)
); );
write_log(log_line); write_log(log_line);
return 0;
} }
int LogWriter::log_copy_start(std::string copy_name) { void LogWriter::log_copy_start(std::string copy_name) {
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is started at " + " is started at " +
@ -65,10 +64,9 @@ int LogWriter::log_copy_start(std::string copy_name) {
std::to_string(getpid()) std::to_string(getpid())
); );
write_log(log_line); write_log(log_line);
return 0;
} }
int LogWriter::log_copy_finish(std::string copy_name) { void LogWriter::log_copy_finish(std::string copy_name) {
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is finished at " + " is finished at " +
@ -77,7 +75,7 @@ int LogWriter::log_copy_finish(std::string copy_name) {
write_log(log_line); write_log(log_line);
} }
int LogWriter::log_copy_error(std::string copy_name) { void LogWriter::log_copy_error(std::string copy_name) {
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is not finished" " is not finished"

View File

@ -6,11 +6,11 @@
class LogWriter { class LogWriter {
public: public:
static int log_start_line(); static void log_start_line();
static int log_counter(long long int counter); static void log_counter(long long int counter);
static int log_copy_start(std::string copy_name); static void log_copy_start(std::string copy_name);
static int log_copy_finish(std::string copy_name); static void log_copy_finish(std::string copy_name);
static int log_copy_error(std::string copy_name); static void log_copy_error(std::string copy_name);
protected: protected:
static std::string get_current_time(); static std::string get_current_time();

View File

@ -22,8 +22,8 @@ struct two_pid {
#else #else
// Для UNIX-подобных систем // Для UNIX-подобных систем
struct two_pid launch_copies() { struct two_pid launch_copies() {
std::string command1 = "main 1"; std::string command1 = "./main 1";
std::string command2 = "main 2"; std::string command2 = "./main 2";
struct two_pid result; struct two_pid result;
result.pid_1 = ProcessLauncher::launch(command1); result.pid_1 = ProcessLauncher::launch(command1);
result.pid_2 = ProcessLauncher::launch(command2); result.pid_2 = ProcessLauncher::launch(command2);
@ -32,11 +32,11 @@ struct two_pid {
#endif #endif
uint64_t get_timestamp_ms() { long long int get_timestamp_ms() {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000); return (((long long int)tv.tv_sec)*1000)+(tv.tv_usec/1000);
} }
@ -63,10 +63,10 @@ int main(int argc, char* argv[]) {
struct two_pid pid_of_copy_processes; struct two_pid pid_of_copy_processes;
uint64_t last_counter_increment = get_timestamp_ms(); long long int last_counter_increment = get_timestamp_ms();
uint64_t last_counter_logging = get_timestamp_ms(); long long int last_counter_logging = get_timestamp_ms();
uint64_t last_copies_launching = get_timestamp_ms(); long long int last_copies_launching = get_timestamp_ms();
uint64_t current_time; long long int current_time;
while (1) { while (1) {
current_time = get_timestamp_ms(); current_time = get_timestamp_ms();