awk doesn't wait for subordinate processes to complete

Jeff Stearns jeff at fluke.UUCP
Tue Jan 15 10:05:24 AEST 1985


Index: 	 usr.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" }' </dev/null
	
	Note that the result is (usually):
	    % awk 'END { print "echo hi" | "sh" }' </dev/null
	    % hi

	rather than:
	    % awk 'END { print "echo hi" | "sh" }' </dev/null
	    hi
	    %

	Try this, and note the changing size of the sorted-output file:	
	    % awk '{ print $0 | "sort -o sorted-output" }' <big-file
	    % wc sorted-output
	    % wc sorted-output
	    % wc sorted-output
	    % wc sorted-output
Fix:
	The following context diff shows the additions made to the file
	/usr/src/usr.bin/awk/main.c:

*** /usr/src/usr.bin/awk/main.old	Mon Jan 14 15:51:47 1985
--- /usr/src/usr.bin/awk/main.c	Mon Jan 14 15:08:44 1985
***************
*** 91,93
  	run();
  	exit(errorflag);
  }
  
--- 91,102 -----
  	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.
+ 	 */
+ 	while (wait (0) != -1)
+ 		;
  	exit(errorflag);
  }



More information about the Comp.bugs.4bsd.ucb-fixes mailing list