CCS 3105-Systems Programming - 1
CCS 3105-Systems Programming - 1
Programming
Lecturer: Musumba
Email: [email protected]
Tel: 0796180811
ICS 3105: Systems Programming
• Systems Programming-
• Computer systems are made up of hardware and software. Software
generically refers to the programs running on the computer.
• While both hardware and software can be modified or upgraded, it
tends to happen more often with software.
• In fact, the major reason to have software is to provide the ability to
change the instruction stream executed on the computer. This means
that it is often expected that new programs will be written, or old
programs will be modified or evolved, during the life cycle of a
computer system.
Introduction
• A number of tools and resources have evolved over the last 30 years
to assist program development.
• These include standard libraries (also called system libraries), system
calls, debuggers, the shell environment, system programs, and
scripting languages.
• Knowledge of these tools greatly enhances the ability of a
programmer.
Introduction
• System programming is defined as the use of system tools during program
development.
• Proper use of these tools serves several purposes.
• First, it saves a great deal of time and effort. Using system libraries saves a
programmer the time it would take to independently develop the same
functions.
• Using a debugger saves an enormous amount of time in finding and fixing
errors in a program.
• Common tasks, such as searching for text within a set of files or timing the
execution of a program, are facilitated by the existence of system programs.
Introduction
• Second, system tools provide opportunities for program development
that are otherwise extremely difficult to come by.
• System calls provide access to the core functions of the operating
system, including memory management, file access, process
management, and inter-process communication.
• Some standard libraries implement complex functions that are beyond
the capability of most programmers.
• For example, the math library includes trigonometric functions and
other real-valued operations that require iterative methods to reach a
solution.
Introduction
• Third, consistent use of system tools promotes standards, so that code
developed for one computer system is more easily ported to another
computer system.
• System libraries provide a layer of abstraction, implementing the same
function calls on multiple computing systems.
• An application can call a system library without worrying about the
details of the underlying hardware.
• In this manner, the application can be ported as long as the destination
system possesses the same system libraries.
Introduction
• Finally, there is the shell environment.
• The shell environment is rich with options, capabilities, and
configurability, to the point that it is overwhelming to novice
programmers.
• However, once some proficiency has been gained, the shell is a
powerful tool for any serious system programmer.
• It offers tremendous flexibility in process control, system management,
and program development.
Required Background
• This course assumes that the learner has a basic understanding of
programming, such as variables, loops, conditionals, and control flow.
• The learner should have a working computer system, with a C
compiler, text editor, shell, and debugger already installed.
• The learner is assumed to be familiar with the basic operation of the
computer system, such as navigating a directory or folder hierarchy
and executing programs.
• Lastly, some shell knowledge is also assumed, such as that obtained
from an introductory programming course.
Why Unix/Linux?
• The Windows operating system is designed to simplify computer
usage.
• A Windows computer is essentially a closed system. The system is
designed to be turnkey, in line with the business model of providing
the typical user with a system that is as easy to use as possible.
• This design strategy necessarily obstructs getting “under the hood,” to
keep the typical user from doing something harmful to the system.
Why Unix?
• In addition, Microsoft publishes limited information on the internal
workings and design of Windows. This is also a business policy to
protect their design from competitors.
• Finally, Windows is monolithic, not allowing for various parts of the
system to be disconnected or swapped for alternatives.
Why Unix?
• Once again, this is a business decision; Microsoft wants to sell its
products and only its products, so it makes its system fully integrated.
• It is straightforward to understand the business motivations for the
closed design, scarcity of published details, and non-modularity of
Windows.
• However, the very properties that aid the typical computer user can
be frustrating for the student studying system operation.
Why Unix?
• Unix, on the other hand, and specifically Linux, has different design
principles.
• It is open source, so that all details of its inner workings can be
studied.
• It is completely modular, so that any system component can be
swapped for an alternative.
• For example, the Linux kernel is developed completely independently
of the desktop environment. Within the kernel, a Linux computer
operator has many choices as to how to configure the operation of
the system.
Why Unix?
• The kernel itself can be swapped or modified.
• One could argue that these properties prevent the more widespread
adoption of Unix by typical computer users, who are not interested in
this flexibility and openness.
• However, it is these design properties that make Unix an attractive
choice for a student studying system operation.
Why C?
• C is closest to the hardware. All programming languages provide various
levels of abstraction to assist in program development.
• For example, the concept of a named variable, as opposed to a numeric
memory address, tremendously simplifies program development.
• Out of all the commonly used programming languages, C provides for the
least abstraction and hence is closest to the hardware.
• Most single C statements translate simply to machine code. The available
data types in C tend to reflect what the hardware directly supports.
• Accessing memory via indirection (pointers) provides the programmer
with the ability to access all parts of the system.
Why C?
• Historically, the development of the Linux kernel, as well as the development
of the original Unix operating system, was done in C.
• Most system software is developed in C. Device drivers are almost always
written in C.
• An indirect benefit of being close to the hardware is speed.
• Code written in C tends to execute faster than code written in other
languages.
• For a programmer who intends to work on system software, or who intends
to develop code that closely interacts with hardware (peripherals or the main
system), studying concepts using the C language provides opportunities to
develop the most practical skills.
The Three Tools
• The three main tools of a system programmer are a shell, a text
editor, and a debugger.
• Familiarity with these tools increases programming skill and decreases
the time it takes to get programs working properly
Shell
• It is assumed that the reader is familiar with basic operations in a
shell, such as generating a file listing.
• There are a number of good books and online references that provide
the necessary details to operate a specific shell.
• A shell is a program that allows the user to run other programs.
• A shell is usually executed in a terminal.
• Historically, a terminal was a simple display and keyboard that was
connected to a computer, providing a text-only interface to the
computer.
Shell
• Today, most operating systems provide a graphical interface in which
multiple virtual terminals can be run simultaneously, each running a
separate shell.
• If a specific terminal is reserved for system administration or error
messages on behalf of the entire computer system, then it is
sometimes referred to as a console.
• In practice, the words shell, terminal, and console are often used
interchangeably.
Shell
• Most Unix systems provide several methods to start a shell. Some
systems provide a shell after login, without the benefits of a graphical
desktop.
• On other systems a shell must be started manually through a menu or
mouse click interface.
• Once started, a typical shell looks like the ones shown in Figure 1.
• Commands are entered into the shell through a text-only interface.
• The shell informs the user that it is waiting for its next command via a
prompt.
Shell (Fig 1)
Shell
• A typical command is the name of a program, which starts execution
of that program. For example, both the shells in Figure 1 show the
execution of the ls program, which provides a listing of files in the
current directory.
• Many of the programs run in a shell have text-only input and output,
similar to ls.
• However, it is also possible to run graphics- (GUI-) based programs
from a shell.
• For example, Figure 2 shows the result of typing xclock at the prompt;
it starts the xclock program.
Shell (Fig 2)
Shell
• A command line argument is anything typed at the shell prompt after
the name of the program to execute.
• It provides information about how the user wishes to run the
program.
Shell
• The control and flexibility offered by command line arguments is often
useful during program development and system administration.
• While many programs can be reconfigured while running, selecting
options through menu interfaces can take time.
• Configuring the program at startup through command line arguments
can save a great deal of time, especially if a program is run multiple
times, such as during development.
Shell
Shell
Shell
• The purpose of both shell internal commands and system programs is
to assist the system programmer.
• It is not terribly important to remember whether a particular operation
is a shell command or a system program.
• Sometimes the operations are listed all together.
• The important thing is to become comfortable with the common
operations that save time and effort.
The Text Editor
• The basic operations of a text editor allow the user to write and edit
code, save it to a file, and load it from a file.
• These operations are not much different from those supported by a
word processor.
• In fact, it is possible to use a word processor to write code (although it
is not recommended).
• However, there are additional features that a text editor can provide,
beyond what a typical word processor provides, that are designed to
support programming.
Text Editor
• Perhaps the most important features a text editor can provide to a
programmer are the ability to display the line number of the program for
the given cursor location, and the ability to move the cursor to a given
line number.
• As a programmer becomes familiar with a given text editor, other useful
features will be learned. The ability to search and/or replace a given
string is often helpful in debugging variable usage.
• The ability to cut and paste a word, line, or block of code is often useful
during code writing. The ability to arrange indentation helps support
good coding practices. Some text editors support color coding of
keywords as well as of code blocks
Text Editor (Screen shot)
Text editor
• Examples
• Emacs
• Vi
• Gedit
Text Editor
• Not all text editors share all features. The text editor vi relies upon
keystroke combinations to access features. The text editor emacs uses a
combination of keystroke and menu operations.
• Newer text editors tend to rely more upon menu operations since they
can be easier to find. It is beyond the scope of this course to delve into all
the features for various text editors.
• One can easily find an online manual as well as online feature guides for
all the popular text editors.
• The important thing to realize is that whichever text editor is used, a
programmer should dedicate some time to becoming comfortable with
those features that support programming.
Debugger
• The debugger is perhaps the most important tool for a system
programmer.
• It allows a programmer to observe the execution of a program,
pausing it while it runs, in order to examine the values of variables.
• It also allows a programmer to determine if and when specific lines of
code are executed.
• It allows a programmer to step through a program, executing it one
line at a time, in order to observe program flow through branches.
Debugger
• The debugger is itself a program, which is executed like any other
program.
• As with shells and text editors, there are many debuggers. In this
course, examples are explained using the GNU debugger, which is
usually executed as gdb.
• Although it is possible for a debugger to interoperate with more than
one compiler, most debuggers are correlated with specific compilers.
• In this course, the concepts and examples are explained using the
GNU C compiler, which is usually executed as gcc.
Debugger
• To explain how a debugger works, we will use the code example.
• Suppose this code is stored in a file called sum.c.
• In order to compile the file, one could execute the following
operation:
ahoover@video> gcc sum.c
ahoover@video>
Debugger
• This produces a file called a.out, which is an executable program.
Typing a.out runs the program:
ahoover@video> a.out
sum=29
ahoover@video>
Debugger
• The program is executed, running until it ends, at which time the shell prompts
• for another command. In order to use the debugger to run the program, one must follow a sequence
of operations:
ahoover@video> gcc -g sum.c
ahoover@video> gdb a.out
(gdb) run