Archive for October, 2007

Best features of Linux

Thursday, October 18th, 2007

Linux has more than what a modern day operating system should typically have. Let us look at some of them

Multitasking: Refers to multiple processes or tasks that execute on a single processor simultaneously without interfering with each other. To better understand, it can be said that, each process will be running independent of the other under the illusion that it is the only process running on the processor and has an exclusive access to all the services of the OS

Multiuser: Linux allows multiple users to use the system simultaneously under the illusion that    each being the sole user

Multiplatform: Linux runs on most computers, laptops and platforms. Several projects are underway to port Linux on to other hardware configurations, truly speaks about its extensible nature. To my knowledge Linux runs on Alpha, Arm, AMD, Cyrix, Power PC, Mips, M68k, Intel, Sparc and many more, the list goes on.(//// to support 40 different architectures.!!)

Multiprocessor: Symmetric Multi-Processing (SMP), a computer architecture that provides   faster performance by making multiple CPUs available to individual processes simultaneously .Linux has SMP support for Alpha, PowerPC, Intel, Sparc, and many more are underway.( SMP technology details along with the Intel Dual Core release).

Multithreading: It is the ability of the Operating System to execute different parts of a    program, called threads, simultaneously. Linux has native kernel support for multiple           independent threads of control within a single process memory space.

Customization: One of the greatest feature of Linux, is that it helps users to customize it to         their requirements and likings. You can make it exactly the way you want and on the platform you want it to run so as to best utilize its services.

Security: Linux implements more security features to prevent intruders. Some of them include Files and File system security, Trojan Horses, Password Security and Encryption, Network Security, Kernel Security, Device Security, Host Security etc.  It also enables you to create customized security features, thanks to its open nature.

 Protection against Crashes : has memory protection between processes, so that one program can’t bring the whole system down.

 Demand loads executables: Linux only reads from disk those parts of a program that are actually used.

 Compatibility: It is mostly compatible with POSIX, System V, and BSD at the source level.

 Open Source Nature: Complete source code, including the whole kernel, drivers, development tools and all user programs, are available and also freely distributable under GPL (GNU General Public License)

 Filesystem Support: As Linux runs on almost every hardware platform, it must support all the filesystem types that are found on those systems, this is an important feature for any OS to co-exist with  the other, so as to share data between them on the same system. Linux includes all modern day journaling filesystems and Windows compatible ones. Some of them are Ext2, Ext3, Fat16, Fat32, MS-DOS, AFF, NFS, NTFS, ISO, UPS, SysV etc.

 Networking protocols: Protocols available in the latest development kernels include TCP, IPv4, IPv6, AX.25, X.25, IPX, DDP (Appletalk), Netrom, and others. Stable network protocols included in the stable kernels currently include TCP, IPv4, IPX, DDP, and AX.25

 Different formats for executable files: Linux has support for many executable file formats.        This helps Linux run executables, that are created on other platforms. Some of them include a.out, and elf32.

 Small and Compact: Linux kernel image and complete root filesystem, including all basic            system programs can fit in to just a 1.4MB floppy diskette.

 Help through user communities: There is huge Linux community representations through           chat rooms, mailing lists and discussion forums, who are ready to help you with your    questions, fixes, updates, software patches and more, anytime and everytime. Moreover,           these people are volunteers and don’t charge anything for helping you.

There is more to Linux than we could ever imagine.

Using GNU Debugger – Basic Tutorial

Wednesday, October 10th, 2007

Accidentally bugs creep into programs, leading to undesired results and behavior. To tackle situations like these we require tools which can take us through the execution process and help us check and fix these bugs that creep up during the process. There are tools like the strace, memwatch, gdb etc which can trace the steps of execution of our program and help us in tracing and debugging.

One of the best tool which I found to debug my applications was the gdb. It is one of the most widely used debugging tool by programmers on Linux. Let us now look into more details and usage of the gdb.

GDB

The gdb performs all the tasks that of a normal debugger like it helps you analyze your buggy program, it can stop your program at different predefined execution points, it can print the values of variables at that point and also facilitates changing those values, it can also track the line in your program where the program exactly crashed, in addition it offers several other debugging features.

Using GDB

To enable debugging our program should be compiled with the -g flag which tells the compiler to add debugging support to our program. For eg. our source file is project.c, now we have to compile it by issuing the following command

 

# gcc -g project.c -o project

Now Start the gdb session. It is started by running a command gdb from the shell

along with program name as parameter.

#gdb  ./project

If there are no bugs in your program it will execute to completion. After the successful execution gdb will get control back. But  if something  goes wrong  gdb will take the control of the program  and interrupt the program, allowing you to examine the cause for the error.

Here in project.c, for loop executes for about 10 times and calculate cumulative and prints the sum.

 

project.c

 

#include <stdio.h>

 main(){

 

        int  i, res, val1=11, val2=5, sum=0;

        for(i=1; i<5; i++)

        {

                res=(val1-val2)/res;

                sum+=res;

                val1--;

                val2++;

                printf("sum is %d\n ",sum);

        }

 }

Now start the program using gdb .

 

 # gdb project

 

This will take you to the gdb prompt.

(gdb)

You can view the code  by using the list command which prints 10 lines of your source code at a time, issue list again to get the next 10 lines of your code.

(gdb) list

1

2       #include <stdio.h>

3        main(){

4

5               int  i, res, val1=11, val2=5, sum=0;

6               for(i=1; i<5; i++)

7               {

8                       res=(val1-val2)/res;

9                       sum+=res;

10                      val1--;

 

(gdb) list

11                      val2++;

12                      printf("sum is %d\n ",sum);

13              }

14       }

Now run the program issuing run

 

(gdb)   run

It will result in something like this

Program received signal SIGFPE, Arithmetic exception.

0x080483ce in main () at project.c:8

8                       res=(val1-val2)/res;

(gdb)

 

Here gdb indicates that there is an Arithmetic exception at line 8 where division is being performed. To check for the values of the variables at this point you can use the print command.

(gdb) print val1

$1 = 11

(gdb) print val2

$2 = 5

At this point you can allow your program to die naturally by using the continue command. Which will result in

(gdb) continue

Continuing.

 

Program terminated with signal SIGFPE, Arithmetic exception.

The program no longer exists.

 

Setting Breakpoints

 To examine whats happening inside your program  you can set break points . In your program you can

set break points in main() by using the break command and stop the execution  at the break point.

 

(gdb) break 8

Breakpoint 1 at 0x80483b6: file project.c, line 8.

 

It shows that break point is set at line number 8. You can also set break point at a particular function in the main() or you can also specify some conditions to satisfy to break/stop the program execution.

Now if you rerun the program the program will stop at the break point i.e., at line 8 and displays the code at that break point.

Breakpoint 1, main () at project.c:8

8                       res=(val1-val2)/res;

Here you can check the values of variables by using the print command.

(gdb) print val1

$1 = 11

(gdb) print val2

$2 = 5

 

You can see that ‘a’ has a value 12 and ‘b’ has a value 5. You can also use the info locals command to print the information of all the local variables.

(gdb) info locals

i = 1

res = 0

val1 = 11

val2 = 5

sum = 0

For line by line execution of the program use the next command. It executes the program line by line but steps over any function calls in the line. Unlike next  the step command executes the program one line at a time, the only difference being step command steps into any function calls in the line.

Using info locals we can see the values of the variables which are resulting to exception. Now we can change these values using shell commands like set for successful execution of the program.

(gdb) set res=2

(gdb) continue

Continuing.

sum is 6

Program exited with code 0144.

You can exit gdb by typing quit.

(gdb) quit

 

So now start debugging with gdb . For more refer the gdb man page.