SlideShare a Scribd company logo
The operating system Linux
and programming languages
An introduction
Joachim Puls and Michael Wegner
Contents:
1 General remarks on the operating system UNIX/Linux
2 First steps at the computer
3 File systems
4 Editing and printing text files
More important commands
5 UNIX-shells
6 Process administration
7 The programming language C++ - an overview
8 Fortran 90/95 - an overview
9 Creating executable programs
Additional material
• vi basics: vi brief.pdf
• reference for vi: vi reference.pdf
• reference for emacs: emacs reference.pdf
Acknowledgements. Many thanks to Tadziu Hoff-
mann for carefully reading the manuscript and useful
comments.
c 2010, Joachim Puls & Michael Wegner (1st ed.)
1 General remarks on the operat-
ing system UNIX/Linux
Classification of UNIX/Linux
UNIX is a Multi-User/Multi-Tasking operating system
and exists in many different versions (“derivates”):
Solaris, AIX, XENIX, HP-UX, SINIX, Linux.
application programs
Operating system
Hardware
User
Operating system (OS): Sum of all programs which
are required to operate a computer and which control
and monitor the application programs.
1
Essential features
UNIX
• has been originally written in the programming
language C, and is therefore a classical platform
for C-programs. UNIX contains well suited en-
vironments for program development (C, C++,
Java, Fortran, ...).
• is mainly used for scientific-technical applications
on mainframes and workstations, but has be-
come, because of Linux, also popular for classical
PC-applications throughout the last years.
• is perfectly suited for application in networks.
Larger systems and networks require an admin-
istrator.
• offers various alternatives for the solution of most
tasks. The multitude of commands (more than
in any other OS) are brief and flexible.
• is originally command-line oriented, but can be
used via a graphical user interface (X Window
system).
Linux is available (also via internet) in different dis-
tributions (S.u.S.E., Fedora, Debian etc.). Mean-
while there is a variety of direct-start (live) systems,
which can be started, without installation, directly
from CD or other bootable storage devices (Knop-
pix, Ubuntu, . . . ). There are also interesting “mini
versions” (<100 MB) designed for a start from USB-
sticks (e.g., Puppy). The source code of Linux is free.
2
Literature
• Peek, J., et al.: Unix Power Tools.
O’Reilly Media 2002 (3rd edition).
• Gilly, D., et al.: UNIX in a Nutshell.
O’Reilly, Köln. 1998 (1st edition).
• Wielsch,M.: Das große Buch zu UNIX.
Data Becker, Düsseldorf. 1994 (1st edition).
• and numerous other text books
• online-tutorial
http://www.ee.surrey.ac.uk/Teaching/Unix
3
2 First steps at the computer
User, logon, logoff
Since UNIX is a multi-user operating system, it can
deal with several users simultaneously. Each user
needs a user account.
Each user has a personal environment (home di-
rectory, shell), which can be accessed only by her-
/himself (and by the system adminstrator and those
people who know the password – legitimate or by
hacking).
Inside the system the user is identified by his user ID
(UID) and his group identity (group ID, GID).
There are two user types:
• ‘normal’ users with restricted rights and the
• system administrator (root) with all privileges.
The latter is responsible for the installation, con-
figuration and maintenance of the system as well
as the user administration.
Each user has to logon and to logoff from the system
(login/logout). Each user account is protected by a
password.
Exercise:
Login to the system with your user account!
4
Graphical user interface
Originally, UNIX is command-line oriented. The X
Window system enables convenient interaction via a
window-oriented graphical interface, similar to other
OS.
The window manager is responsible for the manage-
ment and display of the individual windows. Each
window manager (and there are a variety of such man-
agers) can be distinguished by its own Look and Feel
(appearance of window decorations and control de-
vices etc.). Most window managers can be choosen
at the login-menu.
Examples for simple window managers:
• twm: very simple and resource-saving
• mwm: Motif window manager, more common and
more advanced
• xfce: convenient, simple, and resource-saving
(recommended for use in virtual machines)
Moreover, almost all Linux distributions provide
graphical desktop environments such as KDE or
GNOME, which have a functionality far beyond simple
window managers.
5
command xterm
Syntax:
xterm [options]
Though there is a graphical interface, UNIX needs
the possibility for direct command input for practical
use. Therefore, at least one terminal window needs
to be open. This can be accomplished via the window
manager or the desktop environment (‘console’)
More windows can then be opened with the command
xterm.
Generally, all UNIX commands have a variety of op-
tions, which usually begin with -. For the commands
which will be introduced in the following, we will pro-
vide only the most important ones.
Example:
wegner@arber:~ > xterm -geo 80x40 -fn 10x20
The command xterm is called with two options -geo,
-fn, which, in this case, need additional arguments
(width and height of window, font name & size).
Exercise:
1. Open a terminal window (“terminal program”) via KDE.
2. From there, start another xterm!
6
command man
Syntax:
man command
man -k expression
displays the manual pages (“man pages”) for the pro-
vided command. man -k searches for man pages contain-
ing the expression in the NAME section. A man page
usually consists of the following sections
• NAME command and purpose
• SYNOPSIS syntax of command
• DESCRIPTION of command effect
• FILES which are modified and/or needed
• OPTIONS if present
• EXAMPLE(S) for application (rarely)
• BUGS errors, if known
• SEE ALSO other commands in the same context
Exercise:
Display information about the command xterm!
7
command passwd
Syntax:
passwd
sets a new password.
Passwords should be constructed from a combination
of letters, digits and special characters, and should
not appear in any dictionary or similar list. Otherwise,
the password can be hacked by systematic search al-
gorithms.
The command to set/change the password and
the required conventions (length, number of dig-
its/letters/special characters) can vary from system
to system. The following example is a common one,
e.g., valid for the workstations at the CIP Pool (but
not for the workstations of the USM).
Example:
wegner@arber:~ > passwd
Changing password for wegner
Old password: myoldpasswd
Enter the new password
(minimum of 5, maximum of 8 characters)
Please use a combination
of upper and lower case letters and numbers.
New password: mynewpasswd
Re-enter new password: mynewpasswd
Password changed.
8
Example:
----> Ihr neues Passwort ist in 5 Minuten
im gesamten Pool aktiv! <-----
Connection to 141.84.136.1 closed.
wegner@arber:~ >
9
command who, whoami
Syntax:
who
whoami
who displays information about all users which are
logged into the system
• user name,
• terminal where the corresponding user is working,
• time of login.
whoami is self-explanatory.
Example:
wegner@arber:~ > whoami
arber!wegner pts/5 Oct 20 12:45
10
Working at external terminals
To login to a distant host, one has to provide the
corresponding IP address, either numerical or as the
complete host name name.domain. In local networks
(CIP-Pool), the brief host name (without domain) is
sufficient. To establish the connection and to encrypt
the transmitted data, one should use exclusively the
so-called “secure” commands. Avoid ftp and use sftp
instead. With ftp, even the password is not encoded!
command ssh
Syntax:
ssh (-4) -X -l username hostname
ssh (-4) -X username@hostname
Enables logging in to an arbitrary host which can be
located via an IP address (if one knows the user ac-
count and the password). Logoff with exit, Ctrl-D or
logout.
In case, the option -4 (without brackets) forces an
IPv4 connection (if IPv6 is not working)
Example:
wegner@arber:~ > ssh -X -l wegner lxsrv1.lrz-muenchen.de
Password: mypasswd
Last login: Sun Oct 22 ...
*********************
Mitteilungen
*********************
wegner@lxsrv1:~ > logout
Connection to lxsrv1.lrz-muenchen.de closed.
11
OR (if connection within “own” cluster)
Example:
wegner@arber:~ > ssh -X wegner@arber
Last login: Sun Oct 22 ...
etc. (keine Passwort-Abfrage)
An additional advantage of the secure shell is that
the distant host hostname can display X applications
on the local terminal, without requiring the command
xhost (as in earlier times). For certain hosts, the com-
mand ssh requires the option -X to enable this feature.
command scp
To copy files from one host to another, the command
scp (“secure copy”) is used, see also cp.
Syntax:
scp (-4) file1 username@hostname:file2
scp (-4) username@hostname:file1 file2
The first command copies the local file file1 to the
external host under name file2, the second command
vice versa. Note the colon! scp -r enables to copy
complete directories recursively, compare cp -r.
12
3 File systems
Logics, file types
“In UNIX everything is a file.”
The following file-system objects can be found
• ‘normal’ (text-) files
• executable files (binary files or shell scripts)
• directories
• device files
• pipes
• symbolic or hard links (references to files)
All files and file system objects are ordered within a
hierarchical file tree with exactly one root directory
‘/’.
In contrast to the MS-Windows file system, the
UNIX file system does not distinguish between dif-
ferent drives. All physical devices (hard disks, DVD,
CDROM, USB, floppy) are denoted by specific files
inside a certain directory within the file tree (usually
within /dev).
File names consist of a sequence of letters, digits
and certain special characters, and must not contain
slashes (for convenience, they should neither contain
empty spaces).
13
Avoid characters which might be interpreted by the
shell in a special way.
A file can be referenced within the file tree by either
an absolute or a relative path name. An absolute path
name consists of all directories leading to the file and
the file name, and always begins with a / (the root
directory).
In many shells and application programs, the tilde de-
notes the home directory.
command pwd
Syntax:
pwd
displays the current directory.
Example:
wegner@arber:~ > pwd
/home/wegner
wegner@arber:~ >
Exercise:
Display the current directory!
14
command cd
Syntax:
cd [directory]
Changes into the given directory, or into the home
directory when no parameter is provided.
As in MS-DOS/Windows, “..” denotes the parent
and “.” the current directory.
Example:
wegner@arber:~ > cd /home/puls
wegner@arber:/home/puls > pwd
/home/puls
wegner@arber:/home/puls > cd ..
wegner@arber:/home > pwd
/home
wegner@arber:/home > cd
wegner@arber:~ > pwd
/home/wegner
wegner@arber:~ >
Exercise:
Change to the directory /usr/share/templates and back to your
home directory! (→ file name completion with TAB)
Check for successful change with pwd!
15
Search pattern for file names
In principle, the shell is a specific program which deals
with the interpretation of input commands. If these
commands have parameters which are file names, sev-
eral files can be addressed simultaneously by means of
a search pattern, which is expanded by the shell. In
any case, the file name expansion is performed prior
to the execution of the command.
expression meaning
* ‘almost’ arbitrary (incl. empty) string
of characters
? a single ‘almost’ arbitrary character
[...] a range of characters
[!...] a negated range of characters
‘almost’ arbitrary: leading dot (e.g., hidden files, ../
etc.) excluded
command ls
Syntax:
ls [-alR] [file/directory]
displays the names (and, optionally, the properties)
of files or lists the content of a directory. File and
directory names can be be absolute or relative.
16
Important options
-a list also files/directories which begin with a dot
(hidden)
-l long listing format. Displays permissions, user
and group, time stamp, size, etc.
-R for directories, all sub-directories will be displayed
recursively.
Example:
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/
wegner@arber:~ > ls -a
./ .bash_history .netscape/ hello.cpp
../ .bashrc* .ssh/ hello.f90
.Xauthority .history hello* nsmail/
wegner@arber:~ > ls /var/X11R6
app-defaults/ bin/ lib@ sax/
scores/ xfine/ xkb/
wegner@arber:~ > ls .b*
.bash_history .bashrc*
wegner@arber:~ > ls [a-h]*
hello* hello.cpp hello.f90
wegner@arber:~ > ls *.?[9p]?
hello.cpp hello.f90
wegner@arber:~ >
Exercise:
List the complete content of your home directory!
What is displayed with ls .* ?
17
Copy, move and delete files/directories
In addition to ls there are other commands for working
with files which can be used together with file name
patterns.
command mkdir, rmdir
Syntax:
mkdir directory
rmdir directory
mkdir creates an empty directory, rmdir deletes an
empty directory.
Example:
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/
wegner@arber:~ > mkdir numerik
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/ numerik/
wegner@arber:~ > rmdir numerik
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/
wegner@arber:~ >
Exercise:
Create a directory yourname exercise within your home directory,
where yourname is your actual name!
18
command cp
Syntax:
cp file1 file2
cp file1 [file2 ...] directory
cp -r dir1 dir2
cp -r dir1 [dir2 ...] directory
copies files or directories. The original file/directory
remains unmodified.
option:
-r directories are copied recursively with all sub-
directories.
Several possibilities:
cp file1 file2
file1 is copied to file2. Attention: if file2 already
exists, it is overwritten (mostly without warning), and
the original file2 is lost!!!
cp file1 [file2 file3] dir
If dir exists, file1 [, file2, file3] are copied into
dir. If dir does not exist, you get an error warning
(for more than two arguments), or, for two arguments,
dir is interpreted as a file name and file1 is copied
to a file named dir.
19
cp -r dir1 dir2
If dir2 already exists, dir1 is recursively copied into
dir2. If dir2 does not exist, a recursive copy of dir1
is created and named dir2.
cp -r dir1 dir2 dir3 dir4
If dir4 already exists, dir1, dir2, dir3 are copied into
dir4. If dir4 does not exist, you get an error warning,
as well as for other combinations of files and directo-
ries within the command.
Example:
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/ numerik/
wegner@arber:~ > cp hello.cpp hello2.cpp
wegner@arber:~ > ls
hello* hello.f90 nsmail/
hello.cpp hello2.cpp numerik/
wegner@arber:~ > cp hello.cpp numerik
wegner@arber:~ > ls numerik
hello.cpp
wegner@arber:~ >
Exercise:
a) Check whether the directory ubung0 is present in your home
directory. If not, copy, via scp, the directory ubung0 from
account/host numprakt@ltsp08.usm.uni-muenchen.de to your
home directory.
b) Copy the files from ubung0 into your directory
yourname exercise!
20
command mv
Syntax:
mv file1 file2
mv file1 [file2 ...] directory
mv dir1 dir2
mv dir1 [dir2 ...] directory
Rename or move files or directories. Similar to cp, but
original is ‘destroyed’. First command from above re-
names files, other commands move files/directories.
(Actually, only the pointer in the ‘inode table’ is
changed, but there is no physical move – except if
you move the file to another file system).
Note: no option [-r] required
Several possibilities, analogue to cp.
21
Example:
wegner@arber:~ > ls
hello* hello.f90 nsmail/
hello.cpp hello2.cpp numerik/
wegner@arber:~ > mv hello2.cpp hello3.cpp
wegner@arber:~ > ls
hello* hello.f90 nsmail/
hello.cpp hello3.cpp numerik/
wegner@arber:~ > ls numerik
hello.cpp
wegner@arber:~ > mv hello3.cpp numerik
wegner@arber:~ > ls
hello* hello.cpp hello.f90 nsmail/ numerik/
wegner@arber:~ > ls numerik
hello.cpp hello3.cpp
wegner@arber:~ >
Exercise:
1. Rename your directory yourname exercise to yourname
exercise0! This will be your working directory for the fol-
lowing exercises.
2. Move the file .plan from yourname exercise0 to your home
directory! Try to move an arbitrary file from your home
directory to the root directory. What happens?
22
command rm
Syntax:
rm [-irf] file(s)/directory(ies)
Delete files and/or directories. After deleting, the
deleted files cannot be recovered! Use rm only with
greatest caution. E.g., the command rm -r * deletes
recursively (in most cases without further inquiry) the
complete file tree below the current directory (leaving
the hidden files/directories beginning with . though).
Options:
-i delete only after confirmation
-r directories will be recursively deleted (with all sub-
directories)
-f force: suppress all safety inquiries.
Note: Varying from system to system, rm without the
option -f might need a confirmation or not (the latter
is the standard).
Example:
wegner@arber:~/numerik > ls
hello.cpp hello3.cpp
wegner@arber:~/numerik > rm -i hello3.cpp
rm: remove ‘hello3.cpp’? y
wegner@arber:~/numerik > ls
hello.cpp
wegner@arber:~/numerik >
23
File permissions/Access rights
The UNIX file system distinguishes between three dif-
ferent access rights or file mode bits. (Note: actually,
there are more access rights, but these are of interest
only for administrators.)
r read: permits the reading of file contents, or, for
directories, the listing of their content.
w write: permits the modification of files (incl.
delete). To create or delete files, the parent di-
rectory(ies) need write access as well!
x execute: permits the execution of binary files
(commands, programs) and of shell scripts from
the command line. For directories, the x bit is re-
quired to change into this directory and to access
the files/directories inside.
Access rights are individually defined for
u the owner of the object
g the group to which the object belongs
o all other users
a all users (i.e., u + g + o)
The access rights of a file can be changed by means
of the command chmod.
24
command chmod
Syntax:
chmod [ugoa][+-=][rwx] file(s)/directory(ies)
Change the access rights of files or directories. These
rights are displayed by ls -l according to the pattern
uuugggooo
rwxrwxrwx
Example:
wegner@arber:~/numerik > ls -l
total 4
-rw-r--r-- 1 wegner stud 100 Oct 20 15:02 hello.cpp
wegner@arber:~/numerik > chmod go+w hello.cpp
wegner@arber:~/numerik > ls -l
total 4
-rw-rw-rw- 1 wegner stud 100 Oct 20 15:02 hello.cpp
wegner@arber:~/numerik >
Exercise:
1. Remove the execution right for the directory
yourname exercise0! Try to change to the directory.
2. Remove all rights for the file linux.txt! How can this be
undone?
25
4 Editing and printing text files
To modify (= edit) the content of a text file, an editor
is needed. Within UNIX there is a variety of editors,
which can be distinguished mostly with respect to ease
of use and memory requirements.
The editor vi and vim
vi is the only editor which is present on all UNIX sys-
tems. The editor vi
• can be completely keyboard controlled
• is extremely flexible
• rather difficult to learn
vim is a derivate from vi, and can be controlled also
by the mouse.
Those of you who enjoy a challenge should learn using
this editor.
A somewhat simpler and more convenient alternative,
which is also implemented in (almost) all UNIX sys-
tems, is
The editor emacs
The editor emacs works in an own window, and can be
controlled (in addition to keys) by menus and mouse.
emacs has rather large memory requirements (no prob-
lem for today’s computers), since this ‘editor’ can do
much more than only editing.
26
Exercise:
1. Edit the program hello.f90!
Start emacs with emacs hello.f90 & from the command line.
The ampersand, &, ensures that emacs runs in the back-
ground, so that you can continue your work from the com-
mand line, independent from the emacs window (see Section
‘Process administration’).
Try to change the comments in those lines starting with !
2. Split the screen with Ctrl X 2. Return to one screen with
Ctrl X 1
3. Save the file with Ctrl X Ctrl S!
4. Quit emacs with Ctrl X Ctrl C!
Note: Whenever you save a file in emacs, a backup
of the previous version is automatically created under
name file˜.
Examples for additional possibilities
• Advanced use of man pages (e.g., searching for
certain strings):
In emacs , type Esc X man CR xterm to open the
xterm man pages. To search for ‘terminal’, type
Ctrl S terminal, and then Ctrl S for the next in-
stance.
• Spell checking within emacs via the the command
Esc x ispell. Try it!
Try to learn the most important key controlled com-
mands. After a while, you can edit your files much
faster than by using mouse and menus. A quick ref-
erence is provided in the appendix.
27
command cat
Syntax:
cat file
displays the content of a file on the standard output
channel (usually the screen).
As many other UNIX commands, cat is a filter, which
can read not only from files, but also from the stan-
dard input channel (usually the keyboard via the com-
mand line). Thus, cat can be used to directly create
smaller text files. In this case, the output has to be
re-directed into a file via >. cat then expects some
input from the command line, which must be finished
with Ctrl D.
Example:
wegner@arber:~ > cat > test
This is a test.
^D
wegner@arber:~ > cat test
This is a test.
wegner@arber:~ > more test
This is a test.
wegner@arber:~ >
Exercise:
1. View the file .plan!
2. View the file linux.txt! Is cat a suitable tool?
28
command more
Syntax:
more file
more permits to view also larger files page by page.
Important commands within more are b to scroll back
and q to quit.
Example:
wegner@arber:~ > more hello.f90
Exercise:
View the file linux.txt with more! Which effect do the keys CR
and SPACE have?
29
command lpr, lpq, lprm
Syntax:
lpr -Pprintername file
lpq -Pprintername
lprm job_id
lpr prints a file on the printer named printername.
To find out the printername, ask a colleague or your
administrator.
lpq lists all print jobs on the printer printername and
provides the corresponding job ids.
lprm deletes the print job with id job id from the print-
ing queue.
Example:
wegner@arber:~ > a2ps hello.f90 -o hello.ps
[hello.f90 (Fortran): 1 page on 1 sheet]
[Total: 1 page on 1 sheet] saved into the file ‘hello.ps’
wegner@arber:~ > ls
hello* hello.f90 nsmail/ test
hello.cpp hello.ps numerik/
wegner@arber:~ > lpr -Plp0 hello.ps
wegner@arber:~ >
Exercise:
Print the file linux.txt!
30
More important commands
a2ps converts ASCII text to PostScript. Often re-
quired to print text under Linux.
a2ps [options] textfile
-1, -2, ..., -9 predefined font size and page layout.
E.g., with -2 two pages of text
are displayed side-by-side on one
output page.
-o output file (*.ps)
-P NAME send output to printer NAME
diff file1 file2 compares two files. If they are iden-
tical, no output.
touch file sets the current time stamp for a file. Can
be used to create an empty file.
finger account displays additional information for the
user of a certain account (name of user, project,
etc.)
gv datei.ps displays PostScript files and files of re-
lated formats (e.g., *.eps, *.pdf).
acroread file.pdf displays pdf files and allows for simple
manipulations (e.g., copy text or figures to the
clipboard).
gimp file starts the image manipulation program gimp
(similar to photoshop). Allows to view, manipulate
and print image files (e.g., *.jpg, *.tif, *.png).
31
ps2pdf file.ps converts ps-files to pdf-files. The file
file.pdf will be automatically created.
gzip file. Compresses file via Lempel-Ziv algorithm.
The file file.gz is created and the file file deleted.
Typical compression factor ∼3.
gunzip file.gz. Corresponding decompression.
tar “tape archive”. Nowadays mainly used to create
one single file from a file tree, which then, e.g.,
can be sent by email. Reverse process also with
tar.
tar -cvf direc.tar direc
creates (c) file (f) direc.tar from
directory direc. Verbose progress
is displayed (v).
tar -xvf direc.tar
re-creates original file tree under
original name (./direc).
tar -zcvf direc.tgz direc
tar -zxvf direc.tgz
additional compression/dekompression
via gzip.
Note: This command is extremely ‘powerful’. Ei-
ther read the man pages, or use the command as
given.
32
locate search expression. Lists all files and directories
in the local database, which correspond to the
search expression. Extremely well suited to search
for files (if the database is frequently updated →
system administrator)
find searches recursively for files corresponding to
search expression within the given path.
Example: find . -name ‘*.txt’ searches recur-
sively for all *.txt files, starting within the current
directory.
grep searches for text within given files.
Example: grep ‘test’ ../*.f90 searches for the
text test in all *.f90 files in the parent directory.
The most important option is [-i], which forces
grep to ignore any distinction between upper and
lower case.
33
5 UNIX shells
The shell is a service program through which the user
communicates with the OS and which is responsible
for the interpretation of the input commands.
Different UNIX shells
Since the shell does not directly belong to the OS, a
number of different shells have been developed in the
course of time:
• Bourne shell (sh). A well-known and widespread
shell, named after its inventor Steven Bourne. An
advanced derivate, the bash, Bourne again shell
(note the pun) is most popular under Linux.
• C-Shell (csh). Developed in Berkeley, and uses a
more C-like syntax. An improved version of the
C-shell is the tcsh.
• Bash shell (bash). Advanced Bourne shell and
standard on many systems.
Each shell contains a set of system variables, which
can be augmented by user-defined variables. This set
comprises the process environment for the programs
running inside the shell.
Moreover, the shell can be used to run (system-) pro-
grams via shell scripts.
34
Shell scripts
Shell scripts are small programs consisting of UNIX
commands and shell-specific program constructs
(branches, loops etc), which behave like UNIX com-
mands but are present in text form (instead of binary).
These scripts are interpreted by the shell.
The syntax of shell scripts differs (considerably) from
shell to shell.
Some shell scripts are automatically called under cer-
tain conditions:
• .profile and/or .login are executed, if present,
at login (i.e., for the login shell), and only once.
• .bashrc and .cshrc /.tcshrc are called whenever
a new bash or csh/tcsh is opened, respectively.
Exercise:
1. Copy the file .tcshrc to your home directory and inspect
the file!
2. Open a (new) tcsh by typing tcsh on the command line!
What happens? Exit the tcsh with exit!
35
Re-directing input and output
All UNIX commands use input and output channels to
read data and to output data. Usually, these are the
keyboard and the screen assigned to the specific user,
respectively.
These standard channels can be redirected within the
shell such that a command can either read directly
from a file (instead from the keyboard) and/or write
into a file (instead of the screen). For re-direction,
use the characters ‘>’ (for output) and ‘<’ (for input)
With ‘>>’, the output will be appended to an existing
file. If the file does not exist, this command behaves
as ‘>’.
Example:
wegner@arber:~ > ls
hello.cpp linux.txt numerik/
hello.f90 nsmail/
wegner@arber:~ > cat linux.txt > linux2.txt
wegner@arber:~ > ls
hello.cpp linux.txt nsmail/
hello.f90 linux2.txt numerik/
36
Pipes
Furthermore, many UNIX commands act as so-called
filters: They read from the standard input and write to
the standard output. Thus, they can be combined via
so-called pipes such that the output of one command
acts as the input of another:
pipe
command
1
filter
command
2
pipe
Pipes are constructed on the command line by using
the ‘|’ character between commands.
A re-direction to a file with ‘>’ or ‘>>’ can be present
only at the end of such a chain.
Example:
wegner@arber:~ > man g++ | a2ps -P printer
[Total: 151 pages on 76 sheets]
wegner@arber:~ >
With this pipe, the man pages for g++ are formatted
and printed via one command.
37
6 Process administration
A process is a running program or script and consists
of
• the program/script itself and
• the corresponding environment, which consists of
all required additional information necessary to
ensure a correct program flow.
Characteristics of a process are (among others)
• a unique process ID (PID),
• PID of the parent process (PPID),
• User and group number of the owner and
• priority of the process.
Normally, when a process has been started from a
shell, the shell cannot be used for other input until the
end of the process. But processes and programs can
also be run in the background. To enable this feature,
the command line which calls the process/program
must end with an ampersand, ’&’.
Example:
wegner@arber:~ > firefox &
[1] 21749
wegner@arber:~ >
Exercise:
Start the program xeyes in the background!
38
command ps
Syntax:
ps [-al] [-u user]
Display running processes with their characteristics.
Without options, only the user’s own processes run-
ning in the current shell are displayed.
Important options:
-a display all processes assigned to any terminal (tty)
-l long format display. Additional information about
owner, parent process etc.
-u display all processes which are owned by a specific
user.
Example:
wegner@arber:~ > ps
PID TTY TIME CMD
21733 pts/4 00:00:00 bash
22197 pts/4 00:00:00 xterm
22198 pts/5 00:00:00 bash
22212 pts/4 00:00:00 ps
wegner@arber:~ >
Exercise:
View all current processes within your shell!
39
command kill
Syntax:
kill [-9] PID
Terminates the process with number PID. Can be ex-
ecuted only by the owner of the process or by root.
Important option:
-9 for ‘obstinate’ processes which cannot be termi-
nated by a normal kill.
Example:
wegner@arber:~ > ps
PID TTY TIME CMD
21733 pts/4 00:00:00 bash
22197 pts/4 00:00:00 xterm
22198 pts/5 00:00:00 bash
22212 pts/4 00:00:00 ps
wegner@arber:~ > kill 22197
wegner@arber:~ > ps
PID TTY TIME CMD
21733 pts/4 00:00:00 bash
22214 pts/4 00:00:00 ps
[1]+ Exit 15 xterm
wegner@arber:~ >
Exercise:
Terminate xeyes via kill!
40
7 The programming language
C++: An overview
Programming languages allow to formulate certain
problems or algorithms by means of particular syntac-
tic rules. Such a program can be ‘translated’ by ded-
icated programs (interpreter, compiler) into machine-
readable code and then executed by the computer.
Historically, various programming languages of differ-
ent complexity have been established, where this com-
plexity was and is determined by the progress in com-
puter science/hardware and the intended application.
The approach for solving a certain problem can be
discriminated by different programming paradigms.
Procedural and object-oriented
programming
Procedural programming
The emphasis is on the used algorithm. A program
consists of a hierarchic dissection of the problem into
functional units.
Data and functions are separated, and data are pub-
licly accessible.
Programming languages: C, Fortran77, Pascal, ...
41
Object-oriented programming
useful introduction:
https://www.youtube.com/watch?v=lbXsrHGhBAU
User-defined types (classes) and associated operations
are introduced.
Thus, data and associated functions (methods) build
a unit within a class. Data are (usually) accessible
only via the associated methods.
Inheritance of common properties allows to distin-
guish between general and special attributes of the
types/classes.
Example: Genealogical tree of the various classes of
animals and plants, families, orders, species etc.
Within the emerging hierarchy, similar functions, shar-
ing the same interface but defining a specific be-
haviour for each particular type/class, can be imple-
mented (polymorphism).
By this approach, an (almost) exact model of reality
shall be created which reflects all relevant dependen-
cies within the program.
Advantages of object-oriented programming:
• better abstraction possibilities because of holistic
approach.
• improved structure and modularity, allowing for
easy maintenance of programs.
• reusability.
Programming languages: C++, Java, Python,. . .
42
Essential features of C and C++
• At present, C++ is one of the most used pro-
gramming languages.
• C++ originates from C and is a superset of C.
• C++ allows both for efficient, hardware-oriented
programming (as already C), but also for pro-
gramming on a high, object-oriented abstraction
level. Thus, C++ is sometimes called a hybrid
language.
• The OS UNIX has been completely written in C.
• C++ and C are standardized (by international
ANSI standard).
• C und C++ do not comprise special functions
for input and output, graphics and hardware pro-
gramming etc., but there are corresponding li-
braries.
• The standard library includes a comprehensive set
of functions and classes required for typical appli-
cations, and is installed together with each com-
piler.
• According to afficionados, C++ - programming
is fun!
43
Literature
• Stroustrup, B.: The C++ Programming Lan-
guage: Special Edition,
Addison-Wesley Longman, Amsterdam, 2000.
• Kernighan, B., Ritchie, D.: The C Program-
ming Language,
Prentice Hall, 1988 (2nd ed.).
• Meyers, S.: Effective C++: 55 Specific Ways
to Improve Your Programs and Designs,
Addison-Wesley, München, 2005 (3rd ed.).
• Josuttis, N.: The C++ Standard Library: A
Tutorial and Reference,
Addison-Wesley Longman, Amsterdam, 1999.
• Booch, G., Maksimchuk, R. A., Engle, W.
et al.: Object Oriented Analysis and Design with
Applications,
Addison-Wesley Longman, Amsterdam, 2007
(3rd ed.).
The C++ Standard (INCITS/ISO/IEC 14882-2003)
can be downloaded via http://www.ansi.org (≈ 30$).
44
Online resources
WWW
• C++ tutorial
http://www.cplusplus.com/doc/tutorial
• Bjarne Stroustrup’s homepage (author of the
C++ programming language)
http://www2.research.att.com/~bs
• C++ FAQs
http://www.parashift.com/c++-faq-lite
Newsgroups
• comp.lang.c++.moderated
General C++ discussion forum.
• comp.std.c++
Discussions related to the C++ standard.
45
8 Fortran 90/95: An overview
Brief history
• introduced 1954.
• continued by Fortran II, Fortran IV, Fortran66,
Fortran77 (still in use).
• Fortran 90, since 1991 ISO, since 1992 ANSI
standard.
• Fortran 95, since 1996/97 ISO/ANSI standard.
• meanwhile Fortran 2000 and Fortran 2003 (latest
standard).
• F95 and Fortran 2000 include relatively minor re-
visions of F90.
• Fortran 2003 is a major revision, supporting
(among other features) object-oriented program-
ming (inheritance, polymorphism).
• Fortran 2008 under development.
• most current compilers for F90/95, but Fortran
2003 standard (almost) reached with newest ver-
sion of Intel Fortran compiler, v11.
46
Important features
• Fortran was and is the most used language for
solving physical problems, particularly numerical
simulations.
• The introduction of F90/95 allowed for similar
features as in C/C++, except for the hardware-
orientation. With Fortran 2003, even object-
oriented programming became possible.
• The capabilities of F90 are rather large and there
are numerous standard operations and functions.
E.g., vector- and matrix-operations belong to the
standard:
a = b + c
can mean scalar, vector or matrix addition, de-
pending on the definition of a, b, c. Vector- and
matrix products can be calculated by likewise sim-
ple instructions (fast execution),
a = dot product(b, c); a = matmul(b, c).
• There are comprehensive program libraries, par-
ticularly for Linear Algebra and eigenvalue-
problems (available both as source codes or highly
optimized binary objects), e.g., BLAS, LAPACK,
EISPACK.
• Optimized programs (more-D, parallel) execute
mostly faster than corresponding C++ programs
(factor 2 to 3).
47
• Simple possibility for parallelization via HPF (high
performance Fortran).
• The basic structures are very simple, and the lan-
guage can be learned more easily than C++ (at
least regarding the basic concepts).
• Until the next couple of years, Fortran needs to be
known by any physicist who is not soley interested
in purely experimental/obserservational or purely
theoretical work.
Examples for physical research areas which
use (almost) exclusively Fortran: Aero-/hydro-
dynamics, computational astrophysics, atomic
and nuclear physics, geophysics, meteorology.
• Programming in Fortran is fast!
48
Literature
• Reference manuals
– Gehrke, W.: Fortran90 Referenz-Handbuch,
1991, Hanser, München, ISBN 3446163212.
– ‘Fortran 90’, RRZN (available at the LRZ).
• Text books
– Adams, J.C., et al.: Fortran 2003 Hand-
book: The Complete Syntax, Features and
Procedures,
Springer, Berlin, 2008, ISBN 1846283787.
– Metcalf, M., et al.: Fortran 95/2003 ex-
plained,
Oxford Univ. Press, 2004, ISBN 0198526938
(paperback).
Online resources
• Online-Tutorial at Univ. Liverpool
http://www.liv.ac.uk/HPC/HTMLFrontPageF90.html
• German Fortran Website
http://www.fortran.de
• Metcalf’s Fortran Information
http://www.fortran.com/metcalf
• Michel Olagnon’s Fortran 90 List
http://www.fortran-2000.com/MichelList
49
9 Creating executable programs
Three subsequent phases
During the generation of executable programs from
C++ or Fortran source files, always the same steps
have to be performed (also for other higher program-
ming languages such as Basic or Pascal).
executable
object file object file
pre-processor
compiler
linker
object file program library
source file
*.h
header file
*.cpp / *.f90
*.o *.o *.o *.a
program
The pre-processor replaces symbolic constants and
macros and inserts additional files, if required. Usually
omitted for Fortran programs.
The compiler ‘translates’ the source code to machine
readable code, creating the object files.
The linker binds all present object files (and, option-
ally, object files from libraries) to the executable pro-
gram (executable object).
50
command g++
Syntax:
g++ [-c] [-g] [-O] file.cpp [-o outputfile]
We suggest using the GNU C++ compiler, which is
included in almost any Linux distribution. The linker
is already included, but can be called separately.
Important options:
-o the name of the executable object (program).
Default ‘a.out’.
-c compile only, do not link.
-g include debug information. Necessary for later
debugging. Increases the size and execution time
of the program.
-O optimization (default: intermediate optimization)
Example:
wegner@arber:~ > g++ hello.cpp
wegner@arber:~ > ./a.out
Hello, world!
wegner@arber:~ > g++ hello.cpp -o hello
wegner@arber:~ > ./hello
Hello, world!
51
The character ./ in front of a.out and hello is usually
required to tell the shell that the executable program
is located within the current directory.
Now in two steps: compile at first, then link
Example:
wegner@arber:~ > g++ -c hello.cpp
wegner@arber:~ > g++ hello.o -o hello
If more than one source file is present (e.g., if each
sub-program is contained in a separate source file),
one can avoid unnecessary compilations and a lot
of command-line typing by using so-called makefiles,
which allow updating executable programs using just
one command. For further info, google for makefile
and/or contact your supervisor.
52
command ifort, gfortran
Syntax:
ifort [-c -g -O] file.f90 [-o outputfile]
gfortran [-c -g -O] file.f90 [-o outputfile]
Within the GNU open source project, a GNU Fortran
95 compiler is available under the command gfortran
(if implemented and installed within your Linux distri-
bution/system). Since this compiler (in its final stage)
is quite new and has not been tested thoroughly at our
institute, we recommend to use the Intel Fortran 95
compiler ifort when possible (installed, e.g., at the
workstations of the USM. Sometimes, you need to in-
voke the command module load fortran to make the
compiler available).
Note that both compilers comply with the F95 stan-
dard (ifort v11 and later additionally supports most
Fortran 2000/2003 features), and that gfortran does
not support 16-byte reals, in contrast to ifort.
When you have no possibility to use ifort (e.g., when
working at home without connection to the the USM),
use gfortran, which has identical or similar options as
ifort. gfortran is also available as an MS-Windows
binary.
If you want to debug your code with the GNU debug-
ger ddd or gdb (next topic), we suggest to use gfortran.
(ifort v12 and later works in most cases as well).
In both compilers, the linker is included again, but can
be called separately.
53
Important options for ifort and gfortran:
-o the name of the executable object (program).
Default ‘a.out’.
-c compile only, do not link
-g include debug information (see g++)
-O optimization (default: intermediate optimization)
Example:
wegner@arber:~ > ifort hello.f90 -o hello
wegner@arber:~ > ./hello
Hello, world!
wegner@arber:~ >
Exercise:
1. Compile the Fortran version of HelloWorld with ifort und
start the program!
2. Compile the program with gfortran using the option -c und
link in a second step via gfortran hello.o!
54
Debugging
After you have successfully compiled and linked your
program, it can be started from the shell by giving its
name as command (a.out or ownname when compiled
with -o ownname). This does not mean, however, that
1. your programm runs successfully to its end,
2. it complies with your intentions.
If some run-time error occurs, you have to debug the
program. You can do this either manually (by printing
out certain intermediate results and test statements),
or you use a so-called debugger. Such a tool allows,
e.g.,
• to execute the statements stepwise
• to view the current content of the variables
• to stop the program at so-called breakpoints de-
fined by yourself.
An open source and window-oriented debugger avail-
able under Linux is ddd (Data Display Debugger),
which works for C++ und Fortran programs (at least
those compiled with g++ and gfortran, respectively,
and with option -g).
The debugger is simply called via
ddd program_name
where program name is the name of the executable ob-
ject (e.g., a.out).
55
The core of ddd (which actually provides the window
interface ‘only’) is the debugger gdb, which can be
alternatively used alone to debug the program (from
the console). It is called via
gdb program_name
but needs some knowledge of the involved commands.
Remember: To debug a program, it has to be com-
piled with the option -g in order to include the required
debug information.
Example:
wegner@arber:~ > gfortran -g hello.f90 -o fhello
wegner@arber:~ > ddd fhello
GNU DDD 3.3.11 (x86_64-suse-linux-gnu),
by Dorothea Luetkehaus and Andreas Zeller.
...
(gdb)
Exercise:
1. Compile the program pi.f90 and run it. Re-compile with
the option -g!
2. Start ddd for the executable object. Become acquainted
with the most important entries (Run, Step, Next, ...) in
the control panel und buttons (Break, Print) in the menu
bar.
56

More Related Content

Similar to Linux For Beginners! Ultimate guide to Linux Users!! (20)

PPT
IntroCommandLine.ppt
GowthamRaju15
 
PPT
IntroCommandLine.ppt
TECHWORLDwithphotoed
 
PPT
redhat_by_Cbitss.ppt
VikrantSChohaan
 
PPTX
Unix
Erm78
 
PPTX
Linuxtraining 130710022121-phpapp01
Chander Pandey
 
PDF
Linux Basics
Team-VLSI-ITMU
 
PPTX
Unix Operating System
MahakKasliwal
 
PPT
LINUX
ARJUN
 
PPTX
Linux Systems Programming: Ubuntu Installation and Configuration
RashidFaridChishti
 
PPT
Unix/Linux Basic Commands and Shell Script
sbmguys
 
PPTX
Unix final
MahakKasliwal
 
PPT
Karkha unix shell scritping
chockit88
 
PPTX
Commands and shell programming (3)
christ university
 
PDF
Unix and Linux - The simple introduction
Amity University Noida
 
PPTX
Unix environment [autosaved]
Er Mittinpreet Singh
 
PPTX
A beginners introduction to unix
zafarali1981
 
PPT
Unix
Sudharsan S
 
DOC
Introduction to unix
Chandru Jangin
 
PPT
HISTORY, TYPES OF EMBEDDED LINUX, COMMANDS,
KesavanT10
 
PPTX
Operating System Laboratory presentation .ppt
PDhivyabharathi2
 
IntroCommandLine.ppt
GowthamRaju15
 
IntroCommandLine.ppt
TECHWORLDwithphotoed
 
redhat_by_Cbitss.ppt
VikrantSChohaan
 
Unix
Erm78
 
Linuxtraining 130710022121-phpapp01
Chander Pandey
 
Linux Basics
Team-VLSI-ITMU
 
Unix Operating System
MahakKasliwal
 
LINUX
ARJUN
 
Linux Systems Programming: Ubuntu Installation and Configuration
RashidFaridChishti
 
Unix/Linux Basic Commands and Shell Script
sbmguys
 
Unix final
MahakKasliwal
 
Karkha unix shell scritping
chockit88
 
Commands and shell programming (3)
christ university
 
Unix and Linux - The simple introduction
Amity University Noida
 
Unix environment [autosaved]
Er Mittinpreet Singh
 
A beginners introduction to unix
zafarali1981
 
Introduction to unix
Chandru Jangin
 
HISTORY, TYPES OF EMBEDDED LINUX, COMMANDS,
KesavanT10
 
Operating System Laboratory presentation .ppt
PDhivyabharathi2
 

Recently uploaded (20)

PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Biography of Daniel Podor.pdf
Daniel Podor
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Ad

Linux For Beginners! Ultimate guide to Linux Users!!

  • 1. The operating system Linux and programming languages An introduction Joachim Puls and Michael Wegner Contents: 1 General remarks on the operating system UNIX/Linux 2 First steps at the computer 3 File systems 4 Editing and printing text files More important commands 5 UNIX-shells 6 Process administration 7 The programming language C++ - an overview 8 Fortran 90/95 - an overview 9 Creating executable programs Additional material • vi basics: vi brief.pdf • reference for vi: vi reference.pdf • reference for emacs: emacs reference.pdf Acknowledgements. Many thanks to Tadziu Hoff- mann for carefully reading the manuscript and useful comments. c 2010, Joachim Puls & Michael Wegner (1st ed.)
  • 2. 1 General remarks on the operat- ing system UNIX/Linux Classification of UNIX/Linux UNIX is a Multi-User/Multi-Tasking operating system and exists in many different versions (“derivates”): Solaris, AIX, XENIX, HP-UX, SINIX, Linux. application programs Operating system Hardware User Operating system (OS): Sum of all programs which are required to operate a computer and which control and monitor the application programs. 1 Essential features UNIX • has been originally written in the programming language C, and is therefore a classical platform for C-programs. UNIX contains well suited en- vironments for program development (C, C++, Java, Fortran, ...). • is mainly used for scientific-technical applications on mainframes and workstations, but has be- come, because of Linux, also popular for classical PC-applications throughout the last years. • is perfectly suited for application in networks. Larger systems and networks require an admin- istrator. • offers various alternatives for the solution of most tasks. The multitude of commands (more than in any other OS) are brief and flexible. • is originally command-line oriented, but can be used via a graphical user interface (X Window system). Linux is available (also via internet) in different dis- tributions (S.u.S.E., Fedora, Debian etc.). Mean- while there is a variety of direct-start (live) systems, which can be started, without installation, directly from CD or other bootable storage devices (Knop- pix, Ubuntu, . . . ). There are also interesting “mini versions” (<100 MB) designed for a start from USB- sticks (e.g., Puppy). The source code of Linux is free. 2
  • 3. Literature • Peek, J., et al.: Unix Power Tools. O’Reilly Media 2002 (3rd edition). • Gilly, D., et al.: UNIX in a Nutshell. O’Reilly, Köln. 1998 (1st edition). • Wielsch,M.: Das große Buch zu UNIX. Data Becker, Düsseldorf. 1994 (1st edition). • and numerous other text books • online-tutorial http://www.ee.surrey.ac.uk/Teaching/Unix 3 2 First steps at the computer User, logon, logoff Since UNIX is a multi-user operating system, it can deal with several users simultaneously. Each user needs a user account. Each user has a personal environment (home di- rectory, shell), which can be accessed only by her- /himself (and by the system adminstrator and those people who know the password – legitimate or by hacking). Inside the system the user is identified by his user ID (UID) and his group identity (group ID, GID). There are two user types: • ‘normal’ users with restricted rights and the • system administrator (root) with all privileges. The latter is responsible for the installation, con- figuration and maintenance of the system as well as the user administration. Each user has to logon and to logoff from the system (login/logout). Each user account is protected by a password. Exercise: Login to the system with your user account! 4
  • 4. Graphical user interface Originally, UNIX is command-line oriented. The X Window system enables convenient interaction via a window-oriented graphical interface, similar to other OS. The window manager is responsible for the manage- ment and display of the individual windows. Each window manager (and there are a variety of such man- agers) can be distinguished by its own Look and Feel (appearance of window decorations and control de- vices etc.). Most window managers can be choosen at the login-menu. Examples for simple window managers: • twm: very simple and resource-saving • mwm: Motif window manager, more common and more advanced • xfce: convenient, simple, and resource-saving (recommended for use in virtual machines) Moreover, almost all Linux distributions provide graphical desktop environments such as KDE or GNOME, which have a functionality far beyond simple window managers. 5 command xterm Syntax: xterm [options] Though there is a graphical interface, UNIX needs the possibility for direct command input for practical use. Therefore, at least one terminal window needs to be open. This can be accomplished via the window manager or the desktop environment (‘console’) More windows can then be opened with the command xterm. Generally, all UNIX commands have a variety of op- tions, which usually begin with -. For the commands which will be introduced in the following, we will pro- vide only the most important ones. Example: wegner@arber:~ > xterm -geo 80x40 -fn 10x20 The command xterm is called with two options -geo, -fn, which, in this case, need additional arguments (width and height of window, font name & size). Exercise: 1. Open a terminal window (“terminal program”) via KDE. 2. From there, start another xterm! 6
  • 5. command man Syntax: man command man -k expression displays the manual pages (“man pages”) for the pro- vided command. man -k searches for man pages contain- ing the expression in the NAME section. A man page usually consists of the following sections • NAME command and purpose • SYNOPSIS syntax of command • DESCRIPTION of command effect • FILES which are modified and/or needed • OPTIONS if present • EXAMPLE(S) for application (rarely) • BUGS errors, if known • SEE ALSO other commands in the same context Exercise: Display information about the command xterm! 7 command passwd Syntax: passwd sets a new password. Passwords should be constructed from a combination of letters, digits and special characters, and should not appear in any dictionary or similar list. Otherwise, the password can be hacked by systematic search al- gorithms. The command to set/change the password and the required conventions (length, number of dig- its/letters/special characters) can vary from system to system. The following example is a common one, e.g., valid for the workstations at the CIP Pool (but not for the workstations of the USM). Example: wegner@arber:~ > passwd Changing password for wegner Old password: myoldpasswd Enter the new password (minimum of 5, maximum of 8 characters) Please use a combination of upper and lower case letters and numbers. New password: mynewpasswd Re-enter new password: mynewpasswd Password changed. 8
  • 6. Example: ----> Ihr neues Passwort ist in 5 Minuten im gesamten Pool aktiv! <----- Connection to 141.84.136.1 closed. wegner@arber:~ > 9 command who, whoami Syntax: who whoami who displays information about all users which are logged into the system • user name, • terminal where the corresponding user is working, • time of login. whoami is self-explanatory. Example: wegner@arber:~ > whoami arber!wegner pts/5 Oct 20 12:45 10
  • 7. Working at external terminals To login to a distant host, one has to provide the corresponding IP address, either numerical or as the complete host name name.domain. In local networks (CIP-Pool), the brief host name (without domain) is sufficient. To establish the connection and to encrypt the transmitted data, one should use exclusively the so-called “secure” commands. Avoid ftp and use sftp instead. With ftp, even the password is not encoded! command ssh Syntax: ssh (-4) -X -l username hostname ssh (-4) -X username@hostname Enables logging in to an arbitrary host which can be located via an IP address (if one knows the user ac- count and the password). Logoff with exit, Ctrl-D or logout. In case, the option -4 (without brackets) forces an IPv4 connection (if IPv6 is not working) Example: wegner@arber:~ > ssh -X -l wegner lxsrv1.lrz-muenchen.de Password: mypasswd Last login: Sun Oct 22 ... ********************* Mitteilungen ********************* wegner@lxsrv1:~ > logout Connection to lxsrv1.lrz-muenchen.de closed. 11 OR (if connection within “own” cluster) Example: wegner@arber:~ > ssh -X wegner@arber Last login: Sun Oct 22 ... etc. (keine Passwort-Abfrage) An additional advantage of the secure shell is that the distant host hostname can display X applications on the local terminal, without requiring the command xhost (as in earlier times). For certain hosts, the com- mand ssh requires the option -X to enable this feature. command scp To copy files from one host to another, the command scp (“secure copy”) is used, see also cp. Syntax: scp (-4) file1 username@hostname:file2 scp (-4) username@hostname:file1 file2 The first command copies the local file file1 to the external host under name file2, the second command vice versa. Note the colon! scp -r enables to copy complete directories recursively, compare cp -r. 12
  • 8. 3 File systems Logics, file types “In UNIX everything is a file.” The following file-system objects can be found • ‘normal’ (text-) files • executable files (binary files or shell scripts) • directories • device files • pipes • symbolic or hard links (references to files) All files and file system objects are ordered within a hierarchical file tree with exactly one root directory ‘/’. In contrast to the MS-Windows file system, the UNIX file system does not distinguish between dif- ferent drives. All physical devices (hard disks, DVD, CDROM, USB, floppy) are denoted by specific files inside a certain directory within the file tree (usually within /dev). File names consist of a sequence of letters, digits and certain special characters, and must not contain slashes (for convenience, they should neither contain empty spaces). 13 Avoid characters which might be interpreted by the shell in a special way. A file can be referenced within the file tree by either an absolute or a relative path name. An absolute path name consists of all directories leading to the file and the file name, and always begins with a / (the root directory). In many shells and application programs, the tilde de- notes the home directory. command pwd Syntax: pwd displays the current directory. Example: wegner@arber:~ > pwd /home/wegner wegner@arber:~ > Exercise: Display the current directory! 14
  • 9. command cd Syntax: cd [directory] Changes into the given directory, or into the home directory when no parameter is provided. As in MS-DOS/Windows, “..” denotes the parent and “.” the current directory. Example: wegner@arber:~ > cd /home/puls wegner@arber:/home/puls > pwd /home/puls wegner@arber:/home/puls > cd .. wegner@arber:/home > pwd /home wegner@arber:/home > cd wegner@arber:~ > pwd /home/wegner wegner@arber:~ > Exercise: Change to the directory /usr/share/templates and back to your home directory! (→ file name completion with TAB) Check for successful change with pwd! 15 Search pattern for file names In principle, the shell is a specific program which deals with the interpretation of input commands. If these commands have parameters which are file names, sev- eral files can be addressed simultaneously by means of a search pattern, which is expanded by the shell. In any case, the file name expansion is performed prior to the execution of the command. expression meaning * ‘almost’ arbitrary (incl. empty) string of characters ? a single ‘almost’ arbitrary character [...] a range of characters [!...] a negated range of characters ‘almost’ arbitrary: leading dot (e.g., hidden files, ../ etc.) excluded command ls Syntax: ls [-alR] [file/directory] displays the names (and, optionally, the properties) of files or lists the content of a directory. File and directory names can be be absolute or relative. 16
  • 10. Important options -a list also files/directories which begin with a dot (hidden) -l long listing format. Displays permissions, user and group, time stamp, size, etc. -R for directories, all sub-directories will be displayed recursively. Example: wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ wegner@arber:~ > ls -a ./ .bash_history .netscape/ hello.cpp ../ .bashrc* .ssh/ hello.f90 .Xauthority .history hello* nsmail/ wegner@arber:~ > ls /var/X11R6 app-defaults/ bin/ lib@ sax/ scores/ xfine/ xkb/ wegner@arber:~ > ls .b* .bash_history .bashrc* wegner@arber:~ > ls [a-h]* hello* hello.cpp hello.f90 wegner@arber:~ > ls *.?[9p]? hello.cpp hello.f90 wegner@arber:~ > Exercise: List the complete content of your home directory! What is displayed with ls .* ? 17 Copy, move and delete files/directories In addition to ls there are other commands for working with files which can be used together with file name patterns. command mkdir, rmdir Syntax: mkdir directory rmdir directory mkdir creates an empty directory, rmdir deletes an empty directory. Example: wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ wegner@arber:~ > mkdir numerik wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ numerik/ wegner@arber:~ > rmdir numerik wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ wegner@arber:~ > Exercise: Create a directory yourname exercise within your home directory, where yourname is your actual name! 18
  • 11. command cp Syntax: cp file1 file2 cp file1 [file2 ...] directory cp -r dir1 dir2 cp -r dir1 [dir2 ...] directory copies files or directories. The original file/directory remains unmodified. option: -r directories are copied recursively with all sub- directories. Several possibilities: cp file1 file2 file1 is copied to file2. Attention: if file2 already exists, it is overwritten (mostly without warning), and the original file2 is lost!!! cp file1 [file2 file3] dir If dir exists, file1 [, file2, file3] are copied into dir. If dir does not exist, you get an error warning (for more than two arguments), or, for two arguments, dir is interpreted as a file name and file1 is copied to a file named dir. 19 cp -r dir1 dir2 If dir2 already exists, dir1 is recursively copied into dir2. If dir2 does not exist, a recursive copy of dir1 is created and named dir2. cp -r dir1 dir2 dir3 dir4 If dir4 already exists, dir1, dir2, dir3 are copied into dir4. If dir4 does not exist, you get an error warning, as well as for other combinations of files and directo- ries within the command. Example: wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ numerik/ wegner@arber:~ > cp hello.cpp hello2.cpp wegner@arber:~ > ls hello* hello.f90 nsmail/ hello.cpp hello2.cpp numerik/ wegner@arber:~ > cp hello.cpp numerik wegner@arber:~ > ls numerik hello.cpp wegner@arber:~ > Exercise: a) Check whether the directory ubung0 is present in your home directory. If not, copy, via scp, the directory ubung0 from account/host [email protected] to your home directory. b) Copy the files from ubung0 into your directory yourname exercise! 20
  • 12. command mv Syntax: mv file1 file2 mv file1 [file2 ...] directory mv dir1 dir2 mv dir1 [dir2 ...] directory Rename or move files or directories. Similar to cp, but original is ‘destroyed’. First command from above re- names files, other commands move files/directories. (Actually, only the pointer in the ‘inode table’ is changed, but there is no physical move – except if you move the file to another file system). Note: no option [-r] required Several possibilities, analogue to cp. 21 Example: wegner@arber:~ > ls hello* hello.f90 nsmail/ hello.cpp hello2.cpp numerik/ wegner@arber:~ > mv hello2.cpp hello3.cpp wegner@arber:~ > ls hello* hello.f90 nsmail/ hello.cpp hello3.cpp numerik/ wegner@arber:~ > ls numerik hello.cpp wegner@arber:~ > mv hello3.cpp numerik wegner@arber:~ > ls hello* hello.cpp hello.f90 nsmail/ numerik/ wegner@arber:~ > ls numerik hello.cpp hello3.cpp wegner@arber:~ > Exercise: 1. Rename your directory yourname exercise to yourname exercise0! This will be your working directory for the fol- lowing exercises. 2. Move the file .plan from yourname exercise0 to your home directory! Try to move an arbitrary file from your home directory to the root directory. What happens? 22
  • 13. command rm Syntax: rm [-irf] file(s)/directory(ies) Delete files and/or directories. After deleting, the deleted files cannot be recovered! Use rm only with greatest caution. E.g., the command rm -r * deletes recursively (in most cases without further inquiry) the complete file tree below the current directory (leaving the hidden files/directories beginning with . though). Options: -i delete only after confirmation -r directories will be recursively deleted (with all sub- directories) -f force: suppress all safety inquiries. Note: Varying from system to system, rm without the option -f might need a confirmation or not (the latter is the standard). Example: wegner@arber:~/numerik > ls hello.cpp hello3.cpp wegner@arber:~/numerik > rm -i hello3.cpp rm: remove ‘hello3.cpp’? y wegner@arber:~/numerik > ls hello.cpp wegner@arber:~/numerik > 23 File permissions/Access rights The UNIX file system distinguishes between three dif- ferent access rights or file mode bits. (Note: actually, there are more access rights, but these are of interest only for administrators.) r read: permits the reading of file contents, or, for directories, the listing of their content. w write: permits the modification of files (incl. delete). To create or delete files, the parent di- rectory(ies) need write access as well! x execute: permits the execution of binary files (commands, programs) and of shell scripts from the command line. For directories, the x bit is re- quired to change into this directory and to access the files/directories inside. Access rights are individually defined for u the owner of the object g the group to which the object belongs o all other users a all users (i.e., u + g + o) The access rights of a file can be changed by means of the command chmod. 24
  • 14. command chmod Syntax: chmod [ugoa][+-=][rwx] file(s)/directory(ies) Change the access rights of files or directories. These rights are displayed by ls -l according to the pattern uuugggooo rwxrwxrwx Example: wegner@arber:~/numerik > ls -l total 4 -rw-r--r-- 1 wegner stud 100 Oct 20 15:02 hello.cpp wegner@arber:~/numerik > chmod go+w hello.cpp wegner@arber:~/numerik > ls -l total 4 -rw-rw-rw- 1 wegner stud 100 Oct 20 15:02 hello.cpp wegner@arber:~/numerik > Exercise: 1. Remove the execution right for the directory yourname exercise0! Try to change to the directory. 2. Remove all rights for the file linux.txt! How can this be undone? 25 4 Editing and printing text files To modify (= edit) the content of a text file, an editor is needed. Within UNIX there is a variety of editors, which can be distinguished mostly with respect to ease of use and memory requirements. The editor vi and vim vi is the only editor which is present on all UNIX sys- tems. The editor vi • can be completely keyboard controlled • is extremely flexible • rather difficult to learn vim is a derivate from vi, and can be controlled also by the mouse. Those of you who enjoy a challenge should learn using this editor. A somewhat simpler and more convenient alternative, which is also implemented in (almost) all UNIX sys- tems, is The editor emacs The editor emacs works in an own window, and can be controlled (in addition to keys) by menus and mouse. emacs has rather large memory requirements (no prob- lem for today’s computers), since this ‘editor’ can do much more than only editing. 26
  • 15. Exercise: 1. Edit the program hello.f90! Start emacs with emacs hello.f90 & from the command line. The ampersand, &, ensures that emacs runs in the back- ground, so that you can continue your work from the com- mand line, independent from the emacs window (see Section ‘Process administration’). Try to change the comments in those lines starting with ! 2. Split the screen with Ctrl X 2. Return to one screen with Ctrl X 1 3. Save the file with Ctrl X Ctrl S! 4. Quit emacs with Ctrl X Ctrl C! Note: Whenever you save a file in emacs, a backup of the previous version is automatically created under name file˜. Examples for additional possibilities • Advanced use of man pages (e.g., searching for certain strings): In emacs , type Esc X man CR xterm to open the xterm man pages. To search for ‘terminal’, type Ctrl S terminal, and then Ctrl S for the next in- stance. • Spell checking within emacs via the the command Esc x ispell. Try it! Try to learn the most important key controlled com- mands. After a while, you can edit your files much faster than by using mouse and menus. A quick ref- erence is provided in the appendix. 27 command cat Syntax: cat file displays the content of a file on the standard output channel (usually the screen). As many other UNIX commands, cat is a filter, which can read not only from files, but also from the stan- dard input channel (usually the keyboard via the com- mand line). Thus, cat can be used to directly create smaller text files. In this case, the output has to be re-directed into a file via >. cat then expects some input from the command line, which must be finished with Ctrl D. Example: wegner@arber:~ > cat > test This is a test. ^D wegner@arber:~ > cat test This is a test. wegner@arber:~ > more test This is a test. wegner@arber:~ > Exercise: 1. View the file .plan! 2. View the file linux.txt! Is cat a suitable tool? 28
  • 16. command more Syntax: more file more permits to view also larger files page by page. Important commands within more are b to scroll back and q to quit. Example: wegner@arber:~ > more hello.f90 Exercise: View the file linux.txt with more! Which effect do the keys CR and SPACE have? 29 command lpr, lpq, lprm Syntax: lpr -Pprintername file lpq -Pprintername lprm job_id lpr prints a file on the printer named printername. To find out the printername, ask a colleague or your administrator. lpq lists all print jobs on the printer printername and provides the corresponding job ids. lprm deletes the print job with id job id from the print- ing queue. Example: wegner@arber:~ > a2ps hello.f90 -o hello.ps [hello.f90 (Fortran): 1 page on 1 sheet] [Total: 1 page on 1 sheet] saved into the file ‘hello.ps’ wegner@arber:~ > ls hello* hello.f90 nsmail/ test hello.cpp hello.ps numerik/ wegner@arber:~ > lpr -Plp0 hello.ps wegner@arber:~ > Exercise: Print the file linux.txt! 30
  • 17. More important commands a2ps converts ASCII text to PostScript. Often re- quired to print text under Linux. a2ps [options] textfile -1, -2, ..., -9 predefined font size and page layout. E.g., with -2 two pages of text are displayed side-by-side on one output page. -o output file (*.ps) -P NAME send output to printer NAME diff file1 file2 compares two files. If they are iden- tical, no output. touch file sets the current time stamp for a file. Can be used to create an empty file. finger account displays additional information for the user of a certain account (name of user, project, etc.) gv datei.ps displays PostScript files and files of re- lated formats (e.g., *.eps, *.pdf). acroread file.pdf displays pdf files and allows for simple manipulations (e.g., copy text or figures to the clipboard). gimp file starts the image manipulation program gimp (similar to photoshop). Allows to view, manipulate and print image files (e.g., *.jpg, *.tif, *.png). 31 ps2pdf file.ps converts ps-files to pdf-files. The file file.pdf will be automatically created. gzip file. Compresses file via Lempel-Ziv algorithm. The file file.gz is created and the file file deleted. Typical compression factor ∼3. gunzip file.gz. Corresponding decompression. tar “tape archive”. Nowadays mainly used to create one single file from a file tree, which then, e.g., can be sent by email. Reverse process also with tar. tar -cvf direc.tar direc creates (c) file (f) direc.tar from directory direc. Verbose progress is displayed (v). tar -xvf direc.tar re-creates original file tree under original name (./direc). tar -zcvf direc.tgz direc tar -zxvf direc.tgz additional compression/dekompression via gzip. Note: This command is extremely ‘powerful’. Ei- ther read the man pages, or use the command as given. 32
  • 18. locate search expression. Lists all files and directories in the local database, which correspond to the search expression. Extremely well suited to search for files (if the database is frequently updated → system administrator) find searches recursively for files corresponding to search expression within the given path. Example: find . -name ‘*.txt’ searches recur- sively for all *.txt files, starting within the current directory. grep searches for text within given files. Example: grep ‘test’ ../*.f90 searches for the text test in all *.f90 files in the parent directory. The most important option is [-i], which forces grep to ignore any distinction between upper and lower case. 33 5 UNIX shells The shell is a service program through which the user communicates with the OS and which is responsible for the interpretation of the input commands. Different UNIX shells Since the shell does not directly belong to the OS, a number of different shells have been developed in the course of time: • Bourne shell (sh). A well-known and widespread shell, named after its inventor Steven Bourne. An advanced derivate, the bash, Bourne again shell (note the pun) is most popular under Linux. • C-Shell (csh). Developed in Berkeley, and uses a more C-like syntax. An improved version of the C-shell is the tcsh. • Bash shell (bash). Advanced Bourne shell and standard on many systems. Each shell contains a set of system variables, which can be augmented by user-defined variables. This set comprises the process environment for the programs running inside the shell. Moreover, the shell can be used to run (system-) pro- grams via shell scripts. 34
  • 19. Shell scripts Shell scripts are small programs consisting of UNIX commands and shell-specific program constructs (branches, loops etc), which behave like UNIX com- mands but are present in text form (instead of binary). These scripts are interpreted by the shell. The syntax of shell scripts differs (considerably) from shell to shell. Some shell scripts are automatically called under cer- tain conditions: • .profile and/or .login are executed, if present, at login (i.e., for the login shell), and only once. • .bashrc and .cshrc /.tcshrc are called whenever a new bash or csh/tcsh is opened, respectively. Exercise: 1. Copy the file .tcshrc to your home directory and inspect the file! 2. Open a (new) tcsh by typing tcsh on the command line! What happens? Exit the tcsh with exit! 35 Re-directing input and output All UNIX commands use input and output channels to read data and to output data. Usually, these are the keyboard and the screen assigned to the specific user, respectively. These standard channels can be redirected within the shell such that a command can either read directly from a file (instead from the keyboard) and/or write into a file (instead of the screen). For re-direction, use the characters ‘>’ (for output) and ‘<’ (for input) With ‘>>’, the output will be appended to an existing file. If the file does not exist, this command behaves as ‘>’. Example: wegner@arber:~ > ls hello.cpp linux.txt numerik/ hello.f90 nsmail/ wegner@arber:~ > cat linux.txt > linux2.txt wegner@arber:~ > ls hello.cpp linux.txt nsmail/ hello.f90 linux2.txt numerik/ 36
  • 20. Pipes Furthermore, many UNIX commands act as so-called filters: They read from the standard input and write to the standard output. Thus, they can be combined via so-called pipes such that the output of one command acts as the input of another: pipe command 1 filter command 2 pipe Pipes are constructed on the command line by using the ‘|’ character between commands. A re-direction to a file with ‘>’ or ‘>>’ can be present only at the end of such a chain. Example: wegner@arber:~ > man g++ | a2ps -P printer [Total: 151 pages on 76 sheets] wegner@arber:~ > With this pipe, the man pages for g++ are formatted and printed via one command. 37 6 Process administration A process is a running program or script and consists of • the program/script itself and • the corresponding environment, which consists of all required additional information necessary to ensure a correct program flow. Characteristics of a process are (among others) • a unique process ID (PID), • PID of the parent process (PPID), • User and group number of the owner and • priority of the process. Normally, when a process has been started from a shell, the shell cannot be used for other input until the end of the process. But processes and programs can also be run in the background. To enable this feature, the command line which calls the process/program must end with an ampersand, ’&’. Example: wegner@arber:~ > firefox & [1] 21749 wegner@arber:~ > Exercise: Start the program xeyes in the background! 38
  • 21. command ps Syntax: ps [-al] [-u user] Display running processes with their characteristics. Without options, only the user’s own processes run- ning in the current shell are displayed. Important options: -a display all processes assigned to any terminal (tty) -l long format display. Additional information about owner, parent process etc. -u display all processes which are owned by a specific user. Example: wegner@arber:~ > ps PID TTY TIME CMD 21733 pts/4 00:00:00 bash 22197 pts/4 00:00:00 xterm 22198 pts/5 00:00:00 bash 22212 pts/4 00:00:00 ps wegner@arber:~ > Exercise: View all current processes within your shell! 39 command kill Syntax: kill [-9] PID Terminates the process with number PID. Can be ex- ecuted only by the owner of the process or by root. Important option: -9 for ‘obstinate’ processes which cannot be termi- nated by a normal kill. Example: wegner@arber:~ > ps PID TTY TIME CMD 21733 pts/4 00:00:00 bash 22197 pts/4 00:00:00 xterm 22198 pts/5 00:00:00 bash 22212 pts/4 00:00:00 ps wegner@arber:~ > kill 22197 wegner@arber:~ > ps PID TTY TIME CMD 21733 pts/4 00:00:00 bash 22214 pts/4 00:00:00 ps [1]+ Exit 15 xterm wegner@arber:~ > Exercise: Terminate xeyes via kill! 40
  • 22. 7 The programming language C++: An overview Programming languages allow to formulate certain problems or algorithms by means of particular syntac- tic rules. Such a program can be ‘translated’ by ded- icated programs (interpreter, compiler) into machine- readable code and then executed by the computer. Historically, various programming languages of differ- ent complexity have been established, where this com- plexity was and is determined by the progress in com- puter science/hardware and the intended application. The approach for solving a certain problem can be discriminated by different programming paradigms. Procedural and object-oriented programming Procedural programming The emphasis is on the used algorithm. A program consists of a hierarchic dissection of the problem into functional units. Data and functions are separated, and data are pub- licly accessible. Programming languages: C, Fortran77, Pascal, ... 41 Object-oriented programming useful introduction: https://www.youtube.com/watch?v=lbXsrHGhBAU User-defined types (classes) and associated operations are introduced. Thus, data and associated functions (methods) build a unit within a class. Data are (usually) accessible only via the associated methods. Inheritance of common properties allows to distin- guish between general and special attributes of the types/classes. Example: Genealogical tree of the various classes of animals and plants, families, orders, species etc. Within the emerging hierarchy, similar functions, shar- ing the same interface but defining a specific be- haviour for each particular type/class, can be imple- mented (polymorphism). By this approach, an (almost) exact model of reality shall be created which reflects all relevant dependen- cies within the program. Advantages of object-oriented programming: • better abstraction possibilities because of holistic approach. • improved structure and modularity, allowing for easy maintenance of programs. • reusability. Programming languages: C++, Java, Python,. . . 42
  • 23. Essential features of C and C++ • At present, C++ is one of the most used pro- gramming languages. • C++ originates from C and is a superset of C. • C++ allows both for efficient, hardware-oriented programming (as already C), but also for pro- gramming on a high, object-oriented abstraction level. Thus, C++ is sometimes called a hybrid language. • The OS UNIX has been completely written in C. • C++ and C are standardized (by international ANSI standard). • C und C++ do not comprise special functions for input and output, graphics and hardware pro- gramming etc., but there are corresponding li- braries. • The standard library includes a comprehensive set of functions and classes required for typical appli- cations, and is installed together with each com- piler. • According to afficionados, C++ - programming is fun! 43 Literature • Stroustrup, B.: The C++ Programming Lan- guage: Special Edition, Addison-Wesley Longman, Amsterdam, 2000. • Kernighan, B., Ritchie, D.: The C Program- ming Language, Prentice Hall, 1988 (2nd ed.). • Meyers, S.: Effective C++: 55 Specific Ways to Improve Your Programs and Designs, Addison-Wesley, München, 2005 (3rd ed.). • Josuttis, N.: The C++ Standard Library: A Tutorial and Reference, Addison-Wesley Longman, Amsterdam, 1999. • Booch, G., Maksimchuk, R. A., Engle, W. et al.: Object Oriented Analysis and Design with Applications, Addison-Wesley Longman, Amsterdam, 2007 (3rd ed.). The C++ Standard (INCITS/ISO/IEC 14882-2003) can be downloaded via http://www.ansi.org (≈ 30$). 44
  • 24. Online resources WWW • C++ tutorial http://www.cplusplus.com/doc/tutorial • Bjarne Stroustrup’s homepage (author of the C++ programming language) http://www2.research.att.com/~bs • C++ FAQs http://www.parashift.com/c++-faq-lite Newsgroups • comp.lang.c++.moderated General C++ discussion forum. • comp.std.c++ Discussions related to the C++ standard. 45 8 Fortran 90/95: An overview Brief history • introduced 1954. • continued by Fortran II, Fortran IV, Fortran66, Fortran77 (still in use). • Fortran 90, since 1991 ISO, since 1992 ANSI standard. • Fortran 95, since 1996/97 ISO/ANSI standard. • meanwhile Fortran 2000 and Fortran 2003 (latest standard). • F95 and Fortran 2000 include relatively minor re- visions of F90. • Fortran 2003 is a major revision, supporting (among other features) object-oriented program- ming (inheritance, polymorphism). • Fortran 2008 under development. • most current compilers for F90/95, but Fortran 2003 standard (almost) reached with newest ver- sion of Intel Fortran compiler, v11. 46
  • 25. Important features • Fortran was and is the most used language for solving physical problems, particularly numerical simulations. • The introduction of F90/95 allowed for similar features as in C/C++, except for the hardware- orientation. With Fortran 2003, even object- oriented programming became possible. • The capabilities of F90 are rather large and there are numerous standard operations and functions. E.g., vector- and matrix-operations belong to the standard: a = b + c can mean scalar, vector or matrix addition, de- pending on the definition of a, b, c. Vector- and matrix products can be calculated by likewise sim- ple instructions (fast execution), a = dot product(b, c); a = matmul(b, c). • There are comprehensive program libraries, par- ticularly for Linear Algebra and eigenvalue- problems (available both as source codes or highly optimized binary objects), e.g., BLAS, LAPACK, EISPACK. • Optimized programs (more-D, parallel) execute mostly faster than corresponding C++ programs (factor 2 to 3). 47 • Simple possibility for parallelization via HPF (high performance Fortran). • The basic structures are very simple, and the lan- guage can be learned more easily than C++ (at least regarding the basic concepts). • Until the next couple of years, Fortran needs to be known by any physicist who is not soley interested in purely experimental/obserservational or purely theoretical work. Examples for physical research areas which use (almost) exclusively Fortran: Aero-/hydro- dynamics, computational astrophysics, atomic and nuclear physics, geophysics, meteorology. • Programming in Fortran is fast! 48
  • 26. Literature • Reference manuals – Gehrke, W.: Fortran90 Referenz-Handbuch, 1991, Hanser, München, ISBN 3446163212. – ‘Fortran 90’, RRZN (available at the LRZ). • Text books – Adams, J.C., et al.: Fortran 2003 Hand- book: The Complete Syntax, Features and Procedures, Springer, Berlin, 2008, ISBN 1846283787. – Metcalf, M., et al.: Fortran 95/2003 ex- plained, Oxford Univ. Press, 2004, ISBN 0198526938 (paperback). Online resources • Online-Tutorial at Univ. Liverpool http://www.liv.ac.uk/HPC/HTMLFrontPageF90.html • German Fortran Website http://www.fortran.de • Metcalf’s Fortran Information http://www.fortran.com/metcalf • Michel Olagnon’s Fortran 90 List http://www.fortran-2000.com/MichelList 49 9 Creating executable programs Three subsequent phases During the generation of executable programs from C++ or Fortran source files, always the same steps have to be performed (also for other higher program- ming languages such as Basic or Pascal). executable object file object file pre-processor compiler linker object file program library source file *.h header file *.cpp / *.f90 *.o *.o *.o *.a program The pre-processor replaces symbolic constants and macros and inserts additional files, if required. Usually omitted for Fortran programs. The compiler ‘translates’ the source code to machine readable code, creating the object files. The linker binds all present object files (and, option- ally, object files from libraries) to the executable pro- gram (executable object). 50
  • 27. command g++ Syntax: g++ [-c] [-g] [-O] file.cpp [-o outputfile] We suggest using the GNU C++ compiler, which is included in almost any Linux distribution. The linker is already included, but can be called separately. Important options: -o the name of the executable object (program). Default ‘a.out’. -c compile only, do not link. -g include debug information. Necessary for later debugging. Increases the size and execution time of the program. -O optimization (default: intermediate optimization) Example: wegner@arber:~ > g++ hello.cpp wegner@arber:~ > ./a.out Hello, world! wegner@arber:~ > g++ hello.cpp -o hello wegner@arber:~ > ./hello Hello, world! 51 The character ./ in front of a.out and hello is usually required to tell the shell that the executable program is located within the current directory. Now in two steps: compile at first, then link Example: wegner@arber:~ > g++ -c hello.cpp wegner@arber:~ > g++ hello.o -o hello If more than one source file is present (e.g., if each sub-program is contained in a separate source file), one can avoid unnecessary compilations and a lot of command-line typing by using so-called makefiles, which allow updating executable programs using just one command. For further info, google for makefile and/or contact your supervisor. 52
  • 28. command ifort, gfortran Syntax: ifort [-c -g -O] file.f90 [-o outputfile] gfortran [-c -g -O] file.f90 [-o outputfile] Within the GNU open source project, a GNU Fortran 95 compiler is available under the command gfortran (if implemented and installed within your Linux distri- bution/system). Since this compiler (in its final stage) is quite new and has not been tested thoroughly at our institute, we recommend to use the Intel Fortran 95 compiler ifort when possible (installed, e.g., at the workstations of the USM. Sometimes, you need to in- voke the command module load fortran to make the compiler available). Note that both compilers comply with the F95 stan- dard (ifort v11 and later additionally supports most Fortran 2000/2003 features), and that gfortran does not support 16-byte reals, in contrast to ifort. When you have no possibility to use ifort (e.g., when working at home without connection to the the USM), use gfortran, which has identical or similar options as ifort. gfortran is also available as an MS-Windows binary. If you want to debug your code with the GNU debug- ger ddd or gdb (next topic), we suggest to use gfortran. (ifort v12 and later works in most cases as well). In both compilers, the linker is included again, but can be called separately. 53 Important options for ifort and gfortran: -o the name of the executable object (program). Default ‘a.out’. -c compile only, do not link -g include debug information (see g++) -O optimization (default: intermediate optimization) Example: wegner@arber:~ > ifort hello.f90 -o hello wegner@arber:~ > ./hello Hello, world! wegner@arber:~ > Exercise: 1. Compile the Fortran version of HelloWorld with ifort und start the program! 2. Compile the program with gfortran using the option -c und link in a second step via gfortran hello.o! 54
  • 29. Debugging After you have successfully compiled and linked your program, it can be started from the shell by giving its name as command (a.out or ownname when compiled with -o ownname). This does not mean, however, that 1. your programm runs successfully to its end, 2. it complies with your intentions. If some run-time error occurs, you have to debug the program. You can do this either manually (by printing out certain intermediate results and test statements), or you use a so-called debugger. Such a tool allows, e.g., • to execute the statements stepwise • to view the current content of the variables • to stop the program at so-called breakpoints de- fined by yourself. An open source and window-oriented debugger avail- able under Linux is ddd (Data Display Debugger), which works for C++ und Fortran programs (at least those compiled with g++ and gfortran, respectively, and with option -g). The debugger is simply called via ddd program_name where program name is the name of the executable ob- ject (e.g., a.out). 55 The core of ddd (which actually provides the window interface ‘only’) is the debugger gdb, which can be alternatively used alone to debug the program (from the console). It is called via gdb program_name but needs some knowledge of the involved commands. Remember: To debug a program, it has to be com- piled with the option -g in order to include the required debug information. Example: wegner@arber:~ > gfortran -g hello.f90 -o fhello wegner@arber:~ > ddd fhello GNU DDD 3.3.11 (x86_64-suse-linux-gnu), by Dorothea Luetkehaus and Andreas Zeller. ... (gdb) Exercise: 1. Compile the program pi.f90 and run it. Re-compile with the option -g! 2. Start ddd for the executable object. Become acquainted with the most important entries (Run, Step, Next, ...) in the control panel und buttons (Break, Print) in the menu bar. 56