- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我不确定为什么 waitpid() 会永久挂起。我假设 forks 应该在 execvp() 完成后终止并返回父进程。这没有发生。如果我注释掉 waitpid(),初始输出是正确的,但程序开始出现意外行为。
编辑
- execFirst() 处理列表中第一个命令的执行
- execMid() 处理列表中不是第一个或最后一个命令的任何命令的执行
- execLast() 处理列表中最后一个命令的执行
// Closes pipe
void closePipe(int *pPipe){
close(pPipe[0]);
close(pPipe[1]);
return;
}
// Opens the read end of pPipe and closes the write end
// @Private
void readFromPipe(int *pPipe){
dup2(pPipe[0],0);
close(pPipe[1]);
close(pPipe[0]);
return;
}
// Opens the write end of pPipe and closes the read end
// @Private
void writeToPipe(int *pPipe){
dup2(pPipe[1],1);
close(pPipe[0]);
close(pPipe[1]);
return;
}
// @Private
void execLast(sSettings *pSettings_, sCommand *pCommand, int iPipe[]){
readFromPipe(iPipe);
_execvp(pSettings_, pCommand);
}
// @Private
void execMid(sSettings *pSettings_, sCommand *pCommand, int iPipe1[], int iPipe2[]){
readFromPipe(iPipe1);
writeToPipe(iPipe2);
_execvp(pSettings_, pCommand);
}
// @Private
void execFirst(sSettings *pSettings_, sCommand *pCommand, int iPipe[]){
writeToPipe(iPipe);
_execvp(pSettings_, pCommand);
}
void execIndefDepthPipe(sSettings *pSettings_, sCommandList *pCommandList_){
int iPipe1[2];
int iPipe2[2];
int iStatus, iProcessId, iNumCommands = pCommandList_->iSize;
_pipe(pSettings_, iPipe1);
_pipe(pSettings_, iPipe2);
sCommand *pCommand;
do{
pCommand = popHeadNode(pCommandList_);
iProcessId = _fork(pSettings_);
if(iProcessId){ // If Parent
// continue; // Do nothing
waitpid(iProcessId, &iStatus, WUNTRACED); // It waits forever. I don't know why.
}
else { // Execute the commands as a child
if(pCommand != NULL) {
if(pCommandList_->iSize == (iNumCommands-1)) { // Exec First
//closePipe(iPipe2);
execFirst(pSettings_, pCommand, iPipe1);
}
else if(pCommandList_->iSize == 0) { // Exec Last
closePipe(iPipe1);
execLast(pSettings_, pCommand, iPipe2);
}
else { // Exec mid
execMid(pSettings_, pCommand, iPipe1, iPipe2);
}
}
_exit(0);
}
} while(pCommand != NULL);
return;
}
最佳答案
It is my assumption that the forks should terminate and return back to the parent process after the execvp() is done
如果你的意思和我想的一样,那你就错了。 exec
系列函数不会破坏子进程。他们将在子进程中执行的程序换成另一个,但进程保持不变。特别是,在 exec
调用的程序终止之前,该进程的 waitpid
不会返回。
如果您只是创建一个无需父进程的进一步帮助即可运行完成的子进程,这会很好。但是您正在尝试建立管道。您不能期望管道中的任何进程在它们全部完成之前终止。您尤其不能期望管道中的第一个进程在第二个进程创建之前终止。但这正是您的代码所做的:它在创建第二个进程之前等待第一个进程终止——与此同时,第一个进程已填满其管道并正在等待第二个进程读取它之前的一些数据可以继续。死锁。
您需要做的是在等待任何进程之前创建所有进程。您已经获得了所需的两个循环之一。不是在您调用的地方调用 waitpid
,而是将进程 ID 保存在数据结构中并转到下一次迭代。然后,在使用完整个“命令列表”后的第二个 循环中,在不指定任何特定进程 ID 的循环中调用 waitpid
等待;当每个进程终止时,从保存它们的数据结构中删除它的 ID;当该数据结构为空时,您就完成了。
此外,您需要检查每个系统调用 是否有错误,close
除外。 (这是 POSIX 中的一个错误,允许 close
失败;如果传递的文件描述符编号最初未打开,Unix 的明智实现只会返回错误,我还没有遇到一个需要关心那个的程序。如果你需要确保数据已经写入磁盘,你需要调用fsync
before close
; 但在管道上执行 fsync
是没有意义的。)
当我在这里时,对良好编程风格的一些观察:(1) 如果您停止使用匈牙利语前缀,您的代码将更易于阅读。 (1a) 标识符末尾无意义的下划线按照约定保留给宏内部定义的变量,C 没有宏卫生。 (1b) 以下划线开头的函数名由 C 标准保留,供 C 库内部使用;您围绕 fork
的包装器应命名为 forkWithSettings
- 描述性更强的东西会更好,但不知道这个设置对象中的所有内容我无法提出任何建议。 (2) 在宏伟的计划中放置花括号并不重要,但看在 Ghu 的份上,选择一种样式并在整个文件中始终如一地使用它。
关于c - C 中的无限管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30146801/
我正在使用 Assets 管道来管理我的 Grails 3.0 应用程序的前端资源。但是,似乎没有创建 CoffeeScript 文件的源映射。有什么办法可以启用它吗? 我的 build.gradle
我有一个我想要的管道: 提供一些资源, 运行一些测试, 拆资源。 我希望第 3 步中的拆卸任务运行 不管 测试是否通过或失败,在第 2 步。据我所知 runAfter如果前一个任务成功,则只运行一个任
如果我运行以下命令: Measure-Command -Expression {gci -Path C:\ -Recurse -ea SilentlyContinue | where Extensio
我知道管道是一个特殊字符,我需要使用: Scanner input = new Scanner(System.in); String line = input.next
我再次遇到同样的问题,我有我的默认处理方式,但它一直困扰着我。 有没有更好的办法? 所以基本上我有一个运行的管道,在管道内做一些事情,并想从管道内返回一个键/值对。 我希望整个管道返回一个类型为 ps
我有三个环境:dev、hml 和 qa。 在我的管道中,根据分支,阶段有一个条件来检查它是否会运行: - stage: Project_Deploy_DEV condition: eq(varia
我有 Jenkins Jenkins ver. 2.82 正在运行并想在创建新作业时使用 Pipeline 功能。但我没有看到这个列为选项。我只能在自由式项目、maven 项目、外部项目和多配置之间进
在对上一个问题 (haskell-data-hashset-from-unordered-container-performance-for-large-sets) 进行一些观察时,我偶然发现了一个奇
我正在寻找有关如何使用管道将标准输出作为其他命令的参数传递的见解。 例如,考虑这种情况: ls | grep Hello grep 的结构遵循以下模式:grep SearchTerm PathOfFi
有没有办法不因声明性管道步骤而失败,而是显示警告?目前我正在通过添加 || exit 0 来规避它到 sh 命令行的末尾,所以它总是可以正常退出。 当前示例: sh 'vendor/bin/phpcs
我们正在从旧的 Jenkins 设置迁移到所有计划都是声明性 jenkinsfile 管道的新服务器……但是,通过使用管道,我们无法再手动清除工作区。我如何设置 Jenkins 以允许 手动点播清理工
我在 Python 中阅读了有关 Pipelines 和 GridSearchCV 的以下示例: http://www.davidsbatista.net/blog/2017/04/01/docume
我有一个这样的管道脚本: node('linux'){ stage('Setup'){ echo "Build Stage" } stage('Build'){ echo
我正在使用 bitbucket 管道进行培训 这是我的 bitbucket-pipelines.yml: image: php:7.2.9 pipelines: default:
我正在编写一个程序,其中输入文件被拆分为多个文件(Shamir 的 secret 共享方案)。 这是我想象的管道: 来源:使用 Conduit.Binary.sourceFile 从输入中读取 导管:
我创建了一个管道,它有一个应该只在开发分支上执行的阶段。该阶段还需要用户输入。即使我在不同的分支上,为什么它会卡在这些步骤的用户输入上?当我提供输入时,它们会被正确跳过。 stage('Deplo
我正在尝试学习管道功能(%>%)。 当试图从这行代码转换到另一行时,它不起作用。 ---- R代码--原版----- set.seed(1014) replicate(6,sample(1:8))
在 Jenkins Pipeline 中,如何将工件从以前的构建复制到当前构建? 即使之前的构建失败,我也想这样做。 最佳答案 Stuart Rowe 还在 Pipeline Authoring Si
我正在尝试使用 执行已定义的作业构建 使用 Jenkins 管道的方法。 这是一个简单的例子: build('jenkins-test-project-build', param1 : 'some-
当我使用 where 过滤器通过管道命令排除对象时,它没有给我正确的输出。 PS C:\Users\Administrator> $proall = Get-ADComputer -filter *
我是一名优秀的程序员,十分优秀!