Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How do I fork a daemon process under Win32?

by tachyon (Chancellor)
on Aug 14, 2001 at 05:53 UTC ( #104656=categorized question: print w/replies, xml ) Need Help??
Contributed by tachyon on Aug 14, 2001 at 05:53 UTC
Q&A  > programs and processes

Answer: How do I fork a daemon process under Win32?
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.

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

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; }

Please (register and) log in if you wish to add an answer

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (10)
    As of 2020-09-24 15:15 GMT
    Find Nodes?
      Voting Booth?
      If at first I donít succeed, I Ö

      Results (134 votes). Check out past polls.