ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Proc-FastSpawn/FastSpawn.pm
(Generate patch)

Comparing Proc-FastSpawn/FastSpawn.pm (file contents):
Revision 1.2 by root, Sat Mar 30 12:54:13 2013 UTC vs.
Revision 1.6 by root, Sun Apr 28 00:49:43 2013 UTC

39 39
40So when is fork+exec not fast enough, how can you do it faster, and why 40So when is fork+exec not fast enough, how can you do it faster, and why
41would it matter? 41would it matter?
42 42
43Forking a process requires making a complete copy of a process. Even 43Forking a process requires making a complete copy of a process. Even
44thougth almost every implementation only copies page tables and not the 44thought almost every implementation only copies page tables and not the
45memory istelf, this is still not free. For example, on my 3.6GHz amd64 45memory itself, this is still not free. For example, on my 3.6GHz amd64
46box, I can fork a 5GB process only twenty times a second. For a realtime 46box, I can fork a 5GB process only twenty times a second. For a real-time
47process that must meet stricter deadlines, this is too slow. For a busy 47process that must meet stricter deadlines, this is too slow. For a busy
48and big webserver, starting CGI scripts might mean unacceptable overhead. 48and big web server, starting CGI scripts might mean unacceptable overhead.
49 49
50A workaround is to use C<vfork> - this function isn't very portable, but 50A workaround is to use C<vfork> - this function isn't very portable, but
51it avoids the memory copy that C<fork> has to do. Some systems have an 51it avoids the memory copy that C<fork> has to do. Some systems have an
52optimised implementation of C<spawn>, and some systems have nothing. 52optimised implementation of C<spawn>, and some systems have nothing.
53 53
81 } 81 }
82 } 82 }
83} 83}
84 84
85BEGIN { 85BEGIN {
86 $VERSION = '0.1'; 86 $VERSION = '1.1';
87 87
88 our @ISA = qw(Exporter); 88 our @ISA = qw(Exporter);
89 our @EXPORT = qw(spawn fd_inherit); 89 our @EXPORT = qw(spawn spawnp fd_inherit);
90 require Exporter; 90 require Exporter;
91 91
92 require XSLoader; 92 require XSLoader;
93 XSLoader::load (__PACKAGE__, $VERSION); 93 XSLoader::load (__PACKAGE__, $VERSION);
94} 94}
101 101
102Returns the PID of the new process if successful. On any error, C<undef> 102Returns the PID of the new process if successful. On any error, C<undef>
103is currently returned. Failure to execution might or might not be reported 103is currently returned. Failure to execution might or might not be reported
104as C<undef>, or via a subprocess exit status of C<127>. 104as C<undef>, or via a subprocess exit status of C<127>.
105 105
106=item $pid = spawnp $file, \@argv[, \@envp]
107
108Like C<spawn>, but searches C<$file> in C<$ENV{PATH}> like the shell would
109do.
110
106=item fd_inherit $fileno[, $on] 111=item fd_inherit $fileno[, $on]
107 112
108File descriptors can be inherited by the spawned proceses or not. This is 113File descriptors can be inherited by the spawned processes or not. This is
109decided on a per file descriptor basis. This module does nothing to any 114decided on a per file descriptor basis. This module does nothing to any
110preexisting handles, but with this call, you can change the state of a 115preexisting handles, but with this call, you can change the state of a
111single file descriptor to either be inherited (C<$on> is true or missing) 116single file descriptor to either be inherited (C<$on> is true or missing)
112or not C<$on> is false). 117or not C<$on> is false).
118
119Free portability pro-tip: it seems native win32 perls ignore $^F and set
120all file handles to be inherited by default - but this function can switch
121it off.
113 122
114=back 123=back
115 124
116=head1 PORTABILITY NOTES 125=head1 PORTABILITY NOTES
117 126
119fork+exec, depending on the platform. If your platform has a good vfork or 128fork+exec, depending on the platform. If your platform has a good vfork or
120spawn but is misdetected and falls back to slow fork+exec, drop me a note. 129spawn but is misdetected and falls back to slow fork+exec, drop me a note.
121 130
122On win32, the C<_spawn> family of functions is used, and the module tries 131On win32, the C<_spawn> family of functions is used, and the module tries
123hard to patch the new process into perl's internal pid table, so the pid 132hard to patch the new process into perl's internal pid table, so the pid
124returned should work with other perl functions such as waitpid. Also, 133returned should work with other Perl functions such as waitpid. Also,
125win32 doesn't have a meaningful way to quote arguments containing 134win32 doesn't have a meaningful way to quote arguments containing
126"special" characters, so this module tries it's best to quote those 135"special" characters, so this module tries it's best to quote those
127strings itself. Other typical platform limitations (such as being able to 136strings itself. Other typical platform limitations (such as being able to
128only have 64 or so subprocesses) are not worked around. 137only have 64 or so subprocesses) are not worked around.
129 138
132 Marc Lehmann <schmorp@schmorp.de> 141 Marc Lehmann <schmorp@schmorp.de>
133 http://home.schmorp.de/ 142 http://home.schmorp.de/
134 143
135=cut 144=cut
136 145
1371; 1461
138 147

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines