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

first steps on linux and programming
SMART_READER_LITE
LIVE PREVIEW

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/


slide-1
SLIDE 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

slide-2
SLIDE 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

slide-3
SLIDE 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

How we’ll work:

A few lectures, More practical work, 3 projects (50%), One written exam (50%),

Personal implication:

take good uses, practice programmation, read, search on the web . . .

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

slide-4
SLIDE 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

slide-5
SLIDE 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

slide-6
SLIDE 6

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 ob- ject 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-lille.fr Linux ? Programming ? 6 / 17

slide-7
SLIDE 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

slide-8
SLIDE 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

slide-9
SLIDE 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

slide-10
SLIDE 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

slide-11
SLIDE 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

slide-12
SLIDE 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

slide-13
SLIDE 13

Shell ?

interface between the user and the OS used to run commands / filters filter/command stdin, 0: standard input (default: keyboard) stdout, 1: standard output (default: screen) stderr, 2: error output (default: screen)

  • ther 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

slide-14
SLIDE 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

slide-15
SLIDE 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

slide-16
SLIDE 16

Filesystem

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-17
SLIDE 17

Absolute path (from the root)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ emacs /home/poteaux/CS/slides-shell.tex

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-18
SLIDE 18

Absolute path (from the root)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ emacs $HOME/CS/slides-shell.tex

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-19
SLIDE 19

Absolute path (from the root)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ emacs ~/CS/slides-shell.tex

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-20
SLIDE 20

Relative path (from the current directory)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd $ emacs CS/slides-shell.tex

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-21
SLIDE 21

Relative path (from the current directory)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd $ emacs CS/slides-shell.tex & $ cd CS ;$ emacs slides-shell.tex

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-22
SLIDE 22

Relative path (from the current directory)

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd /directory $ /bin/rm file

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-23
SLIDE 23

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd /directory $ rm file (try echo $PATH)

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-24
SLIDE 24

Redirections

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd ; ls CS > /tmp/toto.txt

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-25
SLIDE 25

Redirections

root bin home

directory

tmp

poteaux

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd ; ls CS > /tmp/toto.txt $ ls CS >> /tmp/toto.txt

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-26
SLIDE 26

A graph organisation

root bin home

directory

tmp

poteaux

.. . link

file CS file slides-shell.tex ls cd rm file toto.txt

$ cd ; emacs ../poteaux/./link similar to $ emacs /directory/file

adrien.poteaux@univ-lille.fr Filesystem 15 / 17

slide-27
SLIDE 27

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-lille.fr Miscellaneous 16 / 17

slide-28
SLIDE 28

Script: short definition

File containing shell commands. Interpret the file:

$ . foo (in the current shell), $ sh foo (in another shell),

. . .

Run the file 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-lille.fr Miscellaneous 17 / 17