Sunday 30 June 2013

The Rising Software Abstraction

- 0 comments
When the first computers were started to be used they were very different from what we see today. Even going back to rather "modern" age of Assembly Language the programmers had to feed each machine instruction to the computer using programs commonly hundreds of thousands of lines in size.



Then came the C language that changed the way programs were written. It brought real abstraction to the programming paradigm and its usage was spread like wildfire in the world.

But the abstraction used by programming languages of today have another objective behind them. The fact that today we can create very advanced software without knowing the system internals is indeed more grim than it sounds. The programmers that came before us knew the machine they worked on. They were aware of every byte-word-doubleword they fed into the assembler and the result of the instructions too. Today frameworks like Java and .NET bury and hide all these intricacies deep inside the byte code generated by them. This byte code is again translated to machine code that the machine could understand and execute.

Now, the problem is that we are turning into an idiot generation that 'knows how to make sandwich from bread but can't bake the bread itself' as a friend would say. Hence, the ideal thing is to learn about the internal structure and working of the system to a good extent while still using high-level languages like Python, C#, Java, etc.

Every programmer should learn using Assembly Language and try making small programs in it to grasp the concepts. It is not just details-- it's a legacy our ancestors left for us.
Read More »

Perl Threads - A Useful Little Tutorial

- 0 comments
Threads are the basis of multi-tasking in a program. Without them everything will have to be done serially-- now imagine how clumsily would our internet browsers, games, and all the applications we use daily would behave if that were so! Thankfully they are out there as our obedient workers running number of different tasks simultaneously.


Today, we shall see how to use threads in Perl programming language. Also, we will learn how to share variables among threads (like friends share cookies). Let's take a very very simple example:

#!/usr/bin/perl

use threads;
use threads::shared;

our $variable1 :shared;
our $variable2 :shared;

my $thread1 = threads->new(\&function1, "function1");
my $thread2 = threads->new(\&function2, "function2");

$thread1->join();
$thread2->join();

sub thread1()
{
    print "hello i am thread1 :)\n";
    print "value of variable1: $variable1 and variable2: $variable2\n";

    variable1 = 1;
    variable2 = 2;
}

sub thread2()
{
    print "hello i am thread2 :)\n";
    print "value of variable1: $variable1 and variable2: $variable2\n";

    variable1 = 3;
    variable2 = 4;
}


See! making threads in Perl is that easy. Just declare to SUB's and assign them each to a thread. Then call join() function for each thread you declared and they will get going.

There are two things to note here:
  • When a thread is created and is running the assigned sub, it receives a new copy of any variable you use in it. This means that any variables declared outside the subroutine are not accessible in it. So a variable being used in another thread is practically invisible to it! Now, we want to share our variable1 and variable2 between the two threads. So we use threads and threads::shared packages and put a little ":shared" thing after our global variables (pun IS intended here :D). Now our threads can modify these variables and read modifications done by each other too!
  •  The threads package used here can be installed from CPAN using the command cpan install threads on all compatible platforms like Linux (for Linux you may have to use sudo cpan install threads), Windows or Mac.
I hope I was able to clear doubts regarding the topic here. I am sure the given example will give you a good starting point to use threads in you sweet little Perl program.
Read More »
All Articles © Asit. No Copying. Powered by Blogger.
 
Copyright © . The Tech Veda - Posts · Comments
Markup Powered by Bluefish Editor 2.2