fork download
  1. <?php
  2.  
  3. $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  4.  
  5. $pid = pcntl_fork();
  6. if ($pid == -1) {
  7. die('could not fork');
  8. } else if ($pid) {
  9. echo 'parent started', PHP_EOL;
  10. for ($i=0; $i != 10; ++$i) {
  11. usleep(10);
  12. echo '-';
  13. }
  14. pcntl_wait($status);
  15. echo 'all done', PHP_EOL;
  16. } else {
  17. echo 'child started', PHP_EOL;
  18.  
  19. foreach ([21, 25, 80] as $port) {
  20. echo "port($port) is ";
  21. echo @socket_connect ($sock, '127.0.0.1', $port) ? 'open' : 'closed';
  22. $error = socket_last_error ($sock);
  23. if ($error) echo ' (', socket_strerror($error), ')';
  24. echo PHP_EOL;
  25. }
  26.  
  27. echo 'child exiting', PHP_EOL;
  28. }
  29.  
Success #stdin #stdout 0.02s 20568KB
stdin
Standard input is empty
stdout
parent started
child started-
port(21) is -closed- (Network is unreachable)
-port(25) is closed (Network is unreachable-)
port(80) is closed (-Network is unreachable)
child exiting
----all done