Heya all,
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
My program needs to open files and work on it, and I want it to make
sure that the file is not being written to (e.g. by scp, cp, mv etc)
before I attampt to open it. Is there any function to see the status of
the file ? Using/implementing locking mechanism is out of the question.
If not provided by the language, is there anything provided by the Unix
operatins system ?
Shilpa 18 7394 sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman sh************@gmail.com wrote:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
As Ben noted, this is best answered on comp.unix.programmer. However,
in C you can use the Shel Silverstein approach, similar to his
algorithm for determining whether a window is open:
#include <stdio.h>
int main(void)
{
FILE *fp;
if( (fp=fopen("foo.txt")) != NULL ) {
/* It wasn't open! Let's try another one! */
fclose( fp );
if( (fp=fopen("bar.txt")) != NULL ) {
/* Woohoo! */
fclose( fp );
}
else {
/* CRASH! Guess that one was... Let's try another one! */
/* ... */
}
}
return 0;
}
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
I think this question qualifies to be put in here.
Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
sh************@gmail.com wrote:
Heya all,
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
My program needs to open files and work on it, and I want it to make
sure that the file is not being written to (e.g. by scp, cp, mv etc)
before I attampt to open it. Is there any function to see the status of
the file ? Using/implementing locking mechanism is out of the question.
If not provided by the language, is there anything provided by the Unix
operatins system ?
Shilpa
Unix doesnt provide exclusive file locking. There are addon file lock
kludges but of course those would require every program to use the
kludges, and none do.
The easiest way is to not try reading the file directly, but instead
require that the providing program or batch file write the file
elsewhere, then when its done, rename the file into your special
directory. So if the file shows up, it's guarantreed to be whole.
If you want a standard C answer, which is all you can expect to
get comp.lang.c, then the answer is "You can't." Hope that
helps.
"BaG" <bi***********@gmail.comwrites:
I think this question qualifies to be put in here.
Ben Pfaff wrote:
>sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer. -- "A lesson for us all: Even in trivia there are traps." --Eric Sosman
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Christopher Benson-Manica schrieb:
sh************@gmail.com wrote:
>>How do I tell, prgrammatically, if a file is opened for read/write by some other process? In unix .. of course.
As Ben noted, this is best answered on comp.unix.programmer. However,
in C you can use the Shel Silverstein approach, similar to his
algorithm for determining whether a window is open:
#include <stdio.h>
int main(void)
{
FILE *fp;
if( (fp=fopen("foo.txt")) != NULL ) {
You forgot the "mode" argument...
/* It wasn't open! Let's try another one! */
fclose( fp );
if( (fp=fopen("bar.txt")) != NULL ) {
/* Woohoo! */
fclose( fp );
}
else {
/* CRASH! Guess that one was... Let's try another one! */
/* ... */
}
}
return 0;
}
I am not sure whether you mean that seriously.
fopen() returning NULL on an already opened file is not
guaranteed and neither is the converse.
Could you please clarify what you mean?
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
BaG wrote:
I think this question qualifies to be put in here.
Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for
read/write by some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
--
BaG wrote:
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Text rearranged.
Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for
read/write by some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
I think this question qualifies to be put in here.
That's wrong. This group deals with ISO standard C. There is no way to
do that in ISO standard C. It's platform dependent, so a newsgroup
dedicated to the platform is needed. The problem specified UNIX, so a
the newsgroup Ben gave was the right one.
Brian
"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote:
>How do I tell, prgrammatically, if a file is opened for read/write by some other process? In unix .. of course.
[...]
>
Unix doesnt provide exclusive file locking.
[...]
That turns out not to be correct, making this an excellent example of
why we try not to answer off-topic questions here. It doesn't address
the question; the OP asked how to tell whether a file is opened by
another process, not how to implement a lock.
Try comp.unix.programmer.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Michael Mair <Mi**********@invalid.invalidwrote:
(W.R.T. the Shel Silverstein "file open" algorithm...)
You forgot the "mode" argument...
Argh!
I am not sure whether you mean that seriously.
Well, I was hoping it would be obvious that mentioning Shel
Silverstein implied <smiletags :-)
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Keith Thompson wrote:
"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
[...]
Unix doesnt provide exclusive file locking.
[...]
That turns out not to be correct,...
Taken out of context, you are correct. But in the context of the guy's
full question, my answer *is* exactly correct. The system commands
and the standard C file library on Unix NEVER call the optional file
locking API's.
But you are correct inasmuch as file locking questions don't fully
match the ( somewhat narrow, de-facto) baliwick of this newsgroup.
Christopher Benson-Manica schrieb:
Michael Mair <Mi**********@invalid.invalidwrote:
>>I am not sure whether you mean that seriously.
Well, I was hoping it would be obvious that mentioning Shel
Silverstein implied <smiletags :-)
I read this name the first time in your message, and http://en.wikipedia.org/wiki/Shel_Silverstein
did not really indicate what to think about this
reference... :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Michael Mair <Mi**********@invalid.invalidwrote:
I read this name the first time in your message, and http://en.wikipedia.org/wiki/Shel_Silverstein
did not really indicate what to think about this
reference... :-)
Well, the Shel Silverstein algorithm for determining whether a window
is open is given in one of his poems, "Stone Telling":
How do we tell if a window is open?
Just throw a stone at it.
Does it make a noise?
It doesn't?
Well, it was open.
Now, let's try another ...
CRASH!
Hell, that one wasn't!
Let's try another!
Using this approach to determining whether files are open or not is
not recommended :-)
--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
"Ancient_Hacker" <gr**@comcast.netwrites:
Keith Thompson wrote:
>"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote: How do I tell, prgrammatically, if a file is opened for read/write by some other process? In unix .. of course.
[...]
>
Unix doesnt provide exclusive file locking.
[...]
That turns out not to be correct,...
Taken out of context, you are correct. But in the context of the guy's
full question, my answer *is* exactly correct. The system commands
and the standard C file library on Unix NEVER call the optional file
locking API's.
But you are correct inasmuch as file locking questions don't fully
match the ( somewhat narrow, de-facto) baliwick of this newsgroup.
<OT>
My response was based on a very quick reading of the lockf(3) man
page. It appears that I misunderstood it, and I believe you're
correct.
</OT>
Ironically, this reinforces my point about off-topic answers. 8-)}
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Christopher Benson-Manica schrieb:
Michael Mair <Mi**********@invalid.invalidwrote:
>>I read this name the first time in your message, and http://en.wikipedia.org/wiki/Shel_Silverstein did not really indicate what to think about this reference... :-)
Well, the Shel Silverstein algorithm for determining whether a window
is open is given in one of his poems, "Stone Telling":
How do we tell if a window is open?
Just throw a stone at it.
Does it make a noise?
It doesn't?
Well, it was open.
Now, let's try another ...
CRASH!
Hell, that one wasn't!
Let's try another!
Using this approach to determining whether files are open or not is
not recommended :-)
Ah, thank you :-)
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. This discussion thread is closed Replies have been disabled for this discussion. Similar topics
8 posts
views
Thread by Peter Abel |
last post: by
|
3 posts
views
Thread by Pernell Williams |
last post: by
|
reply
views
Thread by Prabhu Ramachandran |
last post: by
|
2 posts
views
Thread by Daniel Reber |
last post: by
|
8 posts
views
Thread by pamelafluente |
last post: by
|
1 post
views
Thread by Mike.Duffy |
last post: by
| |
7 posts
views
Thread by lawrence k |
last post: by
|
3 posts
views
Thread by =?Utf-8?B?Um9nZWxpbw==?= |
last post: by
| | | | | | | | | | |