Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2.fluke 9/24/84; site fluke.UUCP
Path: utzoo!watmath!clyde!cbosgd!ihnp4!zehntel!tektronix!uw-beaver!microsoft!fluke!jeff
From: jeff@fluke.UUCP (Jeff Stearns)
Newsgroups: net.bugs.4bsd
Subject: awk doesn't wait for subordinate processes to complete
Message-ID: <2078@vax4.fluke.UUCP>
Date: Mon, 14-Jan-85 20:37:23 EST
Article-I.D.: vax4.2078
Posted: Mon Jan 14 20:37:23 1985
Date-Received: Mon, 21-Jan-85 06:25:02 EST
Organization: John Fluke Mfg. Co., Inc., Everett, WA
Lines: 70

Index: 	 bin/awk/main.c 4.2BSD

Description:
	If one redirects awk output through a filter, awk does not wait
	for the filter to terminate before awk itself terminates.  The
	output therefore appears asynchronously while subsequent commands
	are being processed.  In a shell script, this can be fatal if the
	filter is, for example, sort(1).

Repeat-By:
	% awk 'END { print "echo hi" | "sh" }' 
  #define TOLOWER(c)	(isupper(c) ? tolower(c) : c) /* ugh!!! */
***************
*** 89,91
  	run();
  	exit(errorflag);
  }

--- 92,107 -----
  	run();
+ 	/*
+ 	 *  Awk may have spawned some children (printf "....." | sort).
+ 	 *  The children may still be alive (sort can be quite slow).
+ 	 *  It is a bad idea to exit before our children have completed,
+ 	 *  as the next command to be executed may depend on ALL of our
+ 	 *  processing being complete.  Thus we wait for our kids.
+ 	 */
+ 	{
+ 		int i; for (i = 0; i < NOFILE; i++)
+ 			(void) close (i);
+ 		while (wait (0) != -1)
+ 			;
+ 	}
  	exit(errorflag);
  }
-- 
	Jeff Stearns       (206) 356-5064
	John Fluke Mfg. Co.
	P.O. Box C9090  Everett WA  98043  
	{uw-beaver,decvax!microsof,ucbvax!lbl-csam,allegra,ssc-vax}!fluke!jeff