Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Answer: How do I fork a daemon process under Win32?

by John M. Dlugosz (Monsignor)
on Aug 14, 2001 at 10:11 UTC ( #104680=categorized answer: print w/replies, xml ) Need Help??

Q&A > programs and processes > How do I fork a daemon process under Win32? - Answer contributed by John M. Dlugosz

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.

  • Comment on Answer: How do I fork a daemon process under Win32?
Replies are listed 'Best First'.
Re: Answer: How do I fork a daemon process under Win32?
by Anonymous Monk on Oct 11, 2004 at 11:36 UTC
    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 !
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2019-11-11 22:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Strict and warnings: which comes first?



    Results (63 votes). Check out past polls.

    Notices?