gpt4 book ai didi

linux - 使用管道与子进程进行通信

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:54 26 4
gpt4 key购买 nike

我试图通过管道与分叉流程进行通信。我已经用管道子例程为管道创建了文件处理程序,为子进程创建了关闭的读卡器,为父进程创建了写入器。
但我还是不能从管道里得到任何信息。
我的错在哪里?

#!/usr/bin/perl
# ioforkserv.pl
use warnings;
use strict;
use IO::Socket;
use IPC::Shareable;

my $buffer;
my $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 };
$buffer = "Buffer init";
# Turn on autoflushing
$|=1;
# Open pipe
my ($reader, $writer);
pipe($reader,$writer);

my $port = 4444;
my $server = IO::Socket->new(
Domain => PF_INET,
Proto => 'tcp',
LocalPort => $port,
Listen => SOMAXCONN,
Reuse => 1,
);
die "Bind failed: $!n" unless $server;
# tell OS to clean up dead children
$SIG{CHLD} = 'IGNORE';
print "Multiplex server running on port $port...\n";
print "Initial buffer value is ".$buffer."\n";

while (my $connection = $server->accept) {
my $name = $connection->peerhost;
my $port = $connection->peerport;
if (my $pid = fork) {
close $connection;
print "Forked child $pid for new client $name:$port\n";
next; # on to the next connection
} else {
# child process - handle connection
close($reader);
$buffer = "Child # $$\n";
print $connection "You're connected to the server!\n";
while (<$connection>) {
print $writer "Client $name:$port says: $_";
print $connection "Message received OK\n";
print "Buffer = $buffer\n";
}
print "Client $name:$port disconnected\n";
$connection->shutdown(SHUT_RDWR);
exit;
}
close($writer);
while (my $data = <$reader>) {
print "In pipe : ".$data."\n";
}
}

编辑
我已经编辑了代码,但它只在连接打印后才在管道中显示数据,有什么问题吗?
#!/usr/bin/perl
# ioforkserv.pl
use warnings;
use strict;
use IO::Socket;
use IPC::Shareable;
my $buffer ;
my $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 };
$buffer = "Buffer init";
# Turn on autoflushing
$|=1;
# Open pipe
my ($reader, $writer);
pipe($reader,$writer);

my $port = 4444;
my $server = IO::Socket->new(
Domain => PF_INET,
Proto => 'tcp',
LocalPort => $port,
Listen => SOMAXCONN,
Reuse => 1,
);
die "Bind failed: $!n" unless $server;
# tell OS to clean up dead children
$SIG{CHLD} = 'IGNORE';
print "Multiplex server pid $$ running on port $port...\n";
print "Initial buffer value is ".$buffer."\n";
#Creating new process for taking data from pipe
my $chpid = fork();
if( $chpid == 0 ){
close($writer);
print "Before Reading from pipe $$\n";
while (my $data = <$reader>) {
print "In pipe $$: ".$data."\n";
}
print "After reading from pipe $$ \n";
exit 0;
} else {
print "This is parent process and child ID is $$ and ch $chpid\n";
while (my $connection = $server->accept) {
my $name = $connection->peerhost;
my $port = $connection->peerport;
if (my $pid = fork) {
close $connection;
print "Forked child $pid for new client $name:$port\n";
next; # on to the next connection
} else {
# child process - handle connection
close($reader);
$buffer = "Child # $$\n";
print $connection "You're connected to the server!\n";
while (<$connection>) {
print $writer "Client $name:$port says: $_ \n";
print "Client $name:$port says: $_";
print $connection "Message received OK\n";
print "Buffer = $buffer\n";
}
print "Client $name:$port disconnected\n";
$connection->shutdown(SHUT_RDWR);
exit;
}

}
}

最佳答案

创建管道后尝试添加以下行:

pipe($reader,$writer);
$writer->autoflush(1);

希望这对你有帮助。默认情况下,“自动刷新”处于关闭状态,因此可能是您的问题所在。

关于linux - 使用管道与子进程进行通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246033/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com