From owner-freebsd-fs@FreeBSD.ORG Sat Nov 8 00:16:58 2008 Return-Path: Delivered-To: freebsd-fs@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id AB85E1065674 for ; Sat, 8 Nov 2008 00:16:58 +0000 (UTC) (envelope-from unxfbsdi@gmail.com) Received: from ti-out-0910.google.com (ti-out-0910.google.com [209.85.142.184]) by mx1.freebsd.org (Postfix) with ESMTP id 470978FC1A for ; Sat, 8 Nov 2008 00:16:57 +0000 (UTC) (envelope-from unxfbsdi@gmail.com) Received: by ti-out-0910.google.com with SMTP id a1so255635tib.3 for ; Fri, 07 Nov 2008 16:16:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type; bh=H0IkRPHTyxJ46ZtvTVdqKmud87BhANIde+/x1BK8rm4=; b=AZS2BBiTeMwyNjTGkyPXvJNPKBiHLwnUV/4yaLzQVVxJXLpSgldC6uItSeSyBqGcb3 bHaHPhhvlNSkRR4LnrypVL4s/1UKJKDl13KGsCx2EFNBlCTAsquI0FD2wi+q2Umb//r/ xHd2DbeLqTTr/22+MNnPAEag9DvWuKXh7G4xs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type; b=MbQ1FzLEVjYxx+4lwT6TLS8naFQB7cc04YV7QKSFe1u00WBq8z2crz3WoqWjBXFRC1 l2Ie3CoO+j/tf65yATKrgqXCKgtG1DzEwRHUBtoeSnFo7HiV0/G/dQr9K8e9aKfwTDu9 Zq43ERmuZddJOvBK2056cKlLQ+PtMctHEj6AU= Received: by 10.110.57.6 with SMTP id f6mr4453969tia.55.1226101957996; Fri, 07 Nov 2008 15:52:37 -0800 (PST) Received: from ?210.214.1.209? (static-210-214-1-209.maa.sify.net [210.214.1.209]) by mx.google.com with ESMTPS id y3sm2943501tia.6.2008.11.07.15.52.33 (version=SSLv3 cipher=RC4-MD5); Fri, 07 Nov 2008 15:52:37 -0800 (PST) Message-ID: <4914D501.4090400@gmail.com> Date: Sat, 08 Nov 2008 05:23:37 +0530 From: Manish Jain User-Agent: Thunderbird 2.0.0.17 (Windows/20080914) MIME-Version: 1.0 To: freebsd-fs@freebsd.org Content-Type: multipart/mixed; boundary="------------090009080504050803020107" Subject: A problem with fork() and subsequent flock() X-BeenThere: freebsd-fs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Filesystems List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 Nov 2008 00:16:58 -0000 This is a multi-part message in MIME format. --------------090009080504050803020107 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hi, I am starting out as a C/C++ programmer on FreeBSD and I am stuck with a small but irritating problem. I was trying out the flock() call and wrote flocksample.cpp, which starts out with a fork() and subsequent calls to flock() from both processes (parent as well as child; the child does an initial sleep(1) before anything else). It compiles okay and the parent's flock() call succeeds. But the child's flock() call too succeeds on the same file descriptor even before the first flock() unlocks. Can anyone please point out where the problem is ? I am not even sure whether the problem is FreeBSD specific. Attached is flocksample.cpp Thanks for any help Manish Jain unxfbsdi@gmail.com --------------090009080504050803020107 Content-Type: text/plain; name="flocksample.cpp" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="flocksample.cpp" #include #include #include #include #include #include #include using namespace std; const char * szFileName = "/tmp/flock.log"; int addfunc(int fd); int readfunc(int fd); int main() { int fd = open(szFileName, O_RDWR | O_CREAT); assert(fd > 2); if (fork()) { return addfunc(fd); } else { cout << "Child sleeping for 1s before attempting to read log" << endl; sleep(1); //ensure that child's readfunc starts after parent's addfunc return readfunc(fd); } } int addfunc(int fd) { char buffer[4]; int i = 0; int result = flock(fd, LOCK_EX); assert(result == 0); while(i < 10) { sprintf(buffer, "%d\n\0", ++i); write(fd, buffer, strlen(buffer)); } while(*buffer != 'U') { cout << "Blocking on addfunc. Type U to unlock log" << endl; cin >> buffer; } cout << "Unblocking on addfunc ..." << endl; close(fd); //automatically calls LOCK_UN return 0; } int readfunc(int fd) { struct stat fstat; int result = flock(fd, LOCK_SH); assert(result == 0); cout << "readfunc got hold of log ..." << endl; lstat(szFileName, &fstat); char * buffer = new char[fstat.st_size + 1]; lseek(fd, 0, SEEK_SET); while (result < fstat.st_size) { result += read(fd, buffer + result, (fstat.st_size - result)); } close(fd); //automatically calls LOCK_UN buffer[result] = 0; cout << "Following are the contents of the file flock.log :" << endl; cout << buffer << endl; delete[] buffer; return 0; } --------------090009080504050803020107--