From owner-freebsd-current@FreeBSD.ORG Tue Sep 16 18:22:08 2008 Return-Path: Delivered-To: current@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id D6226106566C; Tue, 16 Sep 2008 18:22:08 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from mail.terabit.net.ua (mail.terabit.net.ua [195.137.202.147]) by mx1.freebsd.org (Postfix) with ESMTP id 5E7A28FC14; Tue, 16 Sep 2008 18:22:03 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from skuns.zoral.com.ua ([91.193.166.194] helo=mail.zoral.com.ua) by mail.terabit.net.ua with esmtp (Exim 4.63 (FreeBSD)) (envelope-from ) id 1KffBZ-000CXX-6S; Tue, 16 Sep 2008 21:22:01 +0300 Received: from deviant.kiev.zoral.com.ua (root@deviant.kiev.zoral.com.ua [10.1.1.148]) by mail.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m8GILvxA086986 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Tue, 16 Sep 2008 21:21:58 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: from deviant.kiev.zoral.com.ua (kostik@localhost [127.0.0.1]) by deviant.kiev.zoral.com.ua (8.14.2/8.14.2) with ESMTP id m8GILvfH078412; Tue, 16 Sep 2008 21:21:57 +0300 (EEST) (envelope-from kostikbel@gmail.com) Received: (from kostik@localhost) by deviant.kiev.zoral.com.ua (8.14.3/8.14.3/Submit) id m8GILvGt078410; Tue, 16 Sep 2008 21:21:57 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: deviant.kiev.zoral.com.ua: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 16 Sep 2008 21:21:57 +0300 From: Kostik Belousov To: Daniel Eischen Message-ID: <20080916182157.GS39652@deviant.kiev.zoral.com.ua> References: <20080916140319.GA34447@nagual.pp.ru> <20080916144502.GA39765@nagual.pp.ru> <3bbf2fe10809160753o7e5e8a78q7c6bd44c02bfd5c2@mail.gmail.com> <20080916150120.GA40087@nagual.pp.ru> <20080916160535.GA40676@nagual.pp.ru> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="Fcn+O7u6afXSKWdN" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Virus-Scanned: ClamAV version 0.93.3, clamav-milter version 0.93.3 on skuns.kiev.zoral.com.ua X-Virus-Status: Clean X-Spam-Status: No, score=-4.4 required=5.0 tests=ALL_TRUSTED,AWL,BAYES_00 autolearn=ham version=3.2.5 X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on skuns.kiev.zoral.com.ua X-Virus-Scanned: mail.terabit.net.ua 1KffBZ-000CXX-6S 4682ca4a8df0952baeb8bc01799e0989 X-Terabit: YES Cc: Attilio Rao , Andrey Chernov , current@freebsd.org Subject: Re: Is fork() hook ever possible? X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Sep 2008 18:22:09 -0000 --Fcn+O7u6afXSKWdN Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Sep 16, 2008 at 12:50:53PM -0400, Daniel Eischen wrote: >=20 > [ Trimmed ] >=20 > On Tue, 16 Sep 2008, Andrey Chernov wrote: >=20 > >On Tue, Sep 16, 2008 at 11:36:03AM -0400, Daniel Eischen wrote: > > > >>Well, you could speed up getpid() by having libc wrap all fork() > >>variants. The idea is that getpid() would only call __sys_getpid() > >>the first time it was called and then only after a fork(). It > >>would return the saved process id for all other cases. > > > >Yes, speeding up getpid() by caching its pid is nice idea. > >But I am completely unaware how to create syscall wrappers inside libc. = :( > >I think about something like that: > > > >__weak_reference(_fork, fork); >=20 > I think you'll have to implement it as __fork() in libc, with > _fork and fork both being weak references to __fork() in libc. The > thread libraries will have to call __fork() instead of __sys_fork() > by implementing "fork" as _fork() and providing a weak reference > from fork to _fork. You can see wait() as an example. >=20 > Probably rfork() and vfork() will need to be handled as well, > though I don't think that the thread libraries care about these. >=20 > >But how it will coexists with the same __weak in thread/thr_fork.c ? > >Are some threading locks required in this code? >=20 > I think you can do it without locks. After a fork() you are > single threaded so you can easily set/clear __cur_thread. > Otherwise, the worst case is that multiple threads will call > _sys_getpid() simultaneously the first time, but as long as > you atomically update __cur_thread, it won't matter - each > thread will have retrieved the same exact process id so it > is okay if they all update __cur_thread. >=20 > pid_t > __getpid(void) > { >=20 > if (__cur_thread !=3D -1) > return (__cur_thread); >=20 > atomic_set_32(&__cur_thread, __sys_getpid()); > return (__cur_thread); > } > __weak_reference(__getpid, getpid); > __weak_reference(__getpid, _getpid); >=20 > Or something like that... Do not forget about rfork(). Not sure about rfork_thread(). --Fcn+O7u6afXSKWdN Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEARECAAYFAkjP+UUACgkQC3+MBN1Mb4iWNwCfcjSyL18xL2QChcJcLtusG7MP ASEAnjQyH/uoKNYxdTCEt8S6KPKHBPfj =gJRs -----END PGP SIGNATURE----- --Fcn+O7u6afXSKWdN--