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