Showing posts with label cpu. Show all posts
Showing posts with label cpu. Show all posts

Wednesday, June 29, 2011

How Computer Programs Work (When They Work!)

Just about everything, from telephones to toothbrushes, has a computer in it these days. Exercise bikes can be programmed to simulate flat beaches and mountainous climbs. (I assume sex toys will soon have similar features, if they don't already.) Coffee makers require rebooting, and microphones can automatically fix your off-pitch singing. Computers win at chess and Jeopardy. Can American Idol be far behind?

So what makes these machines so smart?

Actually they're not smart at all. Computers can only do a few very simple things. All the rest is a combination of those things, as arranged by some clever programmers. And the programs tend to involve a few stock ways of solving problems. Some of the basic problem-solving techniques are:

Repetition - Computers can do the same thing over and over again, very fast, without getting bored (as far as we know.) If you left a piece of candy in one box in a room full of thousands of boxes, the computer would be perfectly happy to look in each box, one after another, until it found the candy.

Condition Testing - If our computer is checking every one of the thousands of boxes in the room, and it finds the candy in the second box, we probably want it to stop looking. For that, the computer's program would contain an instruction like: If you find the candy, then stop. Of course, once you have the candy, you may not care whether the computer keeps on looking or not, and the computer will be perfectly content to keep checking boxes forever, or until it needs a software upgrade.

Recursion - Suppose that in this room of boxes, you might have boxes inside of other boxes, and boxes inside of boxes inside of boxes, etc. Think of a program called CheckBox as a set of instructions:
  1. Look inside box
  2. If the candy is inside, then stop.
  3. Otherwise, if more boxes are inside, do CheckBox on each of those boxes.
In other words, the CheckBox program calls itself if it finds a box inside another box. That will work for boxes inside boxes inside boxes inside ... etc. up to any level. This is a very useful idea for problems that can be broken down into smaller problems, each of which is like a smaller version of the original problem.

Of course, if a person were doing the searching, and found boxes inside boxes, they would automatically look inside without being told. (Then again, computers don't have to be told not to eat a KFC Double Down sandwich.)

Parallelism - The part of a computer that actually computes is called the central processing unit (CPU) or simply processor. More recently, it's called a core, as in dual core, quad core, etc. Lots of modern computers have multiple cores, which means they can do several things at one time. So one smart way to solve a problem is to split it up so that each processor can be working on part of the problem.

It's like having several people search through that room full of boxes you've got. Naturally they'll find the candy more quickly ... but only if they're not all checking the same boxes! You have to make sure each one knows which boxes to check. It would also be nice if the one that finds the candy tells the others to stop looking.

There are lots of variations and combinations of each of these techniques, but these are the basics. Just about every computer program can be viewed as a sequence of these techniques. At some future date, when I really have nothing better to say, I'll discuss some of the kinds of problems programs solve, and how they use these techniques.

Monday, May 23, 2011

Are You Smarter Than a Computer?


C’mon. Admit it. You've felt like that, right? We all have. We hate computers. At best, they're miracles of technology, always out-thinking you, out-communicating you, out-social-networking you and just generally being smart-asses. At worst, they're stubborn pieces of techno-junk that refuse to give you what you want, just because you didn’t use the right secret handshake. ARGGHHH!

That’s why everyone ran out to buy an iPad … Oooh! A computer that will be nice to me! But that honeymoon’s over now too. One of those passionate affairs that burns itself out quickly.  Even the iPad has that passive-aggressive behavior so common in modern electronics.

In old Star Trek episodes, you could destroy a computer just by asking it some paradoxical question. Back then, computers understood speech perfectly, so you could just say “Computer, this sentence is false. True or false?” and it would immediately (because computers are so fast, you see) start spinning reels of tape back and forth, and showering the room with sparks and smoke.

Nowadays, of course, computers are much more robust. Now, to get a computer to crash, you have to do something wild, like … oh, say, try to view a Web page, or accidentally click a button twice or something extreme like that.

Seriously, if computers can process literally billions of instructions every second, why does it take so freakin’ long to do what you want? To understand this, you have to know something about how computers work.

The hub of a computer's nervous system is the Central Processing Unit (CPU).  This is the part that actually runs programs.  On most computers, there's a program running all the time, called the Operating System (or OS).  The OS is the software that waits for you to type and click and scream and swear, and then, if the weather's right and the moon is in the correct phase, it deigns to run whatever program you actually asked for.

But really, the operating system only wants to run a program called Idle.  Idle is what's running when you're not doing anything.  Idle is itself a program that doesn't do anything, but computers seem to find it amusing.  So what the operating system is really trying to do is get rid of you so it can get back to running Idle.  I guess it's kind of like Facebook for machines.

So really, whenever you use a computer, you're interrupting it when it's trying to play its favorite game.  If you've ever been the parent of a teenager, you know how successful that is.

Any more questions?