对整个文件夹进行codeml,同时执行几个进程
2009-10-05 17:33
141 查看
#!/usr/bin/perl -w
# input: directory(contain files to be codeml)
# numberofproceses(how many processes to produce)
# tempDirectoryName(child processes will work under
# directory based on this name)
# output: output files will be in the same directory as input files;
#
# input files must end with .codeml
use strict;
use warnings;
my($dir,$num,$tempDir)=@ARGV;
die ("$0: dir numberOfProcesses tempDirectoryName") unless($dir && $num && $tempDir);
$dir=~s///$//;
# read control file
my $ctlFile;
open(my $f,"<codeml.ctl") or die "cannot find codeml.ctl";
{
local $/;
$ctlFile=<$f>;
}
close($f);
# distribute files
opendir(my $d,$dir) or die $!;
my @files=sort grep {//.codeml$/} readdir($d);
close($d);
my $fileNum=@files;
my $fileno=0;
my @processes;
for(my $i=0;$i<$num;$i++)
{
# assign files
$processes[$i]{'startNo'}=$fileno;
if($fileno+int($fileNum/$num)>$fileNum)
{
$fileno=$fileNum;
}
else
{
$fileno+=int($fileNum/$num);
}
$processes[$i]{'endNo'}=$fileno-1;
# make directories for child processes;
if(-e "${tempDir}_$i")
{
die "${tempDir}_$i exists!! aborted./n";
}
else
{
`mkdir ${tempDir}_$i`;
}
}
# ready to work
my $pid;
my @child_pid;
my $id;
for($id=0;$id<$num;$id++)
{
$pid=fork();
if($pid==0){last;}
else{push(@child_pid,$pid);}
}
# child procese $id
if($pid==0)
{
print STDERR "$id pid=$$ get to work/n";
for(my $fileid=$processes[$id]{'startNo'};$fileid<=$processes[$id]{'endNo'};$fileid++)
{
my $myctlFile=$ctlFile;
`rm ${tempDir}_$id/* 1>>${tempDir}_$id.log`;
open($f,">${tempDir}_$id/codeml.ctl") or die $!;
my $newFileName="$dir/$files[$fileid]";
#$myctlFile=~s/SEQFILE/$newFileName/g;
print $f $myctlFile;
close($f);
my $pwd=`pwd`;
chomp($pwd);
my $exec="cd $pwd/${tempDir}_$id;".
"cp /"$dir/$files[$fileid]/" SEQFILE;".
"codeml codeml.ctl 1>>../${tempDir}_$id.log;".
"mv mlc /"$dir/$files[$fileid].mlc/"";
`$exec`;
}
print STDERR "$id pid=$$ finished./n";
exit(0);
}
# parent process
if($pid!=0)
{
while(wait()!=-1){}
print STDERR "All finished/n";
exit(0);
}
# input: directory(contain files to be codeml)
# numberofproceses(how many processes to produce)
# tempDirectoryName(child processes will work under
# directory based on this name)
# output: output files will be in the same directory as input files;
#
# input files must end with .codeml
use strict;
use warnings;
my($dir,$num,$tempDir)=@ARGV;
die ("$0: dir numberOfProcesses tempDirectoryName") unless($dir && $num && $tempDir);
$dir=~s///$//;
# read control file
my $ctlFile;
open(my $f,"<codeml.ctl") or die "cannot find codeml.ctl";
{
local $/;
$ctlFile=<$f>;
}
close($f);
# distribute files
opendir(my $d,$dir) or die $!;
my @files=sort grep {//.codeml$/} readdir($d);
close($d);
my $fileNum=@files;
my $fileno=0;
my @processes;
for(my $i=0;$i<$num;$i++)
{
# assign files
$processes[$i]{'startNo'}=$fileno;
if($fileno+int($fileNum/$num)>$fileNum)
{
$fileno=$fileNum;
}
else
{
$fileno+=int($fileNum/$num);
}
$processes[$i]{'endNo'}=$fileno-1;
# make directories for child processes;
if(-e "${tempDir}_$i")
{
die "${tempDir}_$i exists!! aborted./n";
}
else
{
`mkdir ${tempDir}_$i`;
}
}
# ready to work
my $pid;
my @child_pid;
my $id;
for($id=0;$id<$num;$id++)
{
$pid=fork();
if($pid==0){last;}
else{push(@child_pid,$pid);}
}
# child procese $id
if($pid==0)
{
print STDERR "$id pid=$$ get to work/n";
for(my $fileid=$processes[$id]{'startNo'};$fileid<=$processes[$id]{'endNo'};$fileid++)
{
my $myctlFile=$ctlFile;
`rm ${tempDir}_$id/* 1>>${tempDir}_$id.log`;
open($f,">${tempDir}_$id/codeml.ctl") or die $!;
my $newFileName="$dir/$files[$fileid]";
#$myctlFile=~s/SEQFILE/$newFileName/g;
print $f $myctlFile;
close($f);
my $pwd=`pwd`;
chomp($pwd);
my $exec="cd $pwd/${tempDir}_$id;".
"cp /"$dir/$files[$fileid]/" SEQFILE;".
"codeml codeml.ctl 1>>../${tempDir}_$id.log;".
"mv mlc /"$dir/$files[$fileid].mlc/"";
`$exec`;
}
print STDERR "$id pid=$$ finished./n";
exit(0);
}
# parent process
if($pid!=0)
{
while(wait()!=-1){}
print STDERR "All finished/n";
exit(0);
}
相关文章推荐
- 多个进程同时等待网络的连接事件,当这个事件发生时,这些进程被同时唤醒,我们知道进程被唤醒,需要进行内核重新调度,这样每个进程同时去响应这一个事件,而最终只有一个进程能处理事件成功,其他的进程在处理该事件失败后重新休眠或其他。
- 关于使用shell开辟几个并发线程同时执行互不相关的应用
- 整个页面的ajax都执行完以后再进行其他的操作,怎么判断一个图片是否正常显示?
- tomcat项目重载 ,同一个项目初始化两次,同时执行两个进程
- Unix网络编程:while中执行waitpid为什么能正确解决同时出现的多个子进程结束
- Linux下多个进程或线程同时对一个文件进行写操作
- DB2执行LOAD操作的同时进行统计信息的收集
- C++ windows多线程 线程描述了进程内代码的执行路径。进程中同时可以有多个线程在执行,为了使他们能够同时运行,操作系统为每个线程轮流分配CPU时间片,为了充分地利用CPU提高软件产品的性能,一
- Linux下多个进程或线程同时对一个文件进行写操作
- Nginx对某个文件夹或整个站点进行登录认证的方法
- 执行一个存储过程,能否实现同时返回几个值和一个行集?或者同时返回几个值和几个行集.
- 在VC中实现一次执行一个进程,如果同时打开两个,则报该程序已经运行
- 2.首先对box2d 的几个重要文件夹里面的内容进行解释
- Linux下多个进程或线程同时对一个文件进行写操作和access函数
- 从文件中读入文本,文本内容为几个字符串,用逗号间隔,将其中同时含有字母和数字的字符串去除,留下仅仅只包含字母或数字的字符串,然后进行排序
- Linux下多个进程或线程同时对一个文件进行写操作
- 利用php利用root权限执行shell脚本必须进行以下几个步骤
- Nginx对某个文件夹或整个站点进行登录认证的方法
- Linux下多个进程或线程同时对一个文件进行写操作