Thursday, June 30, 2016

debug a python program

How to debug a python program:

It is important to get know the debugging method ,if we really started to learn a new programming  language:

In Python programming language we have a tool similar to gdb which is pdb.

You can start debugging as below :

 python -pdb -m selectsort.py





more details on pdb - https://docs.python.org/2/library/pdb.html

http://infohost.nmt.edu/tcc/help/pubs/python22/pdb-commands.html

 
s(tep)
Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function).
n(ext)
Continue execution until the next line in the current function is reached or it returns. (The difference between next and step is that step stops inside a called function, while next executes called functions at (nearly) full speed, only stopping at the next line in the current function.)
unt(il)
Continue execution until the line with the line number greater than the current one is reached or when returning from current frame.
New in version 2.6.
r(eturn)
Continue execution until the current function returns.
c(ont(inue))
Continue execution, only stop when a breakpoint is encountered.
j(ump) lineno
Set the next line that will be executed. Only available in the bottom-most frame. This lets you jump back and execute code again, or jump forward to skip code that you don’t want to run.
It should be noted that not all jumps are allowed — for instance it is not possible to jump into the middle of a for loop or out of a finally clause.
l(ist) [first[, last]]
List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count.
a(rgs)
Print the argument list of the current function.
p expression
Evaluate the expression in the current context and print its value.


Friday, June 24, 2016

Rust pointer implementaiton

https://aml3.github.io/RustTutorial/html/02.html#Starting_to_Corrode:%3Cbr%3EPointers,_Memory,_Strings,_and_I/O

I am really scared of pointers in C programming itself.. Rust have more features than C for pointers .. :-)


But this really good read the above link on Rust memory management. I believe that there are lot of values for learning these new features .

Rust vs C program speed

I was just going through some of the Rust programs and tutorials...

This is one of the site which benchmark programing language speed. Rust programming language doesn't compromise the speed

http://benchmarksgame.alioth.debian.org/u64q/rust.html

http://benchmarksgame.alioth.debian.org/why-measure-toy-benchmark-programs.html


If you are really looking for some Rust applications , here you can browse.

http://pcwalton.github.io/blog/2013/05/20/safe-manual-memory-management/


This is one good link for those who want to start with Rust - https://aml3.github.io/RustTutorial/

Wednesday, June 15, 2016

Search a Tree - C program

Computer is a robot , our program  should identify the repetitive task and make computer do that repetitive task with less lines of code.... programmer should be able to identify the repetitive task and apply recursive functions..

Search a value in a Tree - C program

the beauty of this code is that searching the tree (function:searchtree ) is going to be the one function.But we will make the computer to do the same task repetitively just passing the next node.


#include
#include


struct node {
    int a;
    struct node* left;
    struct node* right;
};

struct node* insert_node(int a){

    struct node* newnode;
    newnode = (struct node*) malloc (sizeof(struct node));
    newnode->a = a;
    newnode->left = NULL;
    newnode->right = NULL;

    return newnode;
}

int searchtree(struct node* root,int num){
    if(root == NULL)
        return 0;
    if(root->a == num ||searchtree(root->left,num))
        return 1;
    if(root->a == num || searchtree(root->right,num)){       
        return 1;
    }   
   
}

       
   
           

int main(){

    int num;
    struct node* root;
   
    root = insert_node(1);
    root->left = insert_node(2);
    root->right = insert_node(3);
    root->right->right = insert_node(4);   
    root->right->left = insert_node(5);   
    root->left->left = insert_node(6);
    root->left->right = insert_node(7);
    root->left->right->left = insert_node(8);

    printf("type a number:");
    scanf("%d",&num);

    if(searchtree(root,num)){
        printf("number found in tree");
    }
    else{
        printf("number not found in tree");
    };
    return(0);
}

Tuesday, June 14, 2016

Python linked list

If you are planning to refresh your data structure knowledge, I would recommend this website http://www.geeksforgeeks.org/data-structures/

Tree or Linked list datastructure  programming heavily use the recursion concept ... recursion is something that sometimes cause me brain crash :-)

But once you understand it and learn how to use recursion , then that is where you start mastering datastructure concepts.

If you look at Tree or linked list, it is a repetition of a node or a same structure(http://quiz.geeksforgeeks.org/binary-tree-set-1-introduction/)   .. so when there is repetition of something, then there is always scope for recursion concepts to apply...

Python Linked List:

Python programming language also have OOP concepts. So that means we can define a class http://www.diveintopython.net/object_oriented_framework/defining_classes.html

You can go through the linked list details in the below link
http://quiz.geeksforgeeks.org/linked-list-set-1-introduction/ . You will see two classes they defined for implementing linked list.

I was just trying to build without 2 classes. We can build a linked list, I dont know why they used 2 classes..I am not sure on how python is doing this dynamic memory allocation.



Program:



Strace:


Monday, June 13, 2016

Strace on a tree structure C program

When I tried to access 'unallocated node value' , as usual 'Segmentation fault' came as output :-)

But i am not fully clear on how mmap() system call allocate the memory...

I also came to know that mprotect() system call sets the protection,

If the calling process tries to access memory in a manner that violates the protection, then the kernel generates a SIGSEGV signal for the process.

Program and Strace details:



 
 
 

Thursday, June 2, 2016

Try Strace on wget command

Its fun to try Strace on different commands to see what actually happens behind the command. There are some very good options to filter out required systems calls from the log.
The following link gives examples on how to use Strace http://www.thegeekstuff.com/2011/11/strace-examples/

one interesting system call is 'select' . I was only heard about 'select' in sql queries,but this is a different place i am seeing select :-)

"select() and pselect() allow a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation (e.g., input possible). A file descriptor is considered ready if it is possible to perform a corresponding I/O operation"



The sigaction() system call is used to change the action taken by a process on receipt of a specific signal.



 socket system call: