By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

Blocking with fopen when file is already open for writing

P: n/a
Hi,
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.

If not how can I change php behavior to block such access?

Thank you,
Efi

Mar 3 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
>I created a very simple application that reads and writes to a file, I
>was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.
You can pretty much count on PHP *not* automatically doing this for
you. For one thing, there are safe ways of two processes both
having the same file open for write and *not* clobbering each other
(e.g. each writes only to its own pre-existing record) that PHP
shouldn't prohibit it.
>If not how can I change php behavior to block such access?
All users of the file can use flock().
Mar 3 '07 #2

P: n/a
Efi Merdler wrote:
Hi,
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.
What happenned when you tested it?

Here's a hint:
<?php
print "ready to start\n";
flush();
$fh=fopen('/tmp/test','w');
if (is_resource($fh)) {
print "opened...\n";
flush();
sleep(30);
print "Exiting\n";
fclose($fh);
} else {
print "failed to open\n";
}
?>

(if you run two instances of this script, the second will do one of three
things - you've only considered two possibilities so far)
If not how can I change php behavior to block such access?
flock()

C.

Mar 3 '07 #3

P: n/a

Colin McKinnon wrote:
Efi Merdler wrote:
Hi,
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.

What happenned when you tested it?

Here's a hint:
<?php
print "ready to start\n";
flush();
$fh=fopen('/tmp/test','w');
if (is_resource($fh)) {
print "opened...\n";
flush();
sleep(30);
print "Exiting\n";
fclose($fh);
} else {
print "failed to open\n";
}
?>

(if you run two instances of this script, the second will do one of three
things - you've only considered two possibilities so far)
If not how can I change php behavior to block such access?

flock()

C.
Thanks,
I'll probably use a semaphore in order achieve this block behavior.

Efi

Mar 4 '07 #4

P: n/a
On Mar 4, 12:21 am, Colin McKinnon
<colin.thisisnotmysurn...@ntlworld.deletemeunlessU RaBot.comwrote:

I think that Windows will lock file exclusively, by default. UNIX and
similar systems will
not, by default.

DG

Mar 4 '07 #5

P: n/a
NC
On Mar 3, 5:06 am, "Efi Merdler" <foo...@gmail.comwrote:
>
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.
Nothing of the sort. You need to expressly lock the file using
flock(),
which, alas, will not help you on Windows due to Windows' thread
model.

Cheers,
NC

Mar 4 '07 #6

P: n/a
NC
On Mar 4, 6:15 am, "Drazen Gemic" <trench...@yahoo.comwrote:
>
I think that Windows will lock file exclusively, by default.
Maybe, but since all instances of the script will run under the same
process ID (although they may have different thread IDs), it's not
going to help; the file will remain writeable to all instances of the
script.

Cheers,
NC

Mar 5 '07 #7

P: n/a

Colin McKinnon wrote:
Efi Merdler wrote:
Hi,
I created a very simple application that reads and writes to a file, I
was wondering if fopen when opening a file for write access blocks
this file, i.e. when another user runs this script the script will
halt until the first user closes its file handle.

What happenned when you tested it?

Here's a hint:
<?php
print "ready to start\n";
flush();
$fh=fopen('/tmp/test','w');
if (is_resource($fh)) {
print "opened...\n";
flush();
sleep(30);
print "Exiting\n";
fclose($fh);
} else {
print "failed to open\n";
}
?>

(if you run two instances of this script, the second will do one of three
things - you've only considered two possibilities so far)
If not how can I change php behavior to block such access?

flock()

C.
Hi,
Thanks for the hint.
I tried to use a semaphore, however my php was not compiled with the
right flag, so I'm left with flock, I tested its behavior using your
scripts, it works as expected.

Efi

Mar 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.