Processing more than one task simultaneously saves a lot of time, but a lot of challenge and complexity comes along with this approach. This talk will clarify the differences between fork and thread, when and why to use one or both, their challenges and tricks that save a lot of development time.
16. Thread
• Is the smallest sequence of programmed instructions
• May also be called as “task”
• When in the same process they share resources such as memory
17. Process
• The operating system keeps its processes separate and allocates the
resources they need
• Every process has an ID, a parent, an owner, and a session
• Contains state of all threads executing in a program
• Process may be made up of multiple threads of execution that
execute instructions concurrently
22. pthreads
• Implementation of POSIX Threads in PHP
• Works on Unix-like and Windows
• Requires PHP with ZTS (Zend Thread Safety) enabled (--enable-
maintainer-zts or --enable-zts on Windows)
• First stable version in 2014
23. pthreads
final class WorkerThreads extends Thread
{
private $workerId;
public function __construct(int $workerId)
{
$this->workerId = $workerId;
}
public function run()
{
sleep(rand(0, 1));
echo sprintf('Worker "%s" ran', $this->workerId).PHP_EOL;
}
}
24. pthreads
for ($index = 1; $index <= 5; ++$index) {
$worker = new WorkerThreads($index);
$worker->start();
}
echo 'Lekker threads!';
29. PCNTL
• Available since PHP 4
• Works only on Unix-like platforms
• Creation, program execution, signal handling and process
termination
• Does not work on web servers
30. PCNTL
$pid = pcntl_fork();
if ($pid == -1) {
error_log('You have failed this fork...');
exit(1);
}
if ($pid > 0) {
echo sprintf(
'I am the Parent Process and my PID is %d. My child's PID is %d’.PHP_EOL,
getmypid(),
$pid
);
exit();
}
echo 'I am the Child Process and my PID is '.getmypid()." xf0x9fx91xb6";
31. PCNTL
$ php fork.php
I am the Parent Process and my PID is 45179. My child's PID is 45180
I am the Child Process and my PID is 45180 👶
32. POSIX
• Available since PHP 4
• Enabled by default the PHP is compiled
• Interface for POSIX (Portable Operating System Interface) functions
• Works only on Unix-like systems
• Manage process, sessions, groups, users and files
33. POSIX
$uid = 501;
if (posix_setuid($uid) === false) {
error_log(posix_strerror(posix_get_last_error()));
exit(3);
}
$gid = 20;
if (posix_setgid($gid) === false) {
error_log(posix_strerror(posix_get_last_error()));
exit(4);
}
echo 'My PID is '.getmypid().', my UID is '.posix_geteuid().', my GID is '.posix_getgid();
34. POSIX
$ php fork+posix.php
I am the Parent Process and my PID is 45271. My child's PID is 45272
My PID is 45272, my UID is 501, my GID is 20
38. Forks share nothing
• Ensure yours new-born process can write and read files with umask(0)
• Do not use the same connections
• Replace file descriptors (STDIN, STDOUT and STDERR) or ensure you
don’t write/read them
• Use a lot of logs
• Replace the working directory with something you know that exists, like
chdir(__DIR__)
39. Daemons are independent
• Fork off and die
• Use the init session by calling posix_setsid()
• Save the PID somewhere
40. About Forks
• They are single core
• PHP is not multi-threaded by default
• Forks will be created in the same CPU of its parents
• Easy to overload a server
41. About Threads
• The memory_limit will be respected
• Does not make sense to use on a single core
43. Henrique Moody
Software developer focused on PHP.
Addicted to code, patterns, and
development methodologies. Open Source
enthusiast, publisher and contributor of
many projects, and core developer of the
Respect components. Brazilian based in
the Amsterdam working for Werkspot.
@henriquemoody
@henriquemoody