Pages

Friday, May 11, 2012

How Computers Work - Software

Source Code

The "source code" is the high level code authored by the programmer and fed into the compiler. Generally just the program.exe file is distributed to users. The programmer retains the source code. Changing the program in the future generally requires access to the source code. For example to add a feature, the programmer would make changes in the source code, and then run the compiler to produce a new version of the program.

Open Source

"Open Source" refers to software where the program includes access to its source code, and a license where the user can make their own modifications. Typically open source software is distributed for free. Critically, beyond the free price, open source software also includes freedom/independence since the user is not dependent on the original vendor to make changes or fixes or whatever to the source code. Since the source code is available, if a user feels strongly enough about some feature, they can add that feature themselves. Typically open source licenses include a requirement that such improvements to the source code be made available back to the community at large. We'll talk about open source more later on, but I wanted to touch on it here since it is a good example of the difference between a program and its source code.

2. New School: Dynamic Languages / Interpreter

There is a broad category of more modern languages such as Java (the world's most popular language, used in CS106A), Javascript, and Python, which do not use the compiler/machine-code strategy. Instead, these languages can be implemented by an "interpreter", and I will lump them into the category of "dynamic" languages.
An interpreter is a program which reads in source code as its input, and "runs" the input code. The interpreter proceeds through the code given to it, line by line. For each line, the interpreter deconstructs what the line says and performs those actions, piece by piece. For example, Javascript which we have been using, is implemented by a Javascript interpreter which is built into Firefox.
So in Javascript when we have code lines like:
  // Javascript code
  a = 1;
  b = a + 2;
The interpreter runs this code, by taking the lines one at a time, and for each, interpreting its actions. For "a = 1;" the interpreter reserves a few bytes to store the value of a, then stores the value 1 into those bytes. Then for "b = a + 2;" the interpreter evaluates (a + 2) getting the value 3, reserves some bytes for the b variable, then stores the 3 into the b bytes.
A compiler translates all the source code into equivalent machine code program.exe to be run later -- it is a bulk translation. An interpreter looks at each line of code, and translates and runs it in the moment, and then proceeds to the next line of source code. The interpreter does not produce a program.exe, instead it performs the actions specified in the source code directly.

Compiler Evaluation

Compiled code generally runs faster than interpreted code. This is because many questions -- how to append to this string, how many bytes do I need here -- are resolved by the compiler at compile time, long before the program runs. The compiler has, in effect, pre-processed the source code, stripping out many questions and complications, leaving the program.exe as lean and direct as it can be to just run.
In contrast, the interpreter deals with each line in the moment, so all the deciphering and overhead costs of interpreting each line are paid as it runs. These overhead costs in effect make the interpreted program run more slowly than the equivalent compiled program.

Dynamic Language Evaluation

Dynamic languages have two main features. (a) they run more slowly, say 10x as very rough rule of thumb, compared to compiled machine code. (b) languages implemented by interpreters can have significant programmer-friendly features that are very difficult to support with compiled code.
"Memory management" is the problem in a program of knowing, over time, when bytes of RAM are needed and when they can be reclaimed and use for something else. Every program must solve this problem. Memory management is an excellent example of a feature different between compiled and dynamic languages -- all modern dynamic languages manage memory automatically. The programmer can focus on the problem to be solved, and the dynamic language will take care of managing the memory.
In contrast, in C and C++, the programmer must think about memory management at times, and author lines of code to help solve it. (Aside: many crashes in C and C++ programs are due to errors in the programmer's memory management scheme. It is a difficult problem to solve manually.)
The memory management is not free. Dynamic languages, in effect, spend CPU cycles to manage the memory. This fits the general pattern that dynamic languages run with more overhead (i.e. more slowly) than compiled languages.
Because dynamic languages like Java and Python have more features, a programmer can often write the code to solve a problem more quickly in a dynamic language than they can in C++. The time and attention of programmers is generally quite scarce (translation: programmers are scarce and expensive, which is why you want to be a CS major, or at least a minor!). Therefore, dynamic languages which allow the programmer to produce a correct program more quickly and reliably are pretty attractive, even if the resulting program uses more CPU and more RAM. Aside: Moore's law in effect, keeps making the programmer relatively more expensive compared to the CPU.
Overall, different computer languages have different strengths and weaknesses, and best language for a particular problem depends on the situation. As above, dynamic languages like Java and Python can run slower and generally operate with higher overhead than C++ code, so for some problems, writing in C or C++ is the best strategy. Also, Java and Python lack certain "low level access" features which are needed in rare cases.

JIT Just In Time Compiler

The most modern form of dynamic language is implemented with an interpreter paired with a Just In Time compiler (JIT) trying to get the best of both worlds. The JIT looks a sections of dynamic code that are being run very frequently, and for those, does a compile to native code for that section on the fly. So the interpreter is used for simple cases, but for important sections of dynamic code (like the inside of a loop), the JIT creates a block of machine code in RAM for that section. The machine code is run for that section of dynamic code, giving similar performance to C++, and is discarded when the program exits. Java and Javascript both use JIT technology extensively. The great speedup of browsers in the last few years has been largely due to the implemented of JIT technology for Javascript. The JIT erases most but not all of the "10x" penalty. Even with JITs, dynamic languages still have higher overhead use of resources compared to C and C++.

Common Scenarios To Understand

  • Computer boots up -- when first powered up, the hardware runs a tiny "boot loader" program which examines the available hardware and allows the user to select what operating system to start. The operating system starts up, sets up housekeeping, and typically launches a file viewer program to show the user the file systems available.
  • User double clicks on Firefox -- the Firefox icon basically points to a file (actually a group of files) that contain the machine code for Firefox (Firefox is written in C++). The OS sets up a new program environment -- some RAM, a window to draw to -- copies the first section of machine code from the .exe file up to RAM, and starts the CPU running that code.
  • User double clicks on a flowers.jpg file the OS launches a viewer program to display the image stored in that file. The ".jpg" "extension" on the filename suggests what type of file it is. Note however, the extension can definitely be wrong; changing it is as easy as renaming the file. Typically, the operating system keeps associations of what file type to open with what program -- open .jpg files with one program, .pdf files with another, and so on. In this case, the OS would launch an image viewing program, pointing it to the flowers.jpg file as the one to display (that is, display the image whose bytes are stored in that file).

How Computers Work - Software

Operating System

The "operating system" of a computer is like a first, supervisory program that begins running when the computer first starts up ("boots up"). The operating system plays an invisible administrative and bookkeeping role behind the scenes. When a desktop or laptop starts up, the operating system typically gets things organized and then launches a "file explorer" program which displays windows and menus etc. that show the user what file systems are available, allowing the user to navigate and operate on the files.
The operating system keeps things organized in the background so that multiple programs can run at the same time, which is known as "multitasking". The operating system gives each program its own area of memory, so each program only accesses its own resources .. attempting to limit what an erroneous or malicious program can do. Keeping the programs separate is sometimes known as "sandboxing" .. mediating the access of each program so it operates independently, without interfering with other programs or the system as a whole. Similarly, each program has some access to the screen through a window, but this output area is separated from the output of other programs.
Recall that a .exe file or whatever is essentially just a file of machine code instructions. When you double-click the program, it is the operating system that "launches" the program, doing the housekeeping steps of allocating an area of memory within RAM for the program, loading the first section of the program's machine code into that memory, and finally directing the CPU to start running that code.
A digital camera is also a little computer. When it starts up, it does not run a file manager program. Instead, after the basic housekeeping is set up, the camera may just run a single program that draws the menus etc. on the camera's screen and responds to clicks on the camera's buttons and so on.

Where Do Programs Come From?

High Level Languages

It is extremely rare to write machine code by hand. Instead, a programmer writes code in a more "high level" language with features that are more useful and powerful than the simple operations found in machine code. For CS101, we write code in Javascript which supports high level features such as strings, loops, and the print() function. None of those high level features are present in the low level machine code; they are added by the Javascript language. There are two major ways that a computer language can work.

1. Old School: Compiled Language

One common computer language strategy is based on a "compiler". The computer languages C and its derivative C++ are popular computer languages that use this strategy, although they are relatively primitive compared to more modern languages (below).
In C++, the programmer writes C++ code which includes high level facilities such as strings and loops (much as we have seen in Javascript). Here is some C++ code to append a "!" at the end of a string.
  // C++ code
  a = "hi";
  b = a + "!";
This code appends the string "!" on to the end of "hi", resulting in the string "hi!" stored into the variable b. The machine code instructions in the CPU are too primitive to implement this append operation as one or two instructions. However, the operation can be accomplished by a longer sequence of machine code instructions strung together.

The Compiler for the C++ language, reads that C++ code and translates and expands it to a larger sequence of the machine code instructions to implement the sequence of actions specified by the C++ code. The output of the compiler is, essentially, a program file (.exe or whatever) made of many machine code instructions that implements the actions specified in the C++ code. The compiler produces the .exe file from the C++ code, and it is finished. Running the .exe can happen later, and is a separate step.

How Computers Work - Software

Double Click to Run

When the user double clicks a program file to run it, essentially the block of bytes of the instructions for the program are copied into RAM, and then the CPU is directed to begin running at the first instruction in that area of RAM.

How Computers Work - Software

Running

The CPU runs instructions using a "fetch-execute" cycle: the CPU gets the first instruction in the sequence, executes it (adding two numbers or whatever), then fetches the next instruction and executes it, and so on. Some of the instructions affect the order that the CPU takes through the instruction sequence .. for example an instruction might direct the CPU to jump back to an earlier point in the instruction sequence (loops are implemented this way), or to skip over the next instruction if a particular condition is true (if-statements are implemented this way).

How Computers Work - Software

Instructions and Programs

The machine code defines a set of individual instructions. Each machine code instruction is extremely primitive, such as adding two numbers or testing if a number is equal to zero. When stored, each instruction takes up just a few bytes. When we said earlier that a CPU can execute 2 billion operations per second, "operations" there refers to these simple machine code instructions.
A program, such as Firefox, is made up of a sequence of millions of these very simple machine code instructions. It's a little hard to believe that something as rich and complicated as Firefox can be built up out of instructions that just add or compare two numbers, but that is how it works. A sand sculpture can be rich and complicated when viewed from a distance, even though the individual grains of sand are extremely simple.

How Computers Work - Software

Machine Code

"Software" is the general category of code which runs on the hardware. If the hardware is a player piano, then the software is the music. The common case is a "program" like Firefox -- software you run on your computer to solve a particular problem. A computer can run multiple programs at the same time, keeping their use of memory, drawing in windows etc. separated so they hopefully do not interfere with each other.
A CPU understands a low level "machine code" language (also known as "native code"). The language of the machine code is hardwired into the design of the CPU hardware; it is not something that can be changed at will. Each family of compatible CPUs (e.g. the very popular Intel x86 family) has its own, idiosyncratic machine code which is not compatible with the machine code of other CPU families.

Input&Output

Input
One of the best features of a computer is the ability to give the computer commands and feed it information.  Without an input device this would not be possible.  Input devices can be built into the computer, like the keyboard in a laptop, or it can be connected to the computer by a cable.  The most common input device is the keyboard.  There are lots of others such as: mice, trackballs, touch pads, touch screens, pens, joy sticks, scanners, bar code readers, video and digital cameras, and microphones.  In addition, storage devices such as disk drives can serve as input devices.

Output
Input is important but equally important is the ability to read what the computer is doing.  The computer output devices are used to serve the user.  The most common output device is the monitor, or screen.  However most computer come with speakers and a printer which are excellent output devices.  Storage devices such as disk drives and diskettes also serve as output devices when it is necessary to write new or updated data files to disk or tape.

Assignment:
Write a 10 question quiz for a classmate.  Requirement: Four of your questions must be multiple choice, 4 more should be true/false.  One of your questions should be short answer and the last question should be essay.  When you are finished print out your quiz.  While you wait to swap the quiz with another person, create a word find for 20 of the computer terms found in this article using the following website: www.youradsense.weebly.com
Print your word find out when you are finished and complete it.
Use Brainpop to watch a video on computers or technology and take the quiz at the end.