From owner-freebsd-hackers@freebsd.org Sat Aug 27 16:22:29 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 8043DB77056 for ; Sat, 27 Aug 2016 16:22:29 +0000 (UTC) (envelope-from hoomanfazaeli@gmail.com) Received: from mail-wm0-x236.google.com (mail-wm0-x236.google.com [IPv6:2a00:1450:400c:c09::236]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 190119E5 for ; Sat, 27 Aug 2016 16:22:29 +0000 (UTC) (envelope-from hoomanfazaeli@gmail.com) Received: by mail-wm0-x236.google.com with SMTP id o80so32407026wme.1 for ; Sat, 27 Aug 2016 09:22:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-transfer-encoding; bh=tzeERnAycSCeuZcMXPajhNKLjfIGxj+cXGp0nd9IVpc=; b=PD4VBJE5i07w4Wi8ZT39X9r6wcdzx41QTgjpjSUcTQu353+gA5JiulOFhaS0jkzUYt O4LqwxrLu5mqn694dMg2m9UNzwnj2vMieuI9RY9R6l1jncsPrmYSOeKHj/rFZVnVd8Am G3kROwvIgZxvXqRueh7RyhfnOI/UT3RuaL6SRKPtBC++XkhbsvPCnGDHIEgyww3EO6Kk eRMPvPzWS3UnZHQI3HzDeM8ifPHGwfCSeFE8dHx9IsaRMbK2vxB5wotMfhpTswlreC8G CLSusDUo+IaX77iwUlN6HtQxn0VAxQdUXjJcW/d0sKjtJQS2n1d+pqXLeMAG2vVErJKX Kihw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-transfer-encoding; bh=tzeERnAycSCeuZcMXPajhNKLjfIGxj+cXGp0nd9IVpc=; b=B3eZEAWeVT1sBm2ESQKjbSV3RBY46BbQnynHj0mRE5FWtnYa/L8rbdiQ6NFFGWa+Q4 8NMeahGnJDNPv1GkZK4PmNqAeLweJsE1AQMItZm5e0eHSQH/guNPV6/MkPFmgTOuWYpv Adeps266Ixd/7Frtf4U4jZDA7QTgt2RtjTVkNmIMVCvly7J5WI3tpuJVzZUOrHDgXSpt 0jQf4WFsv1MmYXHVCvb1WxuDmsF32jQ08/7e8lKLSRCsvcD8SXMszMu2f07JCWC7jkdM 87aTSP0+hpOWtLFPqN1HSBD/JPCz6zQBOVxEdqhX+fW+nFh4pEgCK+i8sVGj8ztjTpwM +o5w== X-Gm-Message-State: AE9vXwOtPXMdTb6Z4ny6GP7XwfeMYh8pUMEpH4GHc5tWyNsKBnFkrYCV1vLOD7mejQ/qUA== X-Received: by 10.194.98.139 with SMTP id ei11mr8891507wjb.19.1472314947635; Sat, 27 Aug 2016 09:22:27 -0700 (PDT) Received: from [192.168.2.30] ([2.190.218.206]) by smtp.googlemail.com with ESMTPSA id a3sm5686798wjw.15.2016.08.27.09.22.26 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 27 Aug 2016 09:22:27 -0700 (PDT) Message-ID: <57C1BE40.2060902@gmail.com> Date: Sat, 27 Aug 2016 20:52:24 +0430 From: Hooman Fazaeli User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 To: "Batutis, Ed" CC: "freebsd-hackers@freebsd.org" Subject: Re: get number of open files in a process? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 27 Aug 2016 16:22:29 -0000 On 2016-01-21 17:04, Batutis, Ed wrote: > Hi, > > I need to determine the number of open files in my process - all types of file handles total - sockets, files, everything. > > Does this work reliably for that purpose? > > int num_open = 0; /* number of open files? */ > kinfo_file *inf = kinfo_getfile(getpid(), &num_open); > if ( inf ) { > free(inf); > } > > > Thanks, > > =Ed > > > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org" My reply may be late and you may already solved the problem. Anyway, one possible solution is to do (slow) fd counting in another thread and update an atomic integer which the main thread can read and act upon: volatile int num_open = 0; fdcount_thread_proc(void *arg){ while (!exit_flag) { num_open = get_the_number_of_open_files_in_whatever_way_you_prefer(); usleep(1000); } } Of course, the main thread does not get the __exact__ number of open files when it reads num_open, butif you just need to know when your are approaching the limit, a close estimation would be enough. -- Best regards Hooman Fazaeli