All pastes #962975 Raw Edit

Someone

public php v1 · immutable
#962975 ·published 2008-03-30 04:10 UTC
rendered paste body
#!/usr/bin/php<?php$intro='########################################################################### Simple multi threading php app as example. Coded by xprog# Spawns itself and give each thread its own workload,# this example its a fake workload, it just md5() a number.# your workload would be bigger and whatever you want it to be.## Yo to ciphercrew && h4ck-y0u##########################################################################';$self = isset($argv[0]) ? $argv[0] : ''; //argv0 is itself, the scripts name.$bit1 = isset($argv[1]) ? $argv[1] : ''; //Bit 1 and 2 are just example vars the script will execute itself with.$bit2 = isset($argv[2]) ? $argv[2] : '';if (empty($self)) die('This is a command line script!');if (empty($bit1) && empty($bit2)) { //making sure $bit1 and 2 arent set, user executed...   fwrite(STDOUT, $intro . "\n\n");   fwrite(STDOUT, "How many threads? (default 3): ");   $threads = trim(fgets(STDIN));   if (empty($threads) || $threads < 1) $threads = 3;   fwrite(STDOUT, "How many times? (default 10): ");   $loops = trim(fgets(STDIN));   if (empty($loops) || $loops < 1 || $loops < $threads) $loops = 10;      fwrite(STDOUT, "THREADS: $threads\n");   fwrite(STDOUT, "LOOPS: $loops\n");   fwrite(STDOUT, "Executing...\n");   fwrite(STDOUT, "--------------------------------------------------------------------------\n");   $times = $loops + $threads;   for ($p=1;$p<=$times;$p++) { //THREAD CODE      for (;$p<=$threads;$p++){ //first time around start opening threads.         $workload = rand(0,1000);         fwrite(STDOUT,"Thread $p given $workload\n");         $cmd = "php $self $workload 123abc"; //bit2 is bogus but you could pass multiple vars here         $fh[$p] = popen($cmd,"r");         // sleep(1); // pausing after opening a thread to make them return more regulated. Needed more when workload is large/slow.      }      if ($p <= $loops) { //checking on responce from threads, but also opening new ones on responce         $proc = $p - $threads;         $st = fgets($fh[$proc]);         pclose($fh[$proc]);         fwrite(STDOUT,"Thread $proc replied with: $st\n");         $workload = rand(0,1000);         fwrite(STDOUT,"Thread $p given $workload\n");                  $cmd = "php $self $workload 123abc"; //bit2 is bogus but you could pass multiple vars here         $fh[$p] = popen($cmd,"r");         // sleep(1); // pausing after opening a thread to make them return more regulated. Needed more when workload is large/slow.      }      else { //done opening threads, getting the last of the responces         $proc = $p - $threads;         $st = fgets($fh[$proc]);         pclose($fh[$proc]);         fwrite(STDOUT,"Thread $proc replied with: $st\n");      }   }//END THREADING   fwrite(STDOUT,"ALL DONE!");}elseif (!empty($bit1) && $bit2=='123abc') { //bit1 isnt empty and bit2 is bogus, just checking to be sure the app ran itself   //THREADED WORKLOAD HERE!   $temp = md5($bit1);   fwrite(STDOUT,$temp);}?>