Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PHP Options&Information : getmypid

getmypid

Gets PHP's process ID (PHP 4, PHP 5)
int getmypid ( )


Code Examples / Notes » getmypid

pure-php

You can use this function also to avoid more than one instance of your app.
You can also use this class.
http://www.pure-php.de/node/20
Usage:
<?php
inlude("ProcessHandler.class.php");
if(ProcessHandler::isActive()){
  die("Already running!\n";);
}else{
  ProcessHandler::activate();
  //run my app
}
?>


barry

Why dont use use uniqid() to get a unique serial nr for security purposes?
Or md5(uniqid()), for extra fun.
Check http://www.php.net/uniqid for more about this..


10-sep-2002 10:46

Something you can use to combine several weak entropy sources, into a better init value for srand() and mt_srand():
mt_srand(time()
   ^ (int)microtime()
   ^ ip2long($_SERVER['REMOTE_ADDR'])
   ^ (int)$_SERVER['REMOTE_PORT']
   ^ @getmypid()
   ^ @disk_free_space('/tmp')
   );
Note that getmypid() may return 0 on some shared PHP installations that have excluded it for "safe"...
The safe mode also exclude the disk_free_space(directory) function call in some cases, for security reason. This explains the use of the @ operator to ignore warnings... the directory should be a highly active directory, which is accessible and typically used for temporary contents generated by PHP.
The above code combines all these sources using XOR binary operations, to avoid 32-bit overflows.


brooke

One good use for this is deciding on a concurrency-safe temporary file or directory name. You can be assured that no two processes on the same server have the same PID, so this is enough to avoid collisions. For example:
$tmpfile = "/tmp/foo_".getmypid();
// Use $tmpfile...
// Use $tmpfile...
// Use $tmpfile...
unlink ($tmpfile);
If you are sharing /tmp over the network (which is odd....) then you can, of course, mix in the PHP server's IP address.


carl

As it is very unlikely today that there will be two PHP processes with
the same process ID within the same microsecond, this function can
be used for something like srand((int)microtime() ^ getmypid() ^ time())
to have a decent chance of avioding a collision.



Just relying on the time in microseconds is a rather Bad Thing to do, since
a contect switch might very well take a lot less than that, so there's a decent
chance of two simultaneous request to get the same ID.



If you're running your php script upon an HTTP request, you might even want to toss in the IP addy of the remote host in your seed for a bit of extra entropy. You can't be too paranoid when it comes to 'random' numbers.


(Side note: I tried using hardware to generate random numbers, but
with my setup it took just about forever to gather enough entropy.
All hail ran3()! )


kroczu

<?
/*
mixed getpidinfo(mixed pid [, string system_ps_command_options])
this function gets PID-info from system ps command and return it in useful assoc-array,
or return false and trigger warning if PID doesn't exists
$pidifo=getpidinfo(12345);
print_r($pidifo);
Array
(
   [USER] => user
   [PID] => 12345
   [%CPU] => 0.0
   [%MEM] => 0.0
   [VSZ] => 1720
   [RSS] => 8
   [TT] => ??
   [STAT] => Is
   [STARTED] => 6:00PM
   [TIME] => 0:00.01
   [COMMAND] => php someproces.php > logfile
)
*/
//////////////////////////////////////////////
function getpidinfo($pid, $ps_opt="aux"){
  $ps=shell_exec("ps ".$ps_opt."p ".$pid);
  $ps=explode("\n", $ps);
 
  if(count($ps)<2){
     trigger_error("PID ".$pid." doesn't exists", E_USER_WARNING);
     return false;
  }
  foreach($ps as $key=>$val){
     $ps[$key]=explode(" ", ereg_replace(" +", " ", trim($ps[$key])));
  }
  foreach($ps[0] as $key=>$val){
     $pidinfo[$val] = $ps[1][$key];
     unset($ps[1][$key]);
  }
 
  if(is_array($ps[1])){
     $pidinfo[$val].=" ".implode(" ", $ps[1]);
  }
  return $pidinfo;
}
?>


webmaster

$_SERVER['REMOTE_ADDR'] is a bad thing to use these days.
Since AOL, and MSN Browsers change the IP address at the
most uncomfortable times, such as going from unsecure to
secure mode, you cannot rely on it staying the same anymore.
Using this Octal-Quad for hanging on to a user, such as in
shopping carts will ALWAYS FAIL with AOL browsers.  FYI.


Change Language


Follow Navioo On Twitter
assert_options
assert
dl
extension_loaded
get_cfg_var
get_current_user
get_defined_constants
get_extension_funcs
get_include_path
get_included_files
get_loaded_extensions
get_magic_quotes_gpc
get_magic_quotes_runtime
get_required_files
getenv
getlastmod
getmygid
getmyinode
getmypid
getmyuid
getopt
getrusage
ini_alter
ini_get_all
ini_get
ini_restore
ini_set
main
memory_get_peak_usage
memory_get_usage
php_ini_scanned_files
php_logo_guid
php_sapi_name
php_uname
phpcredits
phpinfo
phpversion
putenv
restore_include_path
set_include_path
set_magic_quotes_runtime
set_time_limit
sys_get_temp_dir
version_compare
zend_logo_guid
zend_thread_id
zend_version
eXTReMe Tracker