failing like never before

7May/091

Something is Not Quite Right

Take a look at this C function, and see if you can can catch whats wrong with it. (That is, what about this function could produce an error?)

void wait_for_ready(void)
{
    while ((inb(0x1F7) & 0x80) || !(inb(0x1F7) & 0x40))
        continue;
}

This question showed up on my midterm and stumped the hell out of me at the time. Now that I know the answer, I feel like a complete idiot for not spotting the problem initially, seeing as its so amazingly obvious. When I took the exam, I spent way too much time on this problem, completely overthinking it.

In this function, we're using a machine level instruction, inb, to read the value of some hardware port on an x86 machines. So far so good, or so I thought when I was taking my exam. But the problem is, is that the data we're reading can be a shared resource, that is, other applications could be writing and reading to it at the same time, so a race condition ensues. Even on a machine with a single processor, this is still a problem, since wait_for_ready() could read the data memory into register, then a context switch could occur, some application could write to that location, and then wait_for_read() regains control but operates with an old data value.

So simple. And now I feel like an idiot.

28Apr/090

Request for a Change

Hauling out my change of major request essay...

Prior to UCLA my knowledge of anything vaguely associated to the field of electrical engineering was essentially nil, so it should come as no surprise when I say that I cannot for the life of me begin to remember why I chose electrical engineering as a major. And now, the more electrical courses I take, the more tiresome I find the subject matter to be. So the question I should be answering isn't "why do I want to change majors," but rather, "why did I wait till now to change?"

Over the last two years, I have trudged my way through the required introductory math and physics courses, never particularly excited by the material but always telling myself that it was going to get better when I reached the EE courses and that then, everything would be worth it. So it came as somewhat of a surprise to me, that I found the electromagnetics in EE 1 to be as unexciting as the materials in all the prerequisite courses. But after having spent the better part of two quarters convincing myself that "it was going to get better," I continued to drag myself through another uninteresting class, and during my second year, I proceeded to take EE 2, 16, 116L, 10, and 102, continually hoping that things would get better. Happily, they did.

It was during second quarter of this year while I was doing EE homework one night, that I realized that the only EE classes that I have ever actually enjoyed were the multi-departmental classes (16, and 116L) shared by the EE and CS departments, and that CS 35L (informally called “intro to Linux) was without a doubt my most enjoyable class ever. Shocked by my newfound epiphany, I stood up from my desk and declared to my roommates that I did not like electrical engineering and that instead, computer science was the field for me. My roommates were unsurprised by my sudden realization, saying that they had always noticed that I liked computers more than electrical engineering, and that the energetic mannerisms that I adopted when talking about Linux or computer architecture deeply contrasted the dull and tiresome attitude I exhibited when I approached my EE homework. Indeed, during my past two years at UCLA, I have probably spent as much time tinkering with Linux and reading essays like Neal Stephenson’s In the Beginning was the Command Line, and Fred Brooks’ The Mythical Man-Month: Essays on Software Engineering, as I have dedicated to my EE classes.

I am, however, not choosing to switch into computer science, but rather computer science and engineering since my enthusiasm for all things computer related extends below (so to speak) the software level. Now lately, numerous people have pointed out to me the similarities between electrical engineering with the computer engineering option, and computer science and engineering, and asked why I simply do not stay with EE CE. A question to which I can only respond to by saying that the differences between the two majors is quantitatively quite small (That is to say, the difference in course requirements between the two is almost negligible.), but that the content of the almost negligible difference is quite significant to me; I would much rather take a compiler class rather than another electromagnetics course.

Having now realized what subjects I enjoy the most, I am loath to continue in a major I find no pleasure in. Though perhaps over-dramatic, it would not be inaccurate to say that this change of major could have the potential to dictate my future happiness.

22Feb/090

Change of Majors

College freshmen are always told that on average, a college student will change their major at least twice (or something like that, please don't quote me on this). I always thought that I would never change my major; I liked my choice, I had always wanted to be an engineer, and I thought I knew what I wanted. But over the past year, its occurred to me that I have no idea why I decided to pick electrical engineering, and the more EE classes I take, the more I realize how little I actually like the subject; the thought of having to take another electromagnetics class doesn't exactly make me want to dance a jig, and while solid state physics can be interesting sometimes (thanks to my awesome professor), I can't see myself working with wave equations and Fermi-Dirac statistics for the rest of my life. Oh, and plus, I am no longer a math king. Really more of a math peasent then anything else, which is definetely not a good thing when I'm in a major that has so many math requirements that its only two classes short of a math minor.

I came to an epiphany several weeks ago that my major simply was not making me happy, and that it was time for me to change. I've chosen Computer Science and Engineering as my new major, although it remains to be seen whether or not my application into the Computer Science department will be accepted.

7Nov/080

Keeping It Clean

Ernest Hemingway once said, "I write one page of masterpiece to ninety one pages of shit...I try to put the shit in the wastebasket." And just like writers, programmers also manage to produce quite a bit of shit. Of course, most programmers are perfectly capable of recognizing when they've created an inelegant function, but because of time restraints and deadlines, they just keep on going. But students that are still learning to program, often have trouble recognizing when code is in desperate need of deletion.

A university-level programming class usually requires students to spend a great deal of time working on projects and homework. So its quite easy, after hours of coding, to become emotionally attached to one's code. Generally, a student programmer will make a mistake in a function or block of code, and so they'll add in a quick little fix. Eventually, after numerous little fixes, their function starts to become a giant, awkward, lumbering behemoth that is still wrong.

The most obvious choice is to simply, as Hemingway so succinctly said, "put the shit in the wastebasket" and start from the beginning. But the student has managed, after hours of labor, to become extremely attached to their hundred lines of awkward code and just can't manage to throw away the terrible result of all their hard labor. I say this because when I was learning to program, I often found myself becoming too attached to my code. On one particular occasion, I spent two days patching up the code for my self-balancing AVL binary tree, which was frankly, a piece of shit. Eventually, I threw my AVL tree class in the garbage, started again from scratch, and recreated a working tree class in a few hours.  If I had only been a little more emotionally detached, I might have realized how stupid I was acting.

In order to keep code clean and effective, student programmers need to learn to stop falling in love with their misshapen code-children; code cannot love you back, so don't waste your time developing emotions for it. Just keep it clean kids, and save the loving for another time.