Jump to content

Fork: Difference between revisions

245 bytes added ,  15 years ago
→‎{{header|Tcl}}: Note on support
(omit autohotkey: fork not available on windows)
(→‎{{header|Tcl}}: Note on support)
Line 201:
Fork is one of the primitives used for process creation in Unixy systems. It creates a copy of the process that calls it, and the only difference in internal state between the original and the copy is in the return value from the fork call (0 in the copy, but the pid of the copy in the parent).
 
The [http://expect.sf.net/ Expect] package includes a fork. So does the [http://tclx.sf.net/ TclX] package.
 
Example:
 
<lang tcl> package require Expect
# or
package require Tclx
 
for {set i 0} {$i < 100} {incr i} {
set pid [fork]
switch $pid {
-1 {
puts "Fork attempt #$i failed."
}
0 {
puts "I am child process #$i."
exit
}
default {
puts "The parent just spawned child process #$i."
}
}
}
}</lang>
 
In most cases though, one is not interested in spawning a copy of the process one already has, but rather wants a different process. When using POSIX APIs, this has to be done by first forking and then having the child use the exec system call to replace itself with a different program. The Tcl <code>[http://www.tcl.tk/man/tcl8.5/TclCmd/exec.htm exec]</code> command does this fork&exec combination — in part because non-Unix OSs typicallly don't have "make a copy of parent process" as an intermediate step when spawning new processes.
 
Note that <code>fork</code> is only supported at all on unthreaded builds of Tcl. This is because the POSIX threads library does not sit well with the fork() system call.
 
=={{header|Toka}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.