
Jasmine
Administrator
/ Moderator
Feb 3, 2000, 8:29 AM
Post #9 of 11
(817 views)
|
|
Re: Pausing one peace of code while another continues.
[In reply to]
|
Can't Post
|
|
Found something that may be useful (from perl-win32-users@activestate.com) ... Win32::Process to Emulate fork() True forking is not possible on Win32 platforms, but this HTTP server example does some fancy work to get around this. Gisle Aas' HTTP: aemon leaves the forking to the developer, but since Win32 Perl doesn't currently support fork, implementing an HTTP server as it was intended to run is impossible. You cannot open a handle in one process and then expect to access it in another process, but because STDIN gets passed between one process and another, this code can successfully fake a fork by closing STDIN and redirecting the socket to STDIN and then creating the agent process. <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> ########################################## # Copyright 1996-1999, Digital Paper # # This source code is free software; you can redistribute it and/or # modify it under the same terms as Perl itself. # # Listener.pl # use HTTP: aemon; use HTTP::Status; use Win32::Process; use Getopt::Long; ######################### # Listener ######################### GetOptions( "port=s" => \$port, "listen:s" => \$listen); $listen | |= 5; $host = hostip(); my $listener = HTTP: aemon->new(LocalAddr => $host, LocalPort => $port, Listen => $listen) | | exitGracefully("Listener could not be created on $host:$port, listen queue=$listen\n"); print "[Listener] Waiting for Requests on ".$listener->url."\n"; for(;$c = $listener->accept; $c->close) { print "\n[Listener]****ACCEPTED CONNECTION****\n"; binmode $c; print "[Listener] Spawning an Agent\n"; spawn($c); } $c = undef; #************************************************** # SUPPORT FUNCTIONS #************************************************** sub spawn { my $new = shift; # You better define this! my $somedirectory = ''; open(SAVEIN, "<&STDIN"); my $socket_no = $new->fileno; open(STDIN, "<&".$socket_no) | | exitGracefully("Could not open STDIN as new socket\n", 1); $new->close; my $proc; my $cmd = "$somedirectory\\perl.exe $somedirectory\\Agent.pl"; my $app = "$somedirectory\\perl.exe"; my $inherit = 1; my $process_params = &NORMAL_PRIORITY_CLASS; Win32::Process::Create($proc, $app, $cmd, $inherit, $process_params, '.') | | do { &win32_errmsg; exitGracefully("Failed to execute: $app $cmd; $win32_errmsg\n", 1); }; open(STDIN, "<&SAVEIN"); close(SAVEIN); } #--------------------------------------------------------------------- sub win32_errmsg { $win32_errmsg = Win32::FormatMessage(Win32::GetLastError()); } #--------------------------------------------------------------------- sub exitGracefully { my $msg = shift; my $noexit = shift; $ss->close if $ss; undef($ss); print "[Listener] $msg\n"; exit unless $noexit; } #--------------------------------------------------------------------- sub hostip { use Sys::Hostname; use IO::Socket; my $host = @_ | | hostname(); return inet_ntoa(inet_aton($host)); } </pre><HR></BLOCKQUOTE>
|