Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Saturday, February 8, 2014

The history of Unix and Linux

In this post I thought of sharing the history of Unix and Linux in a bullet list for curious people who want to know what has been the history of Unix/Linux in a brief list rather than reading a huge chapter of a classic book.
  • The first UNIX implementation was developed in 1969 (the same year that Linus Torvalds was born) by Ken Thompson at Bell Laboratories, a division of the telephone corporation, AT&T. It was written in assembler for a Digital PDP-7 mini-computer


Ken Thomson

PDP-7 (Which is under reconstruction)

  • In 1970, UNIX was rewritten in assembly language for a newly acquired Digital PDP-11 minicomputer


PDP-11

  • A short time later, Dennis Ritchie, one of Thompson’s colleagues at Bell Laboratories and an early collaborator on UNIX, designed and implemented the C programming language (The language that almost all the modern languages are derived from). C followed an earlier interpreted language, B. B was initially implemented by Thompson and drew many of its ideas from an even earlier programming language named BCPL


Dennis Richie

  • First edition of Unix - By 1971, UNIX was running on the PDP-11 and already had a FORTRAN compiler and versions of many programs still used today, including ar, cat, chmod, chown, cp, dc, ed, find, ln, ls, mail, mkdir, mv, rm, sh, su, and who.
  • Second Edition, June 1972: By this time, UNIX was installed on ten machines within AT&T.
  • Unix implemented in C by Ken Thomson and Dennis Richie in 1973
  • Third Edition, February 1973: This edition included a C compiler and the first implementation of pipes.
  • Fourth Edition, November 1973: This was the first version to be almost totally written in C.
  • Fifth Edition, June 1974: By this time, UNIX was installed on more than 50 systems.
  • Sixth Edition, May 1975: This was the first edition to be widely used outside AT&T.
  • Then, the Unix OS is distributed to Universities with source code.
  • January 1979 saw the release of Seventh Edition of UNIX. This release also contained a number of new tools, including awk, make, sed, tar, uucp, the Bourne shell and a FORTRAN 77 compiler. The release of Seventh Edition is also significant because, from this point, UNIX diverged into two important variants: BSD and System V
  • Bjarne Stroustrup developed a language which is an extension of C language, originally name as "C with Classes"


Bjarne Stroustrup

  • Many new tools and features were developed at Berkeley, including the C shell, the vi editor, an improved file system (the Berkeley Fast File System), sendmail, a Pascal compiler, and virtual memory management on the new Digital VAX architecture.
  • Under the name Berkeley Software Distribution (BSD), this version of UNIX, including its source code, came to be widely distributed. The first full distribution was 3BSD in December 1979. (Earlier releases from Berkeley BSD and 2BSD were distributions of new tools produced at Berkeley, rather than complete UNIX distributions.
  • In 1982, breakup of monopoly between AT&T and the US government on telephone system. This led AT&T to market and sell Unix. This resulted in the release of System III (three) in 1981. System III was produced by AT&T’s UNIX Support Group (USG)
  • In 1983, "C with Classes" was renamed as C++
  • In 1983, the Computer Systems Research Group at the University of California at Berkeley released 4.2BSD. This release was significant because it contained a complete TCP/IP implementation, including the sockets application programming interface (API) and a variety of networking tools. 4.2BSD and its predecessor 4.1BSD became widely distributed within universities around the world. They also formed the basis for SunOS (first released in 1983), the UNIX variant sold by Sun. Other significant BSD releases were 4.3BSD, in 1986, and the final release, 4.4BSD, in 1993.
  • The first release of System V (five) followed in 1983, and a series of releases led to the definitive System V Release 4 (SVR4) in 1989, by which time System V had incorporated many features from BSD, including networking facilities. System V was licensed to a variety of commercial vendors, who used it as the basis of their UNIX implementations.
  • In addition to the various BSD distributions spreading through academia, by the late 1980s, UNIX was available in a range of commercial implementations on various hardware. These implementations included Sun’s SunOS and later Solaris, Digital’s Ultrix and OSF/1 (nowadays, after a series of renaming and acquisitions, HP Tru64 UNIX), IBM’s AIX, Hewlett-Packard’s (HP’s) HP-UX, NeXT’s NeXTStep, A/UX for the Apple Macintosh, and Microsoft and SCO’s XENIX for the Intel x86-32 architecture.
  • In 1985, Richard Stallman founded the Free Software Foundation (FSF), a nonprofit organization to support the GNU project as well as the development of free software in general.
Richard Stallman
  • In 1987, Andrew Tanenbaum, a university professor in Holland created a Unix-like operating system named Minix. This was merely for academic purpose.


Andrew S. Tanenbaum

  • Well-known programs were produced by the GNU project such as Emacs text editor, GCC (originally the GNU C compiler, but now renamed as the GNU compiler collection, comprising compilers for C, C++, and other languages),, the bash shell, and glibc (the GNU C library).
  • POSIX.1 (or more specifically POSIX 1003.1) became an IEEE standard in 1988
  • Release of GNU General Public License (GPL) in 1989
  • FIPS 151-1 was published (Federal Information Processing Standard). This standard required certain optional features of POSIX.1
  • In 1989 (XPG3) X/Open Portability Guide Issue 3 was released. This standard was also based on POSIX.
  • ANSI C standardization led to C89
  • FIPS 151-2 aligned with the 1990 ISO edition of POSIX.1 (FIPS was withdrawn in 2000)
  • By 1990, a complete Unix system was ready; except the most important kernel. But, The GNU project had started work on an ambitious kernel design, known as the GNU/HURD, based on the Mach microkernel. But it was far from complete.
  • POSIX adaptation for ISO standard in 1990 (ISO/IEC 9945-1:1990)
  • Version 2 of the license, released in 1991
  • In 1991, Linus Torvalds, creating the Linux kernel, which is inspired by Minix.


Linus Torvalds

  • XPG4 standard was released in 1992
  • XPG4 version 2 in 1994 - This incorporated some important parts of AT&T’s System V Interface Definition Issue 3
  • By March 1994, the developers were able to release version Linux 1.0. Linux 1.2 appeared in March 1995, Linux 2.0 in June 1996, Linux 2.2 in January 1999, and Linux 2.4 in January 2001. Work on the 2.5 development kernel began in November 2001, and led to the release of Linux 2.6 in December 2003.
  • In 1995 Single UNIX Specification (SUSv1) was formulated by combining XPG4 version 2, X/Open Curses Issue 4 version 2 specification, and the X/Open Networking Services (XNS) Issue 4 specification. This was also called UNIX 95 standard.
  • In 1996, an Organization name 'The Open Group" was formed by merging X/Open and the Open Software Foundation (OSF). Nearly all key companies in the Unix domain are members of this organization
  • SUSv2 in 1997 (This is also occasionally referred to as UNIX 98 and XPG5)
  • The first C++ standard was ratified in 1998 and is known as ISO/IEC 14882:1998
  • Second C language standard led to C99
  • In 2001, POSIX 1003.1-2001 (Also known as SUSv3) in 2001. This then became an ISO standard namely ISO/IEC 9945:2002
  • C++ standard was amended with a Technical Corrigendum in 2003 an is known as ISO/IEC 14882:2003
  • SUSv4 and POSIX.1-2008 in 2008
  • The third C standard was published in 2011. This standard is know as C11
  • The next C++ standard was ratified in 2011. This standard is known as C++11

Sunday, December 8, 2013

Writing Python extension modules in C

In this short guide, we shall look at how to develop an extension module for Python in C. This requirement may come in couple of ways.

You may need to have a Python module, which has to be super-fast. Don't mislead yourself by this statement. Python it self can be used to write fast algorithms and modules. But let's be little
realistic about this. Python isn't anyway a compiled language like C. It's an interpreted language. Therefore, a hardcore algorithm written in C would easily surpass performance of the same written in Python. In that sense, you might want to write a C program for that, and still use Python's ease to develop an application fast.

Another common reason you may want to have an extension module is that, you may already have a library written in C which will fulfill your program requirements. Then, you may use that code with little modification to adapt to Python API, so that Python code can call that libraries' methods. After all, Python extension module is not more than a C library. In Unix/Linux, the the dynamic libraries are Shared Object or .so files, whereas in Windows they are usually referred to as Dynamic Link Libraries or dll.

In this we would build a sample module to be invoked in Python using C. For this exercise, you need to have Python interpreter and its header files along with. In Linux, you can install python dev packages using,
$ sudo apt-get install python-dev
In Windows, the header files must be available with the binary installer package itself. Developing an extension module for Python involves three steps. Namely,
  1. Set of C functions that would need to be invoked from Python 
  2. A table of mappings to Python methods to C functions 
  3. An initialization process 
Let me demonstrate with a sample module called mathC which is going to have an add, complex add and a method to find logarithm. Again, don't mislead yourself. You don't need to write an extension module yourself to find the logarithm of a value in Python. But this is merely for demonstration purpose. Simply, the C program would be like the following.
#include <Python.h>
#include <math.h>

static PyObject *mathC_add(PyObject *self, PyObject *args) {
    double d1, d2 ;
    if(!PyArg_ParseTuple(args, "dd", &d1, &d2)) {
        return  NULL ;
    }

    return Py_BuildValue("d", d1 + d2) ;
}

static PyObject *mathC_addComplex(PyObject *self, PyObject *args, PyObject *kw) {
    char *kwlist[] = { "real1", "complex1", "real2", "complex2", NULL } ;
    double r1, r2=0, c1=0, c2=0 ;
    if(!PyArg_ParseTupleAndKeywords(args, kw, "d|ddd", kwlist, &r1, &c1, &r2, &c2)) {
        return  NULL ;
    }

    return Py_BuildValue("(dd)", (r1 + r2), (c1 + c2)) ;
}

static PyObject *mathC_log(PyObject *self, PyObject *args) {
    double d1 ;
    if(!PyArg_ParseTuple(args, "d", &d1)) {
        return  NULL ;
    }

    return Py_BuildValue("d", log(d1)) ;
}

static PyMethodDef mathC_methods[] = {
    { "add", (PyCFunction) mathC_add, METH_VARARGS, NULL },
    { "addComplex", (PyCFunction) mathC_addComplex, METH_VARARGS | METH_KEYWORDS, NULL },
    { "log", (PyCFunction) mathC_log, METH_VARARGS, NULL },
    { NULL, NULL, 0, NULL }
};

PyMODINIT_FUNC initmathC() {
    Py_InitModule3("mathC", mathC_methods, "My mathC extension module");
}
Read the code carefully. First of all, we need include Python.h before hand. That exposes, the required methods for us to communicate back and forth with python and C. Next thing is, three functions corresponding to three methods that we need to invoke from python. All the functions we need to invoke must have either of the following three function prototypes in C.
PyObject *funct(PyObject *self, PyObject *args);
PyObject *funcWithKeywords(PyObject *self, PyObject *args, PyObject *kw);
PyObject *functWithNoArgs(PyObject *self);
Function names can be anything that we like to have. But normally, it's recommended to have a convention like {moduleName}_{nameDescribingPurpose}. That's why I have three functions; mathC_add, mathC_complex and mathC_log. Normally, the functions would take above first form, as in mathC_add function. That function would accept any number of arguments from python and that is equivalent to a python tuple. Let's get into the implementation of the mathC_add function. I have declared two double variables to get it from arguments. Those are for the two operands to be added. Next, we need to parse the arguments from the tuple args. For that Python API provids us a function named PyArg_ParseTuple. This function accepts the PyObject * parameter arg as the first argument. Second argument is a C style format specifier. But note that here we don't use % sign, instead only the character. 'd' for double, 'i' for integer and 's' for strings or (char *) and so on. This function return 0 when operation fails and we have a check to determine that and return NULL from C function, when failed, so that python interpreter can throw an exception saying the error. Next when succeeded in parsing the tuple, we can do our operations, here in our case adding the two variable. Returning from C to python needs to be compatible with python. For this, Python API provides us a method called Py_BuildValue, which also takes the format specifier for the return value and the actual return value. Easy huh!!!

OK.. Now let's look into the second function, mathC_addComplex. This function is supposed to add two complex numbers. It will take four arguments, orderly, first complex numbers' real part, then complex part and then second complex numbers' real part and the complex part. But, note that this function has the above second prototype. This is to support pythons' keyword arguments. The acceptable keyword list needs to be specified in a null terminated array like kwlist. As of here, the python interpreter can pass arguments with keywords; real1, complex1, real2, complex2. Then I've declared four variables to capture. Here I have initialized r2, c1 and c2 to zero and left r1 to uninitialized. This, is to enforce the python programmer to pass at least one value, which is the real part of the first complex number. That enforcement actually happens in the format specifier in the PyArg_ParseTupleAndKeywords function. It's specified as d|ddd. All the specifier right to | are optional. All the specifier to left of the | are required. Note the function PyArg_ParseTupleAndKeywords. It's different from PyArg_ParseTuple. It needs to know about the keyword list and the keyword arguments. The PyObject * kw is passed as the second argument. And, finally we are returning a tuple. For that, see the format specifier in the return statement. (dd) says the tuple contains two values and those are double. The last function mathC_log is very self explainable. Only difference is that it uses C math libraries' log function. Voila, all three functions are now ready to serve as extensions to python. We are done with the first step of building an extension module.

The second step is to specify the table of mapping. This is specified in an array, which can have any name we like, but of type PyMethodDef provided in Python API. This is a structure of the following form.
 struct PyMethodDef {
  char *ml_name;
  PyCFunction ml_meth;
  int ml_flags;
  char *ml_doc;
 };
ml_name is the method name, that we are going to use from python. ml_meth is the equivalent C function name. ml_flags identify which of the three prototypes we are using in C function. Acceptable values are METH_VARARGS for the first prototype, METH_VARARGS | METH_KEYWORDS for the second and METH_NOARG for the third. The final char * parameter is for a docstring of the method, which python uses, which can be NULL. So to specify our second function, we need to have a structure like following.
{ "addComplex", (PyCFunction) mathC_addComplex, METH_VARARGS | METH_KEYWORDS, NULL }
Now the above mathC_methods specifies together with NULL values for the termination. Now the second step is done.

The last and final step involves expoting our initialization function, so that when this module is imported, the python interpreter knows what methods are available. This initialization method needs to be named init{moduleName}. Since, our module is mathC, this method is named initmathC. Note PyMODINIT_FUNC macro. That's a platform independant way to export this method so that it could be called from outside when we build this library into a dynamic library, .so or .dll. To initialize we use the function called Py_InitModule3 as follows.
Py_InitModule3("mathC", mathC_methods, "My mathC extension module");
This methods accepts three arguments; module name, method declaration array and a doc string for the module. Phew!! all steps are done.

OK.. we are not done yet though. Next we need to build the library and test it by importing. We can build the library in two ways. First is the classic way of building. In Linux/Unix we can build with following gcc command. Of course, you need to have gcc, C compiler installed for doing that. If you don't have the required tools installed, see one of my previous post "Installing Developer Tools in Linux".
$ gcc -shared -I/usr/include/python3.1 mathC.c -o mathC.so -fPIC
In windows, having Visual C++ compiler installed, with following command you can build the library into a dll.
cl /LD /IC:\Python31\include mathC.c C:\Python31\libs\python31.lib
Note it assumes the python 3 is installed, and change to the path where Python header files are residing in your system.

Next to import this module into python code, either your library needs to be in the directory where your python code is. Otherwise it must be in one of the sys.path directories. You can manually copy the library file to one of that directories.

But the second method can save you with all these manual process. It's with using the pythons' setup script with the help of distutils package. If you haven't done the above, you can proceed with the following setup script in a file called setup.py.
from distutils.core import setup, Extension
setup(name=’mathC’, version=’1.0’, ext_modules=[Extension(‘mathC’,[‘math.C.c’])])
It's all self explanatory. You can now install the package using the following command.
$ python setup.py install
In Unix, you may need to provide root access for this. In Windows that's not a problem in general.
Now all done. Let's check our extension module in Python. If you have not placed the library into one of the sys.path or site.packages directory, you may have to navigate to the directory where the library is residing. If you have placed or installed in the previous way with the setup.py all is fine.
[shazni@wso2-ThinkPad-T530 cmath]$ python
Python 2.7.4 (default, Sep 26 2013, 03:20:26) 
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mathC
>>> mathC.add(5.4, 2.3)
7.7
>>> mathC.log(15.4)
2.7343675094195836
>>> mathC.addComplex(1.1, 1.2, 1.3, 1.4)
(2.4000000000000004, 2.5999999999999996)
>>> mathC.addComplex(1.1)
(1.1, 0.0)
>>> mathC.addComplex(real1=1.1, real2=1.2, complex1=1.3, complex2=1.4)
(2.3, 2.7)
Great!!!. All happens as expected. Hope you enjoyed learning how to develop an extension module. This is a great technique, which can make beautiful python even more beautiful.

Saturday, October 5, 2013

Installing Developer Tools in Linux

The first step in developing applications in Linux based computers is mostly to have a setup of your developer tools to be installed properly. This short guide aims to provide you with installation steps required to get started with some of the popular programming languages to develop fascinating software.

This post will provide steps for installing the required tools and provide a classic "Hello World" example to test your installation of the tools. Before attempting to install any of the tools, make sure whether your system already contains the tools. Chances are high, that you, most probably have it installed already.

  • C

To compile a C program in Linux based systems, the most popular tool used is the gcc compiler. This is contained in the GNU Compiler Collection (GCC). To check whether gcc is already installed enter the following command
$ gcc --version
if you see an output like the following, gcc is already installed and you are all set to write C programs.
gcc (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Instead if you see the following or something similar, you will have to install gcc.
bash: gcc: command not found...
Though gcc can be installed separately it's recommended to install a package called 'build-essential', which contains bunch of other useful and related packages. To install it issue the following commands in Debian based Linux.
$ sudo apt-get update
This will update the package list. Now issue the following command in Debian based Linux like Ubuntu.
$ sudo apt-get install build-essential
In Linux systems such as Red Hat Linux or Fedora that has yum based package manager, you can use the following command,
$ sudo yum install gcc
This will install gcc compiler and other development libraries like glibc. You can also use the following command to install whole bunch of development packages which also includes gcc and g++ (for C++)
$ sudo yum groupinstall "Development tools"
Now you can write a sample C program as below to check that everything is working fine. Create a file named HelloWorld.c and type in the following program text.
    #include <stdio.h>

    int main()
    {
        printf("Hello World!\n");
        return 0 ; 
    }
Compile the above program with the following command.
$ gcc -o HelloWorld HelloWorld.c
This will create an executable file named HelloWorld in your current directory. To execute the executable program type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Congratulations!! You are a C guru now.

  • C++

The most popular C++ compiler in Linux is undoubtedly g++. This is also part of the GCC. If you have installed the build-essentials in the Debian or the "Development Tools" in Red hat Linux, you must already have g++. Otherwise, go ahead and install build-essentials or "Development Tools". To check whether g++ is already installed, enter the following command.
$ g++ --version
if you see an output like the following, g++ is already installed and you are all set to write C++ programs.
g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
    Copyright (C) 2012 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Instead if you see the following or something similar, you will have to install g++ by installing build-essential package or the "Development Tools".
bash: g++: command not found...
If you didn't install "Development Tools", and want to install g++ separately using yum, use the following command,
$ sudo yum install gcc-c++ 

This should install g++ and other development files and libraries.

Let's now write a sample C++ program which prints Hello World! on the standard output. Type the following program in a file named HelloWorld.cpp
    
#include <iostream>     
using namespace std ;     

int main()     
{         
    cout << "Hello World!" << endl ;         
    return 0 ;      
} 
Compile the above program with the following command
$ g++ -o HelloWorld HelloWorld.cpp
This will create an executable file named HelloWorld in your current directory. To execute the executable program, type ;
$ ./HelloWorld
You should see the following output.
Hello World!
Quite similar to what we did for C, right? Voila!!! Now you are a C++ expert too.

  • Java

Next we shall look into setting up Java in Linux. Chances are that you already got Java installed. Most probably the OpenJDK version. If you are willing to work with OpenJDK, everything is fine, and you are ready to develop java applications. But you may have a valid reason to use Oracle java for developments. In that case we can remove OpenJDK and install Oracle Java or keep OpenJDK with Oracle Java. Though there are few ways to install and confugre Java in Linux, the best and easy way is to download the JDK zipped file from http://www.oracle.com/technetwork/java/javase/downloads/index.html. Make sure to download the version you want, which is applicable to your system architecture. Once you have downloaded the zip file, extract it to a location of your interest. Let's say the extracted location is /usr/local/java/jdk1.6.0_43. All you need to do is add the JAVA_HOME (your installation directory; e.g: /usr/local/java/jdk1.6.0_43) as a new environment variable and add the bin directory of JAVA_HOME to the PATH environment variable. This can easily be done by adding the following lines to your .bashrc file in your home directory.
JAVA_HOME=/usr/local/java/jdk1.6.0_43
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin     
export JAVA_HOME     
export PATH

If you add these lines to .bashrc, the changes will be effective only to the current user. If changes need to be applied to all the users, the same set of lines can be added to /etc/profile file. Once you added the line, source the respective file to make changes immediate on the same shell. To do that, issue the following command.
$ source ~/.bashrc
Now java is configured on your Linux box. Issue the following commands to verify your installation and environment variables. Expected outputs should be something similar to the following lines.
$ echo $JAVA_HOME      
/usr/local/java/jdk1.6.0_43     

$ echo $PATH      
/usr/local/java/jdk1.6.0_43    

$ java -version
If you see a similar output as follows you are all set to develop java applications
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
Let's write a simple java program, compile and execute it. Create a file named HelloWorld.java and type in the following text.
public class HelloWorld     
{         
 public static void main(String[] args)         
 {               
  System.out.println("Hello World!");         
 }       
}
Make sure the class name match your file name. To compile this program, type in the following.
$ javac HelloWorld.java
This must generate a class file in your current directory named HelloWorld.class. It contains the bytecode of your program. To execute your class file type
$ java HelloWorld
Make sure you do not type in the .class extension when executing. Following output should be shown in the standard output.
Hello World!
Congratulations!! you have written a simple java program. Ok.. Have we done with everything? Ideally yes. But wait a second. We often come across new java versions. Then the question comes, whether we can have two versions of java installed in the Linux side by side? The answer, as you may have guessed, is a big "YES". So how do we go about making that. Ok.. The process is simple. Download the zip file of your interested version of java. And extract it, say to the same location (e.g. /usr/local/java/jdk1.7.0_07). Next we got to inform the Linux that you have two versions of java. Let's collect a few more details.
$ java -version
This should still print the older java we configured earlier, like the following.
java version "1.6.0_43"
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
To find the path of the java that we currently use, issue the following command
$ which java
If you have only the java we set up earlier and followed the instructions above, you would get a similar output as follows.
/usr/local/java/jdk1.6.0_43/bin/java
Ok.. let's make the Linux aware about the two jdk versions we extracted to /usr/local/java. To do that we issue the following commands.
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.7.0_07/bin/java 2
ok... now the Linux knows about the new java, but has set the priority to 2. Issue the following command.
$ java -version
This would print the new java version.
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)     
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
    
Ooops!!. Where the hell my old java went? Ok don't panic. What has happened here is that you have created a symbolic link to /usr/local/java/jdk1.7.0_07/bin/java from /etc/alternatives/java and another symbolic link to /etc/alternatives/java to /usr/bin/java. When you issue the above java command, the system path /usr/bin/java overrides the path we earlier set. You can check this by issuing the following commands.
$ ls -l /usr/bin/java     
lrwxrwxrwx 1 root root 22 Oct  5 11:14 /usr/bin/java -> /etc/alternatives/java     

$ ls -l /etc/alternatives/java     
lrwxrwxrwx 1 root root 36 Oct  5 11:14 /etc/alternatives/java -> /usr/local/java/jdk1.7.0_07/bin/java
We shall now do the same to the old java we had. To do that, issue the following command
$ sudo update-alternatives --install /usr/bin/java java /usr/local/java/jdk1.6.0_43/bin/java 1

If you issue the following command, we should still see the jdk7 is used.
$ java -version     

java version "1.7.0_07"     
Java(TM) SE Runtime Environment (build 1.7.0_07-b10)     
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

Ok. We shall change to the earlier version by issuing following command and selecting 1.
$ sudo update-alternatives --config java

You will be prompted as shown below.
Selection    Path                                  Priority   Status     
------------------------------------------------------------     
* 0          /usr/local/java/jdk1.7.0_07/bin/java   2         auto mode       
  1          /usr/local/java/jdk1.6.0_43/bin/java   1         manual mode       
  2          /usr/local/java/jdk1.7.0_07/bin/java   2         manual mode     

Press enter to keep the current choice[*], or type selection number: 1
      
By selecting 1, you can switch back to the earlier version. To verify, again issue the following.
$ java -version     

java version "1.6.0_43"     
Java(TM) SE Runtime Environment (build 1.6.0_43-b01)     
Java HotSpot(TM) 64-Bit Server VM (build 20.14-b01, mixed mode)
  
Further you have to do the same thing for javac and javaws commands for both the versions like the following.
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.7.0_07/bin/javac 2     
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.7.0_07/bin/javaws 2     
$ sudo update-alternatives --install /usr/bin/javac javac /usr/local/java/jdk1.6.0_43/bin/javac 1     
$ sudo update-alternatives --install /usr/bin/javaws javaws /usr/local/java/jdk1.6.0_43/bin/javaws 1
    
Now you can select your preferred javac version as below.
$ sudo update-alternatives --config javac

and select the appropriate number. That's it. We can now have as many java versions as we need in our system and switch back and forth easily to experiment with.

  • Python

Most probably python is installed on your Linux. You can check for python version by entering following command
$ python -V     

Python 2.7.4
If python is available you should see a similar output as above. If not install python with the following command, whichever is applicable,    $
sudo apt-get install python        or   
$ sudo yum install python
Ok.. Now let's start writing a sample program. Create a file called HelloWorld.py in your current directory. Enter the following text in it.
#!/usr/bin/python     
print("Hello World!")
Save the file and execute the code using python interpreter as follows
$ python HelloWorld.py     

Hello World!
Great.. Now you know how to program in Python too.

  • Perl

Most probably perl is also installed on your Linux. You can check for perl version by entering following command
$ perl -v
You should see a somewhat similar output as follows.
This is perl 5, version 14, subversion 2 (v5.14.2) built for x86_64-linux-gnu-thread-multi
(with 80 registered patches, see perl -V for more detail)

Copyright 1987-2011, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
    
If not, install perl using the following command, whichever applicable
$ sudo apt-get install perl        or     
$ sudo yum install perl
Done.. Now let's start writing a sample program. Create a file called HelloWorld.pl in your current directory. Enter the following text in it.
#!/usr/bin/perl     
print "Hello World!\n";
Save the file and execute the code using python interpreter as follows
$ perl HelloWorld.pl   
  
Hello World!
That's great... Now you are a perl expert as well.

  • Ruby

To check whether ruby is installed on you Linux, enter the following command.
$ ruby -v
If you have ruby installed, you should see somewhat similar output as below.    
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
If you can't see an output like this, you will have to install ruby by issuing the following command, whichever is applicable
$ sudo apt-get install ruby            or      
$ sudo yum install ruby
Ok. Let's write the same HelloWorld program in ruby. Create a file called HelloWorld.rb and enter the following.
#!/usr/bin/ruby      
puts "Hello World!"
Now you can execute the program by entering following.
$ ruby HelloWorld.rb

Hello World!

  • C#

We finally have come to our last section. C# is a Microsoft developed programming language. Hence you should typically be programming in Windows using the .Net framework. But if you still want to develop C# or any .Net application in Linux, you are not to be made worry. You can install the 'mono', which is an open source implementation of the Microsoft .Net framework. To install mono on your Linux type in the following commands. whichever is applicable.
$ sudo apt-get install mono-complete    or     
$ sudo yum install mono-core.x86_64
When this is done, you are ready to develop .Net applications in Linux. To see the mono version installed, type in the following.
$ mono -V
you should see something similar to the following.
Mono JIT compiler version 2.10.8.1 (Debian 2.10.8.1-5ubuntu1)
Copyright (C) 2002-2011 Novell, Inc, Xamarin, Inc and Contributors. www.mono-project.com
 TLS:           __thread
 SIGSEGV:       altstack
 Notifications: epoll
 Architecture:  amd64
 Disabled:      none
 Misc:          softdebug 
 LLVM:          supported, not enabled.
 GC:            Included Boehm (with typed GC and Parallel Mark)
Let's write a small C# program. Create a file called HelloWorld.cs and type in the following.
using System;     

public class HelloWorld     
{         
 static void Main()         
 {               
  Console.WriteLine("Hello World!");        
 }       
}
We can compile the program by issuing the following command.
$ gmcs HelloWorld.cs
This should create a file called HelloWorld.exe in you current directory. (Note : In Microsoft Visual C# in Windows, we use 'csc' command in the command prompt to compile a C# program). To execute the executable we can type in the following.
$ mono HelloWorld.exe     

Hello World!
Voila!!! Now we can develop any .Net application in Linux as well. Here we are. This post explained you to set up development tools (or rather to see whether it's installed) in your Linux box and walked you in creating sample programs to get started. I hope the article has been easy to follow and fun to read. Flourish in the development world!! Good luck.