Describe what happens when a computer program is executed.
Running Computer Programs
Running a computer program refers to the execution of the instructions in the program by the computer. When you run or execute a program, the operating system plays a key role as it provides the program with access to the resources of the computer in a controlled fashion. The following SlideShow describes in more detail the typical sequence of events that occur when a program is run and how the program, the operating system, and the hardware of the computer interact.
Running a program from the command line on Windows and Linux involves different commands and steps based on the operating system. Here’s a simple guide for both:
On Windows:
Open Command Prompt:
Press `Win + R`, type `cmd`, and press `Enter`.
Navigate to the Program's Directory (if necessary):
Use the `cd` command to change directories. For example:
cd C:\path\to\your\program
Run the Program:
Type the name of the executable file and press `Enter`. For example:
program.exe
If the program requires administrator privileges, you might need to open Command Prompt as an administrator by right-clicking the Command Prompt icon and selecting "Run as administrator."
On Linux:
Open Terminal:
You can usually find Terminal in the system’s application menu or use the shortcut `Ctrl + Alt + T`.
Navigate to the Program's Directory (if necessary):
Use the `cd` command to change directories. For example:
cd /path/to/your/program
Run the Program:
If the program is a script (e.g., `.sh` file), you might need to make it executable first:
chmod +x program.sh
Then, run the program by typing:
./program.sh
For binaries or executables, simply type:
./program
Notes:
On Windows, file extensions like `.exe`, `.bat`, or `.cmd` are usually necessary.
On Linux, if the program is in your `PATH` (common directories like `/usr/bin`), you can run it directly without navigating to its directory.
Example Commands:
Windows:
cd C:\Users\YourUsername\Downloads
example.exe
Linux:
cd ~/Downloads
chmod +x example.sh
./example.sh
This should help you run programs from the command line on both Windows and Linux.
Executing Computer Program
Compilation into Machine Code
When you compile your program, the compiler translates the C++ source code into machine
code. The resulting file consists of machine instructions and information on how to load the program into memory prior to execution.
Machine code is sometimes called object code, but we do not use that terminology to avoid confusion with C++ objects. Machine code
files usually have the extension
.obj
.o
Machine Code
For example, the machine code for the hello program might be stored in hello.obj. The machine code file contains only the translation of the code that you wrote. That is not enough to actually run the program. To display a string on a window, quite a bit of low-level activity is necessary. The authors of the iostream package (which defines cout and its functionality) have implemented all necessary actions and placed the required machine code into a library. A library is a collection of code that has been programmed and translated by someone else, ready for you to use in your program. A special program called the linker takes your machine code file and the necessary parts from the iostream library and builds an executable file.
Executable File:
The executable file is usually called hello.exe or hello, depending on your computer system. It contains all machine code necessary to run the program. You can run the program by typing hello at a command prompt, or by clicking on the file icon, even after you exit the C++ environment. You can e-mail that file to another user who does not have a C++ compiler or who may not know that there is such a thing as C++, and that person can run the program in the same way. Your programming activity centers around these files. You start in the editor, writing the source file. You compile the program and look at the error messages. You go back to the editor and fix the syntax errors. When the compiler succeeds, the linker builds the executable file. You run the executable file. If you find an error, you can run the debugger to execute it one line at a time. Once you find the cause of the error, you go back to the editor and fix it. You compile, link, and run again to see whether the error has gone away. If not, you go back to the editor. This is called the edit-compile-debug loop. You will spend a substantial amount of time in this loop in the months and years to come.
The next lesson will introduce you to the language of the computer, typically known as machine language or machine code.