Thursday, October 29, 2009

"hello world" program without a main() function

Hi Friends,

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 /* Needed by all modules */
#include /* Needed for KERN_INFO */
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:
"press tab"make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
"press tab"make -C /lib/modules/$(shell uname -r)/build M=$(PWD) 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 ?

Hey Everyone,

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


How does a C program works :

Note : Following article is based on my understanding of the chapter "Basic Memory Management" , from book Operating Systems , Third Edition by Gary Nutt .

This article describes the various Stages your C code or Program goes through before producing the desired output .

Stage 1 :
Writing of code or program and saving it with “.c” extension .

Stage 2 :
Compile Time : Now you need to compile your program .
In simple terms its job is to check whether your code follows all the rule of the C language (any language for that matter) i.e errors like , related to syntax and declariations etc are not present and convert the code into a language which is understood by your computer .

Let us now explore the compilation process in a bit more detail :

Aim of compile time is to produce a Relocatable Object Module , commonly known as an object file, with extension “.o” .
For a C program relocatable object module has has three logical blocks of addresses :



  1. A Text Segement (also known a Code Segment) , which contains a block of machine instructions .


  2. Data Segment , which is a block of Static Variables .


  3. Stack Segment , which represents the stack which is used while program is in execution .

Now the relocatable object module contains the relative addresses of the various code segments .
For example if you have used a user defined function “foo()” , which is also present in the same file , then it will contain its relative entry address.

However , for funtions like “printf()” or any other function, whose definition is not present in the in the code that you have written , compilier is not able to get their relative addrsses at the Compile time, so what is does it annotate each such reference to an external address , so that linkeditor can place correct addesse at the link time.

Stage 3 :
Link time : Now at link time code and data segments of each relocatable object module are combined to form the absolute module or the load module .
Link editor combines all the datasegment into single data segment and all code segment into a single code segment.
When datasegments are combined the relative addresses of individual static variables are changed. The linkage editor then relocates the addresses in the instructions so that they reference the uptodate addresses in the aggregated data segment .All the undefined addresses references are found by linkage editor as it combines the relocatable module: the resulting composition includes all the program text and data, so that every reference to the data or program entry point is resolved .

The absolute program is stored in a file(in the secondary memory) until a process is ready to use it.
image : courtesy operating system, third edition, Gary Nutt

Stage 4 :
Load time : Now when a process is allocated to use the absolute module , memory manager allocates a block of primary memory to the process. Then the loader copies the absolute program and data into the newly allocated memory.

Now the interseting part is , till now all the adderesses in the absolute program file were relative , starting from 0 , but now program is at some particular Physical address in the primary memory , so Loader Translates all internal logical primary memory addresse , with zero being replaced by the Physical memory address , and othe addresses changed according to it .

At this stage the program is called Executable program , the one expected by the hardware control unit , just before it is added to the primary memory at the proper location. The PC (program counter) is set to the primary memory address of the first ececutable insrtuction i.e. Main enry point for the program .

Now the program is ready to Run , the Static and Dynamic binding concepts which also play an important role , but beyond the scope of this article . May be in some other article :)

References : Operating Systems ,Third Edition by Gary Nutt.





Sunday, October 18, 2009

Thoughts Of MY OWN ... !

its important to learn from your mistakes ...... but its more important to remember what you have learned.

- 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

About Me

My photo
Hi Friends, I am Samarth currently pursuing my Masters degree in Information Technology from International Institute of Information Technology ,Bangalore .