Unix Programming - File access question

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > January 2004 > File access question





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author File access question
Bigdakine

2004-01-23, 5:16 pm

I'm somewhat new to Unix system programming.

It appears I have a need for two processes to write data to the same file.

What is the best way to lock the file while it is being written to, so that the
other process can detect it and won't also attempt to write and botch the file?

Stuart
Jens.Toerring@physik.fu-berlin.de

2004-01-23, 5:16 pm

Bigdakine <bigdakine@aol.comgetagrip> wrote:
quote:

> I'm somewhat new to Unix system programming.


quote:

> It appears I have a need for two processes to write data to the same file.


quote:

> What is the best way to lock the file while it is being written to, so that
> the other process can detect it and won't also attempt to write and botch
> the file?



There are two locking methods, either flock(2) or locking using
fcntl(2). If you just need a lock on the whole file and the file
isn't on a NFS mounted file system, flock(2) might be easier to
use, while fcntl(2) locking is a bit more complicated to implement
but works under more circumstances and also allows locks on parts
of the file.

Of course, the are also other methods you could use like having a
lock file, i.e. a special file that the process creates when it
needs exclusive access to the file and deletes afterwards, using
a semaphore etc, but using flock() or fcntl() is probably the most
straightforward approach.
Regards, Jens
--
\ Jens Thoms Toerring ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.physik.fu-berlin.de/~toerring
Rich Teer

2004-01-23, 5:16 pm

On Sat, 6 Dec 2003, Bigdakine wrote:
quote:

> I'm somewhat new to Unix system programming.
>
> It appears I have a need for two processes to write data to the same file.
>
> What is the best way to lock the file while it is being written to, so that the
> other process can detect it and won't also attempt to write and botch the file?



In addition to Jans answer, if your just dumping data into the
file from the two processes, but not trying to seek it, using
the O_APPEND flag in both processes might also give you acceptable
results.

--
Rich Teer, SCNA, SCSA . * * . * .* .
. * . .*
President, * . . /\ ( . . *
Rite Online Inc. . . / .\ . * .
.*. / * \ . .
. /* o \ .
Voice: +1 (250) 979-1638 * '''||''' .
URL: http://www.rite-online.net ******************
Bigdakine

2004-01-23, 5:16 pm

>Subject: Re: File access question
quote:

>From: Rich Teer rich.teer@rite-group.com
>Date: 12/6/03 12:36 PM Hawaiian Standard Time
>Message-id: <Pine.SOL.4.58.0312061434390.24728@zaphod.rite-group.com>
>
>On Sat, 6 Dec 2003, Bigdakine wrote:
>
>the
>file?
>
>In addition to Jans answer, if your just dumping data into the
>file from the two processes, but not trying to seek it, using
>the O_APPEND flag in both processes might also give you acceptable
>results.
>



Thanks fellas...

Stuart
Dr. Stuart A. Weinstein
Ewa Beach Institute of Tectonics
"To err is human, but to really foul things up
requires a creationist"
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com