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