diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4083906 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +_trash/ \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6a85229 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: g++.exe build active file", + "command": "C:\\MinGW\\bin\\g++.exe", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}\\${fileBasenameNoExtension}.exe" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/main.cpp b/main.cpp deleted file mode 100644 index fde39b7..0000000 --- a/main.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include -using namespace std; - -int main(){ - cout << "Hello World"; - - return 0; -} \ No newline at end of file diff --git a/process_launcher.cpp b/process_launcher.cpp new file mode 100644 index 0000000..09bdaed --- /dev/null +++ b/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/process_launcher.h b/process_launcher.h new file mode 100644 index 0000000..701933f --- /dev/null +++ b/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 diff --git a/test.cpp b/test.cpp new file mode 100644 index 0000000..4f529b2 --- /dev/null +++ b/test.cpp @@ -0,0 +1,29 @@ +#include "process_launcher.h" +#include +#include + +int main() { + std::string command; + + // В зависимости от ОС, выберем команду для запуска +#ifdef _WIN32 + command = "cmd /C echo Hello, World!"; +#else + command = "echo Hello, World!"; +#endif + + // Запускаем процесс + int pid = ProcessLauncher::launch(command); + if (pid == -1) { + std::cerr << "Не удалось запустить процесс." << std::endl; + return -1; + } + + std::cout << "Процесс с PID: " << pid << " запущен." << std::endl; + + // Ожидаем завершения процесса + int exitCode = ProcessLauncher::waitForProcess(pid); + std::cout << "Процесс завершился с кодом: " << exitCode << std::endl; + + return 0; +} \ No newline at end of file diff --git a/test.exe b/test.exe new file mode 100644 index 0000000..080a4af Binary files /dev/null and b/test.exe differ