X-Recipient: archive-cygwin@delorie.com
X-SWARE-Spam-Status: No, hits=-1.7 required=5.0	tests=AWL,BAYES_50,GENERIC_IXHASH,KHOP_RCVD_UNTRUST,KHOP_THREADED,RCVD_IN_HOSTKARMA_W,RCVD_IN_HOSTKARMA_WL
X-Spam-Check-By: sourceware.org
Content-Class: urn:content-classes:message
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Subject: RE: Shell script loop runs out of memory
Date: Thu, 31 May 2012 13:43:47 -0500
Message-ID: <786EBDA1AC46254B813E200779E7AD36023A42C2@srv1163ex1.flightsafety.com>
In-Reply-To: <CANs8wdBYOBGsmp2iFSSOOd5FZ4qb3i3a-E2EM8LBbPKz=su5Pg@mail.gmail.com>
References: <loom.20120531T193933-322@post.gmane.org> <CANs8wdBYOBGsmp2iFSSOOd5FZ4qb3i3a-E2EM8LBbPKz=su5Pg@mail.gmail.com>
From: "Thrall, Bryan" <bryan.thrall@flightsafety.com>
To: <cygwin@cygwin.com>
CC: "Thrall, Bryan" <bryan.thrall@flightsafety.com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
Precedence: bulk
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie.com@cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from base64 to 8bit by delorie.com id q4VIi6Xd024159

AZ 9901 wrote on 2012-05-31: 
> 2012/5/31 Jordan :
> Then, when (bash) scripting under Cygwin, you must take care to avoid
> forking as much as possible.
> 
> You could try to improve the "sleep 1" loop with the following one :
> 
> while md5sum $FILE_TO_CHECK | cut -d " " -f1 | grep -q "^$MD5PRINT$"
> do
>   sleep 1
> done
> 
> Note that MD5PRINTNEW is no more useful here.
> With this loop we avoid the fork done by
> MD5PRINTNEW=`md5sum $FILE_TO_CHECK | cut -d " " -f1`

Doesn't that just replace the 2 MD5PRINTNEW  forks (md5sum and cut) with 3 (md5sum, cut, and grep)?

Seems like the (untested) following would be better (in terms of fewer forks):

TMPFILE=$(mktemp)
md5sum $FILE_TO_CHECK > "$TMPFILE"
...
while md5sum -c "$TMPFILE"
do
  sleep 1
done
rm "$TMPFILE"

--
Bryan Thrall
Principal Software Engineer
FlightSafety International
bryan.thrall@flightsafety.com



