473,326 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Blocking with fopen when file is already open for writing

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
7 4635
>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
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

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
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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Dave O'Hearn | last post by:
I want to open a file for both reading and writing, but when I do "ios::in|ios::out" with an fstream, it creates the file if it doesn't already exist. I don't like that last part. C's fopen has the...
14
by: Aaron Couts | last post by:
I have a program that writes to a log file. It's compiled on RH Linux 7.3 (Kernel 2.4.18-18.7). It's using fopen in append mode. When the file reaches 51200000 bytes in size, the program will no...
4
by: Twiggy182 | last post by:
Hi, I tried to run the following C code but it get stuck on the "fopen()" commande. Is there someone that can help me with that? ============================= FILE *dps=NULL; if...
3
by: Patrice | last post by:
Hi, I would to call fopen function several time in my application. This application permits to read files which path is registered in a configuration file. For exemple: File 1 = toto.txt File 2...
13
by: Blue | last post by:
Hi , Can any one please let me explain me the diffrences between "open"/ "fopen" or "read"/"fread" or "write/fwrite". I know that "open" /"read" / "write" are system calls and "fopen"...
10
by: Longfellow | last post by:
Newbie here... My reading of the description of fopen() led me to expect that, with mode as "w", it would create a file if it did not exist. Checked the FAQ and did not see this question...
3
by: acorn71 | last post by:
hello, i just recently started getting permission errors when writing to a file that's been working fine for a couple weeks now. i have made no changes to the file or folder permission settings and...
5
by: eyalc1978 | last post by:
Hi Does someone knows if fopen() is a non-blocking function in a sense that if there isn't sufficient disk space to open a file in write mode, then the function will not wait but fail? I find...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.