#!/usr/bin/perl # forkit.pl # This is my training script on fork. I will attempt to read and echo data # from three files simultaneously. The files are: file1, file2, file3 use CGI qw(:standard); # Here is an array containing the names of the files that I will open @files = ("file1", "file2", "file3"); # Let's show that we have correctly stored the filenames print "\nThe files I will be looking at are:\n\n"; foreach (@files) { print "$_\n"; } print "\n"; # OK now I will open these files and echo their contents sequentially # without using fork() --- foreach (@files) { open(EP,$_); print "\nI just opened $_\n"; while () { chomp; print "Contents of this file: $_\n"; } } print "\n"; # And now, do the same thing using fork() #### foreach (@files) { open(EP,$_); $pid = fork(); if ($pid == 0) { print "\nI just opened $_\n"; while () { chomp; print "Contents of this file: $_\n"; } } else { print "This is the parent process\n"; } } print "\n";