|
|
|
|
 请教:进程间通讯问题 - RedHat_Yxz [ 2004-12-16 10:40 | 1,025 byte(s)]
 Re: 请教:进程间通讯问题 - wandys [ 2004-12-16 14:36 | 52 byte(s)]
 Re: 请教:进程间通讯问题 - RedHat_Yxz [ 2004-12-16 15:14 | 65 byte(s)]
 pipe与fork合用的实例,给父进程和子进程提供通信的方式。 - RedHat_Yxz [ 2004-12-16 10:52 | 1,017 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
代码如下:
#!/usr/bin/perl
$w=0;$q=0;$e=0;
if($child = fork()){
#parent process
while($q<100){
print $q."parent process
";
$q++;
}
}
else{
#child process
if($retval = fork()){
#child's parent process
while($w<10){
print $w."child's parent process
";
$w++;
}
}
else{
#child's child process
while($e<20){
print $e."child's child process
";
$e++;
}
}
}
我想要实现:
当child's parent process循环结束时,同时也将parent process、child's child process 两个进程停止,请教如何实现?
谢谢!
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
函数名 pipe
调用语法 pipe (infile, outfile);
解说 与fork合用,给父进程和子进程提供通信的方式。送到outfile文件变量的信息可以通过infile文件变量读取。步骤:
1、调用pipe
2、用fork将程序分成父进程和子进程
3、一个进程关掉infile,另一个关掉outfile
例子 pipe (INPUT, OUTPUT);
$retval = fork();
if ($retval != 0) {
# this is the parent process
close (INPUT);
print ("Enter a line of input:
");
$line = <STDIN>;
print OUTPUT ($line);
} else {
# this is the child process
close (OUTPUT);
$line = <INPUT>;
print ($line);
exit (0);
}
结果输出 $
program
Enter a line of input:
Here is a test line
Here is a test line
$
运行结果好像有问题,我也不大理解,所以也无法引用。
如果有人知道,请指点指点。
|
|
|
[Original]
[Print]
[Top]
|
|
|