ready to launch

This commit is contained in:
Ivanov Matvey 2025-01-11 02:35:25 +10:00
parent 2595b0b7b7
commit 3ef5493892
6 changed files with 25 additions and 21 deletions

3
.gitignore vendored
View File

@ -15,3 +15,6 @@ CMakeCache.txt
*.cmake *.cmake
*.a *.a
# for task3
counter.txt

View File

@ -1 +1 @@
233 28

View File

@ -1,7 +1,5 @@
#include <string> #include <string>
#include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
class CounterManager { class CounterManager {

View File

@ -20,18 +20,22 @@ int LogWriter::write_log(const std::string& log_line) {
std::string LogWriter::get_current_time() { std::string LogWriter::get_current_time() {
time_t current_time = std::time(nullptr); time_t rawtime;
return ctime(&current_time); struct tm * timeinfo;
time ( &rawtime );
timeinfo = localtime ( &rawtime );
std::string res = asctime (timeinfo);
res.pop_back();
return res;
} }
int LogWriter::log_start_line(){ int LogWriter::log_start_line(){
std::string current_time = "00:00:00";
std::string log_line = ( std::string log_line = (
"start programm with pid " + "start programm with pid " +
std::to_string(getpid()) + std::to_string(getpid()) +
" at " + " at " +
current_time get_current_time()
); );
write_log(log_line); write_log(log_line);
return 0; return 0;
@ -39,10 +43,9 @@ int LogWriter::log_start_line(){
int LogWriter::log_counter(long long int counter){ int LogWriter::log_counter(long long int counter){
std::string current_time = "00:00:00";
std::string log_line = ( std::string log_line = (
"counter at " + "counter at " +
current_time + get_current_time() +
" in process with pid " + " in process with pid " +
std::to_string(getpid()) + std::to_string(getpid()) +
" is " + " is " +
@ -54,11 +57,10 @@ int LogWriter::log_counter(long long int counter){
int LogWriter::log_copy_start(std::string copy_name) { int LogWriter::log_copy_start(std::string copy_name) {
std::string current_time = "00:00:00";
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is started at " + " is started at " +
current_time + get_current_time() +
" with pid " + " with pid " +
std::to_string(getpid()) std::to_string(getpid())
); );
@ -67,11 +69,10 @@ int LogWriter::log_copy_start(std::string copy_name) {
} }
int LogWriter::log_copy_finish(std::string copy_name) { int LogWriter::log_copy_finish(std::string copy_name) {
std::string current_time = "00:00:00";
std::string log_line = ( std::string log_line = (
copy_name + copy_name +
" is finished at " + " is finished at " +
current_time get_current_time()
); );
write_log(log_line); write_log(log_line);
} }

View File

@ -1,9 +1,7 @@
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h> #include <time.h>
#include <ctime>
class LogWriter { class LogWriter {

View File

@ -1,8 +1,9 @@
#include "process_launcher.h" #include "process_launcher.h"
#include "log_writer.h" #include "log_writer.h"
#include "counter_manager.h" #include "counter_manager.h"
#include <chrono> // for ms in timestamp
#include <iostream> // for debugging #include <sys/time.h>
struct two_pid { struct two_pid {
int pid_1, pid_2; int pid_1, pid_2;
@ -32,8 +33,11 @@ struct two_pid {
uint64_t get_timestamp_ms() { uint64_t get_timestamp_ms() {
using namespace std::chrono; struct timeval tv;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); gettimeofday(&tv, NULL);
return (((long long)tv.tv_sec)*1000)+(tv.tv_usec/1000);
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
@ -48,7 +52,7 @@ int main(int argc, char* argv[]) {
if (argv[1][0] == '2') { if (argv[1][0] == '2') {
LogWriter::log_copy_start("Copy 2"); LogWriter::log_copy_start("Copy 2");
CounterManager::set_counter(CounterManager::get_counter() * 2); CounterManager::set_counter(CounterManager::get_counter() * 2);
// sleep 2s sleep(2);
CounterManager::set_counter(CounterManager::get_counter() / 2); CounterManager::set_counter(CounterManager::get_counter() / 2);
LogWriter::log_copy_finish("Copy 2"); LogWriter::log_copy_finish("Copy 2");
return 0; return 0;