flock usage

T

Time Waster

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

Keith Thompson

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

Walter Roberson

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

CBFalconer

Time said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top