From owner-freebsd-hackers Thu Nov 27 21:44:01 1997 Return-Path: Received: (from root@localhost) by hub.freebsd.org (8.8.7/8.8.7) id VAA21907 for hackers-outgoing; Thu, 27 Nov 1997 21:44:01 -0800 (PST) (envelope-from owner-freebsd-hackers) Received: from anlsun.ebr.anlw.anl.gov (anlsun.ebr.anlw.anl.gov [141.221.1.2]) by hub.freebsd.org (8.8.7/8.8.7) with SMTP id VAA21870 for ; Thu, 27 Nov 1997 21:43:57 -0800 (PST) (envelope-from cmott@srv.net) Received: from darkstar.home (tc-if3-24.ida.net [208.141.171.129]) by anlsun.ebr.anlw.anl.gov (8.6.11/8.6.11) with SMTP id WAA10339 for ; Thu, 27 Nov 1997 22:43:48 -0700 Date: Thu, 27 Nov 1997 22:43:01 -0700 (MST) From: Charles Mott X-Sender: cmott@darkstar.home To: hackers@freebsd.org Subject: flock() question Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@freebsd.org X-Loop: FreeBSD.org Precedence: bulk Below are two test programs. What I observe is that if a file descriptor is opened and a process forks, then advisory locking does not appear to work. On the other hand, if the process forks, and then the file is opened independently by each process, advisory locking works perfectly well. Is this correct? From reading the man page on flock(2), I would have guessed that locking would have worked in both situations. I am running 2.2.2-R. -- Charles Mott *** Test 1: open() before fork() *** #include #include #include #include main() { int fd; fd = open("locktest", O_WRONLY | O_CREAT, 0644); fork(); for (;;) { printf("(1) pid=%d\n", getpid()); flock(fd, LOCK_EX); printf("(2) pid=%d\n", getpid()); sleep(5); printf("(3) pid=%d\n", getpid()); flock(fd, LOCK_UN); } } *** Test 2: open() after fork() *** #include #include #include #include main() { int fd; fork(); fd = open("locktest", O_WRONLY | O_CREAT, 0644); for (;;) { printf("(1) pid=%d\n", getpid()); flock(fd, LOCK_EX); printf("(2) pid=%d\n", getpid()); sleep(5); printf("(3) pid=%d\n", getpid()); flock(fd, LOCK_UN); } }