diff --git a/.gitignore b/task_1_2/.gitignore similarity index 100% rename from .gitignore rename to task_1_2/.gitignore diff --git a/CMakeLists.txt b/task_1_2/CMakeLists.txt similarity index 100% rename from CMakeLists.txt rename to task_1_2/CMakeLists.txt diff --git a/README.md b/task_1_2/README.md similarity index 100% rename from README.md rename to task_1_2/README.md diff --git a/main.cpp b/task_1_2/main.cpp similarity index 100% rename from main.cpp rename to task_1_2/main.cpp diff --git a/process_launcher.cpp b/task_1_2/process_launcher.cpp similarity index 100% rename from process_launcher.cpp rename to task_1_2/process_launcher.cpp diff --git a/process_launcher.h b/task_1_2/process_launcher.h similarity index 100% rename from process_launcher.h rename to task_1_2/process_launcher.h diff --git a/task_3/CMakeLists.txt b/task_3/CMakeLists.txt new file mode 100644 index 0000000..8f1bc14 --- /dev/null +++ b/task_3/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.13) # Проверка версии CMake. + # Если версия установленной программы + # старее указаной, произойдёт аварийный выход. + +project(hello-world-cpp-logger) # Название проекта + + +add_library(log_writer STATIC log_writer.cpp) # Создание статической библиотеки с именем process_launcher + +add_executable(main main.cpp) # Создает исполняемый файл с именем main + +target_link_libraries(main log_writer) # Линковка программы с библиотекой \ No newline at end of file diff --git a/task_3/log_writer.cpp b/task_3/log_writer.cpp new file mode 100644 index 0000000..25d5263 --- /dev/null +++ b/task_3/log_writer.cpp @@ -0,0 +1,22 @@ +#include "log_writer.h" + +int LogWriter::write_log(const std::string& log_line) { + std::string new_line = "\n"; + int fd = open( + "log.txt", + O_WRONLY|O_APPEND|O_CREAT + ); + if (fd == -1) { + return -1; // Ошибка при открытии файла + } + + write(fd, log_line.c_str(), log_line.length()); + write(fd, new_line.c_str(), new_line.length()); + + return 0; +} + + +int LogWriter::log_start_line(int pid){ + time_t now = time(NULL); +} \ No newline at end of file diff --git a/task_3/log_writer.h b/task_3/log_writer.h new file mode 100644 index 0000000..856ff10 --- /dev/null +++ b/task_3/log_writer.h @@ -0,0 +1,29 @@ +#ifndef PROCESS_LAUNCHER_H +#define PROCESS_LAUNCHER_H + +#ifdef _WIN32 + #include +#else + #include + #include + #include +#endif + +#include + +#include +#include +#include + + +class LogWriter { +public: + static int log_start_line(int pid); + + + +private: + static int write_log(const std::string& log_line); +}; + +#endif // PROCESS_LAUNCHER_H \ No newline at end of file diff --git a/task_3/main.cpp b/task_3/main.cpp new file mode 100644 index 0000000..2460dea --- /dev/null +++ b/task_3/main.cpp @@ -0,0 +1,22 @@ +#include "log_writer.h" + +#include + +int main(int argc, char* argv[]) { + if (argc >= 2) { + if (argv[1][0] == '1') { + std::cout << "start copy 1"; + return 0; + } + + if (argv[1][0] == '2') { + std::cout << "start copy 2"; + return 0; + } + } + + std::cout << "launch main part\n"; + int res = LogWriter::write_log("heelo"); + std::cout << res; + return 0; +} \ No newline at end of file diff --git a/task_3/process_launcher.cpp b/task_3/process_launcher.cpp new file mode 100644 index 0000000..09bdaed --- /dev/null +++ b/task_3/process_launcher.cpp @@ -0,0 +1,76 @@ +#include "process_launcher.h" +#include + +#ifdef _WIN32 + +// Для Windows используем CreateProcess +int ProcessLauncher::launch(const std::string& command) { + return launchWindows(command); +} + +int ProcessLauncher::launchWindows(const std::string& command) { + STARTUPINFO si = { sizeof(STARTUPINFO) }; + PROCESS_INFORMATION pi; + if (!CreateProcess(NULL, const_cast(command.c_str()), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) { + std::cerr << "Ошибка запуска процесса: " << GetLastError() << std::endl; + return -1; + } + + return pi.dwProcessId; // Возвращаем PID процесса +} + +int ProcessLauncher::waitForProcess(int pid) { + return waitForProcessWindows((HANDLE)pid); +} + +int ProcessLauncher::waitForProcessWindows(HANDLE processHandle) { + DWORD exitCode; + WaitForSingleObject(processHandle, INFINITE); + if (GetExitCodeProcess(processHandle, &exitCode)) { + return exitCode; + } + return -1; +} + +#else + +// Для UNIX-подобных систем используем fork и exec +int ProcessLauncher::launch(const std::string& command) { + return launchUnix(command); +} + +int ProcessLauncher::launchUnix(const std::string& command) { + pid_t pid = fork(); + if (pid == -1) { + std::cerr << "Ошибка fork" << std::endl; + return -1; + } + + if (pid == 0) { // Дочерний процесс + execlp(command.c_str(), command.c_str(), (char*)NULL); + // Если execlp вернул, значит произошла ошибка + std::cerr << "Ошибка запуска команды: " << command << std::endl; + exit(-1); + } + + return pid; // Возвращаем PID процесса +} + +int ProcessLauncher::waitForProcess(int pid) { + return waitForProcessUnix(pid); +} + +int ProcessLauncher::waitForProcessUnix(pid_t pid) { + int status; + if (waitpid(pid, &status, 0) == -1) { + std::cerr << "Ошибка ожидания процесса" << std::endl; + return -1; + } + + if (WIFEXITED(status)) { + return WEXITSTATUS(status); // Возвращаем код завершения процесса + } + return -1; +} + +#endif \ No newline at end of file diff --git a/task_3/process_launcher.h b/task_3/process_launcher.h new file mode 100644 index 0000000..701933f --- /dev/null +++ b/task_3/process_launcher.h @@ -0,0 +1,34 @@ +#ifndef PROCESS_LAUNCHER_H +#define PROCESS_LAUNCHER_H + +#ifdef _WIN32 + #include +#else + #include + #include + #include +#endif + +#include + +class ProcessLauncher { +public: + // Запуск программы в фоновом режиме + static int launch(const std::string& command); + + // Ожидание завершения процесса и получение его кода возврата + static int waitForProcess(int pid); + +private: +#ifdef _WIN32 + // Для Windows + static int launchWindows(const std::string& command); + static int waitForProcessWindows(HANDLE processHandle); +#else + // Для UNIX-подобных систем + static int launchUnix(const std::string& command); + static int waitForProcessUnix(pid_t pid); +#endif +}; + +#endif // PROCESS_LAUNCHER_H \ No newline at end of file