Professional Documents
Culture Documents
The Operating System Linux and Programming Languages: Contents
The Operating System Linux and Programming Languages: Contents
3 File systems
c
2010, Joachim Puls & Michael Wegner (1st ed.)
1 General remarks on the operat- Essential features
ing system UNIX/Linux UNIX
Exercise:
Login to the system with your user account!
3 4
Graphical user interface command xterm
5 6
command man command passwd
Syntax: Syntax:
• user name,
• time of login.
whoami is self-explanatory.
Example:
• directories
• device files
Exercise: Exercise:
List the complete content of your home directory! Create a directory yourname exercise within your home directory,
What is displayed with ls .* ? where yourname is your actual name!
17 18
command cp cp -r dir1 dir2
If dir exists, file1 [, file2, file3] are copied into a) Check whether the directory ubung0 is present in your home
dir. If dir does not exist, you get an error warning directory. If not, copy, via scp, the directory ubung0 from
(for more than two arguments), or, for two arguments, account/host numprakt@ltsp08.usm.uni-muenchen.de to your
dir is interpreted as a file name and file1 is copied home directory.
to a file named dir. b) Copy the files from ubung0 into your directory
yourname exercise!
19 20
command mv Example:
wegner@arber:~ > ls
Syntax: hello* hello.f90 nsmail/
hello.cpp hello2.cpp numerik/
mv file1 file2 wegner@arber:~ > mv hello2.cpp hello3.cpp
mv file1 [file2 ...] directory wegner@arber:~ > ls
mv dir1 dir2 hello* hello.f90 nsmail/
mv dir1 [dir2 ...] directory hello.cpp hello3.cpp numerik/
wegner@arber:~ > ls numerik
Rename or move files or directories. Similar to cp, but hello.cpp
original is ‘destroyed’. First command from above re- wegner@arber:~ > mv hello3.cpp numerik
names files, other commands move files/directories. wegner@arber:~ > ls
(Actually, only the pointer in the ‘inode table’ is hello* hello.cpp hello.f90 nsmail/ numerik/
changed, but there is no physical move – except if wegner@arber:~ > ls numerik
you move the file to another file system). hello.cpp hello3.cpp
wegner@arber:~ >
Note: no option [-r] required
Exercise:
21 22
command rm File permissions/Access rights
The UNIX file system distinguishes between three dif-
Syntax: ferent access rights or file mode bits. (Note: actually,
there are more access rights, but these are of interest
rm [-irf] file(s)/directory(ies) only for administrators.)
Delete files and/or directories. After deleting, the
deleted files cannot be recovered! Use rm only with r read: permits the reading of file contents, or, for
greatest caution. E.g., the command rm -r * deletes directories, the listing of their content.
recursively (in most cases without further inquiry) the
complete file tree below the current directory (leaving w write: permits the modification of files (incl.
the hidden files/directories beginning with . though). delete). To create or delete files, the parent di-
Options: rectory(ies) need write access as well!
-i delete only after confirmation x execute: permits the execution of binary files
(commands, programs) and of shell scripts from
-r directories will be recursively deleted (with all sub- the command line. For directories, the x bit is re-
directories) quired to change into this directory and to access
the files/directories inside.
-f force: suppress all safety inquiries.
Note: Varying from system to system, rm without the Access rights are individually defined for
option -f might need a confirmation or not (the latter
is the standard). u the owner of the object
Change the access rights of files or directories. These The editor vi and vim
rights are displayed by ls -l according to the pattern vi is the only editor which is present on all UNIX sys-
tems. The editor vi
uuugggooo
rwxrwxrwx • can be completely keyboard controlled
• Advanced use of man pages (e.g., searching for wegner@arber:~ > cat > test
certain strings): This is a test.
In emacs , type Esc X man CR xterm to open the ^D
xterm man pages. To search for ‘terminal’, type wegner@arber:~ > cat test
Ctrl S terminal, and then Ctrl S for the next in- This is a test.
stance. wegner@arber:~ > more test
This is a test.
• Spell checking within emacs via the the command wegner@arber:~ >
Esc x ispell. Try it!
Exercise:
Try to learn the most important key controlled com-
mands. After a while, you can edit your files much 1. View the file .plan!
faster than by using mouse and menus. A quick ref-
erence is provided in the appendix. 2. View the file linux.txt! Is cat a suitable tool?
27 28
command more command lpr, lpq, lprm
Syntax: Syntax:
lprm deletes the print job with id job id from the print-
ing queue.
Example:
31 32
locate search expression. Lists all files and directories 5 UNIX shells
in the local database, which correspond to the
search expression. Extremely well suited to search The shell is a service program through which the user
for files (if the database is frequently updated → communicates with the OS and which is responsible
system administrator) for the interpretation of the input commands.
Example:
Pipes are constructed on the command line by using • a unique process ID (PID),
the ‘|’ character between commands.
• PID of the parent process (PPID),
A re-direction to a file with ‘>’ or ‘>>’ can be present
only at the end of such a chain. • User and group number of the owner and
With this pipe, the man pages for g++ are formatted Exercise:
and printed via one command. Start the program xeyes in the background!
37 38
command ps command kill
Syntax: Syntax:
Exercise: Exercise:
View all current processes within your shell! Terminate xeyes via kill!
39 40
7 The programming language Object-oriented programming
C++: An overview useful introduction:
https://www.youtube.com/watch?v=lbXsrHGhBAU
Programming languages allow to formulate certain
problems or algorithms by means of particular syntac- User-defined types (classes) and associated operations
are introduced.
tic rules. Such a program can be ‘translated’ by ded-
icated programs (interpreter, compiler) into machine- Thus, data and associated functions (methods) build
readable code and then executed by the computer. a unit within a class. Data are (usually) accessible
only via the associated methods.
Historically, various programming languages of differ-
Inheritance of common properties allows to distin-
ent complexity have been established, where this com-
guish between general and special attributes of the
plexity was and is determined by the progress in com- types/classes.
puter science/hardware and the intended application.
Example: Genealogical tree of the various classes of
The approach for solving a certain problem can be animals and plants, families, orders, species etc.
discriminated by different programming paradigms.
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
Procedural and object-oriented shall be created which reflects all relevant dependen-
programming cies within the program.
Advantages of object-oriented programming:
Procedural programming
• better abstraction possibilities because of holistic
The emphasis is on the used algorithm. A program approach.
consists of a hierarchic dissection of the problem into
functional units. • improved structure and modularity, allowing for
easy maintenance of programs.
Data and functions are separated, and data are pub-
licly accessible. • reusability.
Programming languages: C, Fortran77, Pascal, ... Programming languages: C++, Java, Python,. . .
41 42
Essential features of C and C++ Literature
• At present, C++ is one of the most used pro- • Stroustrup, B.: The C++ Programming Lan-
gramming languages. guage: Special Edition,
Addison-Wesley Longman, Amsterdam, 2000.
• C++ originates from C and is a superset of C.
• Kernighan, B., Ritchie, D.: The C Program-
• C++ allows both for efficient, hardware-oriented ming Language,
programming (as already C), but also for pro- Prentice Hall, 1988 (2nd ed.).
gramming on a high, object-oriented abstraction
level. Thus, C++ is sometimes called a hybrid • Meyers, S.: Effective C++: 55 Specific Ways
language. to Improve Your Programs and Designs,
Addison-Wesley, München, 2005 (3rd ed.).
• The OS UNIX has been completely written in C.
• Josuttis, N.: The C++ Standard Library: A
• C++ and C are standardized (by international Tutorial and Reference,
ANSI standard). Addison-Wesley Longman, Amsterdam, 1999.
• C und C++ do not comprise special functions • Booch, G., Maksimchuk, R. A., Engle, W.
for input and output, graphics and hardware pro- et al.: Object Oriented Analysis and Design with
gramming etc., but there are corresponding li- Applications,
braries. Addison-Wesley Longman, Amsterdam, 2007
(3rd ed.).
• The standard library includes a comprehensive set
of functions and classes required for typical appli- The C++ Standard (INCITS/ISO/IEC 14882-2003)
cations, and is installed together with each com- can be downloaded via http://www.ansi.org (≈ 30$).
piler.
43 44
Online resources 8 Fortran 90/95: An overview
WWW
Brief history
• C++ tutorial
http://www.cplusplus.com/doc/tutorial • introduced 1954.
• Bjarne Stroustrup’s homepage (author of the • continued by Fortran II, Fortran IV, Fortran66,
C++ programming language) Fortran77 (still in use).
http://www2.research.att.com/~bs
• Fortran 90, since 1991 ISO, since 1992 ANSI
• C++ FAQs standard.
http://www.parashift.com/c++-faq-lite
• Fortran 95, since 1996/97 ISO/ANSI standard.
45 46
Important features • Simple possibility for parallelization via HPF (high
performance Fortran).
• Fortran was and is the most used language for
solving physical problems, particularly numerical • The basic structures are very simple, and the lan-
simulations. guage can be learned more easily than C++ (at
least regarding the basic concepts).
• The introduction of F90/95 allowed for similar
features as in C/C++, except for the hardware- • Until the next couple of years, Fortran needs to be
orientation. With Fortran 2003, even object- known by any physicist who is not soley interested
oriented programming became possible. in purely experimental/obserservational or purely
theoretical work.
• The capabilities of F90 are rather large and there Examples for physical research areas which
are numerous standard operations and functions. use (almost) exclusively Fortran: Aero-/hydro-
E.g., vector- and matrix-operations belong to the dynamics, computational astrophysics, atomic
standard: and nuclear physics, geophysics, meteorology.
a=b+c
can mean scalar, vector or matrix addition, de- • Programming in Fortran is fast!
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).
47 48
Literature 9 Creating executable programs
• Reference manuals
Three subsequent phases
– Gehrke, W.: Fortran90 Referenz-Handbuch, During the generation of executable programs from
1991, Hanser, München, ISBN 3446163212. C++ or Fortran source files, always the same steps
– ‘Fortran 90’, RRZN (available at the LRZ). have to be performed (also for other higher program-
ming languages such as Basic or Pascal).
• Text books source file header file
*.cpp / *.f90 *.h
– Adams, J.C., et al.: Fortran 2003 Hand-
book: The Complete Syntax, Features and
Procedures, pre-processor
Example:
ifort [-c -g -O] file.f90 [-o outputfile] -c compile only, do not link
gfortran [-c -g -O] file.f90 [-o outputfile]
Within the GNU open source project, a GNU Fortran -g include debug information (see g++)
95 compiler is available under the command gfortran
(if implemented and installed within your Linux distri- -O optimization (default: intermediate optimization)
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 Example:
compiler ifort when possible (installed, e.g., at the
workstations of the USM. Sometimes, you need to in- wegner@arber:~ > ifort hello.f90 -o hello
voke the command module load fortran to make the wegner@arber:~ > ./hello
compiler available). Hello, world!
Note that both compilers comply with the F95 stan- wegner@arber:~ >
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. Exercise:
If you want to debug your code with the GNU debug-
1. Compile the Fortran version of HelloWorld with ifort und
ger ddd or gdb (next topic), we suggest to use gfortran. start the program!
(ifort v12 and later works in most cases as well).
In both compilers, the linker is included again, but can 2. Compile the program with gfortran using the option -c und
link in a second step via gfortran hello.o!
be called separately.
53 54
Debugging The core of ddd (which actually provides the window
interface ‘only’) is the debugger gdb, which can be
After you have successfully compiled and linked your
alternatively used alone to debug the program (from
program, it can be started from the shell by giving its
name as command (a.out or ownname when compiled the console). It is called via
with -o ownname). This does not mean, however, that
gdb program_name
1. your programm runs successfully to its end,
but needs some knowledge of the involved commands.
2. it complies with your intentions.
Remember: To debug a program, it has to be com-
If some run-time error occurs, you have to debug the piled with the option -g in order to include the required
program. You can do this either manually (by printing debug information.
out certain intermediate results and test statements),
or you use a so-called debugger. Such a tool allows, Example:
e.g.,
wegner@arber:~ > gfortran -g hello.f90 -o fhello
• to execute the statements stepwise wegner@arber:~ > ddd fhello
GNU DDD 3.3.11 (x86_64-suse-linux-gnu),
• to view the current content of the variables by Dorothea Luetkehaus and Andreas Zeller.
...
• to stop the program at so-called breakpoints de-
(gdb)
fined by yourself.
ddd program_name 2. Start ddd for the executable object. Become acquainted
with the most important entries (Run, Step, Next, ...) in
where program name is the name of the executable ob- the control panel und buttons (Break, Print) in the menu
ject (e.g., a.out). bar.
55 56