How would I take input and output data on multiple tmux terminal using a cpp program?

The code below creates two panes in tmux and counts down to 20.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#include <iostream>
#include <fstream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
#include <mutex>
#include <string>
#include <array>
#include <memory>
// Function to execute a shell command and get the result
std::string exec(const std::string& cmd) {
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
// Function to be executed by each thread
void threadFunction(const std::string& tty, std::atomic<bool>& running, int threadId) {
int messageCount = 0;
std::ofstream ttyStream(tty);
if (!ttyStream.is_open()) {
std::cerr << "Failed to open tty: " << tty << std::endl;
return;
}
while (running.load()) {
ttyStream << "Thread " << threadId << " is running: message " << messageCount++ << std::endl;
ttyStream.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
}
}
void kill(const std::string& sessionName){
std::string command = "tmux send-keys -t " + sessionName + " " tmux kill-server" Enter";
// Capture output for debugging
std::string output = exec(command); // Assuming `exec` returns the output
std::cerr << "Command output: " << output << std::endl;
if (std::system(command.c_str()) != 0) {
std::cerr << "Error executing command: " << command << std::endl;
}
}
int main() {
const std::string sessionName = "Client";
const int numThreads = 2; // Number of threads and panes
// Create tmux session and panes, and get their TTYs
std::string createSessionCmd = "tmux new-session -d -s " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
std::string pane1 = exec(createSessionCmd);
pane1.pop_back(); // Remove newline character
std::string splitPaneCmd = "tmux split-window -t " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
std::string pane2 = exec(splitPaneCmd);
pane2.pop_back(); // Remove newline character
std::string getTtyCmd1 = "tmux display-message -p -t " + pane1 + " '#{pane_tty}'";
std::string tty1 = exec(getTtyCmd1);
tty1.pop_back(); // Remove newline character
std::string getTtyCmd2 = "tmux display-message -p -t " + pane2 + " '#{pane_tty}'";
std::string tty2 = exec(getTtyCmd2);
tty2.pop_back(); // Remove newline character
std::cout << "TTY1: " << tty1 << std::endl;
std::cout << "TTY2: " << tty2 << std::endl;
std::atomic<bool> running(true);
// Create and run threads
std::thread t1(threadFunction, tty1, std::ref(running), 1);
std::thread t2(threadFunction, tty2, std::ref(running), 2);
std::thread t3([&sessionName]() {
std::this_thread::sleep_for(std::chrono::seconds(20)); // Increase sleep time for testing
std::string command = "tmux new-window -t " + sessionName + " -n thread" + std::to_string(2);
std::system(command.c_str());
kill(sessionName);
});
t1.detach();
t2.detach();
t3.detach();
// Attach to the tmux session
std::string cmd = "tmux attach -t " + sessionName;
std::system(cmd.c_str());
return 0;
}
</code>
<code>#include <iostream> #include <fstream> #include <thread> #include <vector> #include <chrono> #include <atomic> #include <mutex> #include <string> #include <array> #include <memory> // Function to execute a shell command and get the result std::string exec(const std::string& cmd) { std::array<char, 128> buffer; std::string result; std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose); if (!pipe) { throw std::runtime_error("popen() failed!"); } while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) { result += buffer.data(); } return result; } // Function to be executed by each thread void threadFunction(const std::string& tty, std::atomic<bool>& running, int threadId) { int messageCount = 0; std::ofstream ttyStream(tty); if (!ttyStream.is_open()) { std::cerr << "Failed to open tty: " << tty << std::endl; return; } while (running.load()) { ttyStream << "Thread " << threadId << " is running: message " << messageCount++ << std::endl; ttyStream.flush(); std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } } void kill(const std::string& sessionName){ std::string command = "tmux send-keys -t " + sessionName + " " tmux kill-server" Enter"; // Capture output for debugging std::string output = exec(command); // Assuming `exec` returns the output std::cerr << "Command output: " << output << std::endl; if (std::system(command.c_str()) != 0) { std::cerr << "Error executing command: " << command << std::endl; } } int main() { const std::string sessionName = "Client"; const int numThreads = 2; // Number of threads and panes // Create tmux session and panes, and get their TTYs std::string createSessionCmd = "tmux new-session -d -s " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'"; std::string pane1 = exec(createSessionCmd); pane1.pop_back(); // Remove newline character std::string splitPaneCmd = "tmux split-window -t " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'"; std::string pane2 = exec(splitPaneCmd); pane2.pop_back(); // Remove newline character std::string getTtyCmd1 = "tmux display-message -p -t " + pane1 + " '#{pane_tty}'"; std::string tty1 = exec(getTtyCmd1); tty1.pop_back(); // Remove newline character std::string getTtyCmd2 = "tmux display-message -p -t " + pane2 + " '#{pane_tty}'"; std::string tty2 = exec(getTtyCmd2); tty2.pop_back(); // Remove newline character std::cout << "TTY1: " << tty1 << std::endl; std::cout << "TTY2: " << tty2 << std::endl; std::atomic<bool> running(true); // Create and run threads std::thread t1(threadFunction, tty1, std::ref(running), 1); std::thread t2(threadFunction, tty2, std::ref(running), 2); std::thread t3([&sessionName]() { std::this_thread::sleep_for(std::chrono::seconds(20)); // Increase sleep time for testing std::string command = "tmux new-window -t " + sessionName + " -n thread" + std::to_string(2); std::system(command.c_str()); kill(sessionName); }); t1.detach(); t2.detach(); t3.detach(); // Attach to the tmux session std::string cmd = "tmux attach -t " + sessionName; std::system(cmd.c_str()); return 0; } </code>
#include <iostream>
#include <fstream>
#include <thread>
#include <vector>
#include <chrono>
#include <atomic>
#include <mutex>
#include <string>
#include <array>
#include <memory>

// Function to execute a shell command and get the result
std::string exec(const std::string& cmd) {
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd.c_str(), "r"), pclose);
    if (!pipe) {
        throw std::runtime_error("popen() failed!");
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}

// Function to be executed by each thread
void threadFunction(const std::string& tty, std::atomic<bool>& running, int threadId) {
    int messageCount = 0;
    std::ofstream ttyStream(tty);
    if (!ttyStream.is_open()) {
        std::cerr << "Failed to open tty: " << tty << std::endl;
        return;
    }
    while (running.load()) {
        ttyStream << "Thread " << threadId << " is running: message " << messageCount++ << std::endl;
        ttyStream.flush();
        std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    }
}

void kill(const std::string& sessionName){
    std::string command = "tmux send-keys -t " + sessionName + " " tmux kill-server" Enter";

    // Capture output for debugging
    std::string output = exec(command); // Assuming `exec` returns the output
    std::cerr << "Command output: " << output << std::endl;

    if (std::system(command.c_str()) != 0) {
        std::cerr << "Error executing command: " << command << std::endl;
    }
}

int main() {
    const std::string sessionName = "Client";
    const int numThreads = 2; // Number of threads and panes

    // Create tmux session and panes, and get their TTYs
    std::string createSessionCmd = "tmux new-session -d -s " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
    std::string pane1 = exec(createSessionCmd);
    pane1.pop_back(); // Remove newline character

    std::string splitPaneCmd = "tmux split-window -t " + sessionName + " -PF '#{pane_id}' 'tail -f /dev/null'";
    std::string pane2 = exec(splitPaneCmd);
    pane2.pop_back(); // Remove newline character

    std::string getTtyCmd1 = "tmux display-message -p -t " + pane1 + " '#{pane_tty}'";
    std::string tty1 = exec(getTtyCmd1);
    tty1.pop_back(); // Remove newline character

    std::string getTtyCmd2 = "tmux display-message -p -t " + pane2 + " '#{pane_tty}'";
    std::string tty2 = exec(getTtyCmd2);
    tty2.pop_back(); // Remove newline character

    std::cout << "TTY1: " << tty1 << std::endl;
    std::cout << "TTY2: " << tty2 << std::endl;

    std::atomic<bool> running(true);

    // Create and run threads
    std::thread t1(threadFunction, tty1, std::ref(running), 1);
    std::thread t2(threadFunction, tty2, std::ref(running), 2);
    
    std::thread t3([&sessionName]() {
        std::this_thread::sleep_for(std::chrono::seconds(20)); // Increase sleep time for testing
    
        std::string command = "tmux new-window -t " + sessionName + " -n thread" + std::to_string(2);
        std::system(command.c_str());
        kill(sessionName);

    });

    t1.detach();
    t2.detach();
    t3.detach();

    

    // Attach to the tmux session
    std::string cmd = "tmux attach -t " + sessionName;
    std::system(cmd.c_str());

    return 0;
}

The problem I am facing is that the panes are not able to take any input. I want thread to display messages on the assigned pane. But if a user wants to send message the pane can take any input then this input should be displayed in the other pane. Meaning the counting messages stop when it is taking input and then display the message and start counting from where it left off. But in the above code it can’t do that but not just that but I can’t run any command. So even to close the tmux session I have to create a new tmux window and use kill-server on the new tmux window.

If I don’t use tty then I have to make it so that in each pane whenever an user enters a string of characters to not consider it as a command and write those into a file and then will need to have a new thread to read from this file and display that in particular pane.

But when I don’t use tty then each “input + Enter” gives the output that input was not a command and as you can see on the top pane to display anything the cpp file needs to run “echo ‘message’ + Enter”. Meaning the message displays twice

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật