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.
More Midterms
I ended up getting a 48 out of 50 on my math midterm, which is pretty good. Both the points I missed were due to some amazingly stupid mistakes, ("the derivative of 2 is 2" etc.). I got a B+/A- on my chemistry midterm (depends on how the curve works out).
Coming up this week, is another math midterm and my history midterm. Yeah for studying....
How Not To Study For A Test
I've realized that I learn best by doing, especially with math. Some people can understand a mathematical theorem completely after having heard the professor explain it in rather terse math lingo, but I can't understand a theorem until I've taken a good look at it and gone through how and why it works.
My math professor told the class that the best way to learn math, is to practice, he then preceded to say, "when I was in college taking calculus, I did all the problems in the book, and I got really good at doing integrals." At this point, everyone looked at him funny. After all, what kind of a deranged man would solve every single question in a math book on a whim?
I had my math midterm today (Wednesday) and two days ago, I thought about what my professor had said, that the best way to learn math is to practice. So I woke up bright and early Tuesday morning (yesterday, the day before my midterm) and preceded to do all the odd numbered problems in the book. Of the 9 or so sections we had covered so far in class, I managed to complete only 6 due to time constraints. On the last 3 sections, I did a few problems and read through my notes.
I went through a lot of paper in the process.
On the plus side, I ended the day being amazingly proficient at integrating and differentiating logarithmic and exponential functions, at finding inverse functions and their derivatives, and at L'Hopital's Rule and its applications. On the down side, I hardly left my room the whole day (mostly, for food, class, and the restroom) and I didn't go to sleep till 2AM Wednesday morning.
I went into the midterm feeling a bit apprehensive, despite my exhaustive studying. I came out feeling strangely exuberant. It would be presumptuous of me to declare that I got an 'A' on the midterm, though I felt I did quite well, so I'll say that I should get at least a 'B.'
Theres a point to this story, and it is: Do not attempt to study for a math test by doing every problem in the book the day before.
Sage advice if I ever heard any.