As you should be well aware by now, you can use PHP as a command-line scripting system to automate common tasks. We look at this in more depth later as we explore some of the alternative things you can do with command-line PHP. Right now, however, there are somewhat more mundane things to be thinking about, most notably controlling processes through PHP.
Given that PHP scripts are executed by the PHP parser (php.exe on Windows, and just "php" elsewhere), this parser executable file can only run in one processor on your computer. Naturally this is fine if you only have one processor in your machine, but what if you are on a 2-way or 4-way machine? Or, what if you are on a single-processor box but do not want your command-line script to lock up while executing a long operation?
To solve these problems we need to delve into the realm of process control: the art of spawning child processes and controlling them, and also the ability to take advanced control over the original PHP process. Sadly, the process control functions only work on Unix operating systems right now, which is pretty much everything except Windows - if you are using Windows, you are flat out of luck. Sorry! If you're using Unix, you'll need to configure PHP with the flag --enable-pcntl.
Before you continue, it is important that you understand what a process actually is . Put simply, a process is one running instance of a program, which has its own ID number and its own scheduling from the kernel. By "instance of a program", I mean that you are likely to have only one copy of the PHP executable on your machine, but you can have several running at one time - multiple running instances. Each process has its own unique number assigned to it, and this process ID (PID) is the preferred method for accessing a process.
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Taking control of PHP >>
Previous chapter: Miscellaneous topics
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.