From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 4570 invoked by alias); 20 Apr 2018 09:28:49 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 42692 Received: (qmail 3662 invoked by uid 1010); 20 Apr 2018 09:28:49 -0000 X-Qmail-Scanner-Diagnostics: from mailout1.w1.samsung.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(210.118.77.11):SA:0(-6.9/5.0):. Processed in 1.500523 secs); 20 Apr 2018 09:28:49 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS,T_DKIMWL_WL_HIGH,T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1 X-Envelope-From: p.stephenson@samsung.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Filter: OpenDKIM Filter v2.11.0 mailout1.w1.samsung.com 20180420092842euoutp014808d9b68c65c3d2d9f683ba7f422679~nGtocrYKA0644006440euoutp01b DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=samsung.com; s=mail20170921; t=1524216523; bh=py3iwLi42v5+NbApHQiSWN2WEvOklRuXXS/fiLuW+5I=; h=Date:From:To:Subject:In-reply-to:References:From; b=b8jNSoP1DmEGMqCH1ir+jOR6LJWAC2+BOxwEOvpAYKbJXpilZra3owx+5rimH1G8G VFbs2LbGM/cDdJfsAF0dUK9bD66WQvIyFap+DWXaEYDIBXc28pDxxzV763goUKVCtW B7L1irceVRu+wB8JlQy+XFiuVXrnLhj2zp6fB8h4= X-AuditID: cbfec7f2-1dbff70000011644-bc-5ad9b2ca5a6a Date: Fri, 20 Apr 2018 10:28:39 +0100 From: Peter Stephenson To: zsh-workers@zsh.org Subject: Forking earlier for background code Message-id: <20180420102839.073d539b@camnpupstephen.cam.scsc.local> In-reply-to: <20180419104039.7b86ed2b@camnpupstephen.cam.scsc.local> Organization: SCSC X-Mailer: Claws Mail 3.13.2 (GTK+ 2.24.30; x86_64-pc-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFnrCIsWRmVeSWpSXmKPExsWy7djPc7qnNt2MMvgxn8XiYPNDJgdGj1UH PzAFMEZx2aSk5mSWpRbp2yVwZZxbvZOl4LRixcyVvxkbGF+IdTFyckgImEi0/XjD3MXIxSEk sIJRYnnXeyaQhJBAL5PEtD8WMEVzV31hhyhaxigx5fZMVghnGpPExIZTUJkzjBK3p25igXAu MEos2n0LyOHgYBFQlXi9WxNkFJuAocTUTbMZQWwRAXGJs2vPs4DYwgI6Etd2bGYDsXkFnCU+ PtvACmJzCrhItH4+zwQyhl9ASOJCsy3ERfYSR/ecZIIoF5T4Mfke2BhmoDHbtj1mh7DlJTav eQv2moTAFDaJzmObGSGaXSS2tbewQ9jCEq+Ob4GyZSQ6Ow4yQTQ0M0qsvX+fDSLRwygxa3Eo hG0t0Xf7IiPEBj6JSdumM4McJyHAK9HRJgRR4iFx7dJnqJmOEqv2tkFDawG7xJMj3xgnMMrP QnL4LCSHz0Jy+AJG5lWM4qmlxbnpqcWGeanlesWJucWleel6yfm5mxiBcX/63/FPOxi/Xko6 xCjAwajEw/th3Y0oIdbEsuLK3EOMEhzMSiK8CWY3o4R4UxIrq1KL8uOLSnNSiw8xSnOwKInz xmnURQkJpCeWpGanphakFsFkmTg4pRoY9Y7kZR5svmc+ubBh4+XFL/N4ZV5+0A0scpdVWqGR pjX/75814eWmzQ8my0eyv2U+tP/ldbGCV9rnOi82+PlOnJWd/Nm30j6L879SccuFiU8uHFtw LIjb+8jn7ifv5+canp2y5vb6yr1/Uu/yx+ff032c/e3b3svHzwZePHFoz8Nbr5+7lWYFtSqx FGckGmoxFxUnAgDP4wJ79wIAAA== X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFjrELMWRmVeSWpSXmKPExsVy+t/xq7onN92MMrjRLW9xsPkhkwOjx6qD H5gCGKO4bFJSczLLUov07RK4Ms6t3slScFqxYubK34wNjC/Euhg5OSQETCTmrvrC3sXIxSEk sIRR4sK6l1DODCaJ57sXskE45xgl/h54ygzSIiRwAch5HNLFyMHBIqAq8Xq3JkiYTcBQYuqm 2YwgtoiAuMTZtedZQGxhAR2Jazs2s4HYvALOEh+fbWAFsTkFXCRaP59ngpi/gF3i+/1drCAz +QWEJC4020JcZy9xdM9JJoheQYkfk++BzWQW0JLYvK2JFcKWl9i85i3UaeoSN+7uZp/AKDQL ScssJC2zkLQsYGRexSiSWlqcm55bbKhXnJhbXJqXrpecn7uJERiy24793LyD8dLG4EOMAhyM Sjy8H9bdiBJiTSwrrsw9xCjBwawkwptgdjNKiDclsbIqtSg/vqg0J7X4EKM0B4uSOO95g8oo IYH0xJLU7NTUgtQimCwTB6dUA6O71X/NSWcmv66btvRxB/u7O3rh18pLNl+0nKsWL9ltfuwX i9ZZ/X+PBLInyK5xtry8wnXZRevzG5ktBKbfLBeLnt8sPDHRYoV4slSgg4OZ7teeTf5xlV4W 0twxGpbJgpkCEbbxc5S+xTByP9mt7ud+L4ilpVG0u0s4feMiz0v/vNIWVea6KbEUZyQaajEX FScCAAs5L3pVAgAA X-CMS-MailID: 20180420092841eucas1p2b81a22528d83cf67908c923effa9971a X-Msg-Generator: CA CMS-TYPE: 201P X-CMS-RootMailID: 20180324221021epcas1p184507a6328dbd505b97db69c1f9d8194 X-RootMTR: 20180324221021epcas1p184507a6328dbd505b97db69c1f9d8194 References: <180323221959.ZM27569@torch.brasslantern.com> <20180324080514.txxyrb3qiztu4pqt@gmail.com> <180324150945.ZM32251@torch.brasslantern.com> <20180410124545.13fccd5d@camnpupstephen.cam.scsc.local> <20180410145926.64c4f671@camnpupstephen.cam.scsc.local> <180411151025.ZM19332@torch.brasslantern.com> <20180412172342.52df6b10@camnpupstephen.cam.scsc.local> <20180415162326.GA12549@chaz.gmail.com> <20180415185804.GB12549@chaz.gmail.com> <180416223910.ZM32002@torch.brasslantern.com> <20180417101947.5fd347df@camnpupstephen.cam.scsc.local> <180417090926.ZM2456@torch.brasslantern.com> <20180417173558.769503bd@camnpupstephen.cam.scsc.local> <180417105243.ZM2929@torch.brasslantern.com> <20180419104039.7b86ed2b@camnpupstephen.cam.scsc.local> I've renamed this part of the previous pipeline + process group thread. This patch: > This is a first go that doesn't appear to be completely broken. Lots > of rearranging and reindenting, very little novel code. I'm not sure > how much git users feel like being guinea pigs. to fork earlier when a process is being put into the background looks fairly benign to me, so I'll commit it (with a few more comments) in order to see what oddities we can track down. This was the easy case because the test for (how & Z_ASYNC) wasn't combined with anything else. so factored out trivially. Deep breath. It may well be possible to expand the logic to track down other cases where we know we can know we're going to need to fork, so can do so early to get fewer side effects (and slightly optimise the main shell). However, we're going to have to disentangle the code shown below. At the moment this relies on us being able to expand the command line to decide if the command is a builtin or shell. However, with enough banging of heads against granite outcrops it might be possible to rationalise what's going on with "do_exec", "output" and "last1" enough so this isn't always necessary. "last1" --- at the end of the exec list and also the pipeline: it's effectively an "and" of choices made in execlist() and execpline2(). Note special case below. "do_exec" --- last1 and doing an exec, so no need to fork. "output" --- output of pipeline, so zero if not a pipeline (we never pipe output to fd 0). The "if (!forked)" test is the result of the new early fork. One complication is in execpline2() where it says /* if we are doing "foo | bar" where foo is a current * * shell command, do foo in a subshell and do the * * rest of the pipeline in the current shell. */ I think this may just be an early attempt to get the effects Stephane wants? In any case, last1 is passed down to execcmd as 1 here so we don't fork again (except the logic is complicated enough it's not obvious to me that's necessarily true). So I think if "last1" is zero and "output" is non-zero we might be able to fork early, perhaps? And possibly if we do that the special code in execpline2() becomes redundant, maybe? So then we only need to be sensitive to "output", perhaps maybe? (I don't suppose anybody actually understands this?) /************************************************************************** * Do we need to fork? We need to fork if: * * 1) The command is supposed to run in the background. This * * case is now handled above (forked = 1 here). (or) * * 2) There is no `exec' flag, and either: * * a) This is a builtin or shell function with output piped somewhere. * * b) This is an external command and we can't do a `fake exec'. * * * * A `fake exec' is possible if we have all the following conditions: * * 1) last1 flag is 1. This indicates that the current shell will not * * be needed after the current command. This is typically the case * * when the command is the last stage in a subshell, or is the * * last command after the option `-c'. * * 2) We don't have any traps set. * * 3) We don't have any files to delete. * * * * The condition above for a `fake exec' will also work for a current * * shell command such as a builtin, but doesn't really buy us anything * * (doesn't save us a process), since it is already running in the * * current shell. * **************************************************************************/ if (!forked) { if (!do_exec && (((is_builtin || is_shfunc) && output) || (!is_cursh && (last1 != 1 || nsigtrapped || havefiles() || fdtable_flocks)))) { pws