#!/usr/local/bin/perl

#
# to peg the processors of a machine
# Jeremy Kister 2007.12.20

use strict;

my $ttr = 86400; # time to run, seconds

my $num;
if(open(CPU, "/proc/cpuinfo")){
	while(<CPU>){
		$num++ if(/^processor\s+:/);
	}
	close CPU;
}elsif(open(PSR, "psrinfo -v |")){
	while(<PSR>){
		$num++ if(/^Status of/);
	}
	close PSR;
}elsif(open(SYS, "sysctl hw.ncpu |")){
	chop(my $res=<SYS>);
	close SYS;
	$num = $res =~ /^hw.ncpu:\s(\d+)$/;
}else{
	warn "unknown number of cores\n";
	$num=2;
}
warn "setting up for $num cores\n";

my @children;
for(1..$num){
	my $pid = fork();
	if($pid){
		# parent
		push @children, $pid;
	}elsif($pid == 0){
		# child
		eval { 
			local $SIG{ALRM} = sub { die "finished.\n"; };
			alarm($ttr);
			print "child spawned.\n";
			while(1){};
			alarm(0);
		};
		alarm(0);
		warn $@ if($@);
		exit;
	}else{
		die "couldnt fork: $!\n";
	}
}
		
foreach(@children){
	waitpid($_,0);
}
