Allan Lohse
Allan Lohse

Reputation: 23

PHP fork, and get result from child

I need a routine to make 2 or more dns requests at the same time, but only use the first/fastest result.

How do i return the answar from the child's so i only get one result ?

$pid = pcntl_fork();

if ($pid == -1) die("could not fork");

if ($pid) {

  //parent

} else { 

  $pid2 = pcntl_fork(); 

  if ($pid2 == -1) die("could not fork"); 

  if ($pid2) {

    //parent-child

    exec("/usr/bin/dig @8.8.8.8 $name A +short +time=1+tries=1",$ips_array,$digreturn);

  } else {

    exec("/usr/bin/dig @8.8.4.4 $name A +short +time=1 +tries=1",$ips_array,$digreturn);

  }

}

print_r ($ips_array);

This will return 2 times the answer, if both requests are returned

Upvotes: 1

Views: 506

Answers (1)

user3074234
user3074234

Reputation: 111

I suggest a look at socket_create_pair().

In the PHP manual is a very short & easy example of interprocess communication (IPC) between a fork()-parent and the child.

Abd using serialize() und unserialize() You could even transfer complex data types like arrays...

Upvotes: 1

Related Questions