16.1 Process controlThis is NOT the latest copy of this book; click here for the latest version.
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.
|
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|