… | |
… | |
37 | |
37 | |
38 | So when is fork+exec not fast enough, how can you do it faster, and why |
38 | So when is fork+exec not fast enough, how can you do it faster, and why |
39 | would it matter? |
39 | would it matter? |
40 | |
40 | |
41 | Forking a process requires making a complete copy of a process. Even |
41 | Forking a process requires making a complete copy of a process. Even |
42 | thougth almost every implementation only copies page tables and not the |
42 | thought almost every implementation only copies page tables and not the |
43 | memory istelf, this is still not free. For example, on my 3.6GHz amd64 |
43 | memory itself, this is still not free. For example, on my 3.6GHz amd64 |
44 | box, I can fork a 5GB process only twenty times a second. For a realtime |
44 | box, I can fork a 5GB process only twenty times a second. For a |
45 | process that must meet stricter deadlines, this is too slow. For a busy |
45 | real-time process that must meet stricter deadlines, this is too slow. |
46 | and big webserver, starting CGI scripts might mean unacceptable |
46 | For a busy and big web server, starting CGI scripts might mean |
47 | overhead. |
47 | unacceptable overhead. |
48 | |
48 | |
49 | A workaround is to use "vfork" - this function isn't very portable, but |
49 | A workaround is to use "vfork" - this function isn't very portable, but |
50 | it avoids the memory copy that "fork" has to do. Some systems have an |
50 | it avoids the memory copy that "fork" has to do. Some systems have an |
51 | optimised implementation of "spawn", and some systems have nothing. |
51 | optimised implementation of "spawn", and some systems have nothing. |
52 | |
52 | |
… | |
… | |
69 | Returns the PID of the new process if successful. On any error, |
69 | Returns the PID of the new process if successful. On any error, |
70 | "undef" is currently returned. Failure to execution might or might |
70 | "undef" is currently returned. Failure to execution might or might |
71 | not be reported as "undef", or via a subprocess exit status of 127. |
71 | not be reported as "undef", or via a subprocess exit status of 127. |
72 | |
72 | |
73 | fd_inherit $fileno[, $on] |
73 | fd_inherit $fileno[, $on] |
74 | File descriptors can be inherited by the spawned proceses or not. |
74 | File descriptors can be inherited by the spawned processes or not. |
75 | This is decided on a per file descriptor basis. This module does |
75 | This is decided on a per file descriptor basis. This module does |
76 | nothing to any preexisting handles, but with this call, you can |
76 | nothing to any preexisting handles, but with this call, you can |
77 | change the state of a single file descriptor to either be inherited |
77 | change the state of a single file descriptor to either be inherited |
78 | ($on is true or missing) or not $on is false). |
78 | ($on is true or missing) or not $on is false). |
|
|
79 | |
|
|
80 | Free portability pro-tip: it seems native win32 perls ignore $^F and |
|
|
81 | set all file handles to be inherited by default - but this function |
|
|
82 | can switch it off. |
79 | |
83 | |
80 | PORTABILITY NOTES |
84 | PORTABILITY NOTES |
81 | On POSIX systems, this module currently calls vfork+exec, spawn, or |
85 | On POSIX systems, this module currently calls vfork+exec, spawn, or |
82 | fork+exec, depending on the platform. If your platform has a good vfork |
86 | fork+exec, depending on the platform. If your platform has a good vfork |
83 | or spawn but is misdetected and falls back to slow fork+exec, drop me a |
87 | or spawn but is misdetected and falls back to slow fork+exec, drop me a |
84 | note. |
88 | note. |
85 | |
89 | |
86 | On win32, the "_spawn" family of functions is used, and the module tries |
90 | On win32, the "_spawn" family of functions is used, and the module tries |
87 | hard to patch the new process into perl's internal pid table, so the pid |
91 | hard to patch the new process into perl's internal pid table, so the pid |
88 | returned should work with other perl functions such as waitpid. Also, |
92 | returned should work with other Perl functions such as waitpid. Also, |
89 | win32 doesn't have a meaningful way to quote arguments containing |
93 | win32 doesn't have a meaningful way to quote arguments containing |
90 | "special" characters, so this module tries it's best to quote those |
94 | "special" characters, so this module tries it's best to quote those |
91 | strings itself. Other typical platform limitations (such as being able |
95 | strings itself. Other typical platform limitations (such as being able |
92 | to only have 64 or so subprocesses) are not worked around. |
96 | to only have 64 or so subprocesses) are not worked around. |
93 | |
97 | |