12 April 2005

Things I learned from Linux kernel :))

The title might sounds funny for some people, especially for non "geek" (Hey, I am not a geek, I dig Linux internal for money :) ). usually, when we want to code something into computer "realm", humans bring analogy and perception he has and tranform it into block of codes. But in my case, the reverse happens, looks like I learn several stuffs from Linux kernel (version 2.4.x and 2.6.x). Note: Linux kernel itself is a result of bunch of people's ideas and thinking, thus as the time goes (more than a decade), it is getting stable and mature.

OK, here we are:

1. Top halves and bottom halves. In Linux, kernel acknowledges incoming interrupt (e.g. keyboard), grab the important data and put it into a buffer and defers the real task sometime later via bottom halves. This trick make sure interrupt is acknowledged fast but the kernel itself won't get busy doing things everytime interrupt comes

What I learned : Response fast if you face something emergency. Take a note on every important facts or interesting event. Then, if this thing doesn't risk your life or bring doomsday, I am sure it can be "deferred". Later, if there is no higher priority task than those emergency events, I'll try to work on it

Example: brainstorming for article idea. I got idea, i write it down on my agenda...and later ....I'll working on it

2. Dynamic priority of individual task. On Linux kernel,it tries to award bonuses toward a task which sleeps a lot and punish a task which hogs CPU. Periodic sleep is considered a sign by most kernel developer that the task is interactive task, thus whenever it is woken up, it needs to react and run again as fast as possible. At the time a process sleep, the scheduler will pick another runnable process with highest dynamic priority

What I learned: First of all, it doesn't mean that I told you to sleep a lot :)) (more than 10 hours of sleep, gee, low blood pressure? go to a doctor !).

Second, sometimes we are dealing with a kind of task that heavily depends (or probably 100%?) on the response of external sources. What happen if the response we are waiting for isn't arrived yet? Force it? If you can, do so....but if you can't, the best you can do is wait. I know it sounds boring, but sometimes life gives us limited (hard) choice. While awaiting, you can do another job or simply rest. If the response comes, react as fast as you can while suspending everything you do.

Example: It happened to me while searching the answer for "how to make cache condition nearly the same everytime I do mysql benchmark?" Several possible solution fell down from the sky after 3-4 days. So, I simply drop it and do another writing...but still let my brain open for new ideas. when an idea seems like a reasonable one....i quickly test it.

The opposite also happens. Writing mainly occupied my daily life, but I also start an IT consultancy firm. When writing "hogs" my time, I try to lower its priority so I can work on something else, especially when dealing with new clients. This way, clients won't feel abandoned, the business is rolling and I can still write :)

3. Demand paging and Copy on Write, study case on forking new task. Linux has unique method called demand paging whenever it creates new process address space. It does the memory mapping, but it doesn't actually allocates pages for the new task (let's say, when you execute Firebird). So when it allocates a page? If a task refers to a linear address that is mapped to non existent page (but still inside valid address space), kernel will generate page fault, grab the data from block device (e.g. hard disk) and store it on a page frame inside process' VMA list

Copy on Write has another story, if a task fork new child, the child will be assigned (via pointer) the pages of its parent but marked as read only. When the child writes something on it, the memory management system will copy the page into the child's address space. This way, memory usage will be conserved and prevent a worst case, fork-then-exec..because if you do copy the whole pages of parent and then throw it again....guess what....memory thrashing !

What I learned: I try to widen my perspective, but I consider it as milestone only. To fill the actual knowledge, I learn it step by step. This way, I grow with the same speed as my brain grows :) I don't think, being smart but end up in mental house is a smart choice :)

Example: Doing business (real business, not "busy"-ness ) is a tough thing to do (at least for me). There are many things demand big attention. But, there is always major rule such as doing preliminary approach, make proposals, negotiating fee, etc etc. If you try to mmap()-ing all business aspects in your brain, i guess you will get segfault under 5 seconds, maybe a bit longer if you take viagra :) (will you test it for me? ) :)))

cu soon !

1 comment:

Anonymous said...

how do you dig Linux for money ? can u tell me how ? :)

How to execute multiple commands directly as ssh argument?

 Perhaps sometimes you need to do this: ssh user@10.1.2.3 ls It is easy understand the above: run ls after getting into 10.1.2.3 via ssh. Pi...