30. Comparing
the
Apache
&
CLI
SAPI
Web
based
SAPI’s
• HTTP
is
a
stateless
protocol
• Request/response
based
• Limited
interacYon
• Sessions
&
cookies
as
workaround
• ExecuYon
Ymeouts
• Limited
request/response
size
31. Comparing
the
Apache
&
CLI
SAPI
CLI
SAPI
• Controlable
state
• Controlable
script
execuYon
• ConYnuous
interacYon
• No
need
for
sessions
• No
execuYon
Ymeouts
39. Get
INI
informaLon
(-‐i)
Filtering
items
$
php
-‐i
|
grep
“log_”
define_syslog_variables
=>
Off
=>
Off
log_errors
=>
On
=>
On
log_errors_max_len
=>
1024
=>
1024
$
40. Syntax/lint
check
(-‐l)
Only
checks
parse
errors
$
php
-‐l
myFile.php
No
syntax
errors
detected
in
myFile.php
$
41. Module
list
(-‐m)
$
php
-‐m
[PHP
Modules]
bcmath
bz2
calendar
Core
ctype
curl
date
dba
$
59. STDIN
Stream
that
is
opened
<?php by
default
while(!feof(STDIN)){
$line = trim(fgets(STDIN));
if(strlen($line) > 0){
echo strrev($line).PHP_EOL;
}
}
60. STDIN
The
Stream
that
is
opened
proof
! by
default
$
php
-‐r
"var_dump(STDIN);"
resource(1)
of
type
(stream)
$
78. Process
Control
should
not
be
enabled
within
a
web
server
environment
and
unexpected
results
may
happen
if
any
Process
Control
funcYons
are
used
within
a
web
server
environment.
79. Forking PID
value
determines
context Copy
<?php
program
$pid = pcntl_fork();
if ($pid == -1) {
execuYon
//Forking failed
} else if ($pid) {
//Parent logic
} else { PID
of
child
//Child logic
} process
85. Parent
PID
Prefork
PID
POSIX
process
c==
funcLons
of
parent
ontrol
==
<?phpsession
PID parent
PID
echo "[prefork] PID:
".posix_getpid().", parent PID: ".posix_getppid().PH
P_EOL;
$pid = pcntl_fork();
if ($pid == -1) { child
PID
die('could not fork');
child
PID
} else if ($pid == 0) {
echo "[child] PID: ".posix_getpid().", paren
t PID: ".posix_getppid().PHP_EOL; parent
exit;
} else { PID
echo "[parent] PID: ".posix_getpid().", pare
nt PID: ".posix_getppid().PHP_EOL;
pcntl_wait($status);
}
86. POSIX
process
control
funcLons
<?php
$pid=pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
echo "[parent] Killing child process $pid".PHP_EOL;
posix_kill($pid,SIGTERM);
echo "[parent] Child process $pid killed".PHP_EOL;
pcntl_wait($status);
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
} else {
while(true){
sleep(100);
}
exit;
}
87. POSIX
process
control
funcLons
“KILL
0”
checks
<?php
$pid=pcntl_fork(); existence
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
Send
SIGTERM
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
signal
echo "[parent] Killing child process $pid".PHP_EOL;
posix_kill($pid,SIGTERM);
echo "[parent] Child process $pid killed".PHP_EOL;
pcntl_wait($status);
$exists = posix_kill($pid,0)?'still':'no longer';
echo "[parent] Child process $pid $exists exists".PHP_EOL;
} else {
while(true){
Child
process
sleep(100);
}
is
dead
exit;
}
88. Talk
dedicated
to
process
control
Check
this
guy
out
! in
PHP
Jeroen
Keppens:
@jkeppens
http://www.slideshare.net/jkeppens/
zendcon-‐2011-‐php-‐in-‐the-‐dark
89. •January
27th
&
28th
2012
•Best
Western
Hotel
Ter
Elst
Antwerp
(Belgium)
•Call
For
Papers
CLOSED
•Schedule
&
Ycket
announcements
in
a
couple
of
weeks