Friday, August 10, 2012

How to free a port that is bond by a process in linux?


First find the proces ID (pid) of the process that uses the port.

For example, to free the port '51000', first find the pid of the process that uses the port '51000'. Use the command 'netstat -tulpn' for the same.
$netstat -tulpn
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:33660           0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:24800           0.0.0.0:*               LISTEN      1640/synergys   
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -               
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -               
tcp        0      0 0.0.0.0:51000           0.0.0.0:*               LISTEN      11056/sim    
tcp6       0      0 :::445                  :::*                    LISTEN      -               
tcp6       0      0 :::139                  :::*                    LISTEN      -               
tcp6       0      0 :::22                   :::*                    LISTEN      -  
Using the command 'netstat -tulpn', we found that the 'pid' of the process that uses the port '51000' is '11056'. Now we have to kill the process that binds to the port '51000'. Issue the following command for the same.

$sudo kill -9 11056

On doing the above, the port would be freed.

Saturday, July 28, 2012

On linkers


Linker is a program that converts the object files into executables and shared libraries.

What a linker does? can be summarized into the following steps:
  • Parse options on the command line
  • Parse link scripts to read commands of linking
  • Symbol resolution
  • Relocation of instructions, data, symbols and sections
  • Output the linked file

Let's see the working of some existing linkers such as GNU ld, gold and MCLinker.

GNU ld
In the linking flow of GNU ld, the following steps are done iteratively:
  • Read and load the input files.
  • Do format checking.
  • Perform symbol resolution.
  • Apply relocation.

gold
gold separates the linking processes into two stages as follows:
  • stage 1: Do the following steps iteratively until all the files are read.
    • Read and load the input files.
    • Resolve symbols while checking the formats of the input files.
  • stage 2: Perform relocation in parallel.

MCLinker
MCLinker separated the linking processes into three stages as follows:
  • stage 1: Read and load input files. Check the formats.
  • stage 2: Symbol resolution.
  • stage 3: Perform relocation.

Click here for the reference of the above discussed linkers.
Click here for the what and how of linkers by Ian Lance Taylor.

Tuesday, July 3, 2012

Packager for binaries on any UNIX-compatible system


You may require to bundle the binaries built on any UNIX-compatible system for distribution.

For example, say, you have built a compiler using GCC compiler generation framework. And you need to distribute the built compiler binaries to some user. That means here you are required to distribute your gcc compiler installation directory to the user. How would you do that?

You can make a tar of the installation directory and distribute it to the user. But here you have to educate the user on how to untar and where to untar the provided tar file. And this way the user has to put some "extra" effort for using the binary distributed to him. Wouldn't it be better to provide the user with a self-extracting package of binaries?

Yes, you can provide the user with a self-extracting package of binaries. And the user has to just run it to extract the binaries into his desired directory. How to create a self-extracting package of binaries?

There is tool called "makeself". This is a very simple tool that does the job for you. Install this tool in your system and use it for creating the self-extracting package of binaries. For more information on "makeself", refer http://megastep.org/makeself/.

So for our example, assuming "myCompiler" as the installation directory to be distributed, create the self-extracting package "myCompiler.run" as follows:

makeself.sh --notemp myCompiler myCompiler.run "myCompiler generated from GCC framework" echo "myCompiler has been installed"

Sunday, June 10, 2012

Monday, May 7, 2012