From owner-freebsd-hackers@FreeBSD.ORG Sat Feb 26 09:47:21 2005 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E0D5A16A4CE for ; Sat, 26 Feb 2005 09:47:21 +0000 (GMT) Received: from mail25.sea5.speakeasy.net (mail25.sea5.speakeasy.net [69.17.117.27]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9FE8843D4C for ; Sat, 26 Feb 2005 09:47:21 +0000 (GMT) (envelope-from jmg@hydrogen.funkthat.com) Received: (qmail 14502 invoked from network); 26 Feb 2005 09:47:21 -0000 Received: from gate.funkthat.com (HELO hydrogen.funkthat.com) ([69.17.45.168]) (envelope-sender ) by mail25.sea5.speakeasy.net (qmail-ldap-1.03) with SMTP for ; 26 Feb 2005 09:47:20 -0000 Received: from hydrogen.funkthat.com (wfwnet@localhost.funkthat.com [127.0.0.1])j1Q9lKGH041504; Sat, 26 Feb 2005 01:47:20 -0800 (PST) (envelope-from jmg@hydrogen.funkthat.com) Received: (from jmg@localhost) by hydrogen.funkthat.com (8.12.10/8.12.10/Submit) id j1Q9lKLP041503; Sat, 26 Feb 2005 01:47:20 -0800 (PST) Date: Sat, 26 Feb 2005 01:47:20 -0800 From: John-Mark Gurney To: Yan Yu Message-ID: <20050226094720.GN89312@funkthat.com> Mail-Followup-To: Yan Yu , freebsd-hackers@freebsd.org References: <20050226072645.76950.qmail@web26802.mail.ukl.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.1i X-Operating-System: FreeBSD 4.2-RELEASE i386 X-PGP-Fingerprint: B7 EC EF F8 AE ED A7 31 96 7A 22 B3 D8 56 36 F4 X-Files: The truth is out there X-URL: http://resnet.uoregon.edu/~gurney_j/ X-Resume: http://resnet.uoregon.edu/~gurney_j/resume.html cc: freebsd-hackers@freebsd.org Subject: Re: confusion on fopen()/falloc() X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list Reply-To: John-Mark Gurney List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 26 Feb 2005 09:47:22 -0000 Yan Yu wrote this message on Sat, Feb 26, 2005 at 01:10 -0800: > Hello, all, > I have a user program as below: > FILE *fd; > while (1) > { > fd= fopen( "tmp", "r" ); > if ( fd == NULL ) > break; > } > > from my understanding, since i open the same file to read, my process > should create a new file descriptor each time when fopen is called. > Therefore, inside the kernel, fdalloc() should be called, NOT falloc() > (since falloc() allocates a new FILE * struct in addition to a new file > descriptor), > BUT based on what i observed (i instrumented falloc() function), it seems > that falloc() is called each time when fopen() is called. > I am wondering where i missed? first off, if you are looking at this code, you probably want to be calling open instead of fopen... fopen is stdio and is implemented in src/lib/libc/stdio/fopen.c... There it'll do an open system call, but you don't have as direct control over it... Also, the kernel is not responsible for FILE * allocations, that is purely a userland implementation detail.. it will end up getting mapped to a file descriptor opened via open... if you look in src/sys/kern/vfs_syscalls.c at open, it calls kern_open (in the same file) which calls falloc... falloc by it's comment does more than just allocate a file descriptor, it also allocates a new file structure, but then it does call fdalloc to get the file descriptor.. Again, make sure you don't confuse the userland FILE * with the kernel.. -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."