phplibrary/src/Process.php

68 lines
1.4 KiB
PHP

<?php
namespace ctiso;
/**
* str_getcsv
* @param string $input
* @param string $delimiter
* @param string $enclosure
* @param string $escape
* @return array
*/
function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\")
{
$fiveMBs = 1024;
$fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
$data = '';
if (is_resource($fp)) {
fputs($fp, $input);
rewind($fp);
$data = fgetcsv($fp, 1000, $delimiter, $enclosure);
fclose($fp);
}
return $data;
}
/**
* process_exists
* @param int $pid
* @return bool
*/
function process_exists($pid)
{
if (PHP_OS == 'WINNT') {
$content = shell_exec("tasklist.exe /NH /FO CSV");
if (!$content) {
return false;
}
$processes = explode("\n", $content);
foreach ($processes as $process) {
if ($process != "") {
$csv = str_getcsv($process);
if ($pid == $csv[1]) return true;
}
}
return false;
} else {
return file_exists("/proc/$pid");
}
}
/**
* create_single_proces
* @param string $fpid
* @param callable $fn
* @return int
*/
function create_single_process($fpid, $fn)
{
if (file_exists($fpid)) {
if (process_exists((int)file_get_contents($fpid))) {
return 1;
}
}
call_user_func($fn);
return 0;
}