delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2012/05/31/14:44:11

X-Recipient: archive-cygwin AT delorie DOT 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
MIME-Version: 1.0
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 DOT 20120531T193933-322 AT post DOT gmane DOT org> <CANs8wdBYOBGsmp2iFSSOOd5FZ4qb3i3a-E2EM8LBbPKz=su5Pg AT mail DOT gmail DOT com>
From: "Thrall, Bryan" <bryan DOT thrall AT flightsafety DOT com>
To: <cygwin AT cygwin DOT com>
CC: "Thrall, Bryan" <bryan DOT thrall AT flightsafety DOT com>
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Unsubscribe: <mailto:cygwin-unsubscribe-archive-cygwin=delorie DOT com AT cygwin DOT com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
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 DOT thrall AT flightsafety DOT com



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019