Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
function bkg($executable, $args = []) {
$childPid = pcntl_fork();
if ($childPid == 0) {
posix_setsid();
fclose(STDOUT); // в манах говорится, что необходимо закрыть поток стд. вывода
fclose(STDERR); // и ошибок
pcntl_exec($executable, $args);
} elseif($childPid != -1) {
return true;
}
return false;
}
function bkg(){
$childPid = pcntl_fork();
if ($childPid == 0) {
} elseif($childPid != -1) {
echo 'out to cli';
fclose(STDOUT);
$STDOUT = fopen("log." . getmypid() . ".out", "wb");
echo 'out to log.out';
}
}
bkg();
<?php
function bkg(){
$childPid = pcntl_fork();
if ($childPid == 0) {
} elseif($childPid != -1) {
$pid = getmypid();
echo 'iam child, out to cli, my pid is ' . $pid . PHP_EOL;
fclose(STDOUT);
$STDOUT = fopen("log." . $pid . ".out", "wb");
echo 'out to log.out, my pid is ' . $pid . PHP_EOL;
}
}
bkg();
bkg();
bkg();
echo 'iam parent, out to cli, my pid is ' . getmypid() . PHP_EOL;
?>
<?php
function bkg(){
$childPid = pcntl_fork();
if ($childPid == 0) {
$pid = getmypid();
echo 'iam child, out to cli, my pid is ' . $pid . PHP_EOL;
fclose(STDOUT);
$STDOUT = fopen("log." . $pid . ".out", "wb");
echo 'iam child, out to log.' . $pid . '.out, my pid is ' . $pid . PHP_EOL;
} elseif($childPid != -1) {
}
}
bkg();
bkg();
bkg();
echo 'iam parent, out to cli, my pid is ' . getmypid() . PHP_EOL;
?>
function bkg($executable, $args){
$childPid = pcntl_fork();
if ($childPid == 0) {
posix_setsid();
fclose(STDOUT); // в манах говорится, что необходимо закрыть поток стд. вывода
fclose(STDERR); // и ошибок
$pid = getmypid();
$STDOUT = fopen("log." . $pid . ".out", "wb");
echo 'out to log.out, my pid is ' . $pid . PHP_EOL;
pcntl_exec($executable, $args);
} elseif($childPid != -1) {
$pid = getmypid();
echo 'i am still parent, out to cli, my pid is ' . $pid . PHP_EOL;
}
}
bkg('/usr/bin/php', ['/domains/manuals.dev/script/tmp-child.php']);
echo 'iam parent, out to cli, my pid is ' . getmypid() . PHP_EOL;
function daemon()
{
checkPid();
$pid = pcntl_fork();
if ($pid < 0)
die('error call fork()'.PHP_EOL);
if ($pid)
exit();
echo 'start process pid=', posix_getpid(), PHP_EOL;
$sid = posix_setsid();
if ($sid < 0) exit;
global $pidFilename;
file_put_contents($pidFilename, posix_getpid());
fclose(STDOUT);
fclose(STDIN);
fclose(STDERR);
}
start-stop-daemon --start --background --chuid www-data:www-data --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- $LOGFILE || return 1
Задачи, кстати, могут быть не только сайтописательскими, а и носить прикладной характер, скажем, морфологическая обработка неких текстов или иных больших массивов данных, генерация статистической информации и так далее.
сам-то рассчитывал что либо?
практически все статистические расчеты есть — обработка одного большого массива данных.
то же самое можно сказать и про морфологический разбор. Да и в онлайне он точно не нужен.
так что переходим к семафорам? расскажешь, как на практике надо использовать семафоры?
(new MyThread())->run();
$thread = new MyThread();
$thread->start();
$thread = new MyThread();
$thread->start();
$ php --re standard | grep -E 'fwrite|fgets|stream_select|proc_open'
Function [ <internal:standard> function proc_open ] {
Function [ <internal:standard> function fgets ] {
Function [ <internal:standard> function fwrite ] {
Function [ <internal:standard> function stream_select ] {
расширение Stream
Потоки являются неотъемлемой частью PHP, начиная с версии 4.3.0. Для их включения не требуется каких-либо действий.
PHP IPC — Межпроцессное взаимодействие в PHP