- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 C 还很陌生,不确定如何继续。
使用此代码,我尝试创建多个子进程,这些子进程会将其标准输出发送到其父级标准输入,并可使用数组中指针位置的 fdprintf 写入其标准输入。
当使用读取标准输入并打印到其标准输出(应该通过管道返回)的基本程序执行时,代码似乎不起作用。 (在主代码的不同部分中,我 fprintf 到管道开始的位置,然后读取 stdin 等待应写回的内容)。
int plumber(int *pipes[], int numChildren, char* command[]) {
int i;
char id;
int nullSpace = open("/dev/null", O_WRONLY);
for(i = 0; i < numChildren; ++i) {
id = 'A' + i;
pipe(pipes[2 * i]);
pipe(pipes[2 * i + 1]);
switch(fork()) {
case (-1):
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
break;
case 0:
//child
//close child's write, dupe its stdin to read
//close childs old read
close(pipes[2 * i][1]);
if(dup2(pipes[2 * i][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i][0]);
//close child's read, dupe its stdout to write
//close childs old write
close(pipes[2 * i + 1][0]);
if(dup2(pipes[2 * i + 1][1], 1) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][1]);
close(1);
//child stderr to nullspace
if(dup2(nullSpace, 2) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(2);
execlp(command[i], "childprocess", numChildren, id, NULL);
break;
default:
//parent
//close read pipe from writing pipe
close(pipes[2 * i][0]);
//close write pipes and dupe stdin to read
//close parents old read
close(pipes[2 * i + 1][1]);
if(dup2(pipes[2 * i + 1][0], 0) == -1) {
fprintf(stderr, "Unable to start subprocess\n");
exit(4);
}
close(pipes[2 * i + 1][0]);
}
}
close(nullSpace);
return 0;
}
该命令只是运行子进程,它还获取子进程的数量和从 A 到 D 的 id。 *pipes[] 是 numChildren*2 x 2 (因此沿着其子 1 读取管道,child1 写入管道,child2 读取,child2 写入等。请提前提供帮助并致谢。
最佳答案
父级只能有一个 stdin
。每次家长这样做时:
dup2(pipes[2 * i + 1][0], 0)
它正在关闭之前的 stdin
并将其替换为管道的读取端。这意味着除了最后一个 child 之外的所有 child 都将拥有stdout
。具有封闭的读取端,这应该会导致它们收到 SIGPIPE
(如果 EPIPE
被忽略,则 SIGPIPE
错误)如果它们尝试生成任何输出。此外,父级现在已经丢失了原来的 stdin
,这可能重要也可能不重要。
您还应该检查execlp
没有返回错误。如果确实如此,您将得到非常奇怪的行为,因为失败的子级将开始与父级同时生成所有剩余的子级。
如果您确实希望所有子级的输出都出现在单个 stdin
上,那么它们必须都使用相同的管道。然而,这将导致他们的输出随机混合,因此 parent 不可能知道谁发送了什么。如果父级仅将每个管道的读取端保留在其原始描述符中而不是尝试dup2
,那就更好了。将它们设置为 0。这样,父级将保留其原始 stdin
,它可以使用 select
(或 poll
,或...)来确定何时从哪个子项获取输入。
连接 child 的stdin
通常是一个坏主意。和stdout
到同一个进程(父进程),因为它很容易导致死锁。该管道只能缓冲有限数量的数据,并且在满时会对写入器产生反压(即阻塞)。如果父进程和子进程都向stdout
写入大量数据s,它们都会受到背压,这将阻止它们读取输入并排空管道。您必须确保子级在生成自己的任何输出之前消耗掉父级的所有输出,或者找到一种方法来保证您始终可以从 stdin
读取内容。即使stdout
被阻止。后者可以通过让单独的线程处理 stdin
来完成。和stdout
,或者通过启用非阻塞 I/O 并使用 select
等来确定何时可以写入管道。
关于c - 使用 exec 程序从父级读取和写入多个子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26121869/
我有一个层次结构中的项目列表,我正在尝试将此列表解析为实际的对象层次结构。我正在使用modified pre-order tree traversal存储/迭代此列表,所以我拥有的是树的子集,包括所有
给定这个类: public class Parent { public Child[] Children {get;set;} } 还有这个数组: Parent[] parents; 如何使用 L
我面临着一些难以完成这个看似简单的任务。 我在一个嵌套块 (Suggest.phtml) 中,我想从父块 (result.phtml) 获取一个值(搜索结果的数量,以及集合计数) 我错了还是这里没有简
我在访问子模型重复器字段以在没有用户操作的情况下加载时遇到问题,我在父模型 Controller 中使用 RelationController这就是我在父 Controller 中所做的 class
我有一个 datetime 指令,其范围内包含两个属性:date 和 time。如何从父 Controller 访问这些属性?我尝试这样做 Start: End: You
我的父类是: public Class Parent { protected void foo() { bar(); } protected void
所以我知道如何中断列表的继承 (SPList.BreakRoleInheritance(true)),但是如何以编程方式从其父级重新继承? 谢谢 最佳答案 MSDN link . SPList.Res
最近我们将maven版本更改为3.5.4根据https://issues.apache.org/jira/browse/MNG-5940 Maven Super POM 中的 maven-source
我需要在单击页面背景(#page)时触发一个事件(例如隐藏 float 购物车),但在单击内部内容时不会发生此事件。所以我需要这个事件发生在空间:页面减去内容。我该如何实现它?谢谢 如果我有这个结构:
我在另一个 ajax 函数中有一个 ajax 函数 $.ajax({ url: '../...', type: 'POST',
我创建了两个类 - Building 和 Warehouse。 Warehouse 扩展 Building。我创建了 Building map (building_map),其中放置了 Buildin
我有一个获取和处理数据的 Activity ,我想启动一个新 Activity ,该 Activity 能够从父 Activity 访问一些变量(复杂数据结构)。 我不能在这里使用序列化,有没有一种简
在 HTML 中,我有两个函数,一个是仅带有警报的 abc,另一个是使用 window.open() 创建一个新窗口,并填充预定义的 HTML 字符串。我想从父 HTML 调用该函数。我尝试过调用pa
我一直在研究这个脚本并且一切正常,除了 $(this).parents(".clonedInput").clone()。按钮克隆引用放置在 .clonedInput div 中。 如果能帮助我从 cl
我有以下代码: 主要 HTML 文档 $(function(){ $("#i").load(function(){ var $iframeConten
html 的一部分结构如下。我想从中获得工作“标题”和“时间”。我可以单独获取它们,例如: from bs4 import BeautifulSoup pages = ' \
我正在尝试从父 div 中删除底部边框。我已经编写了以下 HTML 代码,但我认为我的方法不正确。看到这张图片 这是我的 HTML 代码 j
通常,我可以从 Activity 调用扩展 DialogFragment 并实现 DialogInterface.OnClickListener() 的对象。 然后,如果我想从那个 Dialog 调用
我花了好几个小时搜索如何从嵌套 Controller 更新服务值。 我的子 Controller 需要更新服务中的值。该值需要显示在父 Controller 中。 我做了一个jsfiddle,让它更清
我有一个包含一些数据的表格,表格中的每个元素都是一个 React 类组件。它看起来像这样: 我想要的只是有一个用于“选中所有”功能的复选框(左上角的复选框)。问题是我不知道如何解决这个问题,因为 pr
我是一名优秀的程序员,十分优秀!