first steps on linux and programming

First steps on Linux and programming Adrien Poteaux CRIStAL, - PowerPoint PPT Presentation

First steps on Linux and programming Adrien Poteaux CRIStAL, Universit de Lille Year 2020-2021 This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. http://creativecommons.org/licenses/by-nc-sa/3.0/


  1. First steps on Linux and programming Adrien Poteaux CRIStAL, Université de Lille Year 2020-2021 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-lille.fr Linux ? Programming ? 1 / 17

  2. About me Associated professor in computer science (CRIStAL), Coming from Mathematics (Master and PhD), Research in Computer Algebra. Interested by Computer Algebra ? Feel free to discuss about internship ! adrien.poteaux@univ-lille.fr This lecture 2 / 17

  3. Refresher course in Computer Science 4 weeks ; 40 hours in total A lot to do: Linux basics / using a shell C language (including makefile) Unix system Personal implication: How we’ll work: take good uses, A few lectures, practice programmation, More practical work, read, search on the web 3 projects (50%), . . . One written exam (50%), Questions are welcome ! Documents avalaible at http://www.fil.univ-lille1.fr/~poteaux/teaching.html My email : adrien.poteaux@univ-lille.fr adrien.poteaux@univ-lille.fr This lecture 3 / 17

  4. Projects and notations During the lectures, 4 projects will get a mark : C project 1 (no pointers) ; 15% C project 2 (with pointers) ; 15% A last project (system) ; 20% These deadlines will be in bold on the webpage. Remaining of the mark will be on paper: Written exam ; 50% adrien.poteaux@univ-lille.fr This lecture 4 / 17

  5. How to send your files ! One archive by email: adrien.poteaux@univ-lille.fr , $ tar cvzf yourname-project.tar.gz file1 file2 ... cf man tar It should contain: The requested files (clean code - indentation, comments are welcome - put your names again there), Description of how to compile your files ( better : makefile) Potentially a README (any general comment you want to make. . . ), No unnecessary file (backup files, intermediate compilations files, executables that are generated by your code. . . ). ⇒ I cannot do anything from them ! = adrien.poteaux@univ-lille.fr This lecture 5 / 17

  6. Interpreted language vs compiled language Compiled language Interpreted language C , C++, Fortran. . . Shell , python, perl, php. . . • The compiler transforms source • The interpreter directly executes code written in a programming a sequence of instructions of the language into binary form (the ob- source code. ject code ) to create an executable program . • No optimisation. • Executable is optimised . • The interpreter is required at • Produced executable unique to each execution. each processor (thus computer). ⇒ must be recompiled ! = adrien.poteaux@univ-lille.fr Linux ? Programming ? 6 / 17

  7. C language portable code (norm for the language and the librairies), high level language (data structure, functions, separated compilation. . . ), low-level language (bit & adresses manipulation), powerful and efficient language. . . . . . but permissive, “more” than a language (operating system) adrien.poteaux@univ-lille.fr Linux ? Programming ? 7 / 17

  8. Compiled language ? 1 Conception. Define your algorithm, write in pseudo-code. . . 2 Coding. Write your source code in a text file file.c for some C code, like this one 3 Compiling your source code from the shell : $ gcc file.c , 4 Execution. $ ./a.out � run the command from the folder where the file is ! � adrien.poteaux@univ-lille.fr Linux ? Programming ? 8 / 17

  9. 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-lille.fr Coding can be messy ! 9 / 17

  10. 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-lille.fr Coding can be messy ! 10 / 17

  11. 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 file.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-lille.fr Coding can be messy ! 11 / 17

  12. 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 file.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-lille.fr Coding can be messy ! 11 / 17

  13. Shell ? interface between the user and the OS used to run commands / filters stdout, 1: standard output ( default: screen ) stdin, 0: standard input filter/command ( default: keyboard ) stderr, 2: error output ( default: screen ) other files One filter = one identifier ( ls ) + options ( -al ) + parameters ( /bin ) Each process returns a retour code (one byte → echo $? ). adrien.poteaux@univ-lille.fr Linux ? Programming ? 12 / 17

  14. 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 file concerned in the current directory. Way faster, and sometimes you have no choice ! adrien.poteaux@univ-lille.fr Linux ? Programming ? 13 / 17

  15. 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-lille.fr Linux ? Programming ? 14 / 17

  16. Filesystem root tmp home bin directory rm ls cd poteaux toto.txt file file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  17. Absolute path (from the root) root tmp home bin directory rm ls cd poteaux toto.txt file $ emacs /home/poteaux/CS/slides-shell.tex file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  18. Absolute path (from the root) root tmp home bin directory rm ls cd poteaux toto.txt file $ emacs $HOME/CS/slides-shell.tex file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  19. Absolute path (from the root) root tmp home bin directory rm ls cd poteaux toto.txt file $ emacs ~/CS/slides-shell.tex file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  20. Relative path (from the current directory) root tmp home bin directory rm ls cd poteaux toto.txt file $ cd file $ emacs CS/slides-shell.tex CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  21. Relative path (from the current directory) root tmp home bin directory rm ls cd poteaux toto.txt file $ cd file $ emacs CS/slides-shell.tex & $ cd CS ;$ emacs slides-shell.tex CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  22. Relative path (from the current directory) root tmp home bin directory rm ls cd poteaux toto.txt file $ cd /directory file $ /bin/rm file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  23. root tmp home bin directory rm ls cd poteaux toto.txt file $ cd /directory file $ rm file ( try echo $PATH) CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  24. Redirections root tmp home bin directory rm ls cd poteaux toto.txt file $ cd ; ls CS > /tmp/toto.txt file CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  25. Redirections root tmp home bin directory rm ls cd poteaux toto.txt file $ cd ; ls CS > /tmp/toto.txt file $ ls CS >> /tmp/toto.txt CS file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

  26. A graph organisation root tmp home bin directory .. rm ls cd poteaux toto.txt file . file link $ cd ; emacs ../poteaux/./link similar to CS $ emacs /directory/file file slides-shell.tex adrien.poteaux@univ-lille.fr Filesystem 15 / 17

Recommend


More recommend