From owner-freebsd-hackers@FreeBSD.ORG Fri Nov 4 05:24:09 2011 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id ADEB8106566B for ; Fri, 4 Nov 2011 05:24:09 +0000 (UTC) (envelope-from tim@kientzle.com) Received: from monday.kientzle.com (99-115-135-74.uvs.sntcca.sbcglobal.net [99.115.135.74]) by mx1.freebsd.org (Postfix) with ESMTP id ACD2F8FC0A for ; Fri, 4 Nov 2011 05:24:08 +0000 (UTC) Received: (from root@localhost) by monday.kientzle.com (8.14.4/8.14.4) id pA45A8j3049549; Fri, 4 Nov 2011 05:10:08 GMT (envelope-from tim@kientzle.com) Received: from [192.168.2.119] (CiscoE3000 [192.168.1.65]) by kientzle.com with SMTP id 86febdfav2wucaipvbucaydwww; Fri, 04 Nov 2011 05:10:07 +0000 (UTC) (envelope-from tim@kientzle.com) Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=us-ascii From: Tim Kientzle In-Reply-To: Date: Thu, 3 Nov 2011 22:10:06 -0700 Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Mark Saad X-Mailer: Apple Mail (2.1251.1) Cc: freebsd-hackers@freebsd.org Subject: Re: What is going on with ash / sh X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Nov 2011 05:24:09 -0000 On Nov 2, 2011, at 1:28 PM, Mark Saad wrote: > Hackers > What is going on here, if I run the following shell script, what is > the expected output . The script is named xxx >=20 > #!/bin/sh > ps -ax | grep -v grep | grep xxx >=20 > Here is what I see >=20 > # sh xxx > 88318 p0 S+ 0:00.00 sh xxx > 88320 p0 R+ 0:00.00 sh xxx > 88321 p0 R+ 0:00.00 sh xxx >=20 > Can someone explain this ? Here's my understanding: * 'sh xxx' starts (process 88318); let's call this the "parent" = process. * It reads and parses the command line 'ps -ax | grep -v grep | grep = xxx' * The parent process forks a copy of itself for the last 'grep xxx'. * The fork returns to the parent, the child (pid 88320) is scheduled = to run later * The parent forks a copy of itself for the 'grep -v grep' * The fork returns to the parent, the child (pid 88321) is scheduled = to run later * The parent runs 'ps -ax', which captures three copies of "sh xxx" = (the parent which is waiting on 'ps -ax' to finish, and the two children = that have not had a chance to run; note that the two children are both = in state 'R' which means they'll run as soon as they get a chance, while = the parent process is 'S'leeping waiting for 'ps -ax' to finish) * The two children (which started as copies of 'sh xxx') finally get = a chance to run and convert themselves into the respective grep commands = (via the exec() system call). The "expected output" is anywhere from one to three copies of 'sh xxx' = and maybe a copy of 'grep xxx', depending on what shell you're using, = how the shell parses the command line, the order in which it spawns = children, and whether the fork returns to the parent or child first. = The number of processors can also impact exactly when the child 'sh xxx' = processes get a chance to call exec(2). Tim