First steps on Linux and programming Adrien Poteaux CRIStAL, Université de Lille Year 2017-2018 This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/ adrien.poteaux@univ-lille1.fr Linux? Programming? 1 / 16
Refresher course in Computer Science 6 weeks ; 50 hours in total A lot to do (not in that order): Linux basics / using a shell C language Fortran language Unix system How we ll work: A few lectures, More practical work, 4 projects (50%), One written exam (50%), Documents avalaible at Personal implication: take good uses, practice programmation, read, search on the web... Questions are welcome! http://cristal.univ-lille.fr/~poteaux/teaching.html My email : adrien.poteaux@univ-lille1.fr adrien.poteaux@univ-lille1.fr This lecture 2 / 16
Projects and notations During the lectures, 4 projects will get a mark : Fortran project ; due September 18th, before 9am ; 15% C project 1 (no pointers) ; 10% C project 2 (with pointers) ; 10% A last project (system) ; 15% These deadlines are in bold on the webpage. Remaining of the mark will be on paper: Written exam ; 50% adrien.poteaux@univ-lille1.fr This lecture 3 / 16
How to send your s! One archive by email: adrien.poteaux@univ-lille1.fr, $ tar cvzf yourname-project.tar.gz 1 2... cf man tar It should contain: The requested s (clean code - indentation, comments are welcome - put your names again there), Description of how to compile your s (better : make) Potentially a README (any general comment you want to make... ), No unnecessary (backup s, intermediate compilations s, executables that are generated by your code... ). = I cannot do anything from them! adrien.poteaux@univ-lille1.fr This lecture 4 / 16
Interpreted language vs compiled language Compiled language Interpreted language C, C++, Fortran... Shell, python, perl, php... The compiler transforms source code written in a programming language into binary form (the object code) to create an executable program. Executable is optimised. Produced executable unique to each processor (thus computer). = must be recompiled! The interpreter directly executes a sequence of instructions of the source code. No optimisation. The interpreter is required at each execution. adrien.poteaux@univ-lille1.fr Linux? Programming? 5 / 16
C and Fortran Fortran C Portable code (norm for the language and the librairies) High level language (data structure, functions, separated compilation... ) generic mathematical functions low-level language (bit & adresses manipulation) powerful and efficient languages not really premissive permissive specialised for scientific computing more general (operating system) multidimensional tables and associated functions adrien.poteaux@univ-lille1.fr Linux? Programming? 6 / 16
Compiled language? 1 Conception. Define your algorithm, write in pseudo-code... 2 Coding. Write your source code in a text.c for some C code,.f90 for some Fortran... like this one 3 Compiling your source code from the shell : $ gcc.c, $ gfortran.f90. 4 Execution. $./a.out run the command from the folder where the is! adrien.poteaux@univ-lille1.fr Linux? Programming? 7 / 16
The patriot bug In 1991, a Patriot anti-missile failed to intercept a Scud missile. 28 people were killed. The code worked with time increments of 0.1 s. But 0.1 is not representable in binary. In the 24-bit format used, the number stored was 0.099999904632568359375 The error was 0.0000000953. After 100 hours = 360,000 seconds, time is wrong by 0.34s. In 0.34s, a Scud moves 500m adrien.poteaux@univ-lille1.fr Coding can be messy! 8 / 16
A real story (appeared at CERN) Using a IA32/x87 FPU (provides a 80 bit double extended format): Use the (robust and tested) standard sort function of the STL C++ library to sort objects by their radius: according to x x +y y. Sometimes (rarely) segfault, infinite loop... Why? Because the sort algorithm works under the following naive assumption: if A B, then, later, A B x x +y y inlined and compiled differently at two points of the program, computation on 64 or 80 bits, depending on register allocation enough to break the assumption (horribly rarely). No programming mistake! (thus very difficult to fix) adrien.poteaux@univ-lille1.fr Coding can be messy! 9 / 16
Another example: equality with floating point numbers What do you believe the following C program will print? (i.e. the execution of its compilation: $ gcc.c ;./a.out) # include <stdio.h> int main() { int i; float ref,index; ref = 169.0 / 170.0; for ( i = 0; i < 250; i ++) { index = i ; if (ref==(index/(index+1.0))) break; } printf ("i = %d\n", i ) ; return 0; } adrien.poteaux@univ-lille1.fr Coding can be messy! 10 / 16
Another example: equality with floating point numbers What do you believe the following C program will print? (i.e. the execution of its compilation: $ gcc.c ;./a.out) # include <stdio.h> int main() { int i; float ref,index; ref = 169.0 / 170.0; for ( i = 0; i < 250; i ++) { index = i ; if (ref==(index/(index+1.0))) break; } printf ("i = %d\n", i ) ; return 0; } Answer: i = 250 adrien.poteaux@univ-lille1.fr Coding can be messy! 10 / 16
Shell? interface between the user and the OS used to run commands / filters stdout, 1: standard output (default: screen) stdin, 0: standard input (default: keyboard) filter/command other s stderr, 2: error output (default: screen) One filter = one identifier (ls) + options (-al) + parameters (/bin) Each process returns a retour code (one byte echo $?). adrien.poteaux@univ-lille1.fr Linux? Programming? 11 / 16
Why using it? Some time to learn how to use it properly but... $ ls *.jpg wc -l will count the number jpeg pictures in the current directory (assuming they are correctly named). $ for i in *.JPG ; do mv $i `basename $i.jpg`.jpg ; done change the extension JPG by jpg for any concerned in the current directory. Way faster, and sometimes you have no choice! adrien.poteaux@univ-lille1.fr Linux? Programming? 12 / 16
Running commands The main idea : (options are parameters!) $ cmd [options] parameters Example, to start Firefox : $ firefox A parameter can be a bloc: $ command arg1 'arg 2 toto' "arg 3'" Running a command in background: $ emacs & Forgot to do so? C-z then $ bg A lot more! Cf chapter 1 of the lecture notes. Only one way to learn : PRACTICE! adrien.poteaux@univ-lille1.fr Linux? Programming? 13 / 16
Filesystem root home bin directory tmp ls cd rm poteaux toto.txt CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Absolute path (from the root) root home bin directory tmp ls cd rm poteaux toto.txt $ emacs /home/poteaux/cs/slides-shell.tex CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Absolute path (from the root) root home bin directory tmp ls cd rm poteaux toto.txt $ emacs $HOME/CS/slides-shell.tex CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Absolute path (from the root) root home bin directory tmp ls cd rm poteaux toto.txt $ emacs ~/CS/slides-shell.tex CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Relative path (from the current directory) root home bin directory tmp ls cd rm poteaux toto.txt $ cd $ emacs CS/slides-shell.tex CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Relative path (from the current directory) root home bin directory tmp ls cd rm poteaux toto.txt CS $ cd $ emacs CS/slides-shell.tex & $ cd CS ;$ emacs slides-shell.tex slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Relative path (from the current directory) root home bin directory tmp ls cd rm poteaux toto.txt $ cd /directory $ /bin/rm CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
root home bin directory tmp ls cd rm poteaux toto.txt CS $ cd /directory $ rm (try echo $PATH) slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Redirections root home bin directory tmp ls cd rm poteaux toto.txt $ cd ; ls CS > /tmp/toto.txt CS slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Redirections root home bin directory tmp ls cd rm poteaux toto.txt CS $ cd ; ls CS > /tmp/toto.txt $ ls CS >> /tmp/toto.txt slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
A graph organisation root home bin directory tmp.. ls cd rm poteaux toto.txt. link $ cd ; emacs../poteaux/./link CS similar to $ emacs /directory/ slides-shell.tex adrien.poteaux@univ-lille1.fr Filesystem 14 / 16
Use the good tools! The key command: man. Uses the less viewer: h will display the available commands, q will quit the viewer, / followed by an expression will search the next apparition of this expression in the document, move to the first results, and highlight the others, n moves to the next apparition of the searched expression, N moves to the previous apparition of the searched expression, Use a programming text editor (emacs, vi). First contact is frightening but worths it! Print the refcard! Learn some shortcuts! (try not to use the mouse) Read more (links on the webpage)! Practice! An Introduction to the Linux Command Shell For Beginners, Unix Programming Tools (sections 4 and 5 at the moment). adrien.poteaux@univ-lille1.fr Miscellaneous 15 / 16
Script: short definition File containing shell commands. Interpret the : $. foo (in the current shell), $ sh foo (in another shell),... Run the directly: Need to be executable (chmod) Magic number: #!/bin/sh interpreted in a another shell Remark /bin/sh is just a link to the shell interpreter you are using. It can be for instance bash, dash or csh. You can see which version you are using via the command $ ls -l /bin/sh adrien.poteaux@univ-lille1.fr Miscellaneous 16 / 16