From owner-freebsd-questions@FreeBSD.ORG Wed Nov 3 18:45:39 2004 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 84C9316A4CE for ; Wed, 3 Nov 2004 18:45:39 +0000 (GMT) Received: from segfault-outgoing-helo.monkeys.com (segfault.monkeys.com [66.60.159.24]) by mx1.FreeBSD.org (Postfix) with ESMTP id 185BC43D39 for ; Wed, 3 Nov 2004 18:45:39 +0000 (GMT) (envelope-from rfg@monkeys.com) Received: from segfault-nmh-helo.monkeys.com (localhost [127.0.0.1]) by segfault.monkeys.com (Postfix) with ESMTP id A57A454AA for ; Wed, 3 Nov 2004 10:45:38 -0800 (PST) To: freebsd-questions@freebsd.org Date: Wed, 03 Nov 2004 10:45:38 -0800 Message-ID: <37501.1099507538@monkeys.com> From: "Ronald F. Guilmette" Subject: Trying to understand flock() X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 03 Nov 2004 18:45:39 -0000 Greetings friends, I wonder if someone would be kind enough to enlighten me about the semantics of the flock(2) function. I have RTFM'd, and I am sad to say that I am still rather mystified that flock() doesn't seem to do what it is documented as doing. (I am testing it on 4.10-RELEASE, by the way.) The short test program attached below illustrates the source of my abundant confusion. When I compile this program with -DUSE_FCNTL (thus forcing it to use fcntl(2) to implement exclusive file locking) and then execute it, the resulting behavior is exactly what I expect, i.e. the program prints the string "Temp file locked (1)", and then it pauses for 10 seconds, and then it prints "Temp file locked (2)". The delay time between the appearance of the two message indicates clearly that exclusive file locking is working as expected. When I compile this program WITHOUT the -DUSE_FCNTL option however (thus forcing the program to use flock() rather then fcntl() for file locking), there is no apparent delay between the printing of the first message and the printing of the second message. That is what has me mystified. Obviously, there is something (or maybe several things) about the actual semantics of flock(2) that I don't understand. I would appreciate it if someone would enlighten me about that. Regards, rfg P.S. My apologies in advance if you try to Cc: me directly on your reply to this posting, and if your response gets rejected by the local spam filters. It's nothing personal. Really. We just have about 2/5ths of the entire Internet blacklisted here due to past spamming incidents. I will look for replies also in the freebsd-general list archives, so if you prefer, you can just repl to the list. Thanks and hasta la vista. ======================================================================== #include #include #include #include #include #include static void die (register char const *const fmt) { fprintf (stderr, fmt, strerror (errno)); fprintf (stderr, "\n"); exit (1); } static int lock_exclusive (register int const fd) { #if USE_FCNTL auto struct flock fl; fl.l_start = 0; fl.l_len = 0; fl.l_pid = 0; fl.l_type = F_WRLCK; fl.l_whence = SEEK_SET; return fcntl (fd, F_SETLKW, &fl); #else return flock (fd, LOCK_EX); #endif } int main (void) { static char template[] = "/tmp/temp.XXXXXXXXXX"; register int fd; fd = mkstemp (template); unlink (template); if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (1)\n"); if (fork () == 0) { if (lock_exclusive (fd) == -1) die ("Error creating exclusive lock: %s"); fprintf (stderr, "Temp file locked (2)\n"); } sleep (10); close (fd); return 0; }