Wednesday, 25 December 2013

Programmer should know about memory

Hereby am sharing some of points about modern memory architecture

- Intel Hyper-threading enabled single processor core to be used for two or more concurrent executions with just little extra hardware

how exec system call works

Interesting implementation of parallel file upload and do socket polling 

Scenario  you need to look for the incoming data, and do ftp file upload. 

From C program you can invoke exec system call to do ftp file upload 

For FTP  either you can use CURL library  / linux / some other libraries .  I recommend CURL, i've been working on this quite a time.

Lets say:  I've a process  created thread in it ,doing socket select operations , 
               when i receive some X message i need to do file upload. 

Better approach: 

From your packet handler thread , spawn process  obliviously do "fork" , let child process do ftp file upload, parent wait for child to complete file upload. 

After file upload child process exited, followed by process detached . 

Here there is no interruption with packet handler routine, since both packet handling and ftp file upload runs in different process context .. so kernel would take care of scheduling them.. As application we achieving parallelism by this approach. 


Exec : system call could be used to  execute your binary inherit from process that has invoked, meaning exec system call replace current process context with new one. 

For this scenario,  "packet handling & ftp file upload post receive message" -- exec system call in new process context is best approach to achieve. 


Exec system call could be used in different scenarios w.r.t requirements.



Tuesday, 3 September 2013

Perforce Commands


Basic Commands:

P4 opened

function to set p4 client

function p4client()
{
        if [ -z $1 ]; then
                curr=`pwd`
                base=`basename $curr`
        else
                base=$1
        fi
        export P4PORT="p4sw1:1666"
        export P4USER="ssankar"
        export P4CLIENT="$base"
        export F10MKVERBOSE=1
}

P4 diff  <file>  or p4 diff – full diff
P4 revert <filename>
P4 submit –c <changelist no>

#save changelist (cl> to p4 server then pathc in other client

P4 shelve –c <cl no>
P4 unshelve –s <changelist no>

#delete p4 shelve
P4 shelve –d –c <changelistno>
 p4 client -d <clientname>
  
If you facing problem in deleting P4 Client ,  check for pending change list associated with P4 Client ,  revert the changes or you can delete forcibly if you have admin permission
p4 client -d  <p4clietn name>
p4 changes -c  <client-name> -s pending
p4 change -d <clno>
 p4 client -d <client-name>





Tuesday, 9 July 2013

print current previous next line for the context grep in a file

this is the simplest way i believe to get the previous,current, next line for content searched in a file

 #  perl -e 'while($line=<>) {if(!$found){if(grep(/netdump/,$line)){$curr=$line;$found=1;}else{$prev=$line;}}else{$next=$line;print $prev,$curr,$next;;last;}}' < /etc/passwd
haldaemon:x:68:68:HAL daemon:/:/sbin/nologin
netdump:x:34:34:Network Crash Dump user:/var/crash:/bin/bash
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
 #


Monday, 20 May 2013

How ping works

Here am going to share snapshot of how ping works,

we all know that ping is the utility program to test connectivity between hosts.

I am taking two categories ,

1. Ping host which is in same subnet
2. ping host which is in different network

case -1 :
    - when ping host (which is in same subnet ), first ARP broadcast happens , then icmp request/reply processed
    - broadcast mac ff:ff:ff:ff:ff:ff , destination ip address ARP broadcast will be zero as its unknown
case -2 :
    - icmp request sent to gateway as the destination address , as ping host is in different network so none of the present route matches
    - the ping can flow in any of the interfaces on the local host, depends on the destination ip subnet
     -     ping request/reply processed between localhost to gateway - remote host    
ARP cache can have its neighbor entries along with its gateway.

Wednesday, 15 May 2013

Difference between define and enum

define
----------
#define - is called symbolic constants
 - this is the one  among processing unit
 - just replaces the value defined , at the first stage of building your code i.e preprocessing stage
 - values can't be looked in debuggers 
-  definition should followed always  upper letters

#define TRUE  1
#define FALSE 0

enum
-----
- collective of constant integers
- it's not a proprocessor,  enumeration happens in compilation stage

-  advantage is auto increment
eg: instead of
#define ONE 1
#define  TWO 2 etc

use enum val {
 ZERO,
ONE,
etc};

- value always starts from zero, until not defined 
- the values can be looked in debuggers
- can use upper / lower characters


Note that both #define and enum are constants, they don't  memory allotted.

Tuesday, 14 May 2013

Register and Volatile Variable

Let us discuss why do we need register variable and how it helps.

- declaring register variable advices to compiler that this variable is heavily used
- register variable are to be placed in machine registers
- register variables can only be applied to automatic/local scope of variables
- register variables also depends on the hardware, since it directly deal with machine registers
- in case if compiler couldn't able to allot machine register for requesting register variable then the variable will be scoped as "automatic/local" variables.

What is Volatile ?  why do we need them ?

- volatile variable declaration advices compiler that don't optimize this variable, means always fetch the value from the harddisk / main memory
- for ex: there are the cases that values could changed intermediately by some other task where as current running would be using the value which was stored in virtual memory .. so the changed value on the disk it not read in the execution == this is how the compiler optimize the program , note  changing values in run time differs from reading the disk / main memory
- to do so  , declare the variable as volatile so that compiler wont optimize it , and it will always fetch from main memory , though it is inefficient
-- use register /  volatile is there is a critical requirement , otherwise it's my opinion to not use of them ..