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

Hi , I new in this one, I make a cgi program that use fork, for printing Dots(".") by the child process , while the parent process is busy. My problem is that the I can’t see the dots . Probably I need to use pip, but I don’t know how to use it properly my program is:
#!/usr/bin/perl -w use strict; use CGI::Carp qw(fatalsToBrowser); $| = 1; print "Content-type: text/html\n\n"; print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> +', "\n"; print "<html><head><title>Testausgabe</title>\n"; print "</head><body>\n"; my $Kind_pid = fork(); if(kill(0,$Kind_pid)) { print "<p>whate while process</p>\n"; for (my $i=0;$i<5000000;$i++) { } print ("end for"); kill("KILL",$Kind_pid); } else{ while(1) { sleep 1; print "."; } } print "</body></html>\n";

Replies are listed 'Best First'.
Re: Cgi , fork , output data
by robartes (Priest) on Mar 11, 2003 at 10:24 UTC
    This doesn't work because of:
    if(kill(0,$Kind_pid)) {
    You are checking whether the child is alive. It is, so you go into this if block. However, both the parent and the child go into this block, whereas it is your intention to have only the parent get into this block, and have the child execute the else block. Change this line to:
    if ($Kind_pid) {
    and watch the dots march across the screen :).

    If you are worried about not properly forking, you can always check for the child's existence (with kill 0) inside the parent's if block.

    Update: Better yet, if you are worried about not forking properly, check for the definedness of the return value of fork:

    die "Could not fork: $!\n" unless (defined(my $Kind_pid = fork() ));

    CU
    Robartes-

      Additionally, it's worth noting that even if your code works, it might not work in a Web environment -- although most browsers have gotten 'smart' about rendering the page as they receive the content since it looks faster, to my knowledge they are under no obligation to do so. You might consider using a refresh instead.

        it's run on a web!!! test
      Tank's it work
•Re: Cgi , fork , output data
by merlyn (Sage) on Mar 11, 2003 at 13:32 UTC