
Jukari
Novice
Nov 2, 2011, 5:18 PM
Post #2 of 6
(670 views)
|
|
Re: [osamaamin] problem in forking using perl/tk
[In reply to]
|
Can't Post
|
|
I really don't have time to explain fork... but I know it can be a pain to work with. Especially when referencing child processes. Here's a copy of my script for pinging it creates fork process to ping... Been a while since I looked at the code. Hope it helps. #!/usr/bin/perl use Net::Ping; use Socket; use Term::ANSIColor; use Win32::Console::ANSI; use Cwd; $cwd = getcwd; $execute = "on"; #################################################################### #################################################################### unlink("Queue.txt"); open(QUEUE,">> Queue.txt") || die("Cannot Open File"); close(QUEUE); sub execute_queue { $ip = $_[0]; if($execute eq "on") { open(QUEUE,">> Queue.txt") || die("Cannot Open File"); print QUEUE "$ip\n"; close(QUEUE); } } #################################################################### #################################################################### sub IP_ENTRY { my $ip = $_[0]; my $iaddr = inet_aton("$ip"); my $host = gethostbyaddr($iaddr, AF_INET); my @hostsplit = split(/\./,$host); my $name = $hostsplit[0]; if($name eq "") { $name = "Not Available"; } else { $name =~ tr/a-z/A-Z/; } #################################### $name_length = length($name); $name_size = (20 - $name_length); undef $name_length; while($name_size > $count) { $count++; $name_space = $name_space." "; } undef $ip_size; undef $count; $ip_length = length($ip); $ip_size = (20 - $ip_length); undef $ip_length; while($ip_size > $count) { $count++; $ip_space = $ip_space." "; } undef $ip_size; undef $count; #################################### undef(@hostsplit); my $online; my $p = Net::Ping->new( "icmp", 1, 64 ); if ( $p->ping($ip) ) { $online = "Online"; print colored ['bold blue']," |"; print colored ['bold green']," $name$name_space$ip$ip_space$online "; print colored ['bold blue'],"|\n"; execute_queue($ip); } else { $online = "Offline"; print colored ['bold blue']," |"; print colored ['bold red']," $name$name_space$ip$ip_space$online "; print colored ['bold blue'],"|\n"; } open(DUMP,">> $dump") || die("Cannot Open File"); print DUMP "$counter,$name,$ip,$online\n"; close(DUMP); exit; } #################################################################### #################################################################### sub NAME_ENTRY { my $name = $_[0]; my $ip; my $packed_ip = (gethostbyname($name)); if(defined $packed_ip eq "") { $ip = "Not Available"; } else { $ip = inet_ntoa($packed_ip); } ######################################### $name =~ tr/a-z/A-Z/; $name_length = length($name); $name_size = (20 - $name_length); while($name_size > $count) { $count++; $name_space = $name_space." "; } undef $count; $ip_length = length($ip); $ip_size = (20 - $ip_length); while($ip_size > $count) { $count++; $ip_space = $ip_space." "; } undef $count; ######################################## my $online; my $p = Net::Ping->new( "icmp", 1, 64 ); if ( $p->ping($ip) ) { $online = "Online"; print colored ['bold blue']," |"; print colored ['bold green']," $name$name_space$ip$ip_space$online "; print colored ['bold blue'],"|\n"; execute_queue($ip); } else { $online = "Offline"; print colored ['bold blue']," |"; print colored ['bold red']," $name$name_space$ip$ip_space$online "; print colored ['bold blue'],"|\n"; } open(DUMP,">> $dump") || die("Cannot Open File"); print DUMP "$counter,$name,$ip,$online\n"; close(DUMP); exit; } #################################################################### #################################################################### #################################################################### #################################################################### system(($^O eq 'MSWin32') ? 'cls' : 'clear'); print colored ['bold cyan'], ,"Computer Pinger \nby: Anthony Brechtel\n\n\n"; if(-e "computers.csv") { $computer_file = 'computers.csv'; } else { if(-e "computers.txt") { $computer_file = 'computers.txt'; } else { print "No Read File\n"; exit; } } $dump = "Done.csv"; unlink($dump); print colored ['bold blue']," ===================================================== \n"; open (COMP, "computers.csv") || die("Cannot Open File"); while ($line = <COMP>) { $counter++; chomp($line); if($line ne "") { $process_counter++; if($process_counter eq 60) { $process_counter = 0; foreach $process(@pids) { waitpid $process, 0; } undef(@pids); } @line_data = split(/,/, $line); my $pid = fork(); if (not defined $pid) { #warn "$counter = No resources\n"; foreach $process(@pids) { waitpid $process, 0; } undef(@pids); my $pid = fork(); if (not defined $pid) { warn "$counter = Still no resources\n"; } elsif ($pid == 0) { if($line_data[0] =~/\./) { IP_ENTRY($line_data[0]) } else { NAME_ENTRY($line_data[0]) } } push(@pids, $pid); } elsif ($pid == 0) { if($line_data[0] =~/\./) { IP_ENTRY($line_data[0]) } else { NAME_ENTRY($line_data[0]) } } push(@pids, $pid); #print "Parent: I talk to child by adding to QUEUE associated with $pid+\n"; } } foreach $process(@pids) { waitpid $process, 0; } print colored ['bold blue']," ===================================================== \n\n\n\n";
|