Jun 22, 2009

Kill a zombie process

Well, yes, everybody knows that zombie processes can't be killed, as they're already dead. That's all very well documented and repeated so many times, and the solution to this would be to: "kill the parent process".


Since killing the parent process makes init the parent of the zombie process which can kill zombies, if the real parent can't.

My problem was - how to get the parent process? That's not written anywhere! Why not? Is it so hard?

After some research it turns out it isn't.

You can get the parent process id (PPID) with the -o parameter in 'ps' command:

# ps auxo ppid | grep Z

You know what 'grep Z' does, don't you? It prints only the processes in the Zombie state.

Well when you get the ppid, the solution to zombie killing or reaping is simple.

Just kill -9 the ppid.

I hope this article saves someone from the midnight frustration on why the machine just won't start any new processes, even if CPU load and memory usage are near zero.

May 5, 2009

ActionScript: Printing

function print_mc(mc) {
 
 var myPrintJob:PrintJob = new PrintJob();

 myPrintJob.start();
 myPrintJob.addPage(mc);
 myPrintJob.send();

}

PHP: generate a photographic mosaic

I have written a command-line, procedural PHP script to generate a photographic mosaic.

Some samples:




The script works in two stages. The first one scans all of the images to work out an average color, and saves that information to a txt file on the disk.

This is then used in the second stage of the process, where a template of the mosaic is made - this is an image, shrinked down in size to as many pixels as is the number of images on the horizontal and vertical axis. This way each image represents one pixel of the template. The second stage begins composing the mosaic from images, that have the closest average color to the current pixel in the template.

The process is optimized by caching average colors to disk (this optimization is mandatory) and an caching smaller images to disk (there's a BIG difference in speed if you try to open a 5MB file or 100K file).

Here's the code that does all the work: http://pastebin.com/f70e3ccf7

May 1, 2009

PHP: average functions: Mean (Arithmetic, Geometric, Harmonic) · Median · Mode

See Wiki for detail on mean, median or mode. Please write your own optimizations / implementations in the comments.
function arithmetic_mean($a) {
 return array_sum($a)/count($a);
}

function geometric_mean($a) {
 foreach($a as $i=>$n) $mul = $i == 0 ? $n : $mul*$n;
 return pow($mul,1/count($a));
}

function harmonic_mean($a) {
 $sum = 0;
 foreach($a as $n) $sum += 1 / $n;
 return (1/$sum)*count($a);
}

function median($a) {
 sort($a,SORT_NUMERIC); 
 return (count($a) % 2) ? 
  $a[floor(count($a)/2)] : 
  ($a[floor(count($a)/2)] + $a[floor(count($a)/2) - 1]) / 2;
}

function modal_score($a) {
 $quant = array();
 foreach($a as $n) $quant["$n"]++;
 $max = 0;
 $mode = 0;
 foreach($quant as $key=>$n) {
  if($n>$max) {
   $max = $n;
   $mode = $key;
  }
 }
 return $mode;
}

Apr 25, 2009

How to download full resolution satellite images from Google Earth/Maps

Google Maps uses the following technique to serve images: It displays 256x256px blocks of images from their server. The server displays the image from the given parameters, for example:
http://khm3.google.com/kh/v=38&hl=en&x=35415&s=&y=23296&z=16
So, with a script, you can fetch all the images from the starting X and Y, to the ending and then stitch them together piece by piece. Below is an example how to get the parameters for the image (v,x,y,z) And here's a script that does all the work:
<?php
error_reporting(E_ALL ^ E_NOTICE);
set_time_limit(0);
ini_set("memory_limit","1024M");

// Bottom Left
//http://khm0.google.com/kh/v=38&hl=en&x=141234&s=&y=93147&z=18&s=G
//Top Right
//http://khm3.google.com/kh/v=38&hl=en&x=141273&s=&y=93105&z=18&s=Gali

//How big is the block (in pixels)
$block_width = 256;
$block_height = 256;

//I don't know what that is, but look at the URL
$v = 38;
//Zoom level
$z = 18;

//Get these parameters from the url
$start_x = 141234;
$start_y = 93105;

$end_x = 141273;
$end_y = 93147;

//Do some simple math... Dimensions of the panorama
$dim_x = $end_x - $start_x;
$dim_y = $end_y - $start_y;

$dim_x *= $block_width;
$dim_y *= $block_height;

print("$dim_x x $dim_y px\n");

//Allocate memory space for the panorama
$img = imagecreatetruecolor($dim_x,$dim_y);

for($x = $start_x ; $x <= $end_x ; $x++) {
 for($y = $start_y ; $y <= $end_y ; $y++) {
  $f = "$x-$y-$z.jpg";
  $url = "http://www.najdi.si/servlet/ServletRedirectTileImage?x=$x&y=$y&zl=$z";
  $url = "http://khm3.google.com/kh/v=$v&x=$x&y=$y&z=$z";
  
  //Download the block
  $cache = "wget \"$url\" -O $f";
  print("$cache\n");
  passthru($cache);
  
  //Put it on the panorama
  $block = imagecreatefromjpeg($f);
  imagecopy($img, $block, ($x-$start_x)*$block_width, ($y-$start_y)*$block_height, 0, 0, $block_width, $block_height);
  imagedestroy($block);
 }
}

imagejpeg($img,"$start_x-$end_x-_-$start_y-$end_y.jpg",100);
?>
I also noticed that many mapping sites are using the same technique. End result:


full.jpg