My Profile Photo

Nithin Krishna


The art challenges the technology, and the technology inspires the art. - John Lasseter


  1. WannaCry Ransomware Attacks

    Friday 12th May witnessed the cyberattack of a RansomWare WannaCry, WannaCrypt0r or WannaDecryptor which targets Microsoft Operating system, encrypting data and demanding a ransome in bitcoin. This ransomeware afected 300,000 computers in 150 countries and the most affected countries were Russia, Taiwan, Ukraine and India. The rate of this attack peaked when 9000 computers got hit per hour last friday. …


  2. Erlang: My First functional programming Language

    Erlang is a general purpose, concurrent functional programming language.I have attended two online courses from futurelearn[futurelearn_link]. The first course mainly focussed on the basic of the Erlang Language and the second course was into concurrent programming in Erlang. The Erland Language is approx 30years old and getting popularity these days due to its wide usage in concurrent environment. The Erlang was orginally designed for improving the development in telephony applications, where thery have to handle a lot of active connections at the same time. Another problem that lead to the development of Erlang is the need for availability and fault tolerance. By 1988 Erlang proved that it was suitable for telephony applications. In 1992 the work for erlang virtual machine BEAM was started( earlier versions of erlang was implemented in Prolog which was found slower hence BEAM). In 1998 Ericsson announced they were able to acheive high availability of nine 9’s using Erlang circuit switched digital telephone exhanges manufactured by Ericsson.

    SOME OF THE FEATURE OF ERLANG (JOE ARMSTRONG)

    • In Erlang everything is a process.
    • Process are strongly isolated.
    • Process creation and destruction are light weight.
    • Message passing is the only way for processes to interact.
    • Process has unique names.
    • If you know the nme of process you can send message to it.
    • Processes share no resources.
    • Error-handling is non-local.
    • Processes do what they are supposed to do else they fail.
    from wiki With all this in mind lets move forward and allow me to write the challenges faced and the new experiences gained during the said courses.

    Course 1: Basic Programming In Erlang.

    Week 1:

    The first week of the course was mainly focussed on Erlang basics. One of the interesting aspect of Erlang that I came to know is the Pattern Matching. In Erlang variables are bind to values using pattern matching, uses single assignment. For example
    Eshell V8.2  (abort with ^G)
    1> A=5.
    5
    2> A.
    5
    3> A=6.
    ** exception error: no match of right hand side value 6
    In the above example , A is bounded to value 5. If we try to bind it again(step 3) we will get an exception. Another thing to note is that the varibales must start with Capital letter otherwise it will become an atom. Atoms are immutables, a literal ,a constant with a name. Functions: A simple function can be defined like this,
    -module(isZero).
    -export([is_zero/1]).
    
    is_zero(0)->
     true;
    is_zero(X)->
     false.
     
    The function isZero checks whether a given value is zero or not. If first clause holds we will get true else false. The clauses are separated by a semi-colon(;) and the final clause have a period(.) at the end. We have a module name isZero and a export list. In export list is_zero/1 says that is_zero is a function which will take a single argument. Every function should be exported to use outside the module. The function vcan be executed after compiling the file isZero.erl(same as the module name) as shown below. This function in action,
        
    1>c(isZero).  %% Compiling
    {ok, isZero}
    2> isZero:is_zero(0). %% Accessing module:function/arity
    true
    3> isZero:is_zero(3).
    false
    Recursion Another different thing I saw in Erlang was that there is no explicit control structures like for , while loops etc. We have to attain looping with recursion alone. An example of factorial problem,
        
    fact(0)->
        1;
    fact(N)->
        fact(N-1)*N.
    we can implement this with Tail Recursion as
    fact(N)->
        fac(N,1).
    
    fac(0, Acc)->
        Acc;
    fac(N, Acc) when N > 0 ->
        fac(N-1, Acc*N).
    Difference between Recursion & Tail Recursion go_here. This Article explains tail recursion in a fantastic way. A while loop implementation:
        counter =5;
        while(counter > 0){
            printf("%d\n",counter);
        	counter--;
        }
    	
    we can implement the same with Erlang like,
    while_loop(N) when N > 0 ->
        io:format("~p~n",[N]),
        while_loop(N-1).
    	  

    Week 2

    Second week on the course was about Lists. List is a collection of Items, where you can mix more than one data types. Ex: [1,2,3,4,5], [“string”,atom,1,[1,2,3],{tuple, 1}] [] <- empty list We can match a list like this
        
       Eshell V8.2  (abort with ^G)
    
       1> List = [1,2,3,4,5].
       [1,2,3,4,5]
       2> [X|Xs] = List.
       [1,2,3,4,5]
       3> X.           %% the head part.
       1
       4> Xs.          %% the tail part.
       [2,3,4,5]
       5> [L,M|Ms] = List.
       [1,2,3,4,5]
       6> L.           %% first element.
       1
       7> M.           %% second element.
       2
       8> Ms.          %% and the rest.
       [3,4,5]
    more on lists. An example on list, sum of the elements of a list
       sum(N)->
         sum_list(N,0).
    
       sum_list([],Acc)->
           Acc;
       sum_list([X|Xs], Acc)->
          sum_list(Xs, Acc+X).
    In Erlang clauses are scanned sequently until a match is found.

    Week 3

    Higher Order Functions: Some of the widely used higher order functions in Erlang are lists:map/2, lists:foldl/3, lists:filter/2 or lists:filtermap/2 etc. The foldl or foldr reduces the list into a single value, this can be used to find the sum of elements in a list. The function map, maps all element in the list. The filter selects all the elements in the list with a particular property. Examples,
       Eshell V8.2  (abort with ^G)
      
       1> List =[1,2,3,4,5].
       [1,2,3,4,5]
       2> Fun  = fun(X,Sum) -> Sum+X end.
       #Fun<erl_eval.12.52032458>
       3> Result = lists:foldl(Fun, 0, List). %% foldl(Function, Acc0, List)
       15
       
    The foldl/3 function takes a function Fn , Acc0 and a list. Fun(Elem, AccIn) on successive elements A of List, starting with AccIn == Acc0. Fun/2 must return a new accumulator, which is passed to the next call. The function returns the final value of the accumulator. Acc0 is returned if the list is empty.
    Eshell V8.2  (abort with ^G)
      
    1> List =[1,2,3,4,5].
    [1,2,3,4,5]
    2> Fun = fun(X) -> X*X end.
    #Fun<erl_eval.6.52032458>
    3> Result = lists:map(Fun, List).
    [1,4,9,16,25]
    The lists:map/2 function maps each element. The above example find the square of all elements in a list.
       1>lists:filter(fun(X) -> X rem 2 /= 0 end, [1,2,3,4,5,6,7,8,9,0]).
       [1,3,5,7,9]
    The lists:filter function filters out the odd numbers from the given List. more on lists And at the end of the course we had an assignment. The assignment was to implement rock-paper-scissors.

    Course 2: Concurrent Programming in Erlang

    Week 1

    The main feature of Erlang that discussed in the second course was the concurrency in Erlang. In this section we were introduced with the concept of message passing. The only way a process could interact with another process is through this message passing. This message passing can be synchronous or asynchronous. In Erlang both concurrency and parallelism can be acheived. On a single processing element, the concurrent process time share(more). We can spawn a process with the help of spawn primitive. On a multicore processor parallelism can be acheived. A process is a separate computation, running on its own space, time sharing with other process. Consider this
      area({square, X})-> %% pattern
          X*X;            %% action for that above pattern
      area({rectangle, X, Y})->
          X*Y.
    we can convert the above sequential code to concurrent like,
    -module(area_mod).
    -export([area/0]).
    
    area()->
        receive
           {square, X}->
                X*X;
           {rectangle, X, Y}->
                X*Y
        end.
    This can now be run from the shell
    1>c(area_mod).
    {ok, area_mod}
    2>Pid = spawn(area_mod, area,[]).
    <0.65.0, ok>
    3> Pid ! {square, 5}.
    10
    4> Pid ! {rectangle, 3,4}.
    12
    Here we have spawned a process with the spawn primitive and send message {square, 5} to it. It will be send to the process’s(<0.65.0>) mail box and from there it will be handled by the receive statement. process-mail-box If we send a message that wont match any pattern in the receive statement, those messages will reside in the mailbox which can be flushed with the help of flush/0. We were introduced with a the idea of a frequency server, where a server is responsible for alloting frequency to clients. So the basic functionality of the server is like allocating frequency to the clients and deallocating the alloacted frequencies. This is implemented with the help of a pair of lists {Free, Allocated}, and a function to allocate frequencies and a function to deallocate frequencies. Both of these function return new states. We were able to implement this like,
    1>c(frequency).
    {ok, frequency}
    2> frequency:allocate().
    {ok, 10} %% 10 is the allocated freqeuncy.
    3> frequency:allocate().
    {ok, 11} %% 11 is allocated frequency.
    4> frequency:deallocate(11).
    ok
    5> NewFreq = [25, 26, 27, 28, 30].
    [25, 26, 27, 28, 29, 30]
    6> frequency:inject(NewFreq).
    ok.     %% new frequencies added to the list of free frequencies.

    Week 2

    Hot code Loading in Erlang is responsible for six Nine’s avalability. That is Erlang production systems are available 99.9999 % of time. By this method we were able to add new freqeuncies to already running server. This implies that the production system was online during the upgradation. A design philosophy in Erlang is “Let it fail”, this means that if a process fail , let it fail. A process can either execute indefinetly, terminate normally or fail. When a process fails system integrity is destroyed, a message can be sent to a non existent process or will wait for a message from a failed process. So this has to be dealt. Linking is the method of linking process together, so that if a process fails all the process linked to it directly or indiretly will get an error signal(when this signal is received by a process , its default behaviour is self termination). But this can be avoided by converting this signal to message by trapping exit. This is done by setting the process_flag to true,
     process_flag(trap_exit,true)
     `
    Then this will be transformed to
    {'EXIT',FromPid,Reason} %% this is put into the mailbox, just like any regular message.
    Links are bidirectional and monitors are unidirectional. Supervisors are responsible for starting, stopping and monitioring its child processes. The basic idea is that the supervisor must keep its child process alive by restarting them as necessary according to some strategy.more. supervision-tree Erlang also supports Exception handling, throw and catch. Some of the errors that can occur include badmatch, badarith, undef, case clause etc.

    Week 3

    In multicore systems Erlang acheives true parallelism. The processes are scheduled per core amd each of core has separate run queues. And because of this multiple process can be active simultaneously one on each core. In multicore sequential computations can be done in parallel. We were asked to scale the frequency server with two servers each of them handling half of the frequencies. We were able to implement this by creating a routing process which checks which server has less load at that particular time and that server will handle the freqeuncy allocation request. more on multicore erlang


  3. Generic Server in Erlang

    NOTE: There is no introduction here …


  4. Concurrency in Erlang Part-2

    For Part 1


  5. Concurrency in Erlang

    “In computer science, concurrency is the decomposability property of a program, algorithm, or problem into order-independent or partially-ordered components or units.” -concurrency as defined by wikipedia.


  6. My first Functional Programming Language

    Hai, this is going to be my first post. I think I will start this blog by narrating the experience with my first functional programming Language. Erlang! So what is Erlang, for me it is a functional programming language. But wikipedia defines Erlang as “Erlang is a general-purpose concurrent, functional programming language, as well as a garbage-collected runtime system.” Now coming to Erlang. The last week I have attended a course in functional programming, a free course from Futurelearn. The course tutor was Simon Thompson, Professor of Logic and Computation/ Director of Innovation, University of kent. The course duration was for 3 weeks. At first the concept was a little difficult for me but later on it becomes a lot more easier. One of the main difference I fond in Erlang is that there is no looping syntax as in c or other programming languages. For acheiving any looping we have to do recursion. For example: The factorial of a number, In c:

     factorial(x); // function call
    Then the function definition will be.
     int factorial(int x){
      
      if(x==1)
       return 1;
      else
        return x*factorial(x-1);
     }
    In Erlang, this can be defined like,
     fact(0)->
       1;
     fact(X)->
       X*fact(X-1).
    Here we can see the recursion. This can be explained like, if we want to find the factorial of fact(5) it will return 120. Let me explain this further, say we want to find the factorial of 5. Then this can be worked out like,
    fact(5)->
      5*fact(4)->
       5*4*fact(3)->
        5*4*3*fact(2)->
    	 5*4*3*2*fact(1)->
    	  5*4*3*2*1*fact(0)->
    	   5*4*3*2*1*1->
    	    120.
    		
    To execute a Erlang program we have the save this code in a file, say fact.erl(.erl for erlang). Next in the file we have to specify the name of the module, and should write a export list. For this example, this would be like,
    -module(fact). 
    -export([fact/1]).
    module(fact) says that the name of this module is fact which should be same as that of the file name. Export list export([fact/1]) exports the fuction so that it can be called outside the module. In fact/1, fact is the name of the function and 1 is the arity (no of arguments). If there are more than one function to be exported then the list would be,
    -export([fn1/arity_of_fn1,fn2/arity_of_fn2,......]).
    The dot after any expression means an end. Another important thing in Erlang is that the order of execution is sequential. To compile the code from the terminal:
    c(fact).
    >>{ok,fact}
    To call the function:
    fact:fact(5).
    >>120