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

multiple open fd's on open(file)

P: n/a
Hi,

When you open a file for writing/appending with open() or fopen(), and
you have multiple applications that might want to write to the same file
at the same time, could that cause weirdness in the file ?

do i have to add manual file locking, or does open()/fopen() provide
file locking ?

Thanks,
Corne'

Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
> When you open a file for writing/appending with open() or
fopen(), and you have multiple applications that might want to
write to the same file at the same time, could that cause
weirdness in the file ?


This question sounds off topic for this group.

Have you asked in comp.unix.programmer?

Nov 13 '05 #2

P: n/a
Corne' Cornelius <co***@no-domain-no-spam.com> wrote:
When you open a file for writing/appending with open() or fopen(), and
you have multiple applications that might want to write to the same file
at the same time, could that cause weirdness in the file ?
First of all, open() isn't a standard C function, so you won't get
an answer here about its behavior, the question can only be answered
in the context of your system that might or might not implement an
open() function. And for fopen() the answer is also simple: since
standard C doesn't doesn't know or care about other processes
running concurrently, there's no requirement that any kind of file
locking is implemented for fopen().
do i have to add manual file locking, or does open()/fopen() provide
file locking ?


Unless your implementation of the C standard library does file locking
for you (which I would rather doubt) you do have to do it yourself.
Otherwise, as you guess, you might end up with the output of the
different applications mixed up in the file in possibly completely
unpredictable ways - but that's of course again depending of how your
operating systems schedules the processes, which isn't something C
is (or should be) concerned with.

Probably it would also be quite a stupid idea to automatically do
file locking when a file is opened with fopen() because it would
keep you from implementing your own scheme - you might actually
want to have two applications write to the same file, using your
own method to keep the applications from writing in uncoordinated
ways to the file, e.g. in a situation where simple file locking
wouldn't be fine grained enough.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.