Pages

Monday, February 28, 2011

NO STOPPING NO STANDING

It is meant to be followed while driving but people tend to abide by it while LIVING.
chalna hi zindagi hai ... chalte hi jaa rahe hai 
NO STOPPING NO STANDING

Sunday, February 27, 2011

A worthy candidate for my desktop wallpaper.


I have no idea what its called, it was attractive enough to grasp my camera's attention :)

Tuesday, February 22, 2011

kill a non responsive process

So what do you do when you a process running on your Linux box hangs? Obviously you terminate it and start it again. But how do you terminate it is the question - series of commands or few clicks in System Monitor?
Frankly this post is for those who use the terminal - the "geeks".
Until today I used to kill programs "in this case" like 
$ ps -e | grep firefox
which gave me output like
2472 ?        00:00:00 firefox
2480 ?        07:16:45 firefox-bin  
And I used to fire
$ kill  2472 2480
to terminate the programs.
But today it just occurred to me to automate the process of looking up the process number and killing them. So I set out to use gawk and xargs.
Using gawk I got the process numbers extracted from the output of grep like this:-
$ ps -e | grep firefox | gawk '{print $1}'
2472
2480
and then pipelined this output to xargs kill to invoke kill on each pid.
$ ps -e | grep firefox | gawk '{print $1}' |xargs kill
It kills two processes but for completely restarting the application go to a terminal and type
$ ps -e | grep firefox | gawk '{print $1}' |xargs kill; firefox &
$ firefox & # starts firefox in the back ground
This served my purpose. Terminating two processes manually is not a big task, but why not automate it and make the computer do it for you if its possible?
And don't forget to have a look at man pages of gawk xargs and grep because that is the ultimate source of knowledge :)

Saturday, February 19, 2011

calculate 5000th fibonacci number

This year programming competition in tech-fest at college, was organised by the friend with whom I team up for participating in competitions of other colleges. It was obviously better than previous years. I mean,questions were good, had considerable participants and most importantly, programming was supposed to be done using gcc/g++/java.
There was a question which asked to print the n^th term of Fibonacci series where 0<=n<=5000. Frankly such a large number does not fit in whatever predefined data type you can think of. Creating something like BigInteger of java would have been impossible during the competition and I am not an expert when it comes to java, so I skipped the problem. The coordinators accepted solution for 20 to 30 terms from other teams but demanded it for 5000 from me :P (being famous has its drawbacks !!!). Still I concentrated on other problems  (got through this one but couldn't go to the finals because its timing clashed with another one :( ).
Later that night, after the competition was over, one of the coordinators pinged me on gtalk and asked sarcastically for the solution. This got on my nerves and I determined that I will smack the 5000th element on their face.
There was a bigint library I had created about an year ago which stored numbers in character arrays and manipulated them. I shuffled through my code directory, found it and quickly created a recursive function using memorisation to serve the purpose. The problem was that it worked fine for inputs upto 1302 but gave a segmentation fault for numbers above that. Puzzled and perplexed I resorted to Internet search and discovered that it was because of default stack size of 1MB allocated to every process. A few minutes later I stumbled upon the solution to increasing it which was as simple as adding
ulimit -s unlimited  
to
/home/<user_name>/.bashrc  
file and starting a new session. This increased the stack size to unlimited i.e now my program could use all the RAM available.
After this was done, my program successfully calculated and printed the sequence upto 7000 but ran out of RAM after this and gave Bus Error. 
I redirected the output of the program to a text file, attached it to a mail and sent it to the coordinators ;)
Here is a screenshot showing a terminal window with the 7000th Fibonacci number and  the state of my system while calculating it.

P.S: The code for my program is hosted at www.github.com/bhanuvrat/bigint .