Page 1 of 1
What is Forking
Posted: Sun Nov 25, 2012 3:50 pm
by Live24x7
What is fork() good for in PHP ?
I read the documentation at
http://au1.php.net/pcntl_fork
but could not understand a word of what is told there.
terms like PID, PPID etc.and subsequent google search leads to proportional integral derivative controller for PID - which does not seem to fit anywhere
I googled for fork and understood that it is the process of breaking down a process into parent-child processes.
- but where is it useful in PHP ?
Thanks a lot
Re: What is Forking
Posted: Sun Nov 25, 2012 7:59 pm
by requinix
It's useful in PHP for the same reasons it's useful everywhere else. It's a very easy way of making a multi-pseudo-threaded (they're not actual threads) application. Like a while ago I made a really over-the-top complicated IRC bot in PHP and used multiple processes to do it. One process watches the IRC stream, passes messages to the child processes as needed, then receives and acts upon messages received from them.
The child processes get messages and are free to act upon them however they want without having to worry about other messages coming in. For example a user sends a message to the bot "!remind 10m pizza". The parent process passes the message to the children and one of them recognizes the "!remind" command. It sleeps for 10 minutes, wakes up, and sends the message "pizza" to the user. You need a second process for this because of the sleeping: if the parent did that then it wouldn't be able to send or receive any messages during that time.
Re: What is Forking
Posted: Mon Nov 26, 2012 1:55 am
by Live24x7
so that means - with forking - the parent can keep working on a process while delegating some work on to its children.
thanks for that example

Re: What is Forking
Posted: Wed May 22, 2013 9:39 am
by johngill2810
Hello,
I have serched about your question and found some information.
It is the process which forks the currently running process.
You can also visit the below link to get more details.
http://php.net/manual/en/function.pcntl-fork.php
http://stackoverflow.com/questions/3173 ... -good-idea