Monday, June 11, 2007

Is this implementation of select() syscall in Linux correct?

Here is the code
linuxMachine:/tmp # uname -a
Linux linuxMachine 2.4.21-309.PTF.97199.1-smp #1 SMP Mon Jul 24 12:20:00 UTC 2006 i686 unknown
linuxMachine:/tmp # cat testSelectCall.c
#include "stdio.h"
#include "sys/time.h"
#include "sys/types.h"
#include "unistd.h"

int
main(void) {
fd_set rfds;
struct timeval tv;
int retval;


FD_ZERO(&rfds);
FD_SET(0, &rfds);

tv.tv_sec = 5;
tv.tv_usec = 0;

retval = select(1, &rfds, NULL, NULL, &tv);
/* Here is the tricky part! */

printf("tv.tv_sec=%i\n",tv.tv_sec);
printf("tv.tv_usec=%i\n",tv.tv_usec);
if (retval)
printf("Data is available now.\n");
else
printf("No data within five seconds.\n");

return 0;
}
linuxMachine:/tmp # !gcc
gcc testSelectCall.c
linuxMachine:/tmp # !time
time ./a.out
tv.tv_sec=0
tv.tv_usec=0

No data within five seconds.

real 0m5.000s
user 0m0.000s
sys 0m0.000s
linuxMachine:/tmp #

On a solaris box

solarisBox>uname -a
SunOS solarisBox 5.10 Generic_118835-02 sun4u sparc SUNW,Sun-Blade-100
solarisBox>gcc testSelectCall.c
solarisBox>!time
time ./a.out
tv.tv_sec=5
tv.tv_usec=0

No data within five seconds.

real 0m5.025s
user 0m0.003s
sys 0m0.010s
solarisBox>


Man Page has the following description
man 2 select

Some code calls select with all three sets empty, n zero, and a non-null
timeout as a fairly portable way to sleep with subsecond precision.

On Linux, the function select modifies timeout to reflect the amount of
time not slept; most other implementations do not do this. This causes
problems both when Linux code which reads timeout is ported to other
operating systems, and when code is ported to Linux that reuses a struct
timeval for multiple selects in a loop without reinitializing it. Con�
sider timeout to be undefined after select returns.

Monday, June 04, 2007

Microsoft announces surface

I am sure, many are aware of it. In case you haven't then check this out

http://www.microsoft.com/surface/

Wednesday, May 30, 2007

Getting shell script variable values

Here is the problem

bash-2.05# cat test1.sh
ATM_PIN="123"
echo "Environment variables" > out.log
env >> out.log
echo $ATM_PIN

bash-2.05# sh test1.sh
123

bash-2.05# cat out.log |grep -i atm
bash-2.05#

While debugging a shell script, if the values of the variables are required in an intermittent state of the shell script execution use the -a option.

bash-2.05# sh -a test1.sh
123
bash-2.05# cat out.log |grep -i atm
ATM_PIN=123
bash-2.05#

Thursday, May 17, 2007

Configuring rsh service on Linux

By default the rsh service is not enabled (obviously).

So here are the steps

1. Check if the rpms are installed. (else use the cd's to install)
redhat# rpm -qa|grep rsh
rsh-server-0.17-25.4
rsh-0.17-25.4
redhat#
2. In /etc/xinetd.d/rsh set disable = no
3. Edit the following files

redhat# cat /etc/hosts.allow
#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
ALL : localhost/hostA
redhat# cat /etc/hosts.equiv
localhost
redhat# cat .rhosts
+ user1

4. /etc/init.d/xinetd restart
5. Test the above
redhat# rsh localhost
redhat#
//password less login to remote machine

Disclaimer: more secure services like ssh are recommended than services like rsh.

Friday, March 30, 2007

How does one sort the files based on size in Unix ???

Again elementary but not used much

ls does not give an option to sort the files based on file sizes. (some os have -S option not sure which)

Here is a way to do it in a very generic way


bash-2.05# ls -hl /tmp/a.out /tmp/test.c |sort -k 4 -n
-rw-r--r-- 1 root other 0 Mar 30 21:41 /tmp/a.out
-rw-r--r-- 1 root other 108 Mar 29 11:49 /tmp/test.c

sort -k does the trick. So position can be any column in the output.
For more details man sort

Wednesday, March 28, 2007

Debugging tomcat applications using IDE's

Problem:
You have deployed your war file into tomcat. You have the source code, but you cannot innitate a DEBUG from the IDE(Eclipse or Netbeans). How does one debug?

Solution:
Run tomcat in DEBUG mode

WINDOWS
EDIT %CATALINA_HOME%/bin/catalina.bat
set JAVA_OPTS=-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=3091,suspend=n

UNIX

EDIT $CATALINA_HOME/bin/catalina.sh
JAVA_OPTS=-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=3091,suspend=n

Restarting tomcat will render it in debug mode listening on port 3091.

Now in your favourite IDE have the REMOTE DEBUGGING listening on port 3091.
Then the course is normal, place breakpoints in your code and trace for possible bugs.

Wednesday, March 14, 2007

Partitioning in postgres

Partioning is useful to drop group of data in a table in bulk. In most cases year old data is purged on a regular basis by an application. Partitions are a useful design design to manage the data.

create table master (i int);

create table slave1 ( CHECK ( i > 0 AND i <> 10 AND i <> 20 AND i < postgres="#"> 0 AND i <> 10 AND i <> 20 AND i < 30) ) inherits (master);

postgres=# insert into master values(5);
INSERT 0 1
postgres=# insert into master values(15);
INSERT 0 1
postgres=# insert into master values(25);
INSERT 0 1
postgres=# select * from master;
i
----
15
25
5
(3 rows)
postgres=# select * from slave1;
i
---
5
(1 row)
postgres=# select * from slave2;
i
---
15
(1 rows)
postgres=# select * from slave3;
i
---
25
(1 rows)

Note: Copy command of postres does not copy the rules associated with the table. So to make sure the rules are reflected, create partitions as a trigger.

Also here is an interesting thing

postgres=# update master set i=15 where i=5;
ERROR: new row for relation "slave1" violates check constraint "slave1_i_check"

This says it all

Saturday, March 03, 2007

Effective JDBC

JDBC supports connection pooling, which essentially involves keeping open a cache of database connection objects and making them available for immediate use for any application that requests a connection. Instead of performing expensive network roundtrips to the database server, a connection attempt results in the re-assignment of a connection from the local cache to the application. When the application disconnects, the physical tie to the database server is not severed, but instead, the connection is placed back into the cache for immediate re-use, substantially improving data access performance.

To get more of it checkout these links
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/conpool.html
http://dev.mysql.com/tech-resources/articles/connection_pooling_with_connectorj.html

From my limited research, I understand tomcat implements connection pool by default.
Here is the link taking at length about it http://www.javapractices.com/Topic75.cjp


Also during the research came up with this nice article by the Martin Fowler talking about the design decisions of allowing certain business logic in the database rather than handling them exclusively in the application software (esp things like orderby, filtering tools (WHERE,LIKE etc))
Here's the link
http://www.martinfowler.com/articles/dblogic.html

This was typically the point made by the oracle database legend Tom Kyte in the article JDBC : SQL vs PL/SQL, Which performs better



Simple anology(not entrirely a perfect analogy) ,
when we know we need to grep for say automountd process to just know the pid of the process
instead of ps -aef|grep auto
a simple ps -a -o comm,pid|grep auto
will be more effective.

This design problem is tackled at across various layers. A simple typical case is the OS, where we typically end up getting huge data( say in truss or ps output), there after prunning the processed(cpu) data using grep,awk like utilities. A tool which stops from generating the unwanted data from being generated always scores over the basic tools we use.

Friday, March 02, 2007

How to know the current process state

This one feature though trivial will be thoughly learnt on a need basis.

When we need to find out what is the current state of a process is (aka Running(R), Sleeping(S), Stopped(T),Zombie(Z) etc), we can use the ps command effectively.

Here is the basic setup
test@shantanu>more simple1.c
#include
int main()
{
int i=0;
printf("Waiting for a console output\n");
scanf("%d",&i);
return(0);
}
test@shantanu>gcc simple1.c -o simple1
test@shantanu>./simple1
Waiting for a console output
........///No input is yet given

In another terminal run
test@shantanu>ps -a -o comm -o s|grep simple1
./simple1 S

So the process is currently sleeping for my input.
All the state changes hereafter can be observed independently.

Tuesday, February 27, 2007

core file generation

Got to know this interesting bit today

By default in solaris core dump occurs when an application misbehaves(say segmentation fault).
But in linux the default core dumping is disabled.

Using ulimit command one can turn on this feature.

Just enable the application to core dump by
$>ulimit -c unlimited

For more on this refer to
http://www.novell.com/coolsolutions/feature/16257.html

Friday, February 23, 2007

Amazon turks now in Indian rupees

Amazon turk now renders Indian rupee transaction. Check it out
http://www.mturk.com/mturk/welcome

Saturday, February 10, 2007

Sun Tech days in Hyderabad

Sun tech days from Feb 21st to 23rd. Though most of the technical community is aware of this event , please check this link to get the details. The list of talks,hands on labs, codecamps are here

Tuesday, February 06, 2007

Continuations in java

Continuation is an object that represents the execution state of a program at a certain point. We can use continuation to restart the execution from the point stored in it.
One area that has seen practical use of continuations is in web programming

Check out an enumeration here

Monday, December 25, 2006

Friday, December 22, 2006

Crystal clear Bangalore on google map

Check this out

Now the map is much more detailed and expressive than the satellite based imagery.

Thursday, October 19, 2006

How to list only directories in Unix

Well this may seem a very simple problem, but its not. So it deserves a thread for sure

Traditional method

ls -l|grep ^d

But u get the other unwanted details, incase u are automating

Slightly Sophisticated

find . -type d

But on execution u will dicern that find is recursive, so "just the" folders of the current directory are not displayed

If its getting slightly complicated try this simpler solution.

ls -l|grep ^d|awk '{print $9}'

Pretty neat.
This command works almost fine. But just in case the folder has a "space", the command fails to get the exact name of the folder.

Here is a solution which "WORKS" and is very simple

ls -p |grep /

Just an indicator of the power of the raw tools invented ages ago. KISS(Keep it Simple S......)

Saturday, October 07, 2006

Google does it again

Check this out. Its out and open for people to use.

Now here are the interesting issues with it.
1) Be prepared for spams. Just try this, you will know.
2) Emotions the poor developers go through :-X . Just pick your slang, google does the rest.I just hope openSolaris is clean!!!!!!!!!!!!!!
3) Some programmers put the default password in sourcecode and Most lazy users never change the default password.This makes it easy to get the default password which can be exploited. Try this

Actually this is not a great innovation in itself. Krugle, Koders, and Codease are into this. But a gaint entering will have its own advatages.

"Good programmers write good code. Great programmers find it on Google!"

Tuesday, September 26, 2006

PHP sucks big time

I was trying to integrate Apache--PHP--mysql for a HR project at janagraha.

The problem is that the php documentation is very old...or I should say it works for lower versions of php and apache.

So here is a small TO-DO on how to go about integrating
1) Download Apache here. Apply your user friendly GUI knowledge of windows and get it installed.
2)Download php here.
3)Unzip in d:\php5.
4)Rename php.ini-recommended to php.ini
5)Add these lines to the top of httpd.conf
# PHP 5 Support
LoadModule php5_module "d:/php5/php5apache2_2_filter.dll"
LoadFile "d:/php5/php5ts.dll"
AddOutputFilter PHP php
PHPIniDir "d:/php5"

***Please note the locations used.
6) Finally process is over. Create a test.php.
Having say

copy to htdocs folder of apache.

7)RESTART APACHE. (hopefully no errors).
Run http://localhost/test.php

Worth a try though

Thursday, September 07, 2006

Google Releases Tesseract as Open Source

Google recently released Tesseract as open source. Originally developed at the HP Labs from 1985-1995, it has been vouched as one of the most accurate Optical Character Recognition (OCR) programs available. You can download Tesseract here.

Checkout one possible use of this technology here

Leaving the rest to imagination....