【问题标题】:Pausing and resuming a shell process from C++从 C++ 暂停和恢复 shell 进程
【发布时间】:2021-10-08 14:26:05
【问题描述】:

在 C++ 中,我希望提交一个进程,暂停、恢复和停止它。为此,我首先使用以下函数在后台运行一个 shell 进程并保存关联的 PID。我在this post 找到了这个函数(并且只删除了标准输入和输出)。

int system2(const char * command)
{
    int p_stdin[2];
    int p_stdout[2];
    int pid;

    if (pipe(p_stdin) == -1)
        return -1;

    if (pipe(p_stdout) == -1) {
        close(p_stdin[0]);
        close(p_stdin[1]);
        return -1;
    }

    pid = fork();

    if (pid < 0) {
        close(p_stdin[0]);
        close(p_stdin[1]);
        close(p_stdout[0]);
        close(p_stdout[1]);
        return pid;
    } else if (pid == 0) {
        close(p_stdin[1]);
        dup2(p_stdin[0], 0);
        close(p_stdout[0]);
        dup2(p_stdout[1], 1);
        dup2(::open("/dev/null", O_RDONLY), 2);
        /// Close all other descriptors for the safety sake.
        for (int i = 3; i < 4096; ++i)
            ::close(i);

        setsid();
        execl("/bin/sh", "sh", "-c", command, NULL);
        _exit(1);
    }

    close(p_stdin[0]);
    close(p_stdout[1]);

    
    return pid;
}

然后,我使用kill 函数来暂停、恢复和停止进程,但它没有按我预期的那样工作。这是一个例子:

int main()
{
    // The process prints on file allowing me to figure out whether the process is paused / stopped, or is running
    const char * command = "for i in {1..1000}; do echo $i >> /Users/remi/test/data.txt; sleep 1s;done";

    // Run the command and record pid
    auto pid = system2(command);
    std::cout << "pid = " << pid << "\n";  
    // with this pid, I could ensure that `ps -p <pid>` returns the correct command

    std::cout << "process should be running!\n"; // It is!

    // wait
    std::this_thread::sleep_for(std::chrono::seconds(10));

    // pause
    kill(pid, SIGTSTP);
    std::cout << "process should be paused!\n"; // But it is not!

    // wait
    std::this_thread::sleep_for(std::chrono::seconds(10));

    // resume process
    kill(pid, SIGCONT);
    std::cout << "process should be running!\n"; // Sure, it is as it has never been stopped

    // wait 
    std::this_thread::sleep_for(std::chrono::seconds(10));

    // Kill process
    kill(pid, SIGSTOP);
    std::cout << "process should be stopped!\n"; // That worked!

    // wait 
    std::this_thread::sleep_for(std::chrono::seconds(10));
}

您能帮我弄清楚如何修复此代码,以确保进程按预期停止并恢复。

仅供参考,我使用的是 macOS,希望该解决方案适用于任何 POSIX 系统。

【问题讨论】:

  • 进程可能会忽略SIGTSTP。使用不可忽略的SIGSTOP 来暂停进程。使用SIGTERMSIGKILL 杀死它。
  • 行得通!我虽然无法从 SIGSTOP 恢复。谢谢。我应该删除这个问题还是我们认为这个问题可能会被其他人使用?
  • 我认为这可能会有所帮助。

标签: c++ signals posix kill


【解决方案1】:

请注意,SIGTSTPSIGSTOP 信号实际上都在暂停进程。过程可以忽略第一个,但不能忽略后者。如果您想暂停该过程,请使用SIGSTOP

要终止进程,请使用SIGTERMSIGKILL

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    相关资源
    最近更新 更多