Running the code below yields different results on OSX and Ubuntu, and I'd like to know more about what happens, why one or the other way might be implemented, and if there could be other differences on other platforms etc.
When sending signals (e.g. SIGUSR1, SIGINT) to the process while it is reading from socket in blocking mode
OSX: remains blocked the entire specified timeout.
Ubuntu: immediately aborts socket read and resumes program execution with
PHP Warning: socket_recvfrom(): unable to recvfrom [4]: Interrupted system call on line 161
Is one way considered correct and the other incorrect?
Why does behaviour differ? Is it because there are different implementations of sockets (and if so, could the socket implementations even be different on two systems of the same kind)? Or do the kernels have different approaches in dealing with process signals?
I personally find Ubuntu's handling superior, because that allows me to use high timeouts, which means the process will use no resources until either the socket recieves data or the process recieves a signal. On OSX, I cannot use more than a one-second timeout, because execution must resume in order for pending signals to be handled.
Can I trust Ubuntu to keep doing things the same way? Do other systems handle things in other ways than those described here?
And in case the implementing code is relevant…
Socket is created and set to a long enough timeout to notice what happens
$this->socketRecieveTimeout = 10;
$this->socketReceive = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_set_option(
$this->socketReceive,
SOL_SOCKET,
SO_RCVTIMEO,
['sec' => $this->socketRecieveTimeout, 'usec' => 0]
);
Main program loop
while (true) {
pcntl_signal_dispatch();
$bytes = socket_recvfrom($this->socketReceive, $buffer, $bufferSize, 0, $name, $this->port);
usleep($sleepTime);
}