Sunday, December 27, 2009
Merry Christmas wishes from President ....
Friday, November 13, 2009
its my life ....
Sunday, November 1, 2009
A real multi agent system problem .... !
On their ship, they decide to split the coins using this scheme:
The oldest pirate proposes how to share the coins, and all pirates remaining will vote for or against it.
If 50% or more of the pirates vote for it, then the coins will be shared that way. Otherwise, the pirate proposing the scheme will be thrown overboard, and the process is repeated with the pirates that remain.
Assuming that all 5 pirates are intelligent, rational, greedy, and do not wish to die, (and are rather good at math for pirates) what will happen?
what is Mr Shah Birth date .. ?
Can you figure out what is the Date of Birth of Mr. Shah? Assume that the current year is 2000.
Thursday, October 29, 2009
"hello world" program without a main() function
This short article is on request of my friend Viswanath who wanted to run a C program without main() function on his computer .
I have done this on Ubuntu OS , with the simple Linux module , taken form guide :
"The Linux Kernel Module Programming Guide"
note : Aim of this article is not to teach you how to write a Linux kernel module , but to simply write a C program which runs on your Linux system and prints "Hello world", without use of main() function
only thing worth mentioning is "output" from "printk" goes to by default "/var/log/messages" , so your "hello world" would be printed their instead of terminal.
Step 1 :
create a file named hello-1.c with code given below :
/*
* hello−1.c − The simplest kernel module.
*/
#include
#include
int init_module(void)
{
printk(KERN_INFO "Hello world 1.\n");
/*
* A non 0 return means init_module failed; module can't be loaded.
*/
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Goodbye world 1.\n");
}
//code ends here
Step 2 :
create a Makefile , with following code : (ie a file name Makefile )
//code starts here
obj-m += hello-1.o
all:
clean:
//code ends here
Step 3:
from terminal with sudo premission run following command :
$ make
you will see output like given below if successful :
hostname:~/lkmpg−examples/02−HelloWorld# make
make −C /lib/modules/2.6.11/build M=/root/lkmpg−examples/02−HelloWorld modules
make[1]: Entering directory `/usr/src/linux−2.6.11'
CC [M] /root/lkmpg−examples/02−HelloWorld/hello−1.o
Building modules, stage 2.
MODPOST
CC /root/lkmpg−examples/02−HelloWorld/hello−1.mod.o
LD [M] /root/lkmpg−examples/02−HelloWorld/hello−1.ko
make[1]: Leaving directory `/usr/src/linux−2.6.11'
hostname:~/lkmpg−examples/02−HelloWorld#
Step 4:
to load your module run command :
$ insmod ./hello-1.ko
now your module should be added in the module list of kernel. For checking this type following command :
$ cat /proc/modules
the first entry should be hello-1 , and thats your module.
Step 5:
to unload your module use following command :
$ rmmod hello-1
Step 6 :
wondering where did your printk " Hello world 1." and " Goodbye world 1."
went just and check the file :
$ cat /var/log/messages
towards the end you will find the two entries ,
Hello world 1.
Goodbye world 1.
Thats it.
Thanx for reading.
Note: if you really want to understand how all this worked please go through the article "The Linux Kernel Module Programming Guide" , which is easily available on web.
What does Compiling / upgrading Linux kernel mean ?
Through this short article i would like to share a concept which most of you might find very trivial , but i myself took sometime to understand it and wanted to share the same if some of you might have felt the same .
Before we proceed lets revise revise four basic concepts very quicky ...
What is an Operating System .. ?
Wikipedia says : The Linux kernel is an operating system kernel used by the Linux family of Unix Like Operating Systems. The Linux kernel was initially conceived and created by Finnish computer science student Linus Torvalds in 1991.
What is a Kernel .. ?
Wikipedia Says : In computing, the 'kernel' is the central component of most computer operating systems it can be thought of as the bridge between application and the actual data processing done at the hardware level. The kernel's responsibilities include managing the system's resources (the communication between hardware and software components).
What is Linux .. ?
Wikipedia Definition : Linux is a generic term referring to Unix-like computer operating systems based on the Linux kernel.
Definition from linux.org : Linux is a free Unix-type operating system originally created by Linus Torvalds with the assistance of developers around the world. Developed under the GNU General Public License , the source code for Linux is freely available to everyone.
What is Linux Kernel .. ?
Wikipedia says : The Linux kernel is an operating system kernel used by the Linux family of Unix Like Operating Systems. The Linux kernel was initially conceived and created by Finnish computer science student Linus Torvalds in 1991.
So from above four question and answers its clear that :
Operating System and Kernel are not the same thing .
So get it clear .... Linux OS and Linux Kernel are not the same thing.
Therefore Ubuntu, fedora , Suse all these are operating Systems based on Linux kernel , started by our friend Linus Torvalds . So when you install any one of these you basically install a full blown Operating System ready to serve you , and this OS internally may be using any Linux Kernel release e.g 2.6.28 or 2.6.30 or .... any other .. they just keep coming with modifications and updates .
Now suppose you have installed a Ubuntu OS on your system with default kernel release 2.6.28 , now after few days you go to www.kernel.org and find the latest kernel release is 2.6.30 , which is ofcourse supposed to be better that 2.6.28 in one or the other aspect.
So now you want to use latest kernel , so now you download the lastest kernel code and " compile that C program code" .
note: to know how to comile kernel code serach web or better look for article written by me : compiling linux kernel 2.6.30
So now after rebooting the system , with new kernel , what difference do you see ....
Practically nothing : Your user name , password are still the same , your wallpaper is still the same , your user setting reamains the same .
Only thing that had changed and is not visible is "Linux Kernel" version , running in background and managing all your resources and processes .
You can check the running version by running following shell command :
$ cat /proc/version
so in the end , just keep in mind Linux based OS and Linux Kernel are two completely different things ... and changing them is also different.
Like you can change your OS from Ubuntu to fedora and both of the may still be using same Linux Kernel 2.6.30 .
Thanx for reading .
How does a C Program Work/Execute
A Text Segement (also known a Code Segment) , which contains a block of machine instructions .
Data Segment , which is a block of Static Variables .
Stack Segment , which represents the stack which is used while program is in execution .
Sunday, October 18, 2009
Thoughts Of MY OWN ... !
- Samarth Gupta
overcome your fears ... they are not that dangerous as they seem to be ... !
- Samarth Gupta
Three people can never be Best Friends to each other ...
- Samath Gupta
... I have Dreams ... and I am ready to fight for them ..... !
- Samath Gupta
..... In the end you have only Three true Friends .... your Mother .... Father ... and MONEY ... !
- Samath Gupta
Every one wants to hear the TRUTH .... only condition is it should not be about Himself ... !
- Samarth Gupta
Never answer a Question .... which is not addressed to you .... !
- Samarth Gupta
Got Bored of EARTH .... planning to go back Home .... !
- Samarth Gupta
If humans, the most intelligent of all beings can't understand each other then wats the point
- Sandeep Bhaskar
my opinion about above thought :
humans understand each other very well ... the point is they dont like what they have understood .... !
- Samarth Gupta
Wednesday, September 23, 2009
Enjoy Your Coffee....
A group of alumni, highly established in their careers got together to visit old university professor. Conversation soon turned into complaints about stress in work and life.
Offering his guest coffee, professor went to the kitchen and brought a large pot of coffee and assortment of cups- porcelain, plastic, glass, crystal. Some plain looking, some expensive and some exquisite, telling them to help themselves to hot coffee.
When all the student had a cup of coffee in hand professor said-
If you noticed, all the nice looking expensive cups were taken up leaving behind the plain and cheap ones. It is normal for you to want only the best for yourself But that is the source of your problem and stress.
What all of you wanted was coffee, not the cup, but you consciously want for the best cup and were eyeing each other’s cup.
Now if life is coffee, than jobs, money and position in society are the cups. They are just tools to hold Life, but the quality of Life doesn’t change. Sometimes by concentrating only on the cup we fail to enjoy the coffee in it.
So don’t let the cups drive you….
Enjoy the coffee instead.
Tuesday, September 22, 2009
printf() interview question ....
Look at the code below.
void main()
{
if(X)
{
printf("Hello");
}
else
{
printf(" World");
}
}
What should X be replaced with inorder to get the output as "Hello World"?
Friday, August 21, 2009
Using pointers for double dimension memory allocation OR Dynamic Memory allocation for Multidimension Array
Many times you need to allocate dynamic memory for a multidimensional array in C language. This article deals with using pointers for dynamic memory allocation for two dimensional array , which can further be extended for higher dimensions.
Example of situation when u require 2-D dynamic allocation :
1. Take the input dimension of an matrix form the user and then get input values and print them.
2. Print a magic square after taking dimensions form user.
3. Implementing Hash Table after taking hash value form user.
I will try to explain the concept by solving the first problem (simply coz its the easy to solve :) and also for others to understand )
Problem Statement : Take the input dimension of an matrix form the user and then get input values and print them.
Solution :
first i will paste a working solution and the explain it :
i tried to copy paste the code but it gave some html error ... so im pasting the image ..... will soon find some way to post the code :)
please right click and open the image in a tab to see the code
Explanation :
Problem is we need to take input from the user at run time , so we cannot declare a 2-D array of fixed dimensions before hand , we need to do it at runtime , using dynamic memory allocation.
For this we use the concept of pointer to a pointer or Double pointer.
Now to revise the basics we can use a normal pointer to allocate memory for an array , i.e. a single dimension array. It can be done in following manner.
int *array;
int arraysize;
printf("enter the size of array");
scanf("%d ",&arraysize);
array=(int *)(malloc(sizeof(int)*arraysize));
what happened above is when i said int *array a pointer named array was assigned to point to a location where some int is supposed to reside. But by writing
array=(int *)(malloc(sizeof(int)*arraysize)) i have allocated memory dynamically equal to the arraysize given by the user, where each element is integer type.
if we analyze the right hand side of the statement:
array=(int *)(malloc(sizeof(int)*arraysize));
(int*) typecast the memory allocated to be a pointer to a integer, since array is of that type .
malloc(sizeof(int)*arraysize) dynamically allocates a memory of elements equal to arraysize and each element of size int , since sizeof(int) is used.
so in the end what we have is a single dimension array, whose base address is pointed by pointer array.
Now coming to the code that i have posted above for 2-D array :
the part of the code that deals with the 2-D dynamic allocation is :
these three lines are the only lines which u need to understand to get the concept :
Line 1: matrix=(int**)(malloc(sizeof(int*)*no_of_rows));
The variable matrix which is a integer pointer to a pointer is allocated memory allocated equal to no_of_rows and each of type pointer to an integer. So what we have now is an Array of integer Pointers of size equal to no_of_rows .
since matrix is a double pointer so we need to typecast the memory allocated to its type by (int**) and since we are allocating memory for array of pointers so we use sizeof(int*) .
Line 2 :
this is a simple for loop which is responsible to run the line below it no_of_rows time, so that each integer pointer from Array of Pointer can point to integer array itself .
Line 3 : *(matrix+i)=(int*)(malloc(sizeof(int)*no_of_column));
By *(matrix+i) we go to each integer pointer in the array of pointers for which we allocated memory in the Line: 1.
By (int*)(malloc(sizeof(int)*no_of_column)) we first typecast the allocated memory since *(matrix+i) is of type integer pointer we use (int*) and now we want to store integers in the allocated space to we use sizeof(int) .
That all the memory is allocated, rest of program is simple input and display.
For any more clarification feel free to comment.
Thursday, August 20, 2009
Format USB drive / Pen Drive on LINUX - Ubuntu
insert the pendrive in the USB port and wait for it to be detected (or mounted according to Linux)
once it is mounted (ie it opens in a window) , go to the terminal and type the following command :
$ df -h
it will display the list of all mounted drives on your system. It will look something like image shown below :
now very carefully note the path for your pendrive , it would be written in front of the name of your pendrive. Like in above example the data to be noted is /dev/sdb
it might be in your case something else like /dev/sd1
Note: be very sure that you get the correct name otherwise you will end up formatting some other drive
Step 2:
Unmount the USB or Pendrive.
It can be done in two ways :
1. simply right click on pendrive icon on desktop and unmount.
or by
2.form terminal run following command :
$ sudo umount /dev/sdb
i prefer the second method since if pendrive is unmounted after command you can be sure you got the correct drive name in step 1.
Remember : you r only supposed to unmount the pendrive not to remove form the usb port.
Step 3 :
run the following command from the terminal :
$ sudo mkfs.vfat -I /dev/sdb
which will format the pendrive. If you want to provide and label or name to ur pendrive use following :
$ sudo mkfs.vfat -n "name/label"
replace
Note: u can format in any file system u wish to by replacing vfat by that name example ext3 .
Wednesday, August 19, 2009
Difference between NULL and NUL
Hi Everyone.....
i seems no matter how much C language you learn it seems it will never end....
i found a very interesting difference between NUL and NULL in C language while going through a book which i would like to share:
NULL is an pointer with value zero (0) , that is you can say it does not correspond to a real memory location in your program. For linux operating system it is defined as ((void *)(0)) .
Whereas NUL is an character with integer value 0 (zero) . It is this NUL character you keep hearing about which comes at the end of every string as "\0" or "(char)(0)" and takes a memory space equal to that of a character .
so to remember NULL is a pointer and NUL is a character :)
Compiling Linux Kernel
Step 1:
You will need to install ncurses on your system if it is not already present. It provides a gui interface for the configuration of the kernel.
The latest version can be downloaded from the link :
http://www.gnu.org/software/ncurses/
untar the package and then from the top source directory type the following commands :
$ ./configure –without-cxx-binding
$ make
$ sudo make install
Step 2 :
Download the linux source code for kernel 2.6.30 from the below link :
http://www.kernel.org/pub/linux/kernel/v2.6/
also make sure you have gcc installed on you linux system .
Step 3 :
As root user copy the tar folder in the /usr/src directory and then untar it . For example if you have saved the .tar file on your desktop you can type the following commands current directory being Desktop :
$ sudo bash
~# cp linux-2.6.30.tar.gz /usr/src/
~# cd /usr/src
~# tar -xvvzf linux-2.6.30.tar.gz
~# cd linux-2.6.30
Step 4 :
from the top source directory of linux kernel folder type the following into terminal :
this will open an GUI , form which you can configue the kernel.
You can simply choose the default options by pressing esc two times :
~# make menuconfig
Step 5 :
Now after configure , to compile the kernel run the following command :
~# make
Step 6 :
for compilation for modules :
~# make modules
Step 7:
To install the kernel follow the following :
~# make modules_install
~# make install
Step 8:
Now the bzImage of the kernel is created which is the bunzipped compressed
image. Copy this image to boot directory by the following command :
~# cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.30
Step 9:
To provide the capability to load a RAM disk by the boot loader run the following command form the terminal . At boot time, the kernel unpacks the created archive into a RAM-based disk, which is then mounted and used at the initial root filesystem. Much of the kernel initialization and bootstrap code can then be moved into this disk and run in user mode. Tasks like finding the real root disk, boot-time networking setup, handling of initrd-style ramdisks, ACPI setup, etc. will be shifted out of the kernel in this way.
~# cd /boot
~# mkinitramfs -o initrd.img-2.6.30 2.6.30
Step 10 :
Every thing is done , now update your grub according to the new kernel :
~# update-grub
Step 11 :
To see the changes run the following command :
~# vim /boot/grub/menu.lst
Go down the screen you will find entries which look like following :
As you can see the “Default” and the “recovery mode” does not have the initrd entry, but the third one , the normal kernel entry has it for kernel 2.6.30 , just copy paste the initrd entry from the third configuration and place it at the corrosponding place in the first kernel entry. After which the file will look like following :
for those who are new to vim editor , you can go to insert mode by pressing “i” and then simply copy paste.
To save the changes type the following after pressing “esc” :
~# :wq
You will now come back to the terminal.
That,s it , now simply restart you system. The new kernel entries should show in the Grub menu when you start the system. Select the new kernel that you have installed and start enjoying .
Step 12:
To check wether you are really running the new kernel or not just run the following command from the terminal :
$ cat /proc/version
you will see the deatils of the kernel being run at present.
Like in my system it showed as :
C program with NO main() function
To understand why some C programs does not require a main() function it is necessary to understand concept called Execution Environment :
Execution Environment:
Whenever we write a piece of C code we do it in an environment known as Execution Environment.
According to C standards the Execution environment includes the characteristics of the processor that will execute the program image (instruction set, number of registers, memory access characteristics, etc.), and the runtime interface to the host environment (storage allocation, function calling conventions, etc.).
In C there are basically two types of execution environments
1. Hosted environment
2. Freestanding environment
Hosted Environment:
Hosted environment is one which a normal developer encounters everyday while writing a piece of code. It already provides inbuilt support of most of the standard libraries and functions. In Hosted environment the developer does not need to worry about the basic
functionally required eg I/O functionality provided by “stdio.h” . GCC is one such platform which provides a Hosted environment which may itself be running on any OS like Linux. Andalso starting point of any hosted environment is the “main function”. Implementations use a variety of techniques to start executing a program image. These usually involve executing some internal, implementation-specific, function before main is called. Then association between this internal function and main is usually made at link-time by having the internal function make a call to “main function “, which is resolved by the
linker in the same way as other function calls. To sum-up hosted environment provides a lot of standard library functions which may or may not be needed by developer but are provided by default.
Freestanding Environment:
Freestanding environment on the other hand is intended to interact directly with the hardware. A freestanding environment does not require any built in library functions. A freestanding implementation is one in which execution may take place without the benefit of an operating system, and has an implementation-defined set of libraries that include certain language-support libraries
Why no “main function” in Linux kernel source code:
In a free standing environment there is no standardize place to begin the execution of the code. In many cases there is no named program at all. Switching on, or resetting, the freestanding host causes the processor instruction pointer to be set to some predefined location in storage, whichever instructions are located at that and subsequent locations being executed. Traditionally there is a small bootstrap loader at this location, which copies a
larger program from a storage device into memory and jumps to the start of that program (it might be a simple operating system). In other cases that storage device is ROM and the program will already be in memory at the predefined location. Translated program instructions are executed directly from the storage device. Once switched on, the host may have buttons that cause the processor to be reset to a different location, causing different
functions to be invoked on startup. For example OS kernel which itself is supposed to provide an hosting environment for
writing C code is written according to freestanding environment in mind, this is the reason there is no “main” function in Linux kernel source code .
Generally in the case on Linux the function placed at the default boot-up address is
start_kernel() .
In most of these cases the code on freestanding environment is used by developers who cannot afford to have all the unnecessary libraries or does not require them like the Embedded System developers .
However there are some libraries which are needed by both hosted and freestanding
environment . They are listed Below :
These libraries fulfil basic needs of C programming and come as a part of Standard Libraries .
About Me
- Samarth
- Hi Friends, I am Samarth currently pursuing my Masters degree in Information Technology from International Institute of Information Technology ,Bangalore .