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
What is Forking
Moderator: General Moderators
Re: What is Forking
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.
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
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
thanks for that example
-
johngill2810
- Forum Newbie
- Posts: 6
- Joined: Tue May 14, 2013 9:52 am
Re: What is Forking
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
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