- 論壇徽章:
- 0
|
本帖最后由 kingwmj 于 2012-08-08 16:02 編輯
寫了一個(gè)程序,因?yàn)樘?所以改用多進(jìn)程,但是與我的原來的不分進(jìn)程的程序結(jié)果不一致,最大的可能是多個(gè)進(jìn)程互相修改某個(gè)變量.但我沒有找到,求高手給看一下.- sub step_3_jaccard_distance_cal(){
- #格式:+1 P08758 Human 0 0 0 0 0 0 0 0 0 0 0 0 0 1
- my $in_file=shift;
- my $in= new IO::File($in_file) or die $!;
- my $out_file=shift;
- my $out = new IO::File(">$out_file") or die $!;
- my $len=0;
- seek $in,0,0;
- print "distance of r and s\n";
- my @matrix=<$in>;
- $len=my @list=split/\s+/,$matrix[0];
- print "$len\n";
- #以下為多進(jìn)程----------------------------------------------------------------------------------------
- my @child;
- $|++;
- print "good\n";
- # my $R;
- my $num_proc = 0;
- ## == number of collected ==
- my $num_collect = 0;
- my $collect;
- ## == get the child signal ==
- $SIG{CHLD} = sub { $num_proc--};
-
-
- for(my $r=0;$r<=$len-4;$r++){ #
- #my @sum=();
- #$sum[$r]=0;
- print "r=$r\n";
- my $pid=fork(); #分多進(jìn)程
- push(@child,$pid);
- if(!defined($pid)){
- print "Error in fork: $!";
- exit 1;
- }
- if($pid==0){ #子程序
- my @sum=();
- $sum[$r]=0;
-
- for(my $s=0;$s<=$len-4;$s++){
- print "s= $s\n";
- my $l_up; #這個(gè)是hs_up共有多少個(gè)鍵 #這個(gè)是分子
- my $l_dn; #這個(gè)是hs_dn共有多少個(gè)鍵 #這個(gè)是分母
- my %hs_up=();
- my %hs_dn=();
- my @d=();
- foreach my $line (@matrix){
- my @data=split/\s+/,$line;
- if(($data[3+$r]!=$data[3+$s]) and (($data[3+$r]==1) or ($data[3+$s]==1))) { #注意,這里是從3開始,把輸入文件整理成前三列是標(biāo)記
- $hs_up{$data[1]}=2; #這里data[1] 是UNIPROT ID
- }
- if(($data[3+$r]==1) or ($data[3+$s]==1)){
- $hs_dn{$data[1]}=2;
- }
- }
- $l_up=keys %hs_up; #
- $l_dn=keys %hs_dn;
- if($l_dn==0){
- print "r=$r, s=$s\n";
- die ; #
-
- }
- else{
- $d[$r][$s]=$l_up/$l_dn;
- #print $out "$d[$r][$s] "
- }
- $sum[$r]+=$d[$r][$s];
- #print $out "$sum[$r] ";
- }
- print $out "$sum[$r] ";
- exit 0; #very important. 運(yùn)行到他,這個(gè)子進(jìn)程就結(jié)束了.所以要放到子進(jìn)程的最后.
- }
- $num_proc ++;
- ## == if need to collect zombies ==
- if (($r-$num_proc-$num_collect) > 0) {
- while (($collect = waitpid(-1, WNOHANG)) > 0) {
- $num_collect ++;
- }
- }
- while($num_proc > 1000){ # 控制子進(jìn)程數(shù)
- sleep(3);
- print "stop\n";
- }
-
-
- }
- print $out "\n";
- foreach (@child){ # 回收所有進(jìn)程
- waitpid($_, 0);
- }
- $in->close;
- $out->close;
- }
復(fù)制代碼 |
|