Close search
Hoa

Hack book of Hoa\Zombie

A zombie is a processus which is neither active, nor finished: between life and death. Hoa\Zombie allows to turn a PHP execution into a zombie!

Table of contents

  1. Introduction
  2. Manipulate zombies
  3. Example
  4. Conclusion

Introduction

Putting a processus between life and death should be, in general, avoided, because it is often synonym of a dysfunction. However, it might happen to be shrewd, notably in the case of a demon (see the Hoa\Worker library). It is important to notice that a zombie is also mute: it can no longer write on its standard output because it is theorically no longer attached to a parent processus.

For now, Hoa\Zombie is able to create zombies only if it runs behind PHP FPM. Remind us how PHP works: a FastCGI server (such as PHP FastCGI or PHP FPM) will start a PHP processus when a request is received and will stop it when a response is sent. Somehow, the PHP processus is synchronized on the FastCGI protocol. Fortunately, when we use PHP FPM, we are able to desynchronize this protocol, and thus, to create zombies, while delegating the processus management to PHP FPM!

Manipulate zombies

Creating a zombie is very simple: we only need to call the Hoa\Zombie\Zombie::fork method.

It is common knowledge that killing a zombie is a very attractive activity, even more if we have the choice of weapons! That's why Hoa\Zombie\Zombie provides the following methods:

All these methods have been proven. Thus:

// I'm alive!
Hoa\Zombie\Zombie::fork();
// I'm a zombie!
Hoa\Zombie\Zombie::decapitate();
// I'm dead…

Hoa\Zombie\Zombie is also able to get the identifier of the processus, which can be useful sometimes, through the Hoa\Zombie\Zombie::getPid method. Finally, the Hoa\Zombie\Zombie::test method allows to know whether we can create a zombie or not.

Example

To create a zombie, we said we need to use PHP FPM. Next, there is different ways to execute our PHP program, which will be our future zombie: either through HTTP (which will automatically create a FastCGI request to PHP FPM) or directly by sending a FastCGI request to PHP FPM. We will choose this latter.

We start with the Zombie.php file:

echo 'I guess I am sick…', "\n";
Hoa\Zombie\Zombie::fork();

// Do whatever you want here, e.g.:
sleep(10);
file_put_contents(
    __DIR__ . DS . 'AMessage',
    'Hello from after-life… or somewhere about!'
);

Next, we continue with the Run.php file which will execute Zombie.php thanks to Hoa\Fastcgi:

$fastcgi = new Hoa\Fastcgi\Responder(
    new Hoa\Socket\Client('tcp://127.0.0.1:9000')
);

// Start Zombie.php and print the response before it becomes undead.
echo $fastcgi->send([
    'GATEWAY_INTERFACE' => 'FastCGI/1.0',
    'REQUEST_METHOD'    => 'GET',
    'SCRIPT_FILENAME'   => __DIR__ . DS . 'Zombie.php'
]);

And finally, we test:

$ php-fpm -d listen=127.0.0.1:9000
$ php Run.php
I guess I am sick…

After 10 seconds, we will see the AMessage file appears, containing Hello from after-life… or somewhere about!.

We can create a zombie at any moment and kill it as we want. If the end of the program is reached, the zombie will kill itself. We will anticipate that a demon is just a zombie that starts a server.

Conclusion

Hoa\Zombie allows to turn a processus into a zombie only when it is used with PHP FPM. We invite you to take a look at Hoa\Worker for a complete example.

An error or a suggestion about the documentation? Contributions are welcome!

Comments

menu