How do I lock a file? 
Author Message
 How do I lock a file?

I have extensive experience in a large number of languages but only limited
experience in C.  I think I am heading in the wrong direction, anyone
willing to point me in the right direction?

Background:
I have an app written in GCC on linux.  It maintains a list of names and
addresses.  A background task fires off a small applet that reads the file
(simple text file with fields separated by commas) and extracts the correct
name (and performs the required task for the person).  The task might be
fired many times a second, all day long.  A couple of times a day, I need to
update the file.  I don't mind multiple instances of apps reading but
obviously, write requires exclusive access.  The applets run in the
background and I generally don't care how long they have to wait.  I need to
block reading while writing, or looking at it from another angle...  No
writing while any app is reading that file.

I tried using the flock command but the 'read' applets were still able to
lock & read while the 'write' applet had the file locked.

I ended up using a hard lock function to make things work.  Although the
routine works, many read requests get queued and eventually catch up when
the busy period is over.  Ideally, the write app would wait until it had
exclusive access to the file.  The ReadApp would wait if the WriteApp had
exclusive else it would open the file with shared access (for multiple
ReadApps).

Is there a webpage that gives some examples of locking?

PS:  Although I am using variable length records, I would gladly go fixed
length if a binary mode was required.
PSS: I should also note that the current app works fine except for the fact
that requests start to build up during the busy periods and catch up later
during the lull.

--- here is a snippet of the hard lock - I would prefer soft locking ---
struct flock cbs_fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
cbs_fl.l_pid = getpid();
if (fcntl(fileno(cbs_lockfile), F_SETLKW, &cbs_fl) == -1)



Sat, 02 Nov 2002 03:00:00 GMT  
 How do I lock a file?
: I have extensive experience in a large number of languages but only limited
: experience in C.  I think I am heading in the wrong direction, anyone
: willing to point me in the right direction?

I can point you in the right direction.

: Background:
: I have an app written in GCC on linux.

Given the above background, I sugget you post your question to either
comp.unix.programmer or comp.os.linux.development.apps.  I am giving
you this redirection because comp.lang.c is a programming group
which deals with standard (ISO/ANSI) C.  As such, standard C has no
facilities for locking files.  To lock files, you need to use platform
specific programming interfaces.  As your code below indicates, you have
already found fnctl() which is the POSIX interface for locking files.
comp.unix.programmer is the homeground for POSIX fluent people.  If you
want to use non standard extensions, look at the man page for lockf()
and flock().  For more detail, post to the newsgroups I mentioned above.

: --- here is a snippet of the hard lock - I would prefer soft locking ---
: struct flock cbs_fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
: cbs_fl.l_pid = getpid();
: if (fcntl(fileno(cbs_lockfile), F_SETLKW, &cbs_fl) == -1)

Good Luck,

Paul

--
Paul D. Boyle

North Carolina State University
http://laue.chem.ncsu.edu/web/xray.welcome.html



Sat, 02 Nov 2002 03:00:00 GMT  
 How do I lock a file?
On Tue, 16 May 2000 12:34:38 -0400, "Mark Bramwell"

Quote:
> I have extensive experience in a large number of languages but only limited
> experience in C.  I think I am heading in the wrong direction, anyone
> willing to point me in the right direction?

> Background:
> I have an app written in GCC on linux.  It maintains a list of names and
> addresses.  A background task fires off a small applet that reads the file
> (simple text file with fields separated by commas) and extracts the correct
> name (and performs the required task for the person).  The task might be
> fired many times a second, all day long.  A couple of times a day, I need to
> update the file.  I don't mind multiple instances of apps reading but
> obviously, write requires exclusive access.  The applets run in the
> background and I generally don't care how long they have to wait.  I need to
> block reading while writing, or looking at it from another angle...  No
> writing while any app is reading that file.

> I tried using the flock command but the 'read' applets were still able to
> lock & read while the 'write' applet had the file locked.

There is nothing named flock() in the C language.

Quote:
> I ended up using a hard lock function to make things work.  Although the
> routine works, many read requests get queued and eventually catch up when
> the busy period is over.  Ideally, the write app would wait until it had
> exclusive access to the file.  The ReadApp would wait if the WriteApp had
> exclusive else it would open the file with shared access (for multiple
> ReadApps).

> Is there a webpage that gives some examples of locking?

Yes, the people in one of the .linux programming groups.  File locking
is an operating system feature, accessed by non-standard, compiler and
operating system extensions.  Such extensions are not part of the
language and thus off-topic here.

Quote:
> PS:  Although I am using variable length records, I would gladly go fixed
> length if a binary mode was required.
> PSS: I should also note that the current app works fine except for the fact
> that requests start to build up during the busy periods and catch up later
> during the lull.

> --- here is a snippet of the hard lock - I would prefer soft locking ---
> struct flock cbs_fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
> cbs_fl.l_pid = getpid();
> if (fcntl(fileno(cbs_lockfile), F_SETLKW, &cbs_fl) == -1)

No such thing as a struct flock, or functions named fcntl() or
fileno() in the C language or its library at all.  You need to ask
this in a Linux programming group.

Jack Klein
--
Home: http://jackklein.home.att.net



Sun, 03 Nov 2002 03:00:00 GMT  
 How do I lock a file?
As others have pointed out, this is not really an ANSI-C question, and
so should be directed to the Unix/Linux groups.

<ot>
However, before you get too deep into file locking issues, you may wish
to consider using the plain Unix filesystem semantics to take care of
your problem.  It sounds to me like you have many readers, but only one
writer at a time, so that you don't actually need to arbitrate write
accesses (which is somewhat more difficult), you just want to make sure
that no reader gets inconsistent data (i.e., a file that changes
underneath it while the reader is trying to read it, akin to a "volatile"
variable in C).  I am no expert, but I believe you can obtain this
result simply by writing all changes to a temp file, and then moving
(renaming) that file onto the existing real filename.  The Unix fs will
ensure that any particular reader sees either the old file in its
entirety, or the new one in its entirety.  Or at least, I *think* it will.
No doubt I will be copiously "corrected" if I'm missing something here....
</ot>

--ben


Quote:

>I have extensive experience in a large number of languages but only limited
>experience in C.  I think I am heading in the wrong direction, anyone
>willing to point me in the right direction?

>Background:
>I have an app written in GCC on linux.  It maintains a list of names and
>addresses.  A background task fires off a small applet that reads the file
>(simple text file with fields separated by commas) and extracts the correct
>name (and performs the required task for the person).  The task might be
>fired many times a second, all day long.  A couple of times a day, I need to
>update the file.  I don't mind multiple instances of apps reading but
>obviously, write requires exclusive access.  The applets run in the
>background and I generally don't care how long they have to wait.  I need to
>block reading while writing, or looking at it from another angle...  No
>writing while any app is reading that file.

>I tried using the flock command but the 'read' applets were still able to
>lock & read while the 'write' applet had the file locked.

>I ended up using a hard lock function to make things work.  Although the
>routine works, many read requests get queued and eventually catch up when
>the busy period is over.  Ideally, the write app would wait until it had
>exclusive access to the file.  The ReadApp would wait if the WriteApp had
>exclusive else it would open the file with shared access (for multiple
>ReadApps).

>Is there a webpage that gives some examples of locking?

>PS:  Although I am using variable length records, I would gladly go fixed
>length if a binary mode was required.
>PSS: I should also note that the current app works fine except for the fact
>that requests start to build up during the busy periods and catch up later
>during the lull.

>--- here is a snippet of the hard lock - I would prefer soft locking ---
>struct flock cbs_fl = { F_WRLCK, SEEK_SET, 0, 0, 0 };
>cbs_fl.l_pid = getpid();
>if (fcntl(fileno(cbs_lockfile), F_SETLKW, &cbs_fl) == -1)



Sun, 24 Nov 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Computer locks up when doing RasDial()

2. num lock, cap lock, and scroll lock

3. Caps Lock / Num Lock / Scroll Lock

4. Opening an Image file without locking the file

5. file locking in db file

6. Lock file or Unlock file ?

7. Lock Statement behavior with other objects within the locked object, and enumerators

8. passing a file pointer - How is it done.

9. How is end-of-file in Windows done?

10. pessimistic locking --locks disappear with 2 RecordSets

11. How to remove garbage characters at the end when doing file.read()

12. Doing #include in header files ??

 

 
Powered by phpBB® Forum Software