36
L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses access to special registers (like page table register) all IO devices, etc. whereas ordinary programs run in USER state only access to VIRTUAL addresses through page tables normally no access to IO devices Programs ask the OS for services (syscall) give me more memory read/write data from/to disk put pixel on screen give me the next character from the keyboard The OS may choose to “map” devices such as the screen into USER space

L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Page 1: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 1Comp 411– Spring 2008 4/24/08

Operating System

The OS is JUST A PROGRAMbut it runs in SUPERVISOR state

access to PHYSICAL addressesaccess to special registers (like page table

register)all IO devices, etc.

whereas ordinary programs run in USER stateonly access to VIRTUAL addresses through

page tablesnormally no access to IO devices

Programs ask the OS for services (syscall)give me more memoryread/write data from/to diskput pixel on screengive me the next character from the keyboard

The OS may choose to “map” devices such as the screen into USER space

Page 2: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 2Comp 411– Spring 2008 4/24/08

Shell

You normally interact with a SHELLIt provides the command prompt, or GUI

(graphical user interface)It is JUST A PROGRAMIt runs in USER state just like your programsIt interprets your mouse clicks or typed

commands and asks the OS to implement your requests

Suppose you “double-click” on a program iconWhat happens?

Page 3: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 3Comp 411– Spring 2008 4/24/08

Program Startup in SHELL

First the SHELL finds the file (using FILE SYSTEM in OS) indicated by the icon

It checks some permissions and suchFinally it calls the EXEC system call with the

file name and possibly some argumentsNow the OS takes over

Page 4: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 4Comp 411– Spring 2008 4/24/08

OS Exec

The OS keeps a PROCESS TABLE of all running programsdisk location of executablememory location of page tablesprioritycurrent status (running, waiting ready, waiting on

an event, etc.)PID (process ID) a number assigned to the process

A PROCESS is an independent program running in its own memory space

The OS allocates a new entry in the PROCESS TABLE

And sets up the PAGE TABLE for the new process

Page 5: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 5Comp 411– Spring 2008 4/24/08

Initial Page Table

foo

foo

swap

0x00000000 0 text segment

0x00001000 0 text segment

0x00002000 1 data segment

0x00003000

0x00004000

0x00005000

0xffffe000

0xfffff000 1 stack

memory

disk

page table

Page 6: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 6Comp 411– Spring 2008 4/24/08

Program StartupNow everything is ready

The PROCESS TABLE entry has been set upThe PAGE TABLE for the process has been initializedThe TEXT SEGMENT is out on diskThe DATA SEGMENT is in memoryThe STACK SEGMENT has been allocated 1 PAGE

The OS is ready to take the leap of faithONLY ONE program runs at a timeWhen your program is running the OS is notTo run your program and maintain control the

OS must trust that is will eventually get control againwhen the program asks for a service

when the program does something illegal

when a timer goes off

Page 7: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 7Comp 411– Spring 2008 4/24/08

Page Fault in the Text

When we branch to the beginning of “main” we get a page fault

So the OS copies the first page of the TEXT of main to a free page in memory

Page 8: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 8Comp 411– Spring 2008 4/24/08

Page Fault in the Text

foo

foo

swap

0x00000000 1 text segment

0x00001000 0 text segment

0x00002000 1 data segment

0x00003000

0x00004000

0x00005000

0xffffe000

0xfffff000 1 stack

memory

disk

page table

Page 9: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 9Comp 411– Spring 2008 4/24/08

Allocate a block of memory

Now suppose the first thing our program needs to do is get 6k of memory for an array

The program uses “new” to make an arrayDown inside “new” it calls “malloc”Down inside “malloc” it uses a system call

to ask the OS for memoryThe OS will have to find 2 pages to hold 6k

Page 10: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 10Comp 411– Spring 2008 4/24/08

Allocate a block of memory

foo

foo

swap

0x00000000 1 text segment

0x00001000 0 text segment

0x00002000 1 data segment

0x00003000 1 heap

0x00004000 1 heap

0x00005000

0xffffe000

0xfffff000 1 stack

disk

page table

Page 11: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 11Comp 411– Spring 2008 4/24/08

Fault in the other page of TEXT

foo

foo

swap

0x00000000 1 text segment

0x00001000 1 text segment

0x00002000 1 data segment

0x00003000 1 heap

0x00004000 1 heap

0x00005000

0xffffe000

0xfffff000 1 stack

memory

disk

page table

Page 12: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 12Comp 411– Spring 2008 4/24/08

Grow the stack

Now our program needs more stack spacePerhaps it has to call a recursive function to

traverse a complex data structureOr perhaps the user declares an

“automatic” array likedouble work[1000];which needs 8000 bytes of memory

Page 13: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 13Comp 411– Spring 2008 4/24/08

Grow the stack

foo

foo

swap

0x00000000 1 text segment

0x00001000 1 text segment

0x00002000 1 data segment

0x00003000 1 heap

0x00004000 1 heap

0x00005000

...

0xffffd000 1

0xffffe000 1

0xfffff000 1 stack

memory

disk

page table

Page 14: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 14Comp 411– Spring 2008 4/24/08

Get partially paged out

Sometime later, some other program running on the system needs more memory

It asks the OSThe OS realizes that not enough physical

memory remains availableSo the OS chooses to PAGE OUT one page

from our programIt would choose one that hasn’t been used

for a whilelike possibly one of the heap segments

Page 15: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 15Comp 411– Spring 2008 4/24/08

Partially Paged Out

foo

foo

swap

0x00000000 1 text segment

0x00001000 1 text segment

0x00002000 1 data segment

0x00003000 0 heap

0x00004000 1 heap

0x00005000

...

0xffffd000 1

0xffffe000 1

0xfffff000 1 stack

memory

disk

page table

Page 16: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 16Comp 411– Spring 2008 4/24/08

Later we need that page

foo

foo

swap

0x00000000 1 text segment

0x00001000 1 text segment

0x00002000 1 data segment

0x00003000 1 heap

0x00004000 1 heap

0x00005000

...

0xffffd000 1

0xffffe000 1

0xfffff000 1 stack

memory

disk

page table

Page 17: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 17Comp 411– Spring 2008 4/24/08

Exit

Finally our program exitsIt calls the “exit” system call to notify the

OS that it is doneThe OS puts the memory back on the free

listCleans up the PAGE TABLE and PROCESS

TABLEAnd goes on about its business...

What does the OS do when no programs are ready?

Page 18: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 18Comp 411– Spring 2008 4/24/08

Interrupts

How does the CPU manage SLOW I/O devices?

Programmed I/OInterrupt Driven I/O

Page 19: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 19Comp 411– Spring 2008 4/24/08

Polling

Advantages

Simple

No surprises

Processor in full control

Disadvantages

Polling can waste lots of time

Page 20: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 20Comp 411– Spring 2008 4/24/08

Interrupt Driven I/O

Advantage

CPU only bothered when actually needed

Disadvantage

Can occur at surprising or inconvenient times

Have to save and restore state

Page 21: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 21Comp 411– Spring 2008 4/24/08

MIPS ExceptionsReset Hardware Errors (Bus Error, Cache Error)External Interrupt (6 inputs)Address ErrorReserved InstructionTLB MissSystem CallBreakpointTrapInteger OverflowFloating Point ErrorTimerAnd a few more

Page 22: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 22Comp 411– Spring 2008 4/24/08

Exception Processing

EPC gets address of faulty instruction or of next instruction depending on type of exception

Switch to kernel modeJump to a new location based on type of

exceptionPC FFFF FFFF BFC0 0000 for ResetPC FFFF FFFF BFC0 0300 for Hardware errorPC FFFF FFFF BFC0 0380 for external interruptsPC FFFF FFFF BFC0 0400 for …

Save registersExamine the “cause” register to find out why

you came hereBranch to code to do the right thing

Page 23: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 23Comp 411– Spring 2008 4/24/08

Magnetic Disk

Long term, nonvolatile storageLarge, inexpensive, and slow

Rotating platter(s) coated with magnetic material

Use a movable read/write head to accessWhen magnetized region zips past coils in

head, a tiny signal is producedForce current through coils to generate

magnetic field to magnetize tiny regions on the disk

Use feedback to keep the head in the right place

Page 24: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 24Comp 411– Spring 2008 4/24/08

Outside

Page 25: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 25Comp 411– Spring 2008 4/24/08

Inside

Page 26: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 26Comp 411– Spring 2008 4/24/08

Platters and Heads

Page 27: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 27Comp 411– Spring 2008 4/24/08

Magnetic Disk Organization

• Cylinder: All tracks under head with arm in a fixed position

• Read/Write time has 3 components

•Seek time to move the arm

•Rotational latency: wait for the desired sector to come by

•Transfer time: transfer bits

Page 28: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 28Comp 411– Spring 2008 4/24/08

Typical Disk Times

Average Seek: 8ms to 12msSum of all possible seek / number of possible seeksLocality reduces this to maybe only 25% of average

number

Rotational Latency:At 5400 RPM 11 msAt 7200 RPM 8 msAt 10000 RPM 6ms

Transfer time depends on:Transfer size (typical 512 bytes)Rotation speedRecording densityDiameterTypical values: 10 to 40MBytes per second

Page 29: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 29Comp 411– Spring 2008 4/24/08

CD

Page 30: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 30Comp 411– Spring 2008 4/24/08

CRT Display

Page 31: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 31Comp 411– Spring 2008 4/24/08

LCD

Page 32: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 32Comp 411– Spring 2008 4/24/08

Graphics Cards

Page 33: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 33Comp 411– Spring 2008 4/24/08

Polygons to Surfaces• Numerical coordinates specify vertex positions in 3D

• Matrix multiply transforms 3D coordinates to eye coordinates

• Divide projects 3D to 2D in perspective

• Pixel processors fill polygons with appropriate colors based on lighting model

Page 34: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 34Comp 411– Spring 2008 4/24/08

Anti-aliasing

Page 35: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 35Comp 411– Spring 2008 4/24/08

Sound

Sound is variations in air pressure

A microphone converts these into an analog electrical signal

An analog-to-digital converter samples this at frequent intervals

The resulting numbers are stored in a file (.wav)

On playback a digital-to-analog converter changes these numbers into an analog electrical signal

And the moving cone of a speaker converts this into varying air pressure

Page 36: L21 – OS and IO 1 Comp 411– Spring 2008 4/24/08 Operating System The OS is JUST A PROGRAM but it runs in SUPERVISOR state access to PHYSICAL addresses

L21 – OS and IO 36Comp 411– Spring 2008 4/24/08

MP3?

The sequence of numbers representing typical sounds is VERY redundantThe next value is closely related to the previousValues aren’t random cause we don’t like noise

Extract this redundancy to get compressionLossy compression: Throw less important info away

cause listener won’t notice