Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 28 Jan 1999 00:52:08 -0800
From:      Josef Grosch <jgrosch@mooseriver.com>
To:        xiyuan qian <xiyuan@dns.hgs.com.cn>, freebsd-isp@FreeBSD.ORG
Subject:   Re: Must I lock the file?
Message-ID:  <19990128005208.A16025@mooseriver.com>
In-Reply-To: <199901280752.PAA01443@dns.hgs.com.cn>; from xiyuan qian on Thu, Jan 28, 1999 at 03:52:52PM %2B0800
References:  <199901280752.PAA01443@dns.hgs.com.cn>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Jan 28, 1999 at 03:52:52PM +0800, xiyuan qian wrote:
> 
> Hi, there is a process running on my box which will open and write a file. 
> Now, I run another program to write some data to this file. Must I first 
> lock the file? Is there any method to append it without problem? With 
> fopen( ,"a+") will work?

When you say that there is a process running on your box I assume that this
program is one that you wrote yourself. In order for two or more processes
to write to the same file and not step on each other and get the data out
of "order" it is necessary to coordinate between the processes. There are a
number of ways to do this. One can use flock, semaphores, or write a daemon
to handling writing to the file and all the processes that want to write to
that file send their data to the daemon. An fopen with an "a+" just opens
an existing file and sets the data pointer to the end of the file. It does not
prevent another process from also opening that file and writing to it. 

I suggest that you read and understand the man pages for flock and fopen,
and read through "Unix Network Programming Vol. 2" by Richard W. Stevens. I
think the section you would want is section 3.


Josef 

-- 
Josef Grosch           | Another day closer to a |    FreeBSD 3.0
jgrosch@MooseRiver.com |   Micro$oft free world  | UNIX for the masses


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-isp" in the body of the message



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990128005208.A16025>