Basic HTML version of Foils prepared Sept 20 97

Foil 73 Execution of UNIX Commands --
fork and exec

From Tutorial on PERL Computational Science for Information Age Course CPS616 -- Sept 20 97. by Geoffrey C. Fox,Nancy McCracken,Tom Scavo


This is most powerful method with fork creating two identical copies of program -- parent and child
unless (fork) { ;} # child indicated by fork=0
; # otherwise fork=child process number for parent
The child program typically invokes exec which replaces child original by the argument of exec. Meanwhile parent should wait until this exec is complete and child has gone away.
unless (fork) {
  • exec("date"); # child process becomes date command sharing environment with parent
}
wait; # parent process waits until date is complete
The child process need not terminate naturally as with exec() and if child code was for instance
print FILEHANDLE @hugefile; # in parallel with parent
exit; # is required else child will continue with parents code whereas we wanted parent and child to work in parallel on separate jobs



© Northeast Parallel Architectures Center, Syracuse University, npac@npac.syr.edu

If you have any comments about this server, send e-mail to webmaster@npac.syr.edu.

Page produced by wwwfoil on Sun Sep 21 1997