在我的一个项目中,我们不得不用C语言编写一个多线程程序,一切看起来都很顺利。教授给了我们一个perl脚本,在我们完成后用C代码运行。然而,在我运行脚本的控制台上,每当我的程序应该输出(我想)时,我就会到处得到GLOB(0x1a6a8830)。这是输出。
全局(0x1a6a8830)
变速器995-589398 987 154107 472 435291
全局(0x1a6a8830)
变速箱397-557927 559 798860 989-240933
全局(0x1a6a8830)
传输289-368267 408 139828 419 842413 277-598329 613 372608 285 508034 256-896287
TRANS(然后是数字)是一个输入,由我的C程序捕获。有人知道这个球是什么吗????
use strict;
use warnings;
use integer;
if (@ARGV < 1) {
print "Use: $0 <program> [<nthreads> [<naccounts> [<seed>]]]\n";
exit 1;
}
my($prgm, $thread, $accts, $seed) = @ARGV;
$thread ||= 10;
$accts ||= 1000;
$seed ||= 0;
srand($seed);
my $outfile = 'testout-'.$$;
my($c_in, $f_in, $c_out, $f_out);
pipe $f_in, $c_in;
pipe $c_out, $f_out;
my $pid = fork;
`if ($pid == 0) {
open STDIN, '<&', $f_in;
open STDOUT, '>&', $f_out;
close $c_in;
close $c_out;
close $f_in;
close $f_out;
exec $prgm, $thread, $accts, $outfile;
exit 1;
} else {
close $f_in;
close $f_out;
}
# make the pipes autoflush
select $c_in; $| = 1;
select $c_out; $| = 1;
select STDOUT;
sub sr {
my $line = shift;
print "\e[33m$line\e[m\n";
print $c_in "$line\n";
my $resp = $c_out;
chomp $resp;
print "\e[32m$resp\e[m\n";
return $resp;
}
my $init_skip = 0;
for my $id (0..($accts/10 - 1)) {
my $line = 'TRANS';
$line .= ' '.($id*10 + $_).' 10000000' for 1..10;
sr $line;
$init_skip++;
}
print "\e[35mWaiting for initial deposits to complete...\e[m\n";
sleep 60;
for my $line (1..1000) {
my $line = "TRANS";
my $bal = 0;
my $len = int rand 8;
my %dups;
for (0..$len) {
my $acct = 1 + int rand $accts;
my $amt = 1_000_000 - int rand 2_000_000;
next if $dups{$acct}++;
$bal += -$amt;
$line .= " $acct $amt";
}
my $acct = 1 + int rand $accts;
next if $dups{$acct};
$line .= " $acct $bal";
sr $line;
}
print "\e[35mWaiting for transactions to complete...\e[m\n";
sleep 60;
for my $id (1..$accts) {
sr "CHECK $id";
}
print "\e[33mEND\e[m\n";
print $c_in "END\n";
print "\e[35mWaiting for program to exit...\e[m\n";
wait;
no integer;
print "\e[35mChecking output file:\e[m\n";
open F, $outfile;
my $trcount = 0;
my $trtime = 0;
my $balcount = 0;
my $baltime = 0;
my $total = 0;
while (<F>) {
if ($init_skip-- > 0) {
die "Bad (initial deposit) output line: $_" unless /^\d+ OK TIME/;
} elsif (/ (OK|ISF \d+) TIME (\d+)(\.\d+) (\d+)(\.\d+)/) {
$trtime += ($4 - $2) + ($5 - $3);
$trcount++;
} elsif (/^\d+ BAL (\d+) TIME (\d+)(\.\d+) (\d+)(\.\d+)/) {
$total += $1;
$baltime += ($4 - $2) + ($5 - $3);
$balcount++;
} else {
die "Bad output line: $_";
}
}
close F;
if ($balcount != $accts) {
print "ERR: There were $accts accounts but only $balcount balance queries\n";
}
print "Final balance of all $accts accounts is $total\n";
my $travg = $trtime / $trcount;
printf 'The %d transactions took %.2fs total, or an average of %.4fs each'."\n",
$trcount, $trtime, $travg;
my $balavg = $baltime / $balcount;
printf 'The %d balance checks took %.2fs total, or an average of %.4fs each'."\n",
$balcount, $baltime, $balavg;
这是整个perl脚本
我的程序充当多线程银行服务器,接受TRANS和CHECK命令
最佳答案
乍一看,我看到贴出的剧本有两处错别字。首先是第25行的`
。第二条线
my $resp = $c_out;
这是将文件句柄分配给
$resp
而不是从中读取。可能是这样的:my $resp = <$c_out>;
关于c - 在C程序上运行Perl脚本时,我得到GLOB(0x1a6a8830)作为输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5271202/