- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在尝试通过 execvp() 运行多个命令时遇到问题。为此,我有一个 while 循环来执行每个命令,解析它,然后调用一个函数来使用 fork + exec。
问题是,我将运行 fork + exec,当我等待 exec 完成时,父级运行并继续第二个循环,即。第二个命令。但根据我的理解,发生的情况是上一个循环中的子进程接管,突然间,子进程就是当前进程。
如何在子进程中运行进程,同时在父进程中保持控制权?我在 SO 上看到的所有示例都是父进程等待子进程终止后再继续,但我在这个任务中没有选择 - 我应该保持子进程运行并稍后检查查看它们是否仍在运行。
这是我的思考过程的一些伪代码:
funct forkprocess() {
//call fork
//call execvp within child process, let process run and return control to parent
//if execvp failed, kill child process
//return child pid
}
int main() {
while(not end of file containing list of commands) :
//parse command for execvp call
//call forkprocess() to run process
//if childpid is returned, report it and store pid
//else, report failure
}
}
我的输出接近于以下内容:
\\parent PID is printed
\\any code outside the if-else ladder for fork is printed
\\it jumps back to main and prints any statements there
[jumps to next iteration in loop, ie. the next command]
\\child PID is printed
\\parent PID is printed
\\any code outside the if-else ladder for fork is printed
\\it jumps back to main and prints any statements there
\\child PID is printed
最佳答案
您需要确保仅在子级中调用 exec
,而不是在父级中调用。当 fork()
返回时,它会执行两次(一次在子级中,一次在父级中),并返回以下 values :
waitpid
.您可能需要考虑使用以下结构:
int pid = fork();
if(pid < 0) {
perror("fork"); // remember to error check!
} else if (pid == 0) {
// We are in the child
// ...
if(execvp(...) < 0) {
perror("exec");
}
} else {
// We are in the parent; wait for the child to terminate using waitpid if desired.
waitpid(...);
}
为了简单起见,我在此处包含了 waitpid
调用;由于您的任务要求您保持子进程运行,因此您可以稍后使用该函数或通过处理 SIGCHLD
来检查它们的状态。
关于子进程在运行 fork() 时接管父进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58386362/
提前感谢大家- 我还没有找到一种简单的方法来做到这一点,但是,是否可以接管写入 JspWriter 的数据(标题和所有内容)的处理?有点像动态管理缓冲区然后控制输出到 JspWriter 的联络员?由
我想接管音量调高和音量调低的过度输入。目前我的代码是: @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Lo
我正在编写这个程序,我正在尝试在用户登录后显示辅助页面。我的第一个屏幕由 LoginInterfaceController 处理,如下所示。 @Controller public class Logi
我一直在为项目的一些新基础设施设置 CloudFormation 模板,并且已将其添加到 Route 53 托管区域。 现在理想情况下,我想创建一个包含我们所有托管区域和基本配置的“核心域”堆栈。问题
我同时使用 Facebook SDK 和 jQuery...出于某种原因,加载了 Facebook SDK,$不再作为 jQuery 对象工作。我所有的 jQuery 调用仍然有效,但我必须使用 jQ
我尝试使用 stopPropagation() 但没有运气。 我有这样的结构: 我有一个绑定(bind)到 Logo 的点击,它在其中附加了 img 元素,结果如下: 我想将点
(我在 Debian 8 上使用 Bash 4.4.12。问题也在 bash mailing list 中提出。) 请参阅以下步骤重现该问题。 来自 tty #1 (pts/2): [STEP 101
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在开发的 Android TV (ATV) 应用具有语音控制功能。基本上,当用户按下 Remote 上的麦克风按钮时,按键事件(由 KeyEvent.KEYCODE_SEARCH 标识)由应用处理
我有使用 iframe 显示外部内容的 Cordova 应用程序(在这种情况下是 youtube 嵌入式视频) 当单击 iframe 内的链接(如 youtube Logo )时,整个 webview
我正在尝试使用远程桌面协议(protocol)在 Ubuntu 15.04 上使用 Remmina 连接到 Windows 7 PC。哪个工作正常。 我遇到的唯一问题是我用作服务器状态监视器的 Win
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我关注了tutorial部署Redis主从部署。Slave和Master都有自己的服务。我有 Spring boot 应用程序,其配置中有主主机以保存/读取其中的数据。因此,当我终止 redis-ma
考虑这段代码(完全基于飞碟的“入门”代码,保留他们的权利): package flyingsaucerpdf; import java.io.File; import java.io.FileOutp
我是一名优秀的程序员,十分优秀!