X-Recipient: archive-cygwin@delorie.com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 610F13857720
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
	s=default; t=1681985976;
	bh=lmXap0tqcj0TglOLDJfh+3Ff1e9OnEQHmWIsjIXQIMM=;
	h=To:Cc:Subject:Date:In-Reply-To:References:List-Id:
	 List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe:
	 From:Reply-To:From;
	b=cGlYOZQ8eDPNoE3ve9TnAZoH79+B0WdkL1dvrfRFTXdu9JmH5WuMBWLIaIodUW/Vc
	 Vj20qoPBs0hIvTo5rBebkY3OtGZJXy849qRFk8IqB0i7Yxt9edfd+vgC6U2u2mHhUr
	 RP7z4siVQT4pWFuoP2bH41c71oIieYANfl6JpW+s=
X-Original-To: cygwin@cygwin.com
Delivered-To: cygwin@cygwin.com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A46F73858D37
ARC-Seal: i=1; a=rsa-sha256; t=1681985940; cv=none;
 d=strato.com; s=strato-dkim-0002;
 b=ry9mp7PVqyXXct59WsMGybWkrDAg5/6Y4WOXpuGAk4NKzRe6G/Lsor88P8lDIj/93G
 Vlehc8iSCV9g7JbVrAgPnusDjS9mx9C+dxmCBbyQOgeTbZt5IC9Hz+OVB1u5xtS+kkOx
 nivj00lawtdNjrpRe81bFWZ/g+/tEFESTcQbntINdjZWA5LOGwWNapSU4Wk117S+Dqfl
 WD0Qye3IDqPr1LNrSUw5qdTib0ldeqhOjMTuSgY6Y/KDCj2rLquNVRBkmv4NaNaq1X+3
 V315cdZPVvcvKpQphrAetdKE5x/V+XMPeqiAnawiVVSKcIjBrIIN6qfxBbthaqJGcXCC
 /UhQ==
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1681985940;
 s=strato-dkim-0002; d=strato.com;
 h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date:
 From:Subject:Sender;
 bh=R2PtNyFR2x3h+LDaFSji3Lt5AineOx8yhJS6Eq56p88=;
 b=sQIfKNeS66axmYWwjcVlyv8pQtqnlpt1YSg6kuBXRYfV9TPtQ0M1fj5/FjSE3+9iNS
 RsenPLd663SK1kk5xWHTiJK8iDYi5qVjd+r2B9eE0LW8d4uOZpk2E/TPm8h3KphhghOQ
 vdTwwYHXMNBRyKlOVOenbh/r7VI1+OJqfL5S/AWYhbt9PZ5uc90fXlLnQLKfFty9La1u
 e1imwNPqJur7gSsIDH0v7Hq5IiPOGjRVc5biYbUfHVOSH8RyC9bzBbYgiulSU3X0dkPX
 T+igEg4NggnHBTjvIzb8Iz8O1NO6JhiKIKaGsWTlFdmoa9esgUACbgafYUcYATD89wcr
 U2Tg==
ARC-Authentication-Results: i=1; strato.com;
    arc=none;
    dkim=none
X-RZG-CLASS-ID: mo00
X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpPDj/1vgIuvkaOwJLsXaUZx50Vobw=="
To: cygwin@cygwin.com
Cc: gs-cygwin.com@gluelogic.com
Subject: Re: posix_spawn facility
Date: Thu, 20 Apr 2023 12:18:59 +0200
Message-ID: <1853259.CQOukoFCf9@nimes>
In-Reply-To: <ZED5SOzW5QAkfxje@calimero.vinschen.de>
References: <1752276.7aRn1RRit1@nimes> <ZEDmai2kO+dfxWut@xps13>
 <ZED5SOzW5QAkfxje@calimero.vinschen.de>
MIME-Version: 1.0
X-Spam-Status: No, score=-3.2 required=5.0 tests=BAYES_00, DKIM_SIGNED,
 DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE,
 RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP,
 T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6
X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on
 server2.sourceware.org
X-BeenThere: cygwin@cygwin.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-request@cygwin.com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
 <mailto:cygwin-request@cygwin.com?subject=subscribe>
From: Bruno Haible via Cygwin <cygwin@cygwin.com>
Reply-To: Bruno Haible <bruno@clisp.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: cygwin-bounces+archive-cygwin=delorie.com@cygwin.com
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie.com@cygwin.com>

Corinna Vinschen wrote:
> Unfortunately you can't expect any noticable difference on Cygwin by
> using posix_spawn.  While Cygwin has a spawn() family of functions, we
> don't (and can't... yet) use them.
> 
> The problem is that we don't have a safe way to perform the spawn
> attributes and file actions which are supposed to be performed between
> the fork() and the exec() step when using the spawn() functions.  This
> would have to be implemented first.

So, you are saying that on Cygwin, spawnvpe() and friends are fast,
because child_info_spawn::worker ends up calling CreateProcessW rather
immediately? But posix_spawn is slow, because it goes another path,
through fork()?

If that's the case, it might help to look at how I created Gnulib's
posix_spawn for native Windows:

  1) Extend the spawnvpe function to a spawnvpech function. It takes
     additional arguments
       - const char *currdir
       - int currdirfd
       - HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle
     (The latter is because the first 3 file descriptors are passed
     specially on Windows.)

  2) Refactor the guts of spawnvpech, moving as much as possible into
     helper functions. In my case, spawnvpech shrunk to only 150 lines
     of code.

     The helper functions, in the native Windows case, were:
       - prepare_spawn
       - compose_command
       - compose_envblock
       - init_inheritable_handles, compose_handles_block,
         free_inheritable_handles
       - convert_CreateProcess_error
     See https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/windows-spawn.h .

     I would expect that similar helper functions can be created in
     Cygwin.

     The "inheritable handles" is a data structure that allows for the
     arbitrary reshuffling of file descriptors required by posix_spawn
     (the addopen, adddup2, addclose actions), i.e. which does the book-
     keeping which HANDLE must in the end be open in the parent and which
     must in the end be open in the child, and at which position. While
     at the same time optimizing the number of DuplicateHandle calls.
     Without this optimization, the code is simpler, but there were many
     redundant DuplicateHandle calls.
     This data structure is essential for posix_spawn[p] to be multithread-
     safe. posix_spawn[p] must not modify the file descriptors of the
     parent; instead it has to create the planned file descriptors for the
     child in memory.

     During this refactoring, it is essential to have a good test suite.
     Because when you make a mistake and leave a fd / HANDLE accidentally
     open (in the parent or in the child), applications that invoke a pipe
     start to hang.

  3) With these helper functions, write a new core for posix_spawn[p].
     In my case, it was less than 250 lines of code, which is not
     straightforward, but also not really hard either.
     See https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=blob;f=lib/spawni.c
     lines 610..852.

     At this step, you can use Gnulib's test suite for verifying that the new
     code works properly.

Hope this helps.

Bruno




-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple
