I have to do some lookups on some servers (snmp and ldap) based on STDIN input i get from a user. problem is the lookups can possibly take some time and it slows the whole works down.. So here are my thoughts; fork off a child that does the lookups periodically and writes the retuned info back to a file. the parent can then (when it needs to based on STDIN) just rip thru the local file real fast and get what it needs.
The problem Im seeing is that the parent and child crash into one another on getting to the file for reads/writes and the whole thing pukes on itself.
Any insights on how to do this... or how to do it better would be greatly apprecaited
#!/usr/bin/perl
use Fcntl qw(:DEFAULT :flock);
$|=1;
if (!defined($child_pid = fork())) {
die "cannot fork: $!";
} elsif ($child_pid) {
# I'm the parent
$file="file.txt";
while ( 1 == 1 ) {
$input = <STDIN>;
chomp($input);
# repeat what was entered st stdin.. then open file.txt, lock
+it so others
# leave it alone, read it and close it, so others can use it a
+gain
print "$input\n";
open FH, "<$file"or die "Cannot open $file for read :$!";
flock(FH, LOCK_SH) or die "can't lock filename: $!";
while (<FH>) {
print " $_";
}
close FH;
}
} else {
# I'm the child
$file="file.txt";
while ( 1 == 1 ) {
#get some info from a process that can be slow, see if file.tx
+t is locked
#if not, then open/create the file.txt and write info, and clo
+se file.
#sleep and repeat
@info = `snmpwalk -v 1 -c public $novell_srv .1.3.6.1.4.1.23.2
+.28.3.8.1.2`;
sysopen(FH, $file, O_WRONLY | O_CREAT) or die "can't open $fil
+e: $!";
flock(FH, LOCK_EX) or die "can't lock $file: $!";
truncate(FH, 0) or die "can't truncate $file: $!";
print FH "@info";
truncate(FH, tell(FH)) or die "can't truncate $file: $!";
close(FH) or die "can't close $file: $!";
sleep 5;
};
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.