first program
This commit is contained in:
parent
70c5b503fc
commit
940781265f
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
_trash/
|
28
.vscode/tasks.json
vendored
Normal file
28
.vscode/tasks.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
8
main.cpp
8
main.cpp
@ -1,8 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
cout << "Hello World";
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
76
process_launcher.cpp
Normal file
76
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
process_launcher.h
Normal file
34
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
|
29
test.cpp
Normal file
29
test.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include "process_launcher.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user