Tuesday, December 27, 2011

Write an example of a multi-threaded program in Perl

#!C:/Perl/bin/perl -w

use strict;
use threads;
use threads::shared;

print "Starting main program\n";
my @threads;

for ( my $count = 1; $count <= 5; $count++)
{
        my $t = threads->new(\&sub1, $count);
        push(@threads, $t);
}

foreach (@threads)
{
        my $num = $_->join;     
}
print "End of main program\n";

sub sub1
{
        my $num = shift;
        my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        print "$num thread started at $hour:$min:$sec\n";
        open FILE, ">$num.txt" or die $!;
        print FILE "$num.txt was created at $hour:$min:$sec.\n";
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        print FILE "$num.txt was closed at $hour:$min:$sec.\n";
        close FILE or die $!;
        return $num;
}

Tuesday, December 6, 2011

What does AWK stand for?

awk = "Aho, Weinberger and Kernighan"
This command was named by its authors Al Aho, Peter Weinberger and Brian Kernighan.