Of system calls and references

I have been mucking around with low-level system programming recently. I must admit, for someone who has a little C knowledge and is used to the glossy GUI of Windows, UNIX and System programming can be…messy to say the least.

Interestingly, most system calls have the following format

void sys_call (int * ret_val)

instead of

int sys_call ( )

Because of this, programmers have to declare a variable for storing the return value from a system call rather than use it directly in computations. This seems counter-intuitive and didn’t really make sense to me for a long time. I checked in many places on the Internet hoping I could understand why this paradigm was chosen but in vain.

Finally, I believe I have solved it!

This paradigm has to do with the user space and kernel space concept of the kernel. System calls provide the interface between these two spaces. The idea is that the kernel will have all permissions to access, modify, create and delete files and data in memory. Whereas, the users have limited permissions.

When you use the void sys_call (int * ret_val) way to use syscalls, You are effectively asking the kernel to do something and put the return value in the memory address (pointer) passed to it by the program. Since the kernel has the access to all the memory addresses, this has no problem.

However, when you use the int sys_call ( ) way. You are asking the kernel to do the computation, allocate a memory address to store the value and then send back the value.

It’s important to understand that UNIX comes from a time when memory was precious and I/O was  terribly, terribly slow. Hence they would try to make it as efficient as possible.

The return int way of doing sys_calls has the overhead of actually allocating a memory in location;copying the data onto this memory location and when returning from the method, copying it onto the left hand side of the expression.

Whereas in the void way, you just have to copy the data onto the memory location. Since RAM was constant time lookup, this took the same time irrespective of the memory address, be it kernel or user space.

That was pretty cool trick to optimize for a problem long forgotten.


Write small; write often

I’ve been here quite a few times. Not writing anything for a long time. Not that I’ve not learnt anything new. Rather, I was too lazy to write. There seemed nothing that I wanted to write about. I guess I was too held up trying to be perfect; trying to get it right the first time. That went well… Anyway, I realized that most of the problems I faced were due to me not sure of who my audience were. Djikstra had the same problem. He came up with his EWDs. Following this tradition, these shall be my SGKs. I won’t be explicitly numbering them though. If anyone wants to, this shall be SGK #0. So, I am going to dump whatever comes to my mind; even the smallest things. A line; a phrase; a small technology. Who cares what I write about? Should anyone other than me care? If they do, thanks a bunch. If not, I am not writing for them anyway. Ok, enough of this rant, back to work.

TL;DR I’m back, again :p

Deepavali in Chennai


Deepavali time always was the best time of the year, the most cherished memories of one’s childhood revolve around deepavali, you would either go to relatives’ homes or they would come to yours. It was always fun to meet up with your cousins, no matter if they were not the same age group as you or not.

A week before deepavali, most of the conversation in schools would be about the new clothes and how much fire crackers everyone bought. The cost was not the topic, our parents never told the exact values anyway, the amount was all that mattered, arm lengths were the standard units during the conversations. No one was jealous of others, it was just an information exchange and a leverage to beg your parents to buy more.It didn’t matter how old you are, deepavali meant fire crackers and everyone shall light at least one, no exceptions. If they didn’t, you made them do it.


Roads during deepavali looked like they were ravaged by war,it’s like a battlefield out there with paper bits all over the place.Bombs would go off all the time, some nearby, some farther away.All adding to the intense battle field effect.The smell of the fire crackers, strong and crisp in the air, was intoxicating.

To put it into context and to parody Apocalypse Now,

“Love the smell of fire crackers in the morning, smells like Deepavali.”

100 walas (garland crackers) would be the automatic rifles and 1000 wala would be the machine guns.The little ones had their toy guys with rolls which went out with a small pop.

No matter how pro you are, you can never be too careful with atom bombs, utmost caution is taken in lighting these green thread covered bad boys, light it and run faster than Usain Bolt to safety. the bang never fails to disappoint,and it’s the final sign that deepavali is finally here.and if it didn’t go off, you better not try to light it again, that little devil might as well blow right in front of your face.True to it’s name the atom bomb had a very loud explosion (120 decibel from the box), if you were close enough, you may even feel the shock waves pushing your hair back.

But this was not the loudest one available, its big brother, the naattu vedi (country bomb) had an even louder explosion and was obviously illegal but if you had the right connections, you can get it. Interestingly, these are used in Temples in Kerala as a part of the daily rituals.


The lone motor cyclist was like a scout travelling through the front lines, always a loose bijli would go off startling the random motor cyclist on his way back home, startled but never fazed.

for the rich, 1000 walas are not enough, they buy even bigger ones, ones that go from one end of the street to another, it shall be the crowning jewel of the festival. The entire family would be there to witness it, from the youngest kids to the oldest grand parents. Not to mention, the entire street too.


But this was not enough,lighting fire crackers the prescribed way is too mainstream, bijlis(the smallest crackers available) would be lit in the hand and thrown up in the sky for an aerial shot.the braver ones would try the same with lakshmi vedi.aerial shots would be lit upside down, just for the heck of it. and fountains laid flat so that they act like ground rockets ( surface to surface missiles)

The night-time was a different story altogether, a pacifist version of deepavali if you like it, but that story later.

Here’s how a deepavali day goes for an average tamilian.
waking up early in the morning, taking a head bath (ganga-snaan), new clothes and finally the crackers which were carefully hidden away from the children before deepavali. finally the wait is over and off we go to the streets, even the worst of enemies wish each other happy deepavali and co-operate to safely light their crackers.

no matter how much you had, you will be done by 11 am or you will take a break to watch the special deepavali pathimandram (debate) by solomon papaiya on Sun tv. Who cares what the topic was!! all that mattered were the jokes. By 2pm, everyone would be done, have a hearty deepavali lunch and go for a sleep, a war hero’s well deserved rest.


The hyper little ones won’t sleep, instead they’ll find some left-over fireworks, get the chemicals from inside them and make a bomb by themselves, guerrilla style. By bomb, I mean they empty out the chemicals into a newspaper sheet and light it up from a distance.It was never quite successful, but doing such things with cousins always had their charm.

you’ll wake up right in time for the 5 pm special movie on sun tv / jaya tv / vijay tv.etc. The movie would have been decided from a large selection through a prolonged, heavy debate, the likes which have never been witnessed in neither the parliament or the UN. Of course, parents always ceding to the childrens’ demands, they had the power to veto.

Finally, with the movie over, now for the nightly round of rockets, fountains, chakras and the like. These would have burnt the biggest holes in one’s wallet. competitions would be held as who would be able to light the most number of fountains with a single sparkler and some would go for the remaining bombs, adding to a night-time battle field complete with artillery attacks. Firecracker companies never fail to impress with more and more innovative firecrackers ( the fancy ones) coming out every year. Of course, the rich guy in the neighbourhood would have bought most of the new ones, but we never cared, we were content in lighting our fountains and watching their fancy new ones. after all, they just had to light the fuse and stay back and watch. There was no fun in lighting the fuse, the watching had all the fun and we had free tickets to that!!

The most interesting one was the butterfly firework, these sneaky little ones were introduced by “Standard Fireworks” and been a big hit ever since, you light the fuse and the cylinder starts oscillating very fast, and by fast, i mean frigging speedy gonzales fast, god knows what they put inside it, it would jump all over the place changing colours and no one and nothing was safe, the movement was too random, even more random than lady gaga’s outfits and we had to alert the neighbours and they would alert us when they’re lighting one, the only advantage is that it gets over within about 10 secs, god bless that, who knows what it would have done if it went for longer durations.

One can never be cautious enough with these

Once you were done with your crackers, it’s time for a moon light dinner, many of the family members would go to the roof for the brilliant display of rockets bursting into beautiful patterns of red, blue, violet,gold all over the place, any where you look, any time, there would be these beautiful display of colours.


The day after, you realize the actual “scale of destruction”. the street were full of the cracker remains. The carnage was unprecedented and the sweepers had a tough time restoring normalcy. you bid farewell to your cousins and promises were made as to who would go to whose home in the winter holidays.

A small sample

A week after deepavali, the conversation would still be on the topic of deepavali, how many crackers you burst, how long the neighbour hood rich guy’s wala was (yeah we bragged about that too). How much fun you had with your cousins and finally how our favourite hero saves the damsel in distress in the movie.

Overall, deepavali was our answer to Christmas of the west. It has never failed to disappoint

<NOTE> This was written around a year ago, about the same time. Happy Deepavali! </NOTE>

Intel Underestimates Error Bounds by 1.3 quintillion

Do checkout Pentium FDIV bug too

Random ASCII

imageIntel’s manuals for their x86/x64 processor clearly state that the fsin instruction (calculating the trigonometric sine) has a maximum error, in round-to-nearest mode, of one unit in the last place. This is not true. It’s not even close.

The worst-case error for the fsin instruction for small inputs is actually about 1.37 quintillion units in the last place, leaving fewer than four bits correct. For huge inputs it can be much worse, but I’m going to ignore that.

I was shocked when I discovered this. Both the fsin instruction and Intel’s documentation are hugely inaccurate, and the inaccurate documentation has led to poor decisions being made.

The great news is that when I shared an early version of this blog post with Intel they reacted quickly and the documentation is going to get fixed!

View original post 2,252 more words