Categories
PHP Programming

PHP Dark Arts: Multi-Processing (Part 2)

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.