Introduction to UNIX
Operating Systems
Chapter 11
Tariq Nuur, Professor
Chapter 11 Objectives
Grasp the essentials of program development
Learn the steps in the process of creating a
program
provides a general description of the available
computer programming languages.
Differentiate between Source Code, Object
Code and Executable Code
Differentiate between Compiler and Interpreter
Reinforce the use of the shell redirection
operator to redirect the output and error
messages of programs.
Introduction
General Problem-Concepts
– Problem Solving In Everyday Life
Program Development Cycles
Problem Definition
Project Analysis
Project Design
Program Coding
Testing, Implementation and Evaluation
System Maintenance
System Documentation as an ongoing process
Problem Definition-Functional
Requirements
Identify the problem
Understand the problem
Identify alternative ways to solve the problem
Select the best way to solve the problem using
selected solutions
List instructions that enable you to solve the
problem using the selected solution
Evaluate the solution
Providing Solutions
Algorithmic Solutions: using series of
actions
Heuristic Solutions: that cannot be reached
through a direct set of steps
Problem-Solving with Computers
– Off the shelf products
– Outsourcing (Offshore)
– In-house Programming
Programming
Concepts
•Programming is the process of writing instructions (a program)
for a computer to solve a problem or perform a function or task.
•These instructions must be written in a computer programming
language.
•Set of instructions that guide the computer in performing its
basic arithmetic and logical operations. A program can be
anything from a simple list of instructions that adds a series of
numbers together, to a large and complex structure that
calculates the payroll of a corporation.
•Like computer hardware, programming languages have evolve
generations.
•Each set of instructions consists of an operation code &
operations
•Opcode specifies the function to be performed
•Each new generation of languages improved on the previous
ones and incorporated more capabilities for programmers.
PROGRAM DEVELOPMENT
• Computer is controlled by programs that are stored in memory
• Memory is only capable of storing 0s & 1s - Binary Form
• Each instruction tells the machine to perform one of its basic
functions, and usually consists of the operation code and one or more
operands.
• The operation code specifies the function to be performed, and the
operands specify the location or data elements to be manipulated.
• Programming language is needed to write a program
Languages
Low level languages
– Machine language
• Instructions are coded as a series of 0s & 1s
• Difficult to write programs
• Only language computers can understand & execute
• Programs written in other programming languages must be
translated into the machine language of the computer on which the
program is to be executed
High level languages
– Programmer’s convenience
– Programs written in these languages cannot be executed in their
original form & must be converted to a low level machine language
– Conversion done by compilers & interpreters
The hierarchy and generations of the programming languages.
Programming Evolution
Low Level Languages
– 1st Generation: Microcodes and Machine Languages:
Machine Language Instructions are coded as a series of
zeros and ones.
– 2nd Generation: Assembler Language
• Instructions are represented in recognized symbols.
• Unique to a particular computer
• Uses recognized symbols called mnemonics (memory aids) to
represent instructions
• Programs must be translated into machine language format for
execution
• Translation program: Assembler
Programming Evolution
Continues
High Level Languages
– 3rd Generation: Linear Programming
Languages e.g. C, COBOL, PASCAL,
FORTRAN, BASIC, PL/1, ADA, RPG etc.
– 4th Generation: Object Oriented/Event-Driven
Programming e.g. C++, Visual Basic etc.
– 5th Generation: Expert System/Artificial
Intelligence e.g. VP-Expert
Qualities of a good program
– Produces a correct output
– Efficient
– Effective
– Usable
– Maintainable
– Robustness
Programming Structure
– Sequence Structure
– Selection Structure
• Decision
• Case Structure
– Looping (Iteration)
PROGRAMMING MECHANICS
The exact steps to follow for producing a program depend on the computer
environment. The following traces the steps in a typical program’s
development.
Steps to Creating an Executable Program
Regardless of the computer operating system and the programming language,
the following steps are necessary to create an executable program:
1. Create the source file (source code).
2. Create the object file (object code/object module).
3. Create the executable file (executable code/load module).
Source Code You use an editor (such as the vi editor) to write a program and
save what you write in a file.
• This file is the source code.
• The source code is written in the programming language of your
choice.
• By itself, your computer does not understand it.
• The goal is to convert the source code file to an executable file.
A source file is a text file, which is also called an ASCII file. You can display
it on the screen, modify it using one of the available editors, or send it to the
printer to obtain a hard copy of your program’s source code.
Object Code Source code is incomprehensible to the computer.
Remember, computers understand only machine language (the pattern of zeros
and ones).
• Source code must be translated to machine-understandable language.
• This is the job of a compiler or an interpreter. They produce the object code.
• The object code is the machine language translation of your source code.
• However, it is not an executable file yet; it lacks some necessary parts.
• These necessary parts are programs that provide the interface between the
program and the operating system.
• They are usually grouped together in files called library files.
You cannot send an object file to the printer. It is a file of zeros and ones.
If you display it, you may lock your keyboard or hear beeps as some of the
zeros and ones are translated into the ASCII codes that represent codes
for locking the keyboard, the beep at your terminal, stop scrolling, and so
on.
Executable Code Object code might refer to other programs that are not part of
the object module.
• Before your program can be executed, these references to other
Programs must be resolved.
• This is the job of the linker or link editor.
• It creates the executable code, the load module.
• The load module is a complete, ready-to-be-executed program with all
its parts put together.
• The load module, like the object file, is not a file to be sent to
the printer or displayed on your terminal.
• This process, starting from a source file and ending with creating an executable file,
as depicted below.
Compilers/Interpreters
The main function of a compiler or an interpreter is to translate your source code
(program instructions) to machine code so the computer can understand your
instructions.
Compiler is a system software program that translates high-level program
instructions, such as a Pascal code, into machine language that the computer can
interpret and execute.
• It compiles the entire program at one time and does not give you
any feedback until it compiles the entire program.
• A separate compiler is required for each programming language.
• To execute C++ and Pascal programs, one must have a C++ compiler
and a Pascal compiler.
• Compilers produce a better and more efficient object code than
interpreters, so a compiled program runs faster and needs less space.
Interpreter translates a high-level language program into machine language.
However, instead of translating the entire source program, it translates a single
line at a time.
• An interpreter gives you immediate feedback.
• If the code contains an error, the interpreter picks it up as soon as you
press [Return] to finish a line of code.
• You can correct errors during program development.
• The interpreter does not produce a separate object code file, and it must
perform the translation process each time a program is executed.
• Interpreters usually are used in an educational environment, and the
executable code they produce is less efficient than the code produced
by a compiler.
Correcting Mistakes:
Programming Errors
Errors in a program are referred to as bugs.
The process of errors removal is called
debugging.
The three error types are:
• Syntax error
• Logic error
• System error
Debugging Techniques
Non-machine debugging
Machine debugging
Validation (Error Check)
Techniques Consideration
Non-trivial Error Check
Trivial Error Check
Redirecting the Standard Errors
Whenever there are errors in a program, the
compiler do not produce any object code.
It is easy to look at one or two error lines on the
screen, but when you have a large amount of
source code, the chances are good that you will
have more than a few lines with errors.
Therefore, it is advisable to save the compiler
error messages in a file for easy reference.
The redirection capability of shell becomes
handy again!( > and >> metacharacters).
– e.g. ksh menu > menuerror
– pg menuerror to view the error listings.
Redirecting the Standard Error
• The shell interprets the > as standard output redirection.
• The notation 1> is the same as > and tells the shell to redirect the
standard
output.
• The number 1 in 1> is the file descriptor number; by default, file
descriptor
1 is assigned to the standard output device.
For example, the following two commands do the same job:
They redirect the output of the ls command to a file.
$ ls -C > list [Return]
or
$ ls -C 1> list [Return]
• File descriptor 2 is assigned to the standard error device.
• The shell interprets the notation 2> as the redirection of the error
output.
Redirect the compilation errors to another file:
$ g++ -c -o first [Link] 2> error [Return] . . . . Error message
redirected
to the error file.
$_ . . . . . . . . . . . . . . . . . . . . . . . . . . . . No error is displayed.
$ cat error [Return] . . . . . . . . . . . . . . . . . . Check the compilation
errors.
"[Link]",: 6: error: expected';' before "std"
UNIX PROGRAMMING TRACKING UTILITIES
• Other computer language compilers are available that work in a
UNIX
environment.
• UNIX provides utilities to help you organize your program
development
process.
• These utilities become especially useful and important when you
are
• developing large-scale software.
The make Utility
• make is useful when a program consists of more than one file.
• make automatically keeps track of the source files that are changed
and that need recompilation, and relinks programs if required.
• make gets its information from a control file.
• control file contains rules that specify source files’ dependencies
and other information.
The SCCS Utility
SCCS (Source Code Control System) is a collection of programs that helps to
maintain and manage the development of programs.
If your program is under SCCS control, then you can create different versions of
your program easily.
Lesson Summary
Two categories of software
Low level programming languages
High level programming languages
Steps to creating an executable program
Compilers vs Interpreters
Redirecting the standard error