div tasks
This commit is contained in:
parent
148d568192
commit
156aaf4f4e
0
.gitignore → task_1_2/.gitignore
vendored
0
.gitignore → task_1_2/.gitignore
vendored
12
task_3/CMakeLists.txt
Normal file
12
task_3/CMakeLists.txt
Normal file
@ -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) # Линковка программы с библиотекой
|
22
task_3/log_writer.cpp
Normal file
22
task_3/log_writer.cpp
Normal file
@ -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);
|
||||
}
|
29
task_3/log_writer.h
Normal file
29
task_3/log_writer.h
Normal file
@ -0,0 +1,29 @@
|
||||
#ifndef PROCESS_LAUNCHER_H
|
||||
#define PROCESS_LAUNCHER_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
class LogWriter {
|
||||
public:
|
||||
static int log_start_line(int pid);
|
||||
|
||||
|
||||
|
||||
private:
|
||||
static int write_log(const std::string& log_line);
|
||||
};
|
||||
|
||||
#endif // PROCESS_LAUNCHER_H
|
22
task_3/main.cpp
Normal file
22
task_3/main.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "log_writer.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
76
task_3/process_launcher.cpp
Normal file
76
task_3/process_launcher.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "process_launcher.h"
|
||||
#include <iostream>
|
||||
|
||||
#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<char*>(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
|
34
task_3/process_launcher.h
Normal file
34
task_3/process_launcher.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef PROCESS_LAUNCHER_H
|
||||
#define PROCESS_LAUNCHER_H
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
|
||||
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
|
Loading…
x
Reference in New Issue
Block a user