Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > flock usage

Reply
Thread Tools

flock usage

 
 
Time Waster
Guest
Posts: n/a
 
      08-30-2007
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!
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      08-30-2007
(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) kst- <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"
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      08-30-2007
In article <y9IBi.11154$Eh5.6838@trndny06>,
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);


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
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-31-2007
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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-31-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
fcntl.flock() not working when called from a function thakadu Python 3 11-04-2005 08:36 PM
Flock (FireFox 1.5) Cool and fast. Old Gringo Firefox 5 10-24-2005 05:24 PM
flock() working mechanism.. Ckid C Programming 1 03-07-2005 11:49 AM
using flock for concurrency control Babu Perl 0 03-01-2005 04:18 PM
BIG BIRDS "FLOCK" TO THE 20D !!! Annika1980 Digital Photography 36 01-11-2005 10:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57