Laboratorio di Calcolo I Docente: prof. Berardi (Dip. Informatica) Lezione introduttiva su Computing...

30
Laboratorio di Calcolo Laboratorio di Calcolo I I Docente: prof. Berardi (Dip. Informatica) Lezione introduttiva su Computing ed esercitazioni di Unix/Linux tenute da Fabrizio Bianchi (saro’ il vostro docente di Laboratorio di Calcolo II) Lezioni “teoriche” tutti insieme Esercitazioni individuali al computer

Transcript of Laboratorio di Calcolo I Docente: prof. Berardi (Dip. Informatica) Lezione introduttiva su Computing...

Laboratorio di Calcolo ILaboratorio di Calcolo I

• Docente: prof. Berardi (Dip. Informatica)

• Lezione introduttiva su Computing ed esercitazioni di Unix/Linux tenute da Fabrizio Bianchi (saro’ il vostro docente di Laboratorio di Calcolo II)

• Lezioni “teoriche” tutti insieme

• Esercitazioni individuali al computer

Textbook & SlidesTextbook & Slides

Trasparenze in:Trasparenze in:

www.to.infn.it/~bianchi/www.to.infn.it/~bianchi/LaboratorioCalcoloI/ LaboratorioCalcoloI/

*.ppt versione power point *.ppt versione power point *.pdf versione Acrobate Reader*.pdf versione Acrobate Reader

Per la stampa usare la versione pdfPer la stampa usare la versione pdf

What is Programming?What is Programming?

A sequence of statements that instruct a A sequence of statements that instruct a computer in how to solve a problem is computer in how to solve a problem is called a called a programprogram..

The act of designing, writing and The act of designing, writing and maintaining a program is called maintaining a program is called programmingprogramming..

People who write programs are called People who write programs are called programmersprogrammers..

What kinds of statementsWhat kinds of statementsdo computers do computers understand?understand?

A computer only understands A computer only understands machine language statements.machine language statements.

A machine language statement is a A machine language statement is a sequence of ones and zeros that sequence of ones and zeros that cause the computer to perform a cause the computer to perform a particular action, such as add, particular action, such as add, subtract, multiply, ...subtract, multiply, ...

Machine Language (ML)Machine Language (ML)

ML statements are stored in a computer’s ML statements are stored in a computer’s memory, which is a sequence of memory, which is a sequence of switches. switches.

For convenience of representation, For convenience of representation, an “on” switch is represented by 1, an “on” switch is represented by 1, and an “off” switch is represented by 0.and an “off” switch is represented by 0.

ML thus appears to be binary (base-2):ML thus appears to be binary (base-2):

00101110101101010010111010110101

Early ComputersEarly Computers

... required a programmer to write in ... required a programmer to write in ML...ML...– Easy to make mistakes!Easy to make mistakes!

– Such mistakes are hard to find!Such mistakes are hard to find!

– Not Not portableportable -- only runs on one kind of -- only runs on one kind of machine!machine!

Programming was very difficult! Programming was very difficult!

A Bright IdeaA Bright Idea

Devise a set of abbreviations (mnemonics) Devise a set of abbreviations (mnemonics) corresponding to the ML statements, plus corresponding to the ML statements, plus a program to translate them into ML.a program to translate them into ML.

The abbreviations are an The abbreviations are an assembly assembly languagelanguage, and the program is called an , and the program is called an assemblerassembler..

Assembler 0010111010110101ADD

Assembly LanguagesAssembly Languages

Allowed a programmer to use Allowed a programmer to use mnemonics, which were more mnemonics, which were more natural than binary.natural than binary.+Much easier to read programsMuch easier to read programs

+Much easier to find and fix mistakesMuch easier to find and fix mistakes

– Still not portable to different machinesStill not portable to different machines

High Level LanguagesHigh Level Languages

Devise a set of statements that are Devise a set of statements that are close to human language (if, while, close to human language (if, while, do, ...), plus a program to do, ...), plus a program to translate them into ML.translate them into ML.

The set of statements is called a The set of statements is called a high high level languagelevel language (HLL) and the (HLL) and the program is called a program is called a compilercompiler..

HLL CompilersHLL Compilers

Where an assembler translates one Where an assembler translates one mnemonic into one ML statement, mnemonic into one ML statement, a HLL compiler translates one HLL a HLL compiler translates one HLL statement into multiple ML statements.statement into multiple ML statements.

Compiler

1010110011110101

0000000000010000

0010111010110101

0000000000010010

0010111011111101

0000000000010100

z = x + y;

HLLsHLLs

High level languages (like C++) areHigh level languages (like C++) are+Much easier to read programsMuch easier to read programs

+Much easier to find and fix mistakesMuch easier to find and fix mistakes

+Portable from one machine to another Portable from one machine to another (so long as they keep to the language (so long as they keep to the language standard).standard).

Objectives in Objectives in ProgrammingProgramming

A program should solve a problem:A program should solve a problem:+correctlycorrectly (it actually solves the problem) (it actually solves the problem)

+efficientlyefficiently (without wasting time or space) (without wasting time or space)

+readablyreadably (understandable by another (understandable by another person)person)

+in a in a user-friendlyuser-friendly fashion fashion (in a way that is easy for its user to use).(in a way that is easy for its user to use).

SummarySummary

There are “levels” to computer There are “levels” to computer languages:languages:

– ML consists of “low” level binary ML consists of “low” level binary statements, that is hard to read, write, statements, that is hard to read, write, and not portable.and not portable.

– Assembly uses “medium” level Assembly uses “medium” level mnemonics: easier to read/write, but not mnemonics: easier to read/write, but not portable.portable.

– C++ is a “high” level language that is C++ is a “high” level language that is even easier to read/write, even easier to read/write, andand portable. portable.

Computer OrganizationComputer Organization

Hardware and Software

Computing SystemsComputing Systems

Computers have two kinds of Computers have two kinds of components:components:

• HardwareHardware, consisting of its physical , consisting of its physical devices (CPU, memory, bus, storage devices (CPU, memory, bus, storage devices, ...)devices, ...)

• SoftwareSoftware, consisting of the programs it , consisting of the programs it has (Operating system, applications, has (Operating system, applications, utilities, ...)utilities, ...)

Hardware: CPUHardware: CPU

Central Processing Unit (CPU):Central Processing Unit (CPU):– the “brain” of the machinethe “brain” of the machine

– location of circuitry that performs location of circuitry that performs arithmetic and logical ML statementsarithmetic and logical ML statements

– measurement: speed (roughly) in measurement: speed (roughly) in megahertz (millions of clock-ticks per megahertz (millions of clock-ticks per second)second)

– examples: Intel Pentium, AMD K6, examples: Intel Pentium, AMD K6, Motorola PowerPC, Sun SPARC, Motorola PowerPC, Sun SPARC,

Hardware: RAMHardware: RAM

Random Access Memory (RAM)Random Access Memory (RAM)– ““main” memory, which is fast, but main” memory, which is fast, but volatilevolatile......

– analogous to a person’s short-term memory.analogous to a person’s short-term memory.

– many tiny “on-off” switches: for conveniencemany tiny “on-off” switches: for convenience• ““on” is represented by 1, “off” by 0.on” is represented by 1, “off” by 0.

– each switch is called a each switch is called a bbinary diginary digitit, or , or bitbit..• 8 bits is called a 8 bits is called a bytebyte..• 221010 bytes =1024 bytes is called a bytes =1024 bytes is called a kilobytekilobyte (1K) (1K)• 222020 bytes is called a bytes is called a megabytemegabyte (1M). (1M).

Hardware (Disk)Hardware (Disk)

Secondary Memory (Disk):Secondary Memory (Disk):– Stable storage using magnetic or optical Stable storage using magnetic or optical

media.media.

– Analogous to a person’s long-term memory.Analogous to a person’s long-term memory.

– Slower to access than RAM.Slower to access than RAM.

– Examples: Examples: • floppy disk (measured in kilobytes)floppy disk (measured in kilobytes)

• hard disk (measured in gigabytes (2hard disk (measured in gigabytes (23030 bytes)) bytes))

• CD-ROM (measured in megabytes), ...CD-ROM (measured in megabytes), ...

Hardware: the BusHardware: the Bus

The Bus:The Bus:– Connects CPU to other hardware devices.Connects CPU to other hardware devices.

– Analogous to a person’s spinal cord.Analogous to a person’s spinal cord.

– Speed measured in megahertz (like the Speed measured in megahertz (like the CPU), but typically much slower than the CPU), but typically much slower than the CPU...CPU...

– The bottleneck in most of today’s PCs. The bottleneck in most of today’s PCs.

Hardware: CacheHardware: Cache

While accessing RAM is faster than While accessing RAM is faster than accessing secondary memory, it is still accessing secondary memory, it is still quite slow, relative to the rate at which quite slow, relative to the rate at which the CPU runs.the CPU runs.

To circumvent this problem, most systems To circumvent this problem, most systems add a fast add a fast cache memorycache memory to the CPU, to to the CPU, to store recently used instructions and data.store recently used instructions and data.

(Assumption: Since such instructions/data (Assumption: Since such instructions/data were needed recently, they will be were needed recently, they will be needed again in the near future.)needed again in the near future.)

Hardware: SummaryHardware: Summary

Putting the pieces together:Putting the pieces together:

CPU

Bus

MainMemory

SecondaryMemorycache

Programs are stored (long-term) in secondary memory, Programs are stored (long-term) in secondary memory, and loaded into main memory to run, from which the and loaded into main memory to run, from which the CPU retrieves and executes their statements.CPU retrieves and executes their statements.

Software: OSSoftware: OS

The The operating systemoperating system (OS) is loaded from (OS) is loaded from secondary memory into main memory when secondary memory into main memory when the computer is turned on, and remains in the computer is turned on, and remains in memory until the computer is turned off.memory until the computer is turned off.

RAM DiskCPUCache

Bus

OS

Software: OSSoftware: OS

The OS acts as the “manager” of the system, The OS acts as the “manager” of the system, making sure that each hardware device making sure that each hardware device interacts smoothly with the others.interacts smoothly with the others.

It also provides the interface by which the It also provides the interface by which the user interacts with the computer, and user interacts with the computer, and awaits user input if no application is awaits user input if no application is running.running.

Examples: MacOS, Windows-98, Windows-Examples: MacOS, Windows-98, Windows-NT, UNIX, Linux, Solaris, ... NT, UNIX, Linux, Solaris, ...

Software: ApplicationsSoftware: Applications

Applications are non-OS programs that Applications are non-OS programs that perform some useful task, including perform some useful task, including word processors, spreadsheets, word processors, spreadsheets, databases, web browsers, C++ databases, web browsers, C++ compilers, ...compilers, ...

Example C++ compilers/environments: Example C++ compilers/environments: – CodeWarrior (MacOS, Win95, WinNT, Solaris) CodeWarrior (MacOS, Win95, WinNT, Solaris)

– GNU C++ (UNIX, Linux)GNU C++ (UNIX, Linux)

– Turbo/Borland C++ (Win95, WinNT)Turbo/Borland C++ (Win95, WinNT)

– Visual C++ (Win95, WinNT)Visual C++ (Win95, WinNT)

Software: User ProgramsSoftware: User Programs

Programs that are neither OS programs Programs that are neither OS programs nor applications are called user nor applications are called user programs.programs.

User programs are what you’ll be writing User programs are what you’ll be writing in this course.in this course.

Putting it all togetherPutting it all together

Programs and applications that are not running Programs and applications that are not running are stored on disk.are stored on disk.

RAM DiskCPUCache

Bus

OS App

Putting it all togetherPutting it all together

When you launch a program, the OS controls the When you launch a program, the OS controls the CPU and loads the program from disk to RAM.CPU and loads the program from disk to RAM.

RAM DiskCPUCache

Bus

OS AppA

ppOS

Putting it all togetherPutting it all together

The OS then relinquishes the CPU to the The OS then relinquishes the CPU to the program, which begins to run.program, which begins to run.

RAM DiskCPUCache

Bus

OS AppA

ppApp

The Fetch-Execute CycleThe Fetch-Execute Cycle

As the program runs, it repeatedly fetches the As the program runs, it repeatedly fetches the next instruction (from memory/cache), executes next instruction (from memory/cache), executes it, and stores any results back to memory.it, and stores any results back to memory.

RAM DiskCPUCache

Bus

OS AppAppA

pp

That’s all a computer does: fetch-execute-That’s all a computer does: fetch-execute-store, store, millions of times each secondmillions of times each second!!

SummarySummary

A computer has two kinds of components:A computer has two kinds of components:– Hardware: its CPU, RAM, Disk(s), ...Hardware: its CPU, RAM, Disk(s), ...

– Software, its OS, Applications, and User Software, its OS, Applications, and User Programs.Programs.