From owner-freebsd-questions Wed Jan 28 08:40:39 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id IAA24330 for questions-outgoing; Wed, 28 Jan 1998 08:40:39 -0800 (PST) (envelope-from owner-freebsd-questions@FreeBSD.ORG) Received: from cerberus.partsnow.com (gatekeeper.partsnow.com [207.155.26.98]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id IAA24323 for ; Wed, 28 Jan 1998 08:40:29 -0800 (PST) (envelope-from don@partsnow.com) Received: (from bin@localhost) by cerberus.partsnow.com (8.8.5/8.6.9) id IAA06404; Wed, 28 Jan 1998 08:40:44 -0800 (PST) X-Authentication-Warning: cerberus.partsnow.com: bin set sender to using -f Received: from wildeweb(192.168.100.10) by cerberus.partsnow.com via smap (V2.0) id xma006402; Wed, 28 Jan 98 08:40:25 -0800 Message-ID: <34CF5F28.569EFFC4@partsnow.com> Date: Wed, 28 Jan 1998 08:39:04 -0800 From: Don Wilde Reply-To: don@partsnow.com Organization: Soligen, Incorporated X-Mailer: Mozilla 4.04 [en] (X11; I; FreeBSD 2.2.5-RELEASE i386) MIME-Version: 1.0 To: grobin@accessv.com, questions@FreeBSD.ORG Subject: Re: How Do You Lock File in UNIX? Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk a simpler way is to create a file called .lock.filename, and delete it when you leave. Your read and write routines will test for this file. Here's some old Perl code (written prior to the P5 flock modules becoming common) which shows the essence. I recommend the use of the modules instead if you use P5. -- oooOOO O O O o * * * * * * o ___ _________ _________ ________ _________ _________ ___==_ V_=_=_DW ===--- Don Wilde [don@PartsNow.com] [http://www.PartsNow.com ] /oo0000oo-oo--oo-ooo---ooo-ooo---ooo-ooo--ooo-ooo---ooo-ooo---ooo-oo--oo  require "html.wilde"; package FILE; sub append { local($file,@newtext) = @_; &lock($file); open(FILE,">>$file") || &HTML'error("FILE_APPEND: $file"); print FILE join("\n",@newtext), "\n\n"; &unlock($file); } sub backup { local($path,$file) = &dirbase($_[0]); chdir($path) || &HTML'error("FILE_BACK: CHDIR_$path"); #' &lock("$_[0]"); unlink("$file.bak"); link($file,"$file.bak") || &HTML'error("FILE_BAK: $file"); &unlock("$_[0]"); } sub lock { local($path,$file) = &dirbase($_[0]); chdir($path) || &HTML'error("2_$path:$_[0]"); local($i) = 0; while ((-e ".lock.$file") && ( $i < 6 )) { sleep(1); $i++; if ($debugging) {print "locked board\n"} } (-e ".lock.$file") && (&HTML'error("3:.lock.$file")); open(LOCK,">.lock.$file") || &HTML'error("6: $path.lock.$file"); close LOCK; } sub unlock { local($path,$file) = &dirbase($_[0]); chdir($path) || &HTML'error(2); #' #chmod(0666,"$file") || print "Couldn't chmod $file\n"; unlink(".lock.$file"); } sub dirbase { $_[0] =~ m%^(/(([^/]*)/)*)([^/]*)%; local($path,$file) = ($1,$4); ($path,$file); } sub last_mod { # returns 12/25/94 for a file last modified on Xmas, 1994 local($mday,$mon,$year) = (localtime((stat("$_[0]"))[9]))[3..5]; $mon++; return("$mon/$mday/$year"); } sub created { # returns 12/25/94 for a file created on Xmas, 1994 local($mday,$mon,$year) = (localtime((stat("$_[0]"))[10]))[3..5]; $mon++; return("$mon/$mday/$year"); } 1;