Shuva's blog
Recession and Alibaba 
Sunday, June 14, 2009, 09:29 AM - Just a thought
One more effect of recession. Children at school now read "Alibaba and the thirty thieves".

Happy reading.//
add comment ( 923 views )   |  0 trackbacks   |  permalink   |   ( 3.1 / 164 )
Tips for speeding up your Windows XP box 
Sunday, April 19, 2009, 08:25 AM - Tips and Tricks
If you also want to speed up your Windows XP box, the following link serves as a good article.

Windows XP Definitive Performance Tweaking guide


Real life incident:
Shuva: (@ office, losing patience waiting for his laptop to shut down). I will take this question to my grave.
Anadi : What question?
Shuva : Why does Windows take a lifetime to shutdown?
Anadi : (brief pause, shakes his legs as usual, thinks)
...
...
Anadi : Everybody takes a lifetime to shutdown.


Happy Tweaking.//
1 comment ( 189 views )   |  0 trackbacks   |  permalink   |   ( 2.9 / 187 )
The Computer Security Radar Page link 
Thursday, April 9, 2009, 04:06 PM - Links and Bookmarks
I liked this live page

Computer Network Defence Operational Picture

Hope you like(bookmark) it too.

Happy Scanning.//
add comment ( 71 views )   |  0 trackbacks   |  permalink   |   ( 3 / 88 )
C++ : A lock with a timeout : Example code 
Thursday, April 9, 2009, 11:18 AM - Programming
When we use a mutex and wait on it, we basically ASSUME that somebody else is going to release the lock. Most of the time this assumption works. But there can be a situation when you dont want to wait forever on the lock. In such cases we want to have a GetLock() function that has a time out.

One such lock exists in Boost and they are:
boost::timed_mutex and
boost::recursive_timed_mutex

Unfortunately there are too many good resources that explain boost locks with examples. Here is one crude example that I had to write for my own practice.


#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/thread/locks.hpp>
#include <sys/types.h>
#include <linux/unistd.h>

_syscall0(pid_t,gettid)

boost::timed_mutex m_mutex;

void Thread1_CreateObjectAndWait() {
//Get the lock
boost::timed_mutex::scoped_lock guard(m_mutex);
std::cout << gettid() << ": Thread got the lock" << std::endl;

boost::system_time timeout = boost::get_system_time() +
boost::posix_time::milliseconds(10000);

std::cout << gettid()
<< ": Waiting for thread 2 to unlock or else timeout"
<< std::endl;
bool ret = m_mutex.timed_lock(timeout);

if (ret == true) {
std::cout << gettid() << ": Got the lock before timeout"
<< std::endl;
} else {
std::cout << gettid() << ": Got the lock after timeout"
<< std::endl;
}

}

void Thread2_Unlock() {
std::cout << gettid() << " : Unlocking the mutex " << std::endl;
m_mutex.unlock() ;
}


int main() {

//Create thread 1, which will lock itself and wait for thread 2
//to unlock or timeout.
boost::thread thread1(&Thread1_CreateObjectAndWait);

//sleep some time to give time for thread 1 to start.
sleep(2);

//create thread 2 which will unlock before thread 1 gets a timeout.
boost::thread thread2(&Thread2_Unlock);

//Create thread 3, which will lock itself and wait for thread 2
//to unlock or timeout.
boost::thread thread3(&Thread1_CreateObjectAndWait);

thread1.join();
thread2.join();
thread3.join();
return 0;
}


The above code compiled with g++ with the following command:

g++ example.cpp -I<path to boost include dir>  <path to boost lib dir>/libboost_thread-gcc34-mt.a  -lpthread


The o/p should look something like:
28061: Thread  got the lock
28061: Waiting for thread 2 to unlock or else timeout
28062 : Unlocking the mutex
28061: Got the lock before timeout
28063: Thread got the lock
28063: Waiting for thread 2 to unlock or else timeout
28063: Got the lock after timeout



PS: To compile on Windows, replace gettid() with GetCurrentThreadId() and remove the linux specific header file.

Happy Boosting.//

add comment ( 1042 views )   |  0 trackbacks   |  permalink   |   ( 3 / 84 )
Bash Arithmetic : Leading 0 (zero) errors : value too great for base 
Sunday, March 22, 2009, 02:35 PM - Tips and Tricks
It can be quite frustrating at time when you cannot get a simple thing as doing a arithmetic expression where the number has a leading zero. I am talking of Bash arithmetic.

Example:
let x=7*5 is OK.


let x=07*5 is also OK

But
let x=08*5 gives the following error:
bash: let: x=081: value too great for base (error token is "081")

The reason is that numbers with leading 0 is treated as octal in Bash. So 01, 02, 03 ...07 would not have any problem. 08, 09 are not valid octal numbers and hence treated so.

OK, OK. How to get rid of a leading zero. Unfortunately in Bash its not so straight forward. Here are some useful hints:

1. Use a loop like this to get rid of leading zeros from the variable x
while [[ $a = 0* ]]
do
a=${a#0}
done


2. The second solution is to explicity mention that it is a base 10 number, which can be quite cumbersome.

y=081
let x=10#$y*7; #Treat 08 as a base 10 digit.



Happy Bashing.//

add comment ( 421 views )   |  0 trackbacks   |  permalink   |   ( 2.9 / 31 )

<<First <Back | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Next> Last>>