computer fundamentals lecture 4 dr robert harle · 2013-11-08 · therefore systems soon shipped...

30
Computer Fundamentals Lecture 4 Dr Robert Harle Michaelmas 2013

Upload: others

Post on 15-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Computer FundamentalsLecture 4

Dr Robert Harle

Michaelmas 2013

Page 2: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

This Week

The roles of the O/S (kernel, timeslicing, scheduling)

The notion of threads

Concurrency problems

Multi-core processors

Virtual machines

Page 3: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

The Origins of the OS

A lot of the initial computer programs covered the same ground – they all needed routines to handle, say, floating point numbers, differential equations, etc. Therefore systems soon shipped with libraries: built-in

chunks of programs that could be used by other programs rather than re-invented.

Then we started to add new peripherals (screens, keyboards, etc). To avoid having to write the control code (“drivers”)

for each peripheral in each program the libraries expanded to include this functionality

Then we needed multiple simultaneous apps and users Need something to control access to resources...

Page 4: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Operating System

Hardware

Application

User

Page 5: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Operating System

Hardware

Operating System

Application

User

Page 6: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Operating System

Hardware

Operating System

Application

User

Application

User

Application

User

Page 7: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

OS Functions Abstracts hardware (allows you to write code to

e.g. access HDD and takes care of the different HDDs for you)

Schedules processes (necessary for multitasking: see later)

Allocates main memory (to individual processes) Provides library of useful functions (e.g. get system

time, load file, etc) Enforces security May provide libraries to create a GUI

Page 8: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Platforms Almost all significant programs make use of

the library functions in an OS (e.g. to draw a window)

Our machine code needs not only a specific instruction set, but also the relevant operating system (with its libraries) installed

So software is typically compiled for a specific platform: a (architecture, OS) pair x86/Windows ARM/Windows x86/Linux ARM/iOS X86/OSX

Page 9: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

The Kernel

The kernel is the part of the OS that runs the system Just software Handles process scheduling (see later) Access to hardware Memory management

Very complex software – when it breaks... game over.

Page 10: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Multitasking by Time-slicing

Modern OSes allow us to run many programs at once (“multitask”). Or so it seems. In reality a CPU time-slices: Each running program (or “process”) gets a

certain slot of time on the CPU We rotate between the running processes with

each timeslot This is all handled by the OS, which schedules the

processes. It is invisible to the running program.

A B C

time

A B C A B CD D

Process D started

Processes A,B,C running

Page 11: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Context Switching

Every time the OS decides to switch the running task, it has to perform a context switch

It saves all the program's context (the program counter, register values, etc) to (main) memory

It loads in the context for the next program Obviously there is a time cost associated with

doing this...

Page 12: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Relinquishing a Timeslot

Sometimes a process is stuck waiting for something to happen (e.g. data to be read from disk)

The process is “blocked” Should release (yield) its timeslot How can we know when to unblock it?

A B C A B C B CD D

A Blocksand yields

B

Page 13: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Interrupts

Modern systems support interrupts Just signals that something has happened.

An interrupt handler is associated with each interrupt

E.g. HDD raises an interrupt to say it's done getting data → scheduler unblocks the process

A B C A B C B CD D A

A blocksInterrupthandler

B

Page 14: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Choosing a Timeslot Size

The computer is more efficient: it spends more time doing useful stuff and less time context switching

The illusion of running multiple programs simultaneously is broken

Appears more responsive More time context switching means

the overall efficiency drops

Longer

Shorter

Page 15: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Threads

Sometimes a program need to do background tasks whilst still performing a foreground task

E.g. run an intensive computation but still process mouse events in case the user hits cancel.

Processes have threads: effectively sub processes that run and are scheduled independently

A1 A2 C

time

A1 A2 B1 B3 A1 A2B2 C

Page 16: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Processes vs Threads

Threads run independently but share memory

Process A

Thread 1

Process B

Thread 1

Thread 2

Memory

Page 17: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Multiple CPUs

Ten years ago, each generation of CPUs packed more in and ran faster. But: The more you pack stuff in, the hotter it gets The faster you run it, the hotter it gets And we got down to physical limits anyway!!

Some systems had multiple CPUs to get speed up

C

B

A

Page 18: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Multicore CPUs

Modern system contain chips with multiple cores: multiple CPUs in a single package

Connections shorter → faster Lower power

C

B

A1

A2

Page 19: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

The New Challenge

Two cores run completely independently, so a single machine really can run two or more applications simultaneously

BUT the real interest is how we write reliable programs that use more than one core or thread This is hard because they use the same

resources, and they can then interfere with each other

Those sticking around for IB CST will start to look at such concurrency issues in far more detail. We will just look at...

Page 20: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

c = c + 1;

c=5

c = c - 1;

Main memory

Thread 1 Thread 2

Page 21: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

LOAD cADD #1STORE c

c=5

LOAD cSUB #1STORE C

Main memory

Thread 1 Thread 2

Page 22: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

LOAD cADD #1STORE c

LOAD cSUB #1STORE C

Thread 1 Thread 2

556665

655

t

566

Thread 1Register

Thread 2Register

MainMemory

Page 23: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

LOAD c

ADD #1STORE c

LOAD cSUB #1STORE C

Thread 1 Thread 2

555446

544t

5

66

Thread 1 Thread 2Thread 1Register

Thread 2Register

MainMemory

Page 24: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

LOAD cADD #1STORE c

LOAD c

SUB #1STORE C

Thread 1 Thread 2

555664

5

44

t

566

Thread 1 Thread 2Thread 1Register

Thread 2Register

MainMemory

Page 25: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Race Conditions

When we have two or more threads sharing a piece memory the result can depend on the order of execution

→ “Race condition”

Hard to detect (non-deterministic) Hard to debug Generally just hard

Page 26: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Solving Race Conditions

LOAD cADD #1STORE c

Risky sets of operations like this one must be made atomic

i.e. no context switching once the code block is started

Not trivial → much of CST IB devoted to this

Page 27: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Aside: The Value of Immutability

If something is immutable, the race conditions go away since you can only read it → remember this for OOP

Page 28: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Virtual Machines

Go back 20 years and emulators were all the rage: programs on architecture X that simulated architecture Y so that programs for Y could run on X

Essentially interpreters, except they had to recreate the entire system. So, for example, they had to run the operating system on which to run the program.

PC operating system

Sega O/S

Game

Now computers are so fast we can run multiple virtual machines on them

Allows us to run multiple operating systems simultaneously!

Page 29: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Virtualisation

Virtualisation is the new big thing in business. Essentially the same idea: emulate entire systems on some host server

But because they are virtual, you can swap them between servers by copying state

And can dynamically load your server room!

Windows 7

Windows 7

Windows xp

Windows 7

Ubuntu

Windows 7

Windows 7

Ubuntu

Android

Heavy load Light load

Page 30: Computer Fundamentals Lecture 4 Dr Robert Harle · 2013-11-08 · Therefore systems soon shipped with libraries: built-in chunks of programs that could be used by other programs rather

Thanks for coming

These optional lectures were an experiment. I'd appreciate any feedback you have. Should they be repeated next year? Was the level about right? Too fast? Too

slow? Was there anything else you'd like to have

had covered? Anything you'd not bother with?