Wednesday, April 13, 2011

Perl - Using threads with shared array of hashes

The following sample code demonstrate sharing of Array of hashes betweenn threads:
use strict;
use warnings;

# Use module thread
use threads ('yield','stack_size' => 64*4096,'exit' => 'threads_only','stringify');

# Use Share
use threads::shared;

# Declare shared array of Files Names
my @FilesList : shared = ();



&RunThreads();
&PrintFilesNames();
###################################################################################
# SubRoutines

sub RunThreads()
{
    # Running thread List
    my @findFilesThreads;

    # Create Threads
    my $dirName = "Directory1";
    my @filesList = ("file11","file12","file13");
    # Launch thread with its argument
    my $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    $dirName = "Directory2";
    @filesList = ("file21","file22","file23");
    # Launch thread with its argument
    $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    $dirName = "Directory3";
    @filesList = ("file31","file32","file33");
    # Launch thread with its argument
    $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    # Wait threads to finish
    foreach (@findFilesThreads)
    {
        my $thread = $_;
        if (defined($thread))
        {
            if ($thread->is_running())
            {
                $thread->join();
            }
            else
            {
                $thread->detach()
            }
        }
    }
}


# The Thread subroutin
sub ThreadFunc()
{
    my ($dirName,@filesName) = @_;

    # Random sleep, So its time the Thread print order will be different
    sleep rand(5);

    print "Add Directory : $dirName\n";
    # Create shread list entries
    my %entry : shared;
    my $dir : shared;
    my @files : shared;
    $entry{directory} = \$dir;
    $entry{files} = \@files;

    # Fill the data
    $entry{directory} = $dirName;
    foreach (@filesName)
    {
        push(@{$entry{files}},$_);
    }

    # lock the shared array
    lock(@FilesList);
    push(@FilesList,\%entry);
    return;
}

sub PrintFilesNames()
{
    print "Directory Tree : \n";
    lock(@FilesList);
    foreach (@FilesList)
    {
        my %entry = %{$_};
        print " Directory name is : $entry{directory}\n";
        print " Files are : \n";
        foreach (@{$entry{files}})
        {
            print "     $_\n";
        }
    }
}
The output of this script will look like (The directories order may change according to the sleep interval):
Add Directory : Directory1
Add Directory : Directory3
Add Directory : Directory2
Directory Tree :
 Directory name is : Directory1
 Files are :
     file11
     file12
     file13
 Directory name is : Directory3
 Files are :
     file31
     file32
     file33
 Directory name is : Directory2
 Files are :
     file21
     file22
     file23

Perl - Using threads

Thread using in Perl:
use strict;
use warnings;
# Use thread module 
use threads ('yield','stack_size' => 64*4096,'exit' => 'threads_only','stringify');

&RunThreads();

#################################################################################
# SubRoutines

sub RunThreads()
{
    # Running thread List
    my @findFilesThreads;

    # Create Threads
    my $dirName = "Directory1";
    my @filesList = ("file11","file12","file13");
    # Launch thread with its argument
    my $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    $dirName = "Directory2";
    @filesList = ("file21","file22","file23");
    # Launch thread with its argument
    $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    $dirName = "Directory3";
    @filesList = ("file31","file32","file33");
    # Launch thread with its argument
    $thr = threads->create('ThreadFunc',($dirName,@filesList ));
    push(@findFilesThreads,$thr);

    # Wait threads to finish
    foreach (@findFilesThreads)
    {
        my $thread = $_;
        if (defined($thread))
        {
            if ($thread->is_running())
            {
                $thread->join();
            }
            else
            {
                $thread->detach()
            }
        }
    }
}


# The Thread subroutin
sub ThreadFunc
{
    my ($dirName,@filesName) = @_;

    # Random sleep, So every time the Thread print order will be different
    sleep rand(5);
    print "Directory name is : $dirName\n";
    print "Files are : \n";
    foreach (@filesName)
    {
        print "     $_\n";
    }
}



The output of this script should look like (Every time the thread order will be different) :
Directory name is : Directory2
Files are :
    file21
    file22
    file23
Directory name is : Directory3
Files are :
    file31
    file32
    file33
Directory name is : Directory1
Files are :
    file11
    file12
    file13

Wednesday, January 26, 2011

Perl - given/when sample with strings

From Perl 5.10, We can use given/take for what is usually known as switch/case statement:
Here a sample with given/take using strings:
use strict;
use warnings;
use feature qw(switch);
my $name = "Monday";
given ($name) {
 when ("Sunday") { print "first\n"}
 when ("Monday") { print "second\n"}
 when ("Tuesday") { print "third\n"}
 default { print "None\n" }
}

print "Bye\n";
the "use feature qw(switch)" line tell the compiler to use Perl6's given/when switch feature

Monday, January 24, 2011

Reload vimrc file in vim

In order to reload the vimrc file after you edit it, We can use the following command:

:source $MYVIMRC

Saturday, September 4, 2010

Perl - Parent and child process

Creating a child process in Perl can be done with the fork command, Sample of the command :
defined ($pid = fork) or die "Cannot create thread";

By using the $pid value we can determine if we currently in the parent process or the child process, If its 0, We are currently in the child process, else, its the parent.

A Quick sample to demonstrate it:
my $pid;
print "
Parent PID is : $$ \n";

defined ($pid = fork) or die "Cannot create process";
if ($pid == 0)
{
print "
Child process Starting with pid : $$ \n";
}
else
{
print "
Parent process Continue with pid : $$ \n";
}
print "
Current PID is $$ (pid=$pid) \n";

The output of the previous script will be (I colored the Parent and the Child outputs):

Parent PID is : 2644

Parent process Continue with pid : 2644

Current PID is 2644 (pid=2645)

Child process Starting with pid : 2645

Current PID is 2645 (pid=0)

Note : $$ is the predefined name for the current running process.

Saturday, August 21, 2010

VIM as Perl IDE

In order to use vim editor as Perl IDE, you should use the Perl-Support Vim plug-in,

This plug-in (with installation instructions) can be found at http://www.vim.org/scripts/script.php?script_id=556.

In case of fedora,This plug-in is part of the "updates" repository (vim-perl-support).

Thursday, May 27, 2010

Fedora 12 on LG R560 laptop - HDMI

The Video worked from the beginning, As soon as I plugged the HDMI cable, I received the "Display - KDE Control Module" which help me configure the Video settings.

The Audio however, did not worked.

So in order to configure the alsa (Advanced Linux Sound Architecture), I had to install the alsa volume control:

yum install pulseaudio alsa-plugins-pulseaudio pulseaudio-esound-compat pulseaudio-libs pulseaudio-libs-glib2 pulseaudio-module-zeroconf pulseaudio-libs-zeroconf xmms-pulse pulseaudio-module-gconf wine-pulseaudio xine-lib-pulseaudio pulseaudio-utils pulseaudio-module-bluetooth gst-mixer padevchooser paman paprefs pavucontrol pavumete

Now launch "Pulse Audio volume control", In the configuration tab choose : "Digital Stereo (HDMI) Output + analog Stereo Input".

And now both Video and Audio are working :-)