From owner-freebsd-questions Fri May 7 8:51:13 1999 Delivered-To: freebsd-questions@freebsd.org Received: from cygnus.rush.net (cygnus.rush.net [209.45.245.133]) by hub.freebsd.org (Postfix) with ESMTP id 82E5914C30; Fri, 7 May 1999 08:50:50 -0700 (PDT) (envelope-from bright@rush.net) Received: from localhost (bright@localhost) by cygnus.rush.net (8.9.3/8.9.3) with SMTP id LAA09584; Fri, 7 May 1999 11:11:43 -0500 (EST) Date: Fri, 7 May 1999 11:11:41 -0500 (EST) From: Alfred Perlstein To: "Sameer R. Manek" Cc: freebsd-questions@FreeBSD.ORG, freebsd-hackers@FreeBSD.ORG Subject: Re: bug with lockf(3)? In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Thu, 6 May 1999, Sameer R. Manek wrote: > I had posted this originally to freebsd-stable but didn't get any response > from that list. > > On freebsd 3-1-stable I'm not sure if this is a bug or my misinterpetation > of how lockf() works. But here's the situation. > > Given 2 processes A and B, both with open file descriptors to a file, > using open ("file.txt",O_RDWR) > A calls lockf (fd,F_LOCK,0) and proceeds to read/write to file > while file is still locked....including using lseek to jump to the > begining of the file > B calls lockf (fd,F_LOCK,0) and enters blocked state > A calls lockf (fd,F_ULOCK,0) > B is still in blocked state, until A exit(3)s. > > According to the man page: > F_LOCK and F_TLOCK requests differ only by the action taken if the > section is not available. F_LOCK blocks the calling process until the > section is available. F_TLOCK makes the function fail if the section is > already locked by another process. According to _further down_ in the man page: F_ULOCK requests release (wholly or in part) one or more locked sections controlled by the process. Locked sections will be unlocked starting at the current file offset through size bytes or to the end of file if size is 0. When all of a locked section is not released (that is, when the be- ginning or end of the area to be unlocked falls within a locked section), the remaining portions of that section are still locked by the process. .... please have 'A' reset the file position via lseek to where it initially created the lock before calling lockf (fd,F_ULOCK,0). if this doesn't work, then something probably is broken in -stable: #include #include #include int main(int argc, char **argv, char **envp) { int fd,x; char s[20]; fd = open("xxx", O_RDWR); if (fd == -1) { perror("open"); exit(1); } fprintf(stderr, "attempting lock...\n"); lockf(fd,F_LOCK,0); fprintf(stderr, "aquired lock.\n"); write(fd, "spam", sizeof("spam")-1); lseek(fd, 0, SEEK_SET); fprintf(stderr, "press enter to unlock...\n"); fgets(s,10,stdin); lockf(fd,F_ULOCK,0); fprintf(stderr, "released lock.\n"); fprintf(stderr, "press enter to exit...\n"); fgets(s,10,stdin); } just make sure "xxx" exists, and run 2 copies of this program at the same time.... this works on my box, perhaps a sample of your code could explain the problem... hope this helps, -Alfred To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message