Monday, June 30, 2008

taming the infinite loop

Because I have made this mistake more times than I can count, I thought I'd write a bit about it.

In operating systems we often learn this type of construct in order for sharing resources:

ask for a resource
if (resource isn't free){
while (resource isn't free){
do nothing
}
}


use the resource
release the resource

This is kind of a semaphore, allowing only one process to access the resource at a time. It works great for operating systems. However it is not an okay construct to use in a single process.

Why the difference? Well, in operating systems, each process is scheduled out--one process does not have to complete before another process executes. So, in the middle of that busy wait, the operating system stops that process from executing and gives the other process(es) a chance to execute. If it didn't, then that first process with the busy wait would never complete because all the CPU time would be used just waiting for that while loop to be over instead of executing the other process that's currently using the resource. That's called deadlock.

Deadlock is exactly what happens when you use that kind of construct in a single, non-threaded program. Instead of executing that other function all your resources are dedicated to wait in that busy loop.

Sometimes this kind of thing is necessary, though. In the program I'm working on right now, I need a function to wait until an object is not null in order to continue. So I've been working for three days on this and this morning I finally realized that I was in deadlock like that. Easy solution: add a timer.

function myFunction(){
if (we're calling this function for the first time)
send request for the resource
else
after a set amount of time call timerFunction
}

function timerFunction(){
if (the resource is available)
do what I need to with it
else
call myFunction
}

Problem solved. While that timer is waiting to execute, it gives the other functions a chance to return that resource.

And now to more interesting problems, such as data logging over a web application. Eww. The stateless nature of web programming makes this an unhappy task. Yay PHP sessions!

crunch time

two weeks left.

feels like about three weeks of work to do.

wonder how this is going to turn out.

In other news, I went to see my amazing sister in WV yesterday. I miss her a lot. I miss my family and my friends back home. I am ready to go home.

I've been making important life decisions, since one day I woke up and realized that grad school applications will be due unbelievably soon, and since time is moving unbelievably fast. I am pretty sure that I will blink and it will all the sudden be Christmas.

I have decided that I am not moving across the country until I am married unless crazy things happen. If that makes me weak, then whatever. I don't care about your opinion. I'm all for seeing my dreams come true and moving across the country to take that perfect job if the opportunity arises, but being near to people I love is part of that dream. It's so important to me to be working with good people and to have people to share my life with. Life is so much richer when you're sharing it with someone, and here close to my friends and family I can do that. If I'm a day's journey away from the people I love it is harder to communicate and much less fun. If/when I move far away I want to have someone to share it with. It's not an independence or incapability issue; I have proven to myself many times that I am capable of starting over in a new place and taking care of myself. It's a matter of my priorities, my desires, my dreams, and what I am convinced will be the happiest life for myself.

I love my life. I'm pretty excited about all that is to come. It's awesome to be here with so many choices and opportunities, and I mean awesome in the full sense of the word: bringing about awe, admiration, wonder, terror. Who would have guessed that I would end up here and now? It is intimidating and unbelievable and incredible. I can't wait to see how it all turns out.

And now...to fit three weeks worth of work into two. I can do this if I work at full potential. Here's to making the most of every moment.

Saturday, June 28, 2008

a simple guide to static variables

I have been doing programming type stuff since middle school now (nine years, whoa) , with my first object-oriented experiences in high school Java class. I remember talking about static variables then (maybe in college too, I don't know). However, last Thursday I was writing some Actionscript code for my research project and realized I was mindlessly typing "public static var" in front of all my declarations, and that I really didn't know what "static" meant. So, I checked to see who was online, and my friend Lane was, and I asked him what static variables are. He explained it this way:

class Orange{
public var count;
public static var staticCount;

Orange(){
count++;
staticCount++;
}

}
...

var valencia = new Orange();
var navel = new Orange();

So, what will valencia.staticCount be? What will navel.staticCount be? What about valencia.count and navel.count?

Turns out that static variables are shared across all instances of a class. So, either of the staticCounts will be 2 after each instance is created (since each time the instance is created, the class increments staticCount), while each count will be 1, since the constructor also increments the non-static, unshared variable as well.

After I asked that question I figured out that a whole bunch of my program's bugs had to do with me declaring static variables where they should not be.

I don't know, it seems like several people I've talked to don't (or didn't) fully understand static variables either, but it's something really helpful to know.

Friday, June 20, 2008

i think this happens a lot

...wellll, except for the whole jumping out the window part, anyhow.


Planning ahead has its merit, but at the same time I think we miss a lot of opportunities in life
if we're letting our planned future get in the way of things that could be really good here and now. That's just my take on it.


(by the way, that's from http://www.xkcd.com)

Thursday, June 19, 2008

a bit of coding wisdom

"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
-- Brian Kernighan

"Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live."

via http://www.codinghorror.com/

the demise of fish #2

Lynn (named after my academic advisor) died today, after Dr. McCrickard confessed at lunch to deriving enjoyment from giving his students sick fish to try to nurture and bring back to health, and after Stacy confessed to licking Larry previous to his death. Hahah.

The third fish is named Tiffany, after Dr. Tiffany Barnes. She's made out of construction paper. I think she'll live a bit longer than the other two.

Other than that, things are going great here. I'm a bit behind on my self-made project timeline, but it's all good, I'll still make deadlines.

Not much else beyond the typical is happening. I'm sure I'll post more later.

Wednesday, June 11, 2008

any summer internship where you end up having nerf gun wars/pillow fights/throwing things at your coworkers both in the lab and in the dorms has to be good.

i'm just sayin.

Sunday, June 8, 2008

miscellany

So, things are going well. Three weeks down at VT. I like my project, and I love the people I live and work with. We work hard, but we play hard too. We play volleyball almost every night and frisbee twice a week. I've learned how to do a cartwheel. I've had my hair completely done in braids. I've seen waterfalls and climbed trees and gone to the duck pond. It's been good.

On a sad note, Larry, my fish, died. He was sick. I was taking him to Petsmart, but he died before I could get him there. Sad day.

This weekend I'm home for my sister's graduation from high school. I'm so very proud of her. We did graduation this morning, and afterwards I got to see a bunch of people from the church I grew up in, which was good. A bunch of us went and saw The Strangers tonight. I laughed the whole way through it, but that's just what I do during cheesy scary movies

Tomorrow, I go to church and then head back to Virginia. For some bizarre reason I volunteered to watch the one-year-old nursery tomorrow during Sunday school. I suck at dealing with kids. I think that when Mom said that they needed help down there tomorrow I thought, hey, this is my opportunity to prove that I can totally take care of small children, despite common belief. So I told her I could help. She laughed at me. I took it as a challenge. I can so handle this.

Today I came to the conclusion that writing code soothes me. I think I take comfort in knowing that my carefully scripted lines will be interpreted in the same way time after time after time and that they won't change by themselves. I'm in control of my code. Nothing else has the power to change it. If there's something wrong, it's my fault, and it is within my power to fix it. I like that.

Does anyone else feel this way?

I think it's why I like housework sometimes too, especially when I'm nervous or upset. It is good to be able to put order into disorder, to make chaos livable.

I'm reading Zero: The Biography of a Dangerous Idea, and it's been talking a lot about how much resistance there was to zero or infinity existing. Early mathematicians had no notation for zero or infinity, and no representation of irrational numbers (numbers that can't be written as fractions), and in many sects suggesting the ideas of these things was tantamount to heresy. People were uncomfortable with the idea of void--it breaks a lot of properties that hold true for every other number. People were also uncomfortable with infinity, mostly because that's something we can't wrap our minds around. Yet zero and infinity are incredibly important concepts for higher level math. Calculus and physics are impossible without them.

To be honest, I'm afraid I'm a bit uncomfortable with infinity and void as well. Not in a numerical sense (I don't run away screamign from calculus textbooks!) but in a "I want my life to fit within these neat constraints" sense. Every day I learn that things are a lot bigger than I am and that there's a whole bunch that is out of my control. There's seemingly infinite time ahead of me and a void filling it. I have no firm plans. My life is different than I expected it to be, in the best of ways. It should be a comfort to me to know a sovereign God who brought all of creation into being even though it started in chaos, but unfortunately I am not as trusting as I should be. I wish to arrange these things myself...but I can't.

Sorry, guess this post was a bit disjointed. My thinking's disjointed right now too. It's time for sleep.