[PHP] about lock some codes.

2012-09-27 Thread lx
Hello:
   I have a question now.the code is:

$ftemp = fopen($fdoc_tmp/temp_proxy, 'w');
fwrite($ftemp, $content);
fclose($ftemp);

exec(/usr/local/bin/gdnsproxy -a -f $fdoc_tmp/temp_proxy
1$fdoc_tmp/dout 2$fdoc_tmp/derr, $data, $ex_result);
echo ret=.$ex_result;

As you see, If a process run these,this is right.But two processes run this
codes.
the order maybe is:
1.process 1 create the temp_proxy1
2.process 2 create the temp_proxy2
3.process 1 exec temp_proxy2
4.process 2 exec temp_proxy2

The temp_proxy1 can't exec.

So,I want to know how to solve it.
Thank you.


Re: [PHP] about lock some codes.

2012-09-27 Thread Maciek Sokolewicz

On 27-09-2012 11:31, lx wrote:

Hello:
I have a question now.the code is:

 $ftemp = fopen($fdoc_tmp/temp_proxy, 'w');
 fwrite($ftemp, $content);
 fclose($ftemp);

 exec(/usr/local/bin/gdnsproxy -a -f $fdoc_tmp/temp_proxy
1$fdoc_tmp/dout 2$fdoc_tmp/derr, $data, $ex_result);
 echo ret=.$ex_result;

As you see, If a process run these,this is right.But two processes run this
codes.
the order maybe is:
1.process 1 create the temp_proxy1
2.process 2 create the temp_proxy2
3.process 1 exec temp_proxy2
4.process 2 exec temp_proxy2

The temp_proxy1 can't exec.

So,I want to know how to solve it.
Thank you.



Well, there are basically 2 ways:
1. introduce locking; you can lock the file and have the other script 
check if the file is locked. If so  have it usleep() and check again.
2. If you can run gdnsproxy in parallel, you could instead change the 
script to make $fdoc_tmp/temp_proxy unique for that script. So instead 
of just writing, you first check if the file exists, if so, create a 
file with a different (non-existent) name and then try again. Otherwise, 
use the first name. Then pass the 'unique' name to gdnsproxy.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] about lock some codes.

2012-09-27 Thread Jim Giner

On 9/27/2012 7:05 AM, Maciek Sokolewicz wrote:

On 27-09-2012 11:31, lx wrote:

Hello:
I have a question now.the code is:

 $ftemp = fopen($fdoc_tmp/temp_proxy, 'w');
 fwrite($ftemp, $content);
 fclose($ftemp);

 exec(/usr/local/bin/gdnsproxy -a -f
$fdoc_tmp/temp_proxy
1$fdoc_tmp/dout 2$fdoc_tmp/derr, $data, $ex_result);
 echo ret=.$ex_result;

As you see, If a process run these,this is right.But two processes run
this
codes.
the order maybe is:
1.process 1 create the temp_proxy1
2.process 2 create the temp_proxy2
3.process 1 exec temp_proxy2
4.process 2 exec temp_proxy2

The temp_proxy1 can't exec.

So,I want to know how to solve it.
Thank you.



Well, there are basically 2 ways:
1. introduce locking; you can lock the file and have the other script
check if the file is locked. If so  have it usleep() and check again.
2. If you can run gdnsproxy in parallel, you could instead change the
script to make $fdoc_tmp/temp_proxy unique for that script. So instead
of just writing, you first check if the file exists, if so, create a
file with a different (non-existent) name and then try again. Otherwise,
use the first name. Then pass the 'unique' name to gdnsproxy.
Even simpler - use the 'tempnam' or 'tmpfile' functions to always have a 
unique name and avoid having to check for an existing file.


Amazing what the php Manual has in it...

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] about lock some codes.

2012-09-27 Thread Maciek Sokolewicz

On 27-09-2012 15:13, Jim Giner wrote:

Even simpler - use the 'tempnam' or 'tmpfile' functions to always have a
unique name and avoid having to check for an existing file.

Amazing what the php Manual has in it...


Good find, I completely forgot about the existence of those functions ;) 
It's been a long time since I've had to meddle with unique filenames


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php