What is Forking

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

What is Forking

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: What is Forking

Post 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.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: What is Forking

Post 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 :roll:
johngill2810
Forum Newbie
Posts: 6
Joined: Tue May 14, 2013 9:52 am

Re: What is Forking

Post 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
Post Reply