$this->pid = pcntl_fork(); if($this->pid == -1) throw new Exception('Could not fork'); else if(!$this->pid) { //THIS IS THE CHILD PROCESS if(socket_write($sockets[0],str_pad("RUNNING", Threader::MESSAGE_LENGTH), Threader::MESSAGE_LENGTH) === false) throw new Exception("socket_write() failed. Reason: ". socket_strerror(socket_last_error($sockets[0]))); for($i = 0;$i < 100000;$i++) { if (socket_write($sockets[0],str_pad("UPTO $i\n", Threader::MESSAGE_LENGTH), Threader::MESSAGE_LENGTH) === false) throw new Exception("socket_write() failed. Reason: ". socket_strerror(socket_last_error($sockets[0]))); } if(socket_write($sockets[0],str_pad("STOP", Threader::MESSAGE_LENGTH), Threader::MESSAGE_LENGTH) === false) throw new Exception("socket_write() failed. Reason: ". socket_strerror(socket_last_error($sockets[0]))); socket_close($sockets[0]); }