Reading 1
Reading 1
Reading 1
Contents 1
Course Contents
Part 1: Introduction, UNIX
Part 2: C/C++ Basics
Basic types, expressions, flow control, functions, passing parameters
Part 3: C/C++ Basics Continued
C-structures, arrays, pointers, references, input/output, C-strings, dynamic memory allocation
Part 4: Code Modularization and Abstract Data Types
Part 5: Bits and Bytes
manipulating data at the bit level
Part 6: UNIX File I/O
Part 7: Parallel Computation with POSIX Threads
Part 8: C++, the better C
Template functions, class templates, Standard Template Library
Contents 2
[DOCUMENT FINALIZED]
Motivation 3
Motivation
Typical Personal Computer (PC) Mainboard
Motivation 4
Schematic Design of a PC
631
CPU = Central Processing Unit (Brain of the computer, manages data flows from connected devices to and from memory)
ROM = Read-Only Memory (doesnt lose content when power
switched off, contains start-up code)
RAM = Random Access Memory (misnomer: better would be
read/write memory, loses content when switching power off, is
filled with code/data from harddrive when booting and launching
applications)
Motivation 5
Motivation 6
CPU
Register File
ALU
Data
R0
R1
...
Rn
Memory
(sequence of bytes)
Program Counter PC
93
1
0
239
92
1
0
42
235
3
LD 1, R1
R1 := R1 + 1
ST R1, 1
Machine Program
(Assembly Language)
JMP 1003
1003
Motivation 7
Motivation 8
Problems:
How can we handle a large variety of machine architectures and machine codes?
We want to write programs at a more abstract level
than machine code, so that they are easier to read,
write, debug and reuse.
We want to write one program that can run on many
different machines without changes.
Motivation 9
Solution:
Design operating systems that abstract hardware peculiarities by means of device drivers that interact with
hardware and provide a uniform programming interface.
Write programs in high-level programming languages
that get translated into particular machine code by a
program called compiler.
So, instead of having to produce N machine code versions of our program for N different machine architectures, we only write our program once and let the
N different compilers (that somebody else wrote ,)
translate our program.
Motivation 10
Software/Hardware Layers
CMPUT201
CMPUT379
CMPUT229
Libraries
User Programs
Operating System
CPUs
Devices Memory
Motivation 11
Linux
As a computing scientist, its a good experience to install and administer your own Linux system at home.
There are many freely downloadable distributions appropriate for new users, such as Ubuntu or Linux Mint.
Two options for running Linux at home:
Install to a hard drive partition, either as your only
OS, or alongside Windows or Mac OS X.
live CD or USB stick. Boot up to Linux without
having to install it on your hard drive.
If you run Linux at home, you can program in C using
the same tools that we will use in the lab.
Mac OS X
Mac OS X is based on Darwin, which is Apples version
of NetBSD UNIX. OS X ships with all of the development tools we will use in the labs, such as:
the bash shell (a command line interface)
emacs and vi (text editors)
gdb and ddd (debuggers).
gcc / g++ or clang (C / C++ compilers, see below)
If you dont have these tools installed, you can use the
homebrew package manager to install them for you.
Homebrew is free, and makes it easy to install and maintain open-source programs on your Mac.
OS X has a small number of differences from Linux
when it comes to C / C++, that we probably will not
encounter in this course. Your programs should compile
and run on both without changes.
Windows
For Windows, tools like Visual Studio let you write,
compile, and debug C and C++ programs.
Most of the language is the same as it is on Linux,
although functions for interfacing with the OS are often
different.
The C and C++ languages are continuing to evolve
over time (such as the C89, C99, C++0x / C++11,
and upcoming C++14 standards). Microsoft / Visual
C++ hasnt kept up to date with these standards, while
the UNIX world largely has.
Although you could use something like Visual Studio
for programming exercises, UNIX is a central part of
this course and you should use it. Its easy to log into
a Linux lab machine from home - well explain how in
a few pages.
Getting Started 21
Getting Started
Two ways of accessing a UNIX computer:
1. Sitting in front of it and typing in a command window
2. Connecting to it from a remote machine using ssh
(secure shell)
ssh ug01.cs.ualberta.ca
Both require you to provide a Comp Sci username and
password.
In what follows we will present a UNIX refresher.
Command Shell 22
Command Shell
In interactive mode, shells are command line interfaces (text window with keyboard attached to it)
e.g. xterm
Issue operating system or internal shell commands
directly via keyboard input; e.g.
ls (list directory contents)
cd (change directory)
mv (move/rename)
ls -lrt
cd workdir
mv old-file new-file
mkdir AS1
rm -rf dir
cat text
echo hello
exit
man man
apropos root
Shell continued
Special directories:
/ root directory, everything is stored beneath
. current directory
cp ./foo ./bar = cp foo bar
.. parent directory
cd ../.. : 2 levels up
~ home directory
cd ~/foo
cd = cd
Command history/editing
use arrow keys to navigate, <delete> or
<backspace> keys to remove characters
The shell is also a simple programming language
variables, functions, command aliases
The file ~/.bashrc (if using the bash shell) contains shell code that is run when the shell starts
You can customize this!
function ll() { ls -l "$@"; }
(files starting with . are hidden but can still be
edited, or seen with ls -a)
Launching Programs 25
Launching Programs
Type program name (+ parameters) and hit the return key <ret>
ls<ret>
emacs foo.c<ret>
Shell interprets the first word as command name and
tries to locate a function definition with this name
(see ~/.bashrc). If this fails it searches in the directories listed in variable $PATH (try echo $PATH)
To run programs in your current directory, you need
to add ./ to specify that you want this program.
MyProgram<ret> (command not found)
./MyProgram<ret> (works!)
To detach a program from the terminal to run it in
the background, type:
./program &<ret>
Or, if a program is already running, type:
<ctrl-z>bg<ret> (bg: background)
To regain control of a background-ed program, type:
fg
Wildcards 26
Wildcards
* matches all strings
? matches one character
Examples:
wc *.c
count the words in all files with names that end with
.c
ls foo?bar
list all filenames that start with foo, followed by an
arbitrary character and bar
Hidden Files 27
Hidden Files
Files with names starting with . are hidden, they are
not listed nor matched by wildcards
This is why ls does not show . nor ..
Useful for avoiding clutter
(e.g. many resource files .*rc in ~)
ls -a reveals them
Filename Completion 28
Filename Completion
Many shells have a filename completion feature: when
hitting the <tab> key the shell tries to complete the
filename. Saves typing!
cat super<tab>
will complete the command to
cat supercalifragilisticexpialidocious
if this is the only filename starting with super
Input/Output Redirection 29
Input/Output Redirection
Output of programs can be stored in a file using >:
cat file1 file2 > file3
[writes content of files file1 and file2 to file3]
Generates error message if file3 already exists
Use >! to override
cat > foo
[copy keyboard input ended by <ctrl-d> to file foo]
Input can also be redirected:
grep foo < text
[display all lines in file text that contain foo]
Or both: sort < file > file.sorted
Pipes 30
Pipes
Powerful UNIX feature: output of commands can become input for subsequent commands
grep aaa file | wc -l
[count the number of lines in file that contain aaa]
sort file | uniq | wc -l
[count the number of unique lines in file]
Edit Textfiles 31
Edit Textfiles
Several good editors exist: emacs, vim, ...
We recommend emacs, its powerful!
Type emacs x <ret> to edit file x in a separate
window.
To edit within the terminal window, launch emacs
with emacs -nw x <ret>
Large number of commands bound to keys. E.g.
<ctrl-x> <ctrl-s> : save buffer
<ctrl-x> <ctrl-f> : load file
<ctrl-x> <ctrl-c> : exit
<ctrl-s> : search
<alt-%> : search and replace
<ctrl-x> 2 : split window; <ctrl-x> o : switch buffer
<alt-x> command : launch external commands such as gdb
More Details
Lab 0:
UNIX commands
emacs and bash customization
editing and compiling C++ programs
Edit Textfiles 32
First C Program 33
First C Program
Create file hello.c using emacs and save it
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}