Subscribe for email updates

Get all updates to your inbox to stay tune.

Curve tracing programs - Draw sin(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Graphics programming in C

Learn how to write basic and advanced graphics programming in c language

Curve tracing programs - Draw Cos(x) curve using Graphics programming in C

Learn how to draw curve tracing graphics programs in c language

Make file tutorial

A step by step guide to learn how to write make file in Linux with examples

Thursday, December 8, 2011

C programming best practices

 this article describes some of best practices in C programming. Will be useful.
check it out.

Click here

Friday, December 2, 2011

MALLOC Failure Test Program in C

Wanna test malloc failure without changing the binary of the application. [Works good for C]

Try the below program. 

#############
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <unistd.h>

static int mCnt;
typedef void* (*MallocFn)(size_t size);

MallocFn mMalloc;
extern "C" {
  void* malloc(size_t size)
  {
    mCnt++;
    if (!mMalloc){
        mMalloc = (MallocFn)dlsym(RTLD_NEXT, "malloc");
    }
    if(mCnt >=20){
      printf("MyMalloc is called\n");
      return NULL;
    }
    else{
      return mMalloc(size);
    }
  }
}
 
#############
compilation : gcc -shared -ldl -fPIC mallocTest.c -o libMyMalloc.so
before running your application, export the LD_PRELOAD evn.
export LD_PRELOAD=<absolute path of the libMyMalloc.so file>
run the binary or application. 

    if(mCnt >=20){

Above line controls the malloc behaviour, when application uses 20th malloc onwards, memory will not be allocated.


Regards 
-Praveen

Thursday, December 1, 2011

Regular Expression in C [gnu library]

How to use regular expression in C

Sample Program. 

################# 
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define RE_DIGIT     "^[0-9]*$"
#define RE_ALPHA     "^[a-zA-Z]*$"
#define RE_ALNUM     "[[:alnum:]]"
#define RE_IPV4      "^((([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])\\.){3})([2][5][0-5]|([2][0-4]|[1][0-9]|[0-9])?[0-9])$"
int main(int argc, char **argv) {
    int result;
    regex_t rx;
    regmatch_t *matches;
    char buf[1024];

    result = regcomp(&rx, RE_IPV4, REG_EXTENDED);
    if (result != 0) {
        fprintf(stderr, "Invalid regular expression: %s\n", buf);
        regfree(&rx);
        return -1;
    }

    matches = (regmatch_t *) malloc((rx.re_nsub + 1) * sizeof(regmatch_t));
    if (!matches) {
        fputs("Out of memory!\n", stderr);
        return -1;
    }

    for (;;) {

        fputs("Enter IP Address string (empty line to exit): ", stderr);
        memset(&buf, '\0', 1024);
        if (!gets(buf) || !buf[0])
            break;

        result = regexec(&rx, buf, rx.re_nsub + 1, matches, 0);

        if (!result) {
            int i;
            if (matches[i].rm_so != -1) {
                fprintf(stderr,
                        "Match %2d from %2d to %2d: \"%.*s\"\n",
                        i,
                        matches[i].rm_so,
                        matches[i].rm_eo,
                        matches[i].rm_eo - matches[i].rm_so,
                        buf + matches[i].rm_so);
            }
        } else {
            regerror(result, &rx, buf, sizeof(buf));
            fprintf(stderr, "No match: %s\n", buf);
        }

    }

    regfree(&rx);
    fputs("Goodbye\n", stderr);
    exit(0);
}

################ 
-- 
-Praveen

Wednesday, November 30, 2011

power of proc4: process IDs

proc file system list all process IDs as  directories along with other entries.
Each process ID shows as a directory and in that all information about process like open fds,environment when process started, stack.. etc will be shown as files. You can just cat the file to see the process information.


# ls /proc
1     13762  1435  19     22421  27815  304  38   42   50    56  65  786  940        dma          kallsyms    meminfo       schedstat      timer_list
10    1383   1437  2      22425  28     31   389  43   5024  57  66  79   acpi       driver       kcore       misc          scsi           timer_stats
1042  1391   1439  20     22472  289    32   39   44   5028  58  67  8    buddyinfo  execdomains  keys        modules       self           tty
11    1398   1441  21     22482  29     33   390  45   51    59  68  80   bus        fb           key-users   mounts        slabinfo       uptime
110   14     1449  22     23     290    34   391  46   5170  6   7   9    cgroups    filesystems  kmsg        mtd           softirqs       version
12    1404   1450  22150  24     29665  35   392  47   5174  60  73  900  cmdline    fs           kpagecount  mtrr          stat           vmallocinfo
1207  1415   15    22152  25     29990  36   393  476  52    61  74  901  cpuinfo    interrupts   kpageflags  net           swaps          vmstat
1260  1417   16    22154  26     3      368  4    48   53    62  75  902  crypto     iomem        loadavg     pagetypeinfo  sys            zoneinfo
13    1431   17    22369  27     30     37   40   49   54    63  76  903  devices    ioports      locks       partitions    sysrq-trigger
1307  1433   18    22373  27810  303    370  41   5    55    64  77  904  diskstats  irq          mdstat      sched_debug   sysvipc

Monday, November 28, 2011

power of proc-3: Meminfo

Run the following command to get the memory information about your system. It also shows much more info , I have just truncated the output :-)


# cat /proc/meminfo
MemTotal: 3924928 kB
MemFree: 537432 kB
Buffers: 674604 kB
Cached: 2303392 kB
SwapCached: 2248 kB
Active: 2034388 kB
Inactive: 963744 kB

Power of proc-2: cpu info

Run the following command to get the cpu info on your system.
It shows how many processor ,speed, model.. etc.

#cat /proc/cpuinfo

processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 37
model name : Intel(R) Xeon(R) CPU X5690 @ 3.47GHz
stepping : 1
cpu MHz : 3458.000
cache size : 12288 KB
fpu : yes
fpu_exception : yes
cpuid level : 11
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts xtopology tsc_reliable nonstop_tsc aperfmperf unfair_spinlock pni pclmulqdq ssse3 cx16 sse4_1 sse4_2 popcnt aes hypervisor lahf_lm ida arat dts
bogomips : 6916.00
clflush size : 64
cache_alignment : 64
address sizes : 40 bits physical, 48 bits virtual
power management:

Sunday, November 13, 2011

jobs @ redbus

checkout. may be suits for your profile.
http://www.redbus.in/jobs.aspx

amazing Linux stuff

Hello friends
I could see  that this site has got lot of info about linux and especially GDB.

 I was looking for such info since long time probably my search word tick correct today. If you really interested in how GDB works and to know internals of booting, why not printf for debug.. etc. I  strongly recommend to visit this site.

I started reading today. I will post more about this site and about its content. So that you will get benefit out of it.

Here we go >>>  http://www.dirac.org/linux/

Thanks and Hats up to  Mr Peter.

chance to win $5k for your idea.

An idea can win $5K.
You have an idea, then check it out.

Click here to see your idea can win $5k

Saturday, November 12, 2011

Power of Proc-1: Linux Version

Lets start something interesting about proc pseudo file system.
To know which version of Linux you are using. Cat this file


# cat /proc/version
Linux version 2.6.32-35-generic (buildd@allspice) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #78-Ubuntu SMP Tue Oct 11 16:11:24 UTC 2011

There may be many ways to get this info, but lets limit this discussion to proc file system.

Friday, November 11, 2011

Display message (banner) during Login into Linux machine

There may be cases where you have shared your linux machine password with your colleagues and friends. some times it may require that you want to inform them something about that machine during login. Or you want to welcome them immediately after login into the machine.
 This can be done using a file /etc/motd in linux.
 Open this file, edit it. Add whatever message you want to display during login. 

For example
------------------------------------
NOTE: This machine is used for final
test please do not alter configuration.
 If any queries, please write to
adminuserxyz@blaablaa.com
------------------------------------
 have fun!!

Friday, November 4, 2011

EPF balance online

Here's some useful information.

Here's a link where you can check your EPF balance online. This helpful for getting the PF from your previous employer transfered to your current emploter PF account.
Click here

Celebrate !! :-)

Thursday, November 3, 2011

Check Aadhar card/UID enrollment status online

It is very useful link as everyone eager to see Aadharcard.

You can get status here.
Check Aadhar card enrollment status online

Monday, October 31, 2011

Valgrind Manual pdf

Valgrind is such a powerfull tool, very useful for developers. I like it very much.

Valgrind Manual pdf version

rpm --repackage option is removed.

Hey, I was trying to use rpm repackage optio for rollback purpose. But recently I came to know that this option is removed in rhel61. This option was never been supported option.

There are few discussion I could see on this topic. You can find more information here.

More info can be found here.
#1 https://www.redhat.com/archives/rhelv6-beta-list/2010-June/msg00055.html
#2 https://www.redhat.com/archives/rhelv6-beta-list/2010-June/msg00056.html
#3 http://lists.rpm.org/pipermail/rpm-maint/2008-February/001906.html

#2 has got two alternatives to handle rollbacks.

Saturday, October 1, 2011

test

http://www.blogcatalog.com/directory/technology/internet/web_programming/

Saturday, April 23, 2011

cool tips introduction video.

Introduction video.

The reason why i am posting this is to let you know that you can create your own vedios with own scripting. Give it a try. Happy direction.

Thursday, April 14, 2011

cool video on Virtual Memory management

Friend, I found this video in youtube which is really good.
Clear cut explaination of VMM the Virtual memory Management.
I would recomment - watch this video to learn about VMM.
This is very useful for beginners.
http://www.youtube.com/watch?v=Fx_I6-dL__0&feature=player_detailpage

Thanks learnwithtechies team for such a nice tutorial.

Sunday, April 3, 2011

Congrats to one and all - Chak de India

It is pleasure and no hesitation to be proud to say "Chak de India".
I would also like to say "WOW Gautham. Awesome. extrardinory play."

yesterday after India won the match I came out at BTM bangalore. Roads are blocked and all came outside, starting from kid to old, everyone is dancing on the roads. ofcourse I joined too.

I am not sure how to express the happyness but thats what happyness.

Great Cup. India deserved it.

once again 'Chak De India"

Friday, March 18, 2011

C Developer tip - Printing source info in C Programme

Lets start soemthing intresting which is commonly useful for C developers.
 
if you want to print filename, function name and line number  in a c programme, you can make use of following macros.
__FILE__
__func__
__LINE__
 
 
 
Example:
/tmp# cat x.c
main()
{
    printf("file='%s' \n function='%s' line='%d' \n",__FILE__,__func__,__LINE__);
}
/tmp# ./a.out
file='x.c'
 function='main' line='3'
/tmp#
 
This is very useful while programme is in panic and  you want to exit gracefully.
For example: malloc not succeeded.
 
 

Testing Posting message from mail

Hello, This is test msg which I am posting directly from email.

Thursday, March 17, 2011

Some info about SIM CARD


do you know what is SIM stands for

SIM stands for Subscriber Identity MOdule.
What is the name for the card which we insert into Mobile.
That is called
UICC- Universal Intergated circuit card.

Monday, March 14, 2011

Valgrind usage

Valgrind is a such a great tool to identify memory leaks, invalid frees, usage.. etc.
I would like give brief intro

Install valgrind on Ubuntu
apt-get install valgrind

Example Programme:
-----------------------------
main()
{
int *x;
x=malloc(10);
x=malloc(10);
free(x);
free(x);
}
-----------------------------

Valgrind report:
root@ubuntu:/home/manu/valgrindtest# valgrind --tool=memcheck --leak-check=full --show-reachable=yes ./a.out
==4060== Memcheck, a memory error detector
==4060== Copyright (C) 2002-2009, and GNU GPL'd, by Julian Seward et al.
==4060== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==4060== Command: ./a.out
==4060==
==4060== Invalid free() / delete / delete[]
==4060== at 0x4C270BD: free (vg_replace_malloc.c:366)
==4060== by 0x40059F: main (in /home/manu/valgrindtest/a.out)
==4060== Address 0x51b0090 is 0 bytes inside a block of size 10 free'd
==4060== at 0x4C270BD: free (vg_replace_malloc.c:366)
==4060== by 0x400593: main (in /home/manu/valgrindtest/a.out)
==4060==
==4060==
==4060== HEAP SUMMARY:
==4060== in use at exit: 10 bytes in 1 blocks
==4060== total heap usage: 2 allocs, 2 frees, 20 bytes allocated
==4060==
==4060== 10 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4060== at 0x4C274A8: malloc (vg_replace_malloc.c:236)
==4060== by 0x400575: main (in /home/manu/valgrindtest/a.out)
==4060==
==4060== LEAK SUMMARY:
==4060== definitely lost: 10 bytes in 1 blocks
==4060== indirectly lost: 0 bytes in 0 blocks
==4060== possibly lost: 0 bytes in 0 blocks
==4060== still reachable: 0 bytes in 0 blocks
==4060== suppressed: 0 bytes in 0 blocks
==4060==
==4060== For counts of detected and suppressed errors, rerun with: -v
==4060== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 4 from 4)


Identified
Invalid frees
Leaks

There is more than this. wait and see.

Wednesday, January 26, 2011

build framework for a new project

Dont you know how to create build framework for a new project. NO worries. QMAKE tool will help you.

Install qmake and follow qmake steps to build a application or Library.

This works on Unix platforms. I have used it in RHEL

User Library Directory

If you build a C library and you might need this library to be available in /usr/lib on system whever you run programme which needs this library.

The simplest solution is export this path.

export LD_LIBRARY_PATH=./mylib

double free Error

Sometimes because of freeing a pointer two times may result in double free error.
For temporary execution purpose, one can proceed the execution by setting the variable as shown below.

export MALLOC_CHECK_=0
and try to run your programme. You wont get double free error.

But this is not the perfect solution. It may cause problem in long running.