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

flock usage

P: n/a
Is this a stupid use of flock:

FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);

Does this accomplish real locking, or just narrow down the
race quite a bit? (Race existing between the unlock and the
fclose(), i guess.)

Normally I would think you'd want a separate file to do
nothing but the locking, and guard the use of the real data
file with locks on the lockfile.

Also, assuming the above is stupid, is the following a wee
bit smarter:

FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
fflush(fp); <<---- at least try to make
fdatasync(fp); <<---- sure contents out before unlock
flock(fileno(fp),LOCK_UN);
fclose(fp);

(Or does this add very little?)
TIA!
Aug 30 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
bf*@fenway.UUCP (Time Waster) writes:
Is this a stupid use of flock:

FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);
[snip]

This is not a good place to ask. flock() is not a standard C
function; in fact, standard C provides no facility for locking files.

comp.unix.programmer is a better place to ask about this -- but since
flock() isn't defined by the POSIX standard either, they might advise
you to use lockf() instead.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 30 '07 #2

P: n/a
In article <y9IBi.11154$Eh5.6838@trndny06>,
Time Waster <no***@nowhere.comwrote:
>Is this a stupid use of flock:
>FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);
flock() is not defined by the C language; it's properties are
OS specific; for example, there are important differences
between flock() for BSD or System V Unices.

flock() isn't even defined by POSIX.1. If you were intending to
use extensions, you could at least use extensions defined by
the POSIX.1 standard, such as using the POSIX fcntl() with
F_SETLK.

The most obvious stupidity in the code is that it doesn't
check the return values from flock(), so it will go ahead
and scribble on the file if a lock is denied. Not checking
that the open worked is stupid too.
You would probably have better success discussing this in
a newsgroup more specific to the variety of operating system
you are targetting.
--
All is vanity. -- Ecclesiastes
Aug 30 '07 #3

P: n/a
Time Waster wrote:
>
Is this a stupid use of flock:

FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);
Maybe you should be asking this somewhere control of herds of birds
is topical. Standard C contains no such routine as 'flock', which
is thus off-topic on c.l.c.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Aug 31 '07 #4

P: n/a
CBFalconer wrote:
>
Time Waster wrote:

Is this a stupid use of flock:

FILE *fp=fopen(SOME_FILE_CONSTANT,"r+");
flock(fileno(fp),LOCK_EX);
something important here, including reads and a write to fp
flock(fileno(fp),LOCK_UN);
fclose(fp);

Maybe you should be asking this somewhere control of herds of birds
is topical.
Flocks can contain either feathers or wool.

--
pete
Aug 31 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.