From owner-freebsd-questions Tue Jan 27 23:21:41 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id XAA09627 for questions-outgoing; Tue, 27 Jan 1998 23:21:41 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from allegro.lemis.com (allegro.lemis.com [192.109.197.134]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id XAA09616 for ; Tue, 27 Jan 1998 23:21:33 -0800 (PST) (envelope-from grog@lemis.com) Received: from freebie.lemis.com (freebie.lemis.com [192.109.197.137]) by allegro.lemis.com (8.8.7/8.8.5) with ESMTP id RAA22010; Wed, 28 Jan 1998 17:51:11 +1030 (CST) Received: (from grog@localhost) by freebie.lemis.com (8.8.8/8.8.7) id RAA18287; Wed, 28 Jan 1998 17:50:54 +1030 (CST) (envelope-from grog) Message-ID: <19980128175054.16073@lemis.com> Date: Wed, 28 Jan 1998 17:50:54 +1030 From: Greg Lehey To: grobin@accessv.com Cc: questions@FreeBSD.ORG Subject: Re: How Do You Lock File in UNIX? References: <34CC3209.55603446@accessv.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.84e In-Reply-To: <34CC3209.55603446@accessv.com>; from Geoffrey Robinson on Mon, Jan 26, 1998 at 01:49:45AM -0500 Organization: LEMIS, PO Box 460, Echunga SA 5153, Australia Phone: +61-8-8388-8286 Fax: +61-8-8388-8725 Mobile: +61-41-739-7062 WWW-Home-Page: http://www.lemis.com/~grog Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk On Mon, Jan 26, 1998 at 01:49:45AM -0500, Geoffrey Robinson wrote: > I'm working on a CGI program in C that will be running very frequently > and will undoubtedly corrupt its data files if I don't lock them. I > discovered the flock command, which I think will do what I need but I've > never dealt with file locking in UNIX before and it uses terminology I'm > not familiar with like file descriptor and shared vs. exclusive locks so > I'm not quite sure how to use it. I simply want to be able to lock a > file for > either reading, writing or both but I'll settle for just both if that's > the only option. > > Could somebody please explain how to properly lock a file and give me an > example, it would be a big help. Use fcntl locking. Stevens describes it in "Advanced Programming in the UNIX Environment". Greg