Monday, January 31, 2005

How to avoid BROKEN PIPE errors in code (SIGPIPE)

Recently I had to struggle a lot to fix a issue generating BROKEN PIPE on a AMD64 platform, this code works on EMT64 and other 32-bit platforms but fails on AMD64. The bottom line is be really careful when you do use a fflush.

void *stream = (FILE *)......;
............
............
//To make sure the data is written to disk (HA)
if(!fflush((FILE *)stream)){
printf("Flush failed due to %d error refer error.h\n",errno);
}
//This is really very dangerous code if you dont have a signal handler
//written for SIGPIPE (either you write a sig handler for SIGPIPE) or
//use the following code//

/*Safe Code*/
if(!fsync(fileno((FILE *)stream)))){
printf("Sync failed due to %d error refer error.h\n",errno);
}

I found the standards guide on context of libc functions really useful
http://www.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_05.html
Hope you will not go through the same pain....
Vamsi

Monday, January 17, 2005

Symbol referencing errors for static variable in C++

Are you stuck with static symbol referencing errors in ur code??
What could be wrong in the following definition of a c++ class to find out how many instances of MyClass are created??

class MyClass{
private:
static int __instance_count;
public:
MyClass(){
__instance_count++;
}
int getInstances(){
return __instance_count;
}
};
main(){
....
}

When complied with g++ says that __instance_count is not defined...this can be resolved by defining __instance_count outside the class definition,as follows.
int MyClass::__instance_count;
Why do we need this seperate definiton of static variables?

Saturday, January 01, 2005

04 to 05 with a 4-5 puzzle....

01-01-05_1857
Originally uploaded by vamsi.
I have seen people circulating this 04 to 05 puzzle to check how difficult 2005 will be compared to 2004. It's crazy but timepass. Get the square
here cut it into 5 pieces and solve it.....finally keep track of how much time you took to solve this and this will be proportional to the hard times you will have to face in 2005....how about people who solved this already :)) not problems at all in 2005??