Программа на Ubuntu Создайте трубку и используйте оболочку, но когда я хочу отправить команду для оболочки, уже закрытуюC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Программа на Ubuntu Создайте трубку и используйте оболочку, но когда я хочу отправить команду для оболочки, уже закрытую

Сообщение Anonymous »

У меня есть кодовый клиент на C ++, клиент работает на Ubuntu. Когда я создаю сеанс Нью -Йорк Бэш закрыт. Почему? Если у вас есть идея, напишите, пожалуйста < /p>

Код: Выделить всё

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 

std::atomic running(true);

void signalHandler(int signal) {
if (signal == SIGINT) {
running = false;
}
}

class ShellHandler {
private:
int pipeIn[2];  // Pipe for sending input to the shell
int pipeOut[2]; // Pipe for receiving output from the shell
pid_t shellPid;

public:
ShellHandler() : pipeIn{-1, -1}, pipeOut{-1, -1}, shellPid(-1) {
// Create pipes for communication with the shell
if (pipe(pipeIn) == -1 || pipe(pipeOut) == -1) {
throw std::runtime_error("Failed to create pipes.");
}

// Fork a child process to run the shell
shellPid = fork();
if (shellPid == -1) {
throw std::runtime_error("Failed to fork.");
}

if (shellPid == 0) {
// Child process: Run the shell
close(pipeIn[1]);  // Close the write end of the input pipe (не используется в дочернем процессе)
close(pipeOut[0]); // Close the read end of the output pipe (не используется в дочернем процессе)

// Redirect stdin and stdout to the pipes
if (dup2(pipeIn[0], STDIN_FILENO) == -1) {
std::cerr 

Подробнее здесь: [url]https://stackoverflow.com/questions/79504744/program-on-ubuntu-create-pipe-and-use-shell-but-when-i-want-send-command-for-sh[/url]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»