Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!tut.cis.ohio-state.edu!purdue!mentor.cc.purdue.edu!pur-ee!pur-phy!sawmill!rjk From: rjk@sawmill.UUCP (Richard Kuhns) Newsgroups: comp.unix.questions Subject: truncating an open (locked) file Keywords: truncate portable Message-ID: <269@sawmill.UUCP> Date: 17 May 89 15:22:12 GMT Distribution: comp Organization: Grauel Enterprises, Laf. IN Lines: 35 In an earlier article, I asked for a portable way to truncate (to size 0) an open file, in the following situation: open the file (for writing); try to lock the file exclusively; if (I can't) tell user someone else is running this program; exit; endif truncate the file (size 0); <--- how do I do this? start writing; I received several responses (which I greatly appreciate), which basically said the same thing -- truncate it by open(2)ing it a second time with the O_TRUNC flag set, and then close the excess file descriptor. This works fine (and is the method I'm currently using), with the following proviso: Don't close(2) the second file descriptor! If you close *either* file descriptor, the lock goes away. At least, it does on a 3B1 running 3.51 -- is this behaviour standard? For this particular application the extra baggage hasn't been a problem, but with several database applications we're running awfully close to the edge (18 active file descriptors), and I can't afford to waste one if there's a way around it. So now, I have a slight variation of my original question: Does anyone have a portable way to truncate (to size 0) an open file, which both maintains an existing lock on the file and doesn't waste a file descriptor? Once again, Thanks in Advance... Rich Kuhns pur-phy!sawmill!rjk