Note: Part 1 of this series can be found here. Also, after feedback from the development community, this series was renamed to “Multi-Processing” instead of “Multi-Threading”. To most people the distinction probably doesn’t matter, but this title is more accurate.
Priority
The priority of a process is a ranking given to it. The higher priority the process is given, the more CPU time it will get. How much time on the processor each process gets is determined by the scheduling algorithm that is used, but in general, higher priority processes will get more CPU time. All this begs the question, what is process X’s priority and how can I set it?
When compiled with the –enable-pctl option, PHP gives you access to the pcntl_getpriority() function. This function is, well, obvious. It gets the priority of the current process, or if a PID (Process ID) is specified, that process instead.
1 2 3 4 5 | $priority = pcntl_getpriority(); echo "This process priority is: {$priority}."; $priority = pcntl_getpriority(1234); echo "Process 1234's priority is: {$priority}."; |
So that one’s pretty easy. Not much explaining to do there. But what about setting process priority? For that, we use pcntl_setpriority().
1 2 3 4 5 6 7 8 9 10 11 | if(pcntl_setpriority(-15)) { echo "Priority set successfully."; } else { echo "Failed to set priority."; } if(pcntl_setpriority(15, 1234)) { echo "Priority of process 1234 set successfully."; } else { echo "Failed to set priority of process 1234."; } |
The first parameter of this function is the priority. In most cases, this value can range between -20 and 20. The lower the number, the higher priority your process receives (counter-intuitive right?). These numbers can change though, so you may want to view the man page for your operating system’s setpriority(2) function. The second parameter is the process id. If left blank, it sets the current processes priority, otherwise it tries to set the priority of the PID that is given.
Signals
A signal is a limited form of inter-process communication used in Unix systems. Believe it or not, you probably use signals often when you are using a Linux box (CTRL+C anyone?). Signals can be a problem when you’re doing important work that you don’t want interrupted, so PHP allows us to install signal handlers so that we can handle these situations gracefully. To install a custom signal handler, you use the pcntl_signal() function. Calling this function isn’t as straight-forward as the others though. The first parameter is the signal which you are intercepting, which is an integer. Most of the signals are defined as constants(here), so that’s what we’ll be using. The second parameter is a callback function to handle the signal processing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | declare(ticks = 1); function signal_callback($signalNumber) { switch($signalNumber) { case SIGTERM: echo "Handling shutdown tasks."; exit; break; case SIGHUP: echo "Handling restart stuff."; break; default: echo "Handling all other signals."; } } pcntl_signal(SIGTERM, "signal_callback"); pcntl_signal(SIGHUP, "signal_callback"); |
The above code first declares our callback function, then has a simple switch statement to handle the different signals. After the function is constructed, we just call pcntl_signal() to set up the handler. But why would you want to handle signals in the first place? There are many reasons, but a good example would be financial data. Wouldn’t you want to make sure a transaction goes all the way through before the system dies? Or perhaps you want to send a message to someone if all transactions couldn’t be completed? It’s situations like these where handling signals makes sense.
Now that we know how to handle signals, there are all sorts of neat functions we can play with. If you interested in learning more, I suggest you check out the PHP documentation on the subject.
13 replies on “PHP Dark Arts: Multi-Processing (Part 2)”
[…] Read more: PHP Dark Arts: Multi-Processing (Part 2) | Re-Cycled Air […]
[…] Przeczytaj artykuł: PHP Dark Arts: Multi-Processing (Part 2) | Re-Cycled Air […]
[…] Thе second раrt οf thе PHP Dаrk Arts: Multi-Processing series, thіѕ post talks аbουt process priority аnd signal handling. View full post οn arts – Google Blog Search […]
[…] This post was mentioned on Twitter by marcelobernard, eBot and pok rap, Hacker News. Hacker News said: PHP Dark Arts: Multi-Processing (Part 2): http://bit.ly/aIj3vo Comments: http://bit.ly/b8YwCE […]
Amazing… sometimes this makes me think that PHP might be as good as ASP (Classic) but then I snap back to reality.
PHP definitely has it’s up and downs compared to ASP (classic). I used to do a bit of development with ASP and I can’t really say whether I like it better or not. What do you consider the make or break features that ASP has versus PHP?
please describe how to daemonize a process… or something useful. This is like taking 8th grade algebra at a community college.
Obnoxious quip aside, how to daemonize a process does sound like a good subject to write about. I’ll add it to my queue.
[…] here: PHP Dark Arts: Multi-Processing (Part 2) | Re-Cycled Air 分类: php 标签: php, process-priority, second-part 评论 (0) Trackbacks (0) […]
[…] Note: Part 2 of this post can be found here. […]
[…] my previous articles on using PHP for multi-process programming, we kept it very simple. By simple, I mean we […]
[…] order to follow the example, you’ll probably want to read up on multi-processing in PHP and using POSIX in PHP. Aside from that, keep an open mind. There are probably better […]
[…] PHP Dark Arts: Multi-Processing (Part 2) (Priority and Signals) […]