#!/usr/bin/perl -w

use strict;
use Getopt::Std;

my $options = {};
getopts('c', $options);

if (defined($options->{'c'}) && (@ARGV == 2)){
    my ($cluster, $job) = @ARGV;
    my $child;
    my $jobs = 16;

    my $i = 1;

    while ($i<=$jobs) {
	if(($child = fork()) <= 0){
	    last;
	}
	$i++;
    }

    if ($child == 0) {
	my $node = sprintf("rsh %s%d", $cluster, $i);
	exec("$node $job");
	die "exec() failed!\n";
    }
    else{
	while ((my $wait_return = wait())) {
	    if (($wait_return == -1)) {
		last;
	    }
	}
	print "All children finished.\n";
    }
}
else{
    die "Usage: $0 -c <cluster> <job>\n"; 
}
