Thursday, July 2, 2015

Yum to Install Packages to RHEL

If you are not registered or subscribed to RHN, you can easily install packages using yum with the help of CentOS 5/6 repo. All you need to do is follow this procedure:

with super user open a new file
   # vim /etc/yum.repos.d/centos.repo
   Paste the below text and hardcode either 5 or 6 based on your redhat version

      [centos]
      name=CentOS $releasever - $basearch
      baseurl=http://ftp.heanet.ie/pub/centos/6/os/$basearch/
      enabled=1
      gpgcheck=0


Now execute the yum command to install any packages that you need like gcc
# yum install gcc 


Friday, January 9, 2015

10 rules for developing safe code

1.  Restrict all code to very simple control flow constructs. Do not use GOTO statements, setjmp or longjmp constructs, or direct or indirect recursion.

2.   All loops must have a fixed upper bound. It must be trivially possible for a checking tool to statically prove that a preset upper bound on the number of iterations of a loop cannot be exceeded. If the loop-bound cannot be proven statically, the rule is considered violated.

3.     Do not use dynamic memory allocation after initialization.

4.     No function should be longer than what can be printed on a single sheet of paper (in a standard reference format with one line per statement and one line per declaration.) Typically, this means no more than about 60 lines of code per function.

5.    The assertion density of the code should average a minimum of two assertions per function. Assertions must always be side effect-free and should be defined as Boolean tests.

6.     Data objects must be declared at the smallest possible level of scope.

7.  Each calling function must check non-void function return values, and the validity of parameters must be checked inside each function.

8.   Preprocessor use must be limited to the inclusion of header files and simple macro definitions. Token pasting, variable argument lists (ellipses), and recursive macro calls are not allowed.

9.     The use of pointers should be restricted. Specifically, no more than one level of dereferencing is allowed. Pointer dereference operations may not be hidden in macro definitions or inside typedef declarations. Function pointers are not permitted.

10.  All code must be compiled, from the first day of development, with all compiler warnings enabled at the compiler’s most pedantic setting. All code must compile with these setting without any warnings. All code must be checked daily with at least one—but preferably more than one—state-of-the-art static source code analyzer, and should pass the analyses with zero warnings.

refer:http://sdtimes.com/nasas-10-rules-developing-safety-critical-code

Friday, August 1, 2014

Perl Modular Programming .. Tutorial and short notes

This post describes Perl Modules in short or kind of notes taken from "Advance Perl Programming" book.

PERL Modules

        * Modules are nothing but library packages, that can be
          reused efficiently and provides a means for Modular
          Programming

        * Perl uses keywork 'package' to define modules and loads
          predefined modules using 'use' and 'required' keywords.
          To access package specific variables and function it
          uses '::' notation.

        * required keyword is similar like C #include, which loads
          the perl file. e.g. required "myfunction.pl";

        * 'use' statement is executed as soon as it is parsed. So
          at compile time it is guarantee that all required modules
          have been successfully loaded at compile time.

        * Perl modules files using stored with extension '.pm'

Perl Package/Module example


simple example of perl package includes variable and function 
definitions. E.g. mypackage.pm

        #!/usr/bin/perl

        package mypackage; # same as filename

        # global variables
        my $space_count;

        sub readspaces
        {
                my ($string) = (@_);
                $space_count = map $_, $string =~ /\s/gs;
                return $space_count;
        }

        sub replacespaces
        {
                my ($string) = (@_);
                $string =~ s/\s/\#/g;
                return $string;
        }

        1; # to avoid error "mypackage.pm did not return a true value"
           # also when it is included with 'use' or 'required' module
           # is expected to return a boolen success value


Main package and variables

In absence of explicit package declaration, perl assumes a package 'main'. The built-in variables such as $!, $_, $@, @ARGV and %ENV are belongs to package main and these are only truly  global variables in Perl.

 

Symbolic References

Used more in Object orientation. Calling a function or variable using symbols.    E.g.  Calling function from package mypackage,
 
             $mypackage::myfunction(10,20);
    
The symbolic reference for this above function calling is:
           
             &{"mypackage::myfunction"}(10,20);


Package Initialization & Destruction

Use of BEGIN block does a initialization work for perl modules. The perl when it seens BEGIN block it executes right away and does further compilation later on. Simple requirement when we want to include a library before compilation, we can add below line in BEGIN block:

          sub BEGIN {
              use lib qw(../include); #prepends the directory to @INC
          }
 

Similarly END block is used to do clean-up before the program exits.

Enforcing privacy

Use 'my' operator at file scope.
          e.g.
                package A;
                my $a = 10;

                package B;
                print $A::a; # error, no such variable in package A
                print $a # print 10
 
Define a function with 'my'
          e.g.
                my $rs_func  = sub {
                        };
                #call as
                &$rs_func;

 

Importing Symbols

Creating namespaces and using function names or variables directly without using '::' operator. While defining the package use Exporter module insert all the package function names and variables to Exporter array. Which this inherits to the calling program. e.g.

          package BankAccount;
          use Exporter;
          @ISA = ('Exporter');
          @Exporter_OK = qw/withdraw deposite/; # or use below
          # Exporter_OK states only these symbols are ok to export
          @Exporter = qw/withdraw/; # this gets all the exported symbols

          sub withdraw { ... }
          sub deposite { ... }
          1;
 
          # myfile.pl
          #!/usr/bin/perl
          use BankAccount ('deposite'); # this is analogus to
             # BEGIN { require BankAccount;
             #         BankAccount::import('deposite');
             # }

 

Package nesting

Since all packages are global in scope nesting is not permitted. but one can use a directory, the '::' gets replaced with a filename separator. e.g.
 
          use Math::Poisson; # Math/Poisson.pm
          Math::Poisson::Calculate_Probability($lamda, $a);

 

Autoloading

If you invoke a function called Test::func(), for example, and if func() has not been defined in module Test, Perl automatically looks for a subroutine called Test::AUTOLOAD(). If such a subroutine exists, Perl calls it with the arguments
that were passed to func(). In addition, a variable called  $AUTOLOAD is set to the full name of the function that was  called ("Test::func").

Usefull example is calling system with linux command
        #-----------------------------------------------
        package Shell;
        #------------------------------------------
        sub AUTOLOAD {
            my($program) = $AUTOLOAD;
            # We are just interested in the command name, not in
            # the package name
            $program =~ s/^.*:://;
            system ("$program @_");
        }
 
        #------------------------------------------------
        use Shell;
        ls ('-lR'); # Triggers a call to AUTOLOAD since sub ls()
        # doesn't exist
        mail ('-s "This is a test" joe@foo.com < letter.txt');


Accessing Symbol table


Dumping a package symbol table using stashing the function  e.g. function name foo can be accessed by a hash %foo
                TODO
 


Questions on perl modules/packages


1. How do you make variables public, private, or protected in Perl?

        use 'my' for private and 'our' keyword for public
        use 'local' might bring protected version

2. use vs require keyword. How to create module

        Use and Require are most common methods used to load modules.
        Use:
          1) It is evaluated at compile time only.
          2) it is used only to load modules.
          3)No need to give file extensions when we are using "use"
        Require:
          1)This is evaluated at Run time
          2)This method is used for both libraries and modules.
          3)Need to give file extensions like pl,pm etc..

3. List the programming guidelines that must be followed when programming
   with Perl modules

        * The Module and the package name must be the same.
        * The only allowable package name needs to have the extension “.pm”
        * The package must be derived from the exporter class when no
          object-oriented technique is used


Friday, July 18, 2014

GDB Tutorial (In short)

Run GDB with argument
-----------------

gdb
..
(gdb) run arg1 arg2

Breakpoints
-----------
before run
(gdb) break
(gdb) break
(gdb) break e.g. break hello.c:5

To clear breakpoints use clear command or for breakpoint number
        with delete command 'enable' and 'disable' breakpoints

Examining variable
-----------------
Use 'display' command e.g. (gdb) disp var1
And 'undisplay' to forget variable display

Use 'print' command e.g. (gdb) print var1

Use 'printf' command e.g. (gdb) printf "%d\n", var1


Stack Manipulation
------------------
Use command 'backtrace' or 'bt' this will show you current function
        call stack

more info:

backtrace -- Print backtrace of all stack frames
bt         -- Print backtrace of all stack frames
down -- Select and print stack frame called by this one
frame -- Select and print a stack frame
return -- Make selected stack frame return to its caller
select-frame -- Select a stack frame without printing anything
up         -- Select and print stack frame that called this one
list           -- list the ten lines before previous ten line listing.
                                    list

Stepping methods
----------------
Use 'finish' command to exit from current function and return to 
        calling function
        Use 'stepi' to step for a single assembly instruction
        Use 'advance' to continue to specified location e.g. advance 
       

Use 'jump' and 'continue' with location specified

Changing variable and values at run time
----------------------------------------
Use 'set variable' e.g. (gdb) set (i = 20) or 
        (gdb) set variable i = 20

Hardware watchpoint
-------------------
Use 'watch' command , its a special breakpoints this will trigger 
        whenever expression changes.
e.g. (gdb) watch var1
shows 'old value' and 'new changed value'

Attach to a running process
---------------------------
Need process ID and then use 'attach' command with the PID 
        to attach and break the running program

GDB coredump
------------
set ulimit -c 'unlimited'
gdb -tui -c core
In TUI mode, you'll be greeted with a screen of information, 
        telling you why the program exited  ("signal 11, Segmentation fault"),
        and the highlight will be on the offending line. 
use bt 

Window functions
----------------
All with -tui option (terminal user interface)
       In TUI mode, you can get a list of existing windows with the 
       info win command. You can then change which window has
       focus with the focus (or fs) command. Focus takes either a 
       window name, or "prev" or "next" as an argument. 
       Valid window names are "SRC" (source window), "CMD" 
       (command window), "REGS" (registers window), and "ASM" 
       (assembly window). 
       See the next section for how to use these other windows.

Display Registers and Assembly
------------------------------

in TUI mode, use following commands
layout src    Standard layout—source on top, command 
                                window on the bottom
 
layout asm Just like the "src" layout, except it's an assembly 
                                window on top

layout split Three windows: source on top, assembly in the 
                                middle, and command at the bottom layout reg
                                Opens the register window on top of either source
                                or assembly, whichever was opened last
        
        tui reg general Show the general registers
 
tui reg float    Show the floating point registers

tui reg system    Show the "system" registers

tui reg next Show the next page of registers—this is important 
                                because there might be pages of 
        registers that aren't in the "general", "float", or "system" sets

Working with macro
--------------------
To see the definition of all macros use command 'info macro [-a]
e.g. (gdb) info macro MAXVAL
      (gdb) info macro __STDC__
macro define    -- Define a new C/C++ preprocessor macro

macro expand    -- Fully expand any C/C++ preprocessor macro
                                               invocations in EXPRESSION

macro expand-once   -- Expand C/C++ preprocessor macro
                                                invocations appearing directly in EXPRESSION

macro list     -- List all the macros defined using the `macro define'
                                                command

macro undef     -- Remove the definition of the C/C++ preprocessor
                                                macro with the given name

Specifying debugging targets
 --------------------------- 

It is possible to run GDB for different target architectures. Use following commands: 
         set architecture    
         show architecture 
         set processor
         processor

         target child        -- Win32 child process (started by the "run" command) 
         target core         -- Use a core file as a target
         target exec         -- Use an executable file as a target
         target extended-remote   -- Use a remote computer via a serial line
         target record     -- Log program while executing and replay execution 
                                        from log 
         target record-btrace    -- Collect control-flow trace and provide the 
                                                   execution history

TBD....







GCC tutorial (in short)

* GCC - GNU Compiler Collection

* Current version: 4.9.0 (for this document)

* Supporting languages: 
        C, C++, Fortran, Objective-C, Objective-C++, Java,  Ada and Go

* Front end compilation: 
       Part of compiler that is specific to programming languages is called front end

* GCC compiler names for particular languages:
C    - gcc
C++  - g++
Ada  - GNAT

* Supported C standard: Currently support three standards

  ANSI C - (ISO standard) C89 and C90 as defined in year 1989 (ANSI C) 
       and 1990 (ISO).
       
       > For selecting standards use options '-ansi, -std=c90 or -std=iso9890:1990'
       > To obtain all the diagnostics required by the standard use option '-pedantic' 
          or '-pedantic-error'
       > Amended standard (AMD1) - C94 and C95; use option '-std=c9899:199409'
>New edition of ISO std - C99; use option '-std=c99 or -std=iso9899:1999'
       >C11 - use option '-std=c11 or -std=iso9899:2011"

        By default GCC uses extensions to c standards and uses '-std=gnu90 or 
        -std=gnu99 or -std=gnu11' default is -std=gnu90

* Supported C++ standard:

> -ansi, -std=c++98, -std=c++03, -std=c++11
        >  By default g++ support '-std=gnu++98' option    

* GCC performs following steps after invoking

       >Preprocessing: option -E is used to stop after preprocessing stage
       >Compilation:    option -c is used to just compile the program, and 
                                 doesn't run the linker. The output are object files 
                                 by the assembler.
       >Assembly:        -S option is used to stop the compilation stage and 
                                should not assemble.
       >Linking

* Kind of output and compilation option (based on file name extensions)

file.c, c source code that must be preprocessed
     file.i, c source code that should not be preprocessed
     file.h, c header file to be turned into precompiled header
file.C, c source file that must be preprocessed
file.s, assembler code
file.S, assembler code that must be preprocessed

* Specifying input language

Use option -x to specify input language e.g.
        -x c : C programming (c-header cpp-output)
        -x c++ : C++ programming (c++-header, c++-cpp-output

* Common gcc command line options used

     -c : Compile or assemble the source file, but do not link. 
           The ultimate output is an object file
     
     -E : Stop after preprocessing stage, do not run compiler. 
           The output is in the form of preprocessed source code

     -o filename : Place output in a file
  
     -v : Print the compilation stages on standard output, kind of 
           verbose like output of compilation process.

    -### : Similar to '-v' but print only non-executing data like configure 
              options, compile path, library path etc

    -pipe : Use of pipes to communicate between various stages of compilation.
               Use for optimizing compile time where gcc creates lots of temporary 
               files and to avoid that pipes are used. (uses more RAM)

    -wrapper : Invoke all sub-commands under the wrapper program. e.g.
                     gcc -c a.c -wrapper gdb, --agrs
                     This invokes all subprograms of gcc under ‘gdb --args’, 
                     thus the invocation of cc1 is ‘
                     gdb --args cc1

    -fplugin=name.so : Load the plugin code in file name.so, shared object 
                                   is opened by dlopen by compiler 
                                 -fplugin-arg-name-key=value

     -fno-builtin
     -fno-builtin-function : Do not recognize build in function that do not begin 
                                        with '__builtin__' as a prefix
                                        e.g.
                                       #define abs(n) __builtin_abs ((n))
                                       #define strcpy(d, s) __builtin_strcpy ((d), (s))

     -g : Produces debugging information

     -ggdb : Produces debugging flags used by gdb debugger

* GCC diagnostic message formatting options

-fmessage-length=n : Format error message to n characters
-fdiagnostics-color[never|always|auto] : Use of color in diagnostic messages

* GCC options to request or suppress warnings

-fsyntax-only : Check the code for syntax errors

-fmax-errors=n : Limit the max error messages to n

-w : Inhibit all warnings

-Werror  : Make all warnings as errors

-Wfatal-errors : Abort compilation once a first error occurred

-Wpedantic
-pedantic   : Issue all warnings demanded by strict c standards

-Wall : Enables all the warnings

-Wextra : enables all extra warning flags like pointer comparison 
                  with int zero etc

-Wformat : Check calls to printf scanf formatting functions to make 
                     sure argument supplied have type appropriate formatting.

Avoid:

1. Grouping of multiple options e.g., -dv is different than -d -v 







Thursday, March 20, 2014

Tag list to efficiently browse source code files for different programming languages: vim plugin/ctags

Installing ctag utility


Download and install ctag utility (ctags-5.8.tar.gz ) from location: http://ctags.sourceforge.net/
Follow the instructions:
1.       Extract the tar contents by executing tar command
tar –xvzf ctags-5.8.tar.gz
2.       change directory to ctags-5.8 and execute following command to install ctags
./configure
make
make install
3.       If the ctag installation uses non-standard location, Setup .vimrc as per installed ctag binary path: set the following path variable in .vimrc
 let Tlist_Ctags_Cmd = "//bin/ctags"

Few tips to setup .vimrc for indentation, line numbers, syntax highlighting etc   

Example .vimrc
 set nocompatible | filetype indent plugin on | syn on |
 set nu
 set shiftwidth=4
 set tabstop=4
 let Tlist_Ctags_Cmd = "/opt/rhel/bin/ctags"
nnoremap   :TlistToggle

Installing vim-taglist plugin


Download and install taglist plugin for vim version 6.0 or above, download location: http://sourceforge.net/projects/vim-taglist/files/
Follow the instructions as:
1.       Copy the downloaded file to your home directory containing .vim directory
2.       Extract the zip contents
3.        The unzip contains plugin/taglist.vim file (main taglist plugin file) and doc/taglist.txt (documentation file)
4.       Open any source code file in vim and use ‘:TlistToggle’ to open-close tag list window.


  

Monday, October 28, 2013

How to conduct fresher interview, just to start with....



Marks
Topics
Max marks
Marks obtained
English fluency
5

General Awareness
5

Enthusiasm/Quick learnability
5

Thought Processing
5

Programming concepts, C
5

OS concepts
5

Networking concepts
5

Program implementation and walkthrough
5

Extra testing or debugging tools
Knowledge
5

Curriculum
5

Total Max Marks                                              50                           Obtained Marks              

Status:

Feedback/Comments:


Checklist details:
English fluency: Check the presentation skills and in general English communication
General Awareness: Verify the candidate is aware of new technologies and trends in his/her area of interest. Like IPv6, data networks, visualization, could/mobile computing, multicore cpus etc
Enthusiasm/Quick learn-ability: Ask candidate about his/her any quick help in curriculum projects or in team work that helped others/project to fix some issues. Check candidate’s attitude and how much he is passionate and sincere about this work/interest.
Thought Processing:  Ask him/her to solve a puzzle and while solving ask him to speak on about his/her approach
Programming concepts: Ask him/her general programing concepts like auto/static variables, operators, recursion, structures, lists, arrays etc
OS concepts: Check candidate knowledge about OS basics, IOs, Memory etc
Networking concepts: Ask candidate about purpose of OSI/TCP models, each layer functionality and example devices that work in specific layers, different layered protocols and their implementation.
Program implementation and walk-through: Ask candidate to write down small program on a paper and ask him to iterate with some example inputs. Also ask him to add more to his code-base for performance and security. The program should be any of like string manipulation, factorial, array sorting etc
Extra testing or debugging tools Knowledge: Add-on likes any diploma courses, extra knowledge about tools like compilers, debuggers, sniffers, code management tools, testing methodologies etc
Curriculum:  his/her graduation performance


Few interview questions:
A. Ask him about his/her final year project, what was his/her role and why he/she choose that role in team. Discuss with him/her 5 mins and ask your doubts, check how he respond.

B. Networking:
1. Define networking?
2. List OSI layers and explain each layer with examples
3. Explain application protocols like SNMP, HTTP, DNS, DHCP, SSH, etcs
4. What is IP addressing? Explain different classes? Explain subnets and its benefits
5. What is bridges? What is gateway? difference between these two devices
6. what is firewall?
7. what is Routers and Switches and differences?
8. Explain end to end flow of a information using OSI/TCP model

C. C programming:
1. What is difference between array and link list? which is more efficient technique and why?
2. What is static keyword?
3. Difference between pass by reference and pass by value data to an function. Explain with an example
4. Write down a loop to calculate prime numbers
5. write down a code-base for reversing a string