zedar has asked for the wisdom of the Perl Monks concerning the following question:

I have some code that forks 15 processes, waits until they complete then prints up some html with some results. When i run it just from the shell it all runs as expected with 1 set of html coming out nicely. However when i run it from a web brower it gets the html 15 times kinda mushed together. My forking section is..
for ($count=0;$count<5;$count++){ unless (defined ($pid[$count] = fork)) { print "im dying\n"; die "cannot fork: $!"; } unless ($pid[$count]) { print "im a kid with pid $$\n"; `do my thing`; exit; #child stops ehre } } # Parent continues here
then after that i have the usual
print "Content-Type:text/html\n\n"; print $q->pre("Will be tracing to $trace<BR>\n"); print ("your coming from $ENV{REMOTE_ADDR}<BR>\n");
along with the rest of the html, not in a loop or anything and only starting after all child processes have finished. The output im getting in the brower is in the form of
1 1 2 1 2 3 1 2 3 4
where 1,2,3,4 are the first few lines of html. When run from the shell though it runs perfectly, this only happens in browsers.. is there something about the forking im missing? thanks Andre

Replies are listed 'Best First'.
Re: CGI printing output x times, where x is the number of forks.
by Anonymous Monk on May 08, 2001 at 08:08 UTC
    When you run it from the shell, STDOUT is line buffered, but this is not the case when run by the web server, thus the children inherit the parent's output buffer. $| = 1; will fix the problem.