简体   繁体   English

是否有 C++ function 暂停恢复子进程?

[英]Is there a C++ function to suspend of resume a child process?

Is there a way to suspend and resume a process using C++?有没有办法使用 C++ 暂停和恢复进程? Today, I created a mini shell that will stop some background processes to execute a new foreground process.今天,我创建了一个迷你 shell,它将停止一些后台进程以执行新的前台进程。 Thanks,谢谢,

No. The C++ language is entirely agnostic of the concept of processes and thus also of child processes, and thus doesn't have any functions 1 to directly interact with processes.不,C++ 语言完全不知道进程的概念,因此也不知道子进程的概念,因此没有任何函数1可以直接与进程交互。

1 With the exception of std::system inherited from the C standard library which passes a string to a command processor of the host system in an implementation defined manner, and which is in practice typically implemented by creating a subprocess. 1除了从 C 标准库继承的std::system之外,它以实现定义的方式将字符串传递给主机系统的命令处理器,实际上通常通过创建子进程来实现。

You said Powershell in the tags so I assume Windows.您在标签中说 Powershell,所以我假设 Windows。

If you have a handle to a process then you can do things to that process.如果你有一个进程的句柄,那么你可以对那个进程做一些事情。 The list of things you can do is documented in MSDN.您可以做的事情的列表记录在 MSDN 中。 In this case you would need to get handles to the individual threads in a process so you can suspend the threads: https://docs.microsoft.com/en-us/windows/win32/procthread/suspending-thread-execution在这种情况下,您需要获取进程中各个线程的句柄,以便可以挂起线程: https://docs.microsoft.com/en-us/windows/win32/procthread/suspending-thread-execution

Doing this in POSIX is a bit easier.在 POSIX 中执行此操作要容易一些。 You just need to send a SIGSTOP signal to the target process.您只需向目标进程发送一个 SIGSTOP 信号。 Then it and all its threads will stop running until you send SIGCONT.然后它及其所有线程将停止运行,直到您发送 SIGCONT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM