我希望将子进程写入父进程的@数组。我读过有关管道的文章,但我对如何实际实现它感到非常困惑:
use Parallel::ForkManager;
my @array;
my $pm=new Parallel::ForkManager(3);
for((1..5)){
$pm->start and next;
print "child: ".$_."\n";
push(@array,$_); # what do I do here to put it into the parent's @array????
在我的C++程序中,我需要执行bash脚本。然后,我需要返回在我的c++程序中运行脚本所获得的结果。我有两种可能性:
1. use system(script.sh). In script.sh I redirect the output in a file which is processd after I return to the c++ program.
2. use popen
考虑到script.sh返回的输出可能很大(100 M),我感兴趣的是这些方法中的哪一种。谢谢。
我对管道的了解是,它是用于单向通信的,它有助于两个相关过程之间的通信。我从一本书中得到了下面的管道编程代码示例。我正在尝试使用printf理解代码,并在代码的每一行之后打印出所有的点。但是我不明白程序是如何在每一行之后运行的。我的代码如下:
管:
//using PIPE to communicate with a child process
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
/* Write COUNT copies of MESSAGE to filename, pa
我在c中有一个基本管道,我已经向子进程发送了一个整数,子进程用1增加了这个数字,并将返回到父进程。我的问题是:如果在写函数之后关闭写文件描述符,会发生什么?程序将显示1 (正确的输出是2)
int main(){
int p[2];
pipe(p);
int n=1;
write(p[1], &n, sizeof(int));
close(p[1]); // don't work => the output is 1
if(fork() == 0) {
read(p[0], &n, s