#! /usr/local/bin/perl pipe(read_1,write_1); # open pipe 1 pipe(read_2,write_2); # open pipe 2 # unbuffer all i/o on the pipes $old_h=select(read_1); $|=1; select(read_2); $|=1; select(write_1); $|=1; select(write_2); $|=1; select($old_h); $|=1; if ($pid=fork) { # the parent do not do anything here} # so close off the read_1 and write_2 # hence wirte to write_1 and get results off read_2 $old_h=select(read_2); $|=1; select(write_1); $|=1; select($old_h); close(read_1); close(write_2); } elsif (defined $pid) { # this is the child so... # close off read_2 and write_1 # redirect stdin to read_1 and stdout to write_2 # then start up the work code $old_h=select(read_1); $|=1; select(write_2); $|=1; select($old_h); close(read_2); close(write_1); close(STDOUT); open(STDOUT,">& write_2"); select(STDOUT); close(STDIN); open(STDIN,"<& read_1"); close(read_1); sleep(1); system("/data/02/steve/mag_stuff/calc_mag") || print ("Is this working?\n"); die "ending child\n"; } # 1. need lat lon depth of the event # 2. the file to read and calc the magnitudes if ($#ARGV<3) { print stderr "Need 4 input arguements ...\n"; print stderr "latitude longitude depth amplitude_file\n"; } $lat=$ARGV[0]; $lon=$ARGV[1]; $depth=$ARGV[2]; $file=$ARGV[3]; open (in_file,"$file"); while () { if (!/^\#/ && length($_)>1) { ($station,$type,$amplitude,$period,$index)=split; $station=substr($station,0,length($station)-3); $type=substr($type,0,2); $frequency=sprintf "%7.4lf",1/$period if ($period !=0); print "$station $type $amplitude $frequency $lat $lon $depth\n"; print write_1 "$station $type $amplitude $frequency $lat $lon $depth\n"; $new_line=; print $new_line; @n=split(/\s+/,$new_line); $s_mag=$n[$#n]; $st_mag{$n[1]}+=$s_mag; $sm_count{$n[1]}+=1; } } foreach $f (keys(%sm_count)) { $n_mag=$st_mag{$f}/$sm_count{$f} if ($sm_count{$f}!=0); printf "$f %4.2f for $sm_count{$f} readings\n",$n_mag; }