From owner-freebsd-isp Thu Jan 28 00:52:18 1999 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id AAA10015 for freebsd-isp-outgoing; Thu, 28 Jan 1999 00:52:18 -0800 (PST) (envelope-from owner-freebsd-isp@FreeBSD.ORG) Received: from superior.mooseriver.com (superior.mooseriver.com [208.138.31.113]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id AAA10004 for ; Thu, 28 Jan 1999 00:52:16 -0800 (PST) (envelope-from jgrosch@superior.mooseriver.com) Received: (from jgrosch@localhost) by superior.mooseriver.com (8.8.8/8.8.8) id AAA16140; Thu, 28 Jan 1999 00:52:08 -0800 (PST) (envelope-from jgrosch) Message-ID: <19990128005208.A16025@mooseriver.com> Date: Thu, 28 Jan 1999 00:52:08 -0800 From: Josef Grosch To: xiyuan qian , freebsd-isp@FreeBSD.ORG Subject: Re: Must I lock the file? Reply-To: jgrosch@mooseriver.com References: <199901280752.PAA01443@dns.hgs.com.cn> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.91.1i In-Reply-To: <199901280752.PAA01443@dns.hgs.com.cn>; from xiyuan qian on Thu, Jan 28, 1999 at 03:52:52PM +0800 Sender: owner-freebsd-isp@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.org 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