暂停和恢复一个shell过程从c++



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

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系统上都能工作。

注意,SIGTSTPSIGSTOP信号实际上都在暂停进程。前者可以被忽略,而后者则不能。如果您希望无论如何都要暂停进程,请使用SIGSTOP.

终止进程,使用SIGTERMSIGKILL

最新更新