Chinaunix
标题:
关于仙子的“简简单单管道”疑问
[打印本页]
作者:
xiaobing927
时间:
2010-08-12 20:23
标题:
关于仙子的“简简单单管道”疑问
原文“open过程中实际会发生一个fork过程,这个fork对父进程返回子进程的pid,通常是个正整数;对子进程返回0”
这句话不太懂,open是open,fork是fork,翻了好几本书,没有说open操作实际有fork过程,关于这点,大牛们能否深入的讲解下?不胜感激呀
作者:
兰花仙子
时间:
2010-08-13 08:40
fork是指系统调用。
perl -le 'open P,"tail -f /etc/passwd|";while(<P>){print}'
pstree看一下:
+-bash---perl---tail
tail是perl打开的子进程。
作者:
xiaobing927
时间:
2010-08-13 15:02
是不是说,使用open的时候实际会发生fork系统调用?学习了
作者:
兰花仙子
时间:
2010-08-13 16:07
是不是说,使用open的时候实际会发生fork系统调用?学习了
xiaobing927 发表于 2010-08-13 15:02
这个,看怎么open哦。。Perl的open太复杂了。
perldoc -f open
作者:
socyno
时间:
2010-08-15 08:30
我怎么觉得不太像是真正的fork吧,起码我知道在 windows 下这个fork就是perl 自身模拟出来的一个东东,资源占用相当大,而且不太稳定的说好像有些。 只是个人理解,还请高手指教。
作者:
黑色阳光_cu
时间:
2010-08-15 09:54
本帖最后由 黑色阳光_cu 于 2010-08-15 10:24 编辑
#!/bin/env perl
use strict;
use warnings;
open(PIPE, "-|", "ls");
print <PIPE>;
复制代码
#!/bin/env perl
use strict;
use warnings;
substr($^O, 0, 2) ne "MS" or die "Cannot running undef Windows!\n";
defined (my $pid = open(PIPE, "-|")) or die "Fork fail!\n";
if ($pid != 0)
{
print <PIPE>;
waitpid($pid, 0);
}
else
{
exec("ls");
exit(1);
}
复制代码
#!/bin/env perl
use strict;
use warnings;
substr($^O, 0, 2) ne "MS" or die "Cannot running undef Windows!\n";
pipe(PIPE, W);
defined (my $pid = fork()) or die "Fork fail!\n";
if ($pid != 0)
{
close W;
print <PIPE>;
waitpid($pid, 0);
}
else
{
close PIPE;
open(STDERR, ">&", *STDOUT);
open(STDOUT, ">&", *W);
close W;
exec("ls");
exit(1);
}
复制代码
作者:
黑色阳光_cu
时间:
2010-08-15 10:16
上面的代码演示,open一个管道时 for read 时,大概产生了什么动作。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2