http://www.perlmonks.org?node_id=104656

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

How do I fork a daemon process under Win32?

Originally posted as a Categorized Question.

  • Comment on How do I fork a daemon process under Win32?

Replies are listed 'Best First'.
Re: How do I fork a daemon process under Win32?
by John M. Dlugosz (Monsignor) on Aug 14, 2001 at 10:11 UTC
    Huh? Win32 doesn't have a fork system command, and it's process model doesn't work that way. The current version of Perl uses ithreads to emulate fork to a useful degree. But the "parent" and the "child" are different instances of the Perl interpreter running in the same process.

    Detaching the child from the console and/or creating another console will affect the "parent", too!! In your example, you detach the parent before forking, and then immediatly block the parent. Why bother to fork?

    A more realistic example would be to have the parent continue running and outputting to STDOUT while the "deamon" runs in the background, and I think this will have problems.

    Don't use fork. The program that's a deamon can detach from the console, and the parent that started it (with system or Win32::CreateProcess) doesn't worry about that, and continues running normally. Also, you can specify flags in CreateProcess to run detached in the first place.

    Also, you can use wperl.exe instead of perl.exe and run in the GUI instead of the CONSOLE mode, so there is no console window at all.

      On the same subject (Win32::Console). I am wondering why the free() method doesn't detatch from the calling cmd (console) process.

      Under cmd.exe (windowz2000), the process doesn't detach from the console. But it does detach (free) itself from the console under command.com (win98) ?

      When I run the following, for example, under cmd.exe win2000, the cmd (console) isn't returned.
      Ie; the cmd (console) process seems to be just waiting for the script-process to end?
      ######################################################## use Win32::Console; use Tk; my $mw = MainWindow->new ( -height=>30, -width=>120, -background=>black ); my $lb = $mw->Listbox ( -height=>30, -width=>120, -background=>black, -foreground=>yellow ); $lb->pack; defined ( my $pid = fork() ) or die "Bummer dude etc\n"; if ($pid) { MainLoop; exit; } else { my $console = Win32::Console->new(); $console->Alloc(); $console->Free(); # Returns under win98, not under win2000 my $stupid_incrament=0; while(1) { $lb->insert(end,$stupid_incrament); sleep 1; $stupid_incrament+=32; } } ########################################################
      What gives !
Re: How do I fork a daemon process under Win32?
by tachyon (Chancellor) on Aug 14, 2001 at 05:59 UTC
    You can use Win32::Console to fork a detached/daemon process. Here is a little alarm clock example. When it runs it pops a console and asks for a sleep time. It then detaches from the console which closes. Next it forks. The parent just exits whilst the child sleeps for x seconds, pops a new console window and prints a message (just to prove it is alive and kicking). This new console disappears when the child exits 5 seconds later.
    #!/usr/bin/perl -w use strict; use Win32::Console; my $con = Win32::Console->new(); $con->Display; $con->Write("Sleep how many seconds? "); chomp(my $sleep = <STDIN>); $con->Free(); # detache our script from the console which closes # now let's fork of an alarm clock child defined ( my $pid = fork() ) or die "Can't fork $!\n"; if ($pid) { exit; } else { sleep $sleep; $con->Alloc() or die $!; $con->Write("BZZT - this is your wake up call"); sleep 5; exit; }