Вы находитесь на странице: 1из 34

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005 www.idc.ac.

il/tecs

Chapter 12:
Operating System
Usage and Copyright Notice:

Copyright 2005 © Noam Nisan and Shimon Schocken

This presentation contains lecture materials that accompany the


textbook “The Elements of Computing Systems” by Noam Nisan &
Shimon Schocken, MIT Press, 2005.

The book web site, www.idc.ac.il/tecs , features 13 such presentations,


one for each book chapter. Each presentation is designed to support
about 3 hours of classroom or self-study instruction.

You are welcome to use or edit this presentation for instructional and
non-commercial purposes.

If you use our materials, we will appreciate it if you will include in them a
reference to the book’s web site.
And, if you have any comments, you can reach us at tecs.ta@gmail.com

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 1
Where we are at:

Human Abstract design Software


abstract interface
Thought hierarchy
Chapters 9, 12
H.L. Language Compiler
& abstract interface
Chapters 10 - 11
Operating Sys.
Virtual VM Translator
abstract interface
Machine Chapters 7 - 8

Assembly
Language

Assembler

Chapter 6

abstract interface
Computer
Machine Architecture
abstract interface
Language
Chapters 4 - 5
Hardware Gate Logic
abstract interface
Platform Chapters 1 - 3 Electrical
Chips & Engineering
Hardware Physics
Logic Gates
hierarchy

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 2
Jack revisited

/**
/** Computes
Computes the
the average
average of
of aa sequence
sequence of
of integers.
integers. */
*/
class Main
class Main { {
function
function void
void main()
main() {{
var
var Array
Array a;
a;
var int length;
var int length;
var
var int
int i,
i, sum;
sum;

let
let length
length == Keyboard.readInt(”How
Keyboard.readInt(”How many
many numbers?
numbers? ”);
”);
let
let aa == Array.new(length);
Array.new(length); //
// Constructs
Constructs the
the array
array
let i =
let i = 0;0;

while
while (i(i << length)
length) {{
let
let a[i]
a[i] == Keyboard.readInt(”Enter
Keyboard.readInt(”Enter the
the next
next number:
number: ”);
”);
let sum = sum + a[i];
let sum = sum + a[i];
let
let ii == ii ++ 1;
1;
}}

do
do Output.printString(”The
Output.printString(”The average
average is:
is: ”);
”);
do Output.printInt(sum / length);
do Output.printInt(sum / length);
do
do Output.println();
Output.println();
return;
return;
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 3
Jack revisited

/**
/** Computes
Computes the
the average
average of
of aa sequence
sequence of
of integers.
integers. */
*/
class Main
class Main { {
function
function void
void main()
main() {{
var
var Array
Array a;
a;
var int length;
var int length;
var
var int
int i,
i, sum;
sum;

let
let length
length == Keyboard.readInt(”How
Keyboard.readInt(”How many
many numbers?
numbers? ”);
”);
let
let aa == Array.new(length);
Array.new(length); //
// Constructs
Constructs the
the array
array
let i =
let i = 0;0;

while
while (i(i << length)
length) {{
let
let a[i]
a[i] == Keyboard.readInt(”Enter
Keyboard.readInt(”Enter the
the next
next number:
number: ”);
”);
let sum = sum + a[i];
let sum = sum + a[i];
let
let ii == ii ++ 1;
1;
}}

do
do Output.printString(”The
Output.printString(”The average
average is:
is: ”);
”);
do Output.printInt(sum / length);
do Output.printInt(sum / length);
do
do Output.println();
Output.println();
return;
return;
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 4
Typical OS functions

Language extensions / standard library System-oriented services

„ Mathematical operations „ Memory management


(abs, sqrt, ...) (objects, arrays, ...)

„ Abstract data types „ I/O device drivers


(String, Date, ...)
„ Mass storage
„ Output functions
„ File system
(printChar, printString ...)
„ Multi-tasking
„ Input functions
(readChar, readLine ...) „ UI management (shell / windows)

„ Graphics functions „ Security


(drawPixel, drawCircle, ...)
„ Communications
„ And more ...
„ And more ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 5
The Jack OS

„ Math: Provides basic mathematical operations;

„ String: Implements the String type and string-related operations;

„ Array: Implements the Array type and array-related operations;

„ Output: Handles text output to the screen;

„ Screen: Handles graphic output to the screen;

„ Keyboard: Handles user input from the keyboard;

„ Memory: Handles memory operations;

„ Sys: Provides some execution-related services.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 6
Jack OS API
class
class Math
Math {{
function
function voidvoid init()
init()
Class String
Class String
function int {{
function int abs(int
abs(int x)
x)
constructor
constructor
function int String
String new(int
multiply(int new(int
x, maxLength)
maxLength)
function
Class int
Arraymultiply(int
{ x, int
int y)
y)
Class
method
method Array
void
void {dispose()
dispose()
function
function int
int divide(int
divide(int x,x, int
int y) y)
method
method
function int
function
intint length()
Array new(int
length() size)
function int min(int
function
class Array
min(int
Output
x,new(int
{{x,
int
int y)y) size)
method
methodclass
char
char Output
charAt(int
charAt(int j)
j)
function
function int
int max(int
max(int x,
x, int
int y)
y)
method
method function
method
void void
function
void void
dispose()
void moveCursor(int
dispose()moveCursor(int
setCharAt(int j, char i,
i, int
c) int j)j)
method
function
function intintvoid
sqrt(int
Class
sqrt(intsetCharAt(int
x)
Screenx) { j, char c)
Class Screen
function
function void {
void printChar(char
printChar(char c)
c)
}} method
method
} String
String appendChar(char
appendChar(char c)
c)
} function
functionfunction
void void
void clearScreen()
clearScreen()
printString(String s)
method
method voidfunction
void void printString(String
eraseLastChar()
class Memory
eraseLastChar() {{ s)
class
function
function Memory
void
void setColor(boolean
setColor(boolean b)
b)
method function
function
int void
void
intValue() printInt(int
printInt(int i)
i)
method int function intValue()
void drawPixel(int x,
function
function function
function
void
function
void void int
println()
int
println() peek(int
drawPixel(int
peek(int x, int
address)int y)
address) y)
method void
method voidfunction setInt(int
Class
setInt(int j)
Keyboard
j) {
Class
function voidKeyboard
void {
drawLine(int
drawLine(int x1,
x1, int
int y1,
y1,
function function
function
char void
void
backSpace()backSpace()
backSpace()
function
function char backSpace()
function void
void poke(intint
poke(int x2,
int x2, int
address, y2)
int
int y2)
address, int value)
value)
}} function
function char
char keyPressed()
keyPressed()
function char doubleQuote()
Class Sys
function char function
function
function
void
doubleQuote()
Class
void Sys {
drawRectangle(int
Array {
drawRectangle(int
alloc(int
x1,
x1, int
size) int y1,
y1,
function char function
newLine()
function Array
char alloc(int int
readChar() x2,
size) int
int x2, int y2) y2)
function char newLine() function charvoidreadChar()
function void function
drawCircle(int halt():
x,
}} function
function voidfunction
drawCircle(int
void deAlloc(Array x, int
void halt(): int y,
o) y, int
int r)
r)
function
function void deAlloc(Array
String o)
readLine(String message)
}} function
functionStringvoid readLine(String
error(int message)
errorCode)
}} function void error(int errorCode)
function
function int
int readInt(String
readInt(String message)
message)
function void wait(int
function void wait(int duration) duration)
}}
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 7
A typical OS:

„ Is modular and scalable

„ Empowers programmers (language extensions)

„ Empowers users (file system, GUI, ...)

„ Closes gaps between software and hardware

„ Runs in “protected mode”

„ Typically written in some high level language

„ Typically grow gradually, assuming more and more functions

„ Must be efficient.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 8
Efficiency first

„ We have to implement various operations on n-bit binary numbers


(n = 16, 32, 64, ...). Example: multiplication

„ Naïve algorithm: to multiply x*y: { for i = 1... y do sum = sum + x }

Run-time is proportional to y

In a 64-bit system, y can be as large as 264.

The multiplication will take years to complete

If the run-time were proportional to 64 instead, we are OK

„ In general, algorithms that operate on n-bit inputs are either:

z Naïve: run-time is prop. to the value of the n-bit inputs

z Good: run-time is proportional to n.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 9
Example I: multiplication

„ Run-time: proportional to n

„ Can be implemented in SW or HW

„ Division: similar idea.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 10
Example II: square root
„ The square root function has two useful properties:
z An inverse function that we know how to compute
z Monotonically increasing
„ Ergo, square root can be computed via binary search:

„ Number of loop iterations is bound by n/2, thus the run-time is O(n).

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 11
Math operations (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class
class Math
Math {{ class Sys {
class Sys {
function (…)
function (…)

function
function void
void init()
init() }
}

function
function int
int abs(int
abs(int x)
x)

3 function
function int
int multiply(int
multiply(int x,
x, int
int y)
y)

3 function
function int
int divide(int
divide(int x,
x, int
int y)
y)

function
function int
int min(int
min(int x,
x, int
int y)
y)

function
function int
int max(int
max(int x,
x, int
int y)
y)

3 function
function int
int sqrt(int
sqrt(int x)
x)

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 12
String processing (in the Jack OS)) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
Class
Class String
String {{ class Output {
class Screen {
class Screen {
constructor
constructor String
String new(int
new(int maxLength)
maxLength)
class Memory {
class Memory {
class Keyboard {
class Keyboard {
method
method void
void dispose()
dispose() class Sys {
class Sys {
function (…)
function (…)


}
method
method int
int length()
length() }

method
method char
char charAt(int
charAt(int j)
j)
method
method void
void setCharAt(int
setCharAt(int j,
j, char
char c)
c)
method
method String
String appendChar(char
appendChar(char c)
c)
method
method void
void eraseLastChar()
eraseLastChar()
method
method int
int intValue()
intValue()
method
method void
void setInt(int
setInt(int j)
j)
function
function char
char backSpace()
backSpace()
function
function char
char doubleQuote()
doubleQuote()
function
function char
char newLine()
newLine()
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 13
Converting a single digit to its ASCII code

„ ASCIICode(digit) == 48 + digit
„ Reverse conversion: easy.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 14
Converting a number to a string

„ SingleDigit–to-character conversions: done


„ Number–to-string conversions:

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 15
Memory management (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

class
class Memory
Memory {{
function
function int
int peek(int
peek(int address)
address)
function
function void
void poke(int
poke(int address,
address, int
int value)
value)
function
function Array
Array alloc(int
alloc(int size)
size)
function
function void
void deAlloc(Array
deAlloc(Array o)
o)
}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 16
Memory management (simple)
„ When a program constructs (destructs) an object, the OS has to allocate
(de-allocate) a RAM block on the heap:
z alloc(size): returns a reference to a free RAM block of size size
z deAlloc(object): recycles the RAM block that object points at

„ The data structure that


this algorithm manages
is a single pointer: free.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 17
Memory management (improved)

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 18
Peek and poke

class
class Memory
Memory {{
function
function int
int peek(int
peek(int address)
address)
function
function void
void poke(int
poke(int address,
address, int
int value)
value)
function
function Array
Array alloc(int
alloc(int size)
size)
function
function void
void deAlloc(Array
deAlloc(Array o)
o)
}}

„ Implementation: exploiting exotic casting in Jack:

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 19
Graphics primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

Class
Class Screen
Screen {{

function
function void
void clearScreen()
clearScreen()

function
function void
void setColor(boolean
setColor(boolean b)
b)

function
function void
void drawPixel(int
drawPixel(int x,
x, int
int y)
y)

function
function void
void drawLine(int
drawLine(int x1,
x1, int
int y1,
y1, int
int x2,
x2, int
int y2)
y2)

function
function void
void drawRectangle(int
drawRectangle(int x1,
x1, int
int y1,int
y1,int x2,
x2, int
int y2)
y2)

function
function void
void drawCircle(int
drawCircle(int x,
x, int
int y,
y, int
int r)
r)

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 20
Memory-mapped screen

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 21
Pixel drawing

„ Implementation: using poke(address,value)

screen refresh
program
driver mechanism

part of the screen part of the physical


application
operating system memory map hardware screen

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 22
Line drawing

„ dx=0 and dy=0 are not handled


„ Must also handle (dx,dy<0), (dx>0,dy<0), (dx<0,dy>0)
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 23
Line drawing

a/dx < b/dy is the same as a*dy < b*dx

And so we are back to addition …

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 24
Circle drawing

The screen
origin (0,0)
is at the top
left.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 25
Character output primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)

class
class Output
Output {{ }

}

function
function void
void moveCursor(int
moveCursor(int i,
i, int
int j)
j)

function
function void
void printChar(char
printChar(char c)
c)

function
function void
void printString(String
printString(String s)
s)

function
function void
void printInt(int
printInt(int i)
i)

function
function void
void println()
println()

function
function void
void backSpace()
backSpace()

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 26
Character output

„ Given: a physical screen, say 256 rows by 512 columns


„ We can allocate an 11 by 8 grid for each character
„ Hence, our output package should manage a 23 lines by 64 characters screen
„ Each displayable character must have a bitmap
„ In addition, we have to manage a “cursor”.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 27
A font implementation (in the Jack OS)

class
class Output
Output {{
static
static Array
Array charMaps;
charMaps;
function
function void initMap()
void initMap() {{
let
let charMaps
charMaps == Array.new(127);
Array.new(127);
//
// Assign aa bitmap
Assign bitmap for
for each
each character
character
do Output.create(32,0,0,0,0,0,0,0,0,0,0,0);
do Output.create(32,0,0,0,0,0,0,0,0,0,0,0); //
// space
space
do Output.create(33,12,30,30,30,12,12,0,12,12,0,0);
do Output.create(33,12,30,30,30,12,12,0,12,12,0,0); // ! // !
do
do Output.create(34,54,54,20,0,0,0,0,0,0,0,0);
Output.create(34,54,54,20,0,0,0,0,0,0,0,0); //
// ““
do
do Output.create(35,0,18,18,63,18,18,63,18,18,0,0); // ##
Output.create(35,0,18,18,63,18,18,63,18,18,0,0); //
...
...
do
do Output.create(48,12,30,51,51,51,51,51,30,12,0,0);
Output.create(48,12,30,51,51,51,51,51,30,12,0,0); // // 00
do
do Output.create(49,12,14,15,12,12,12,12,12,63,0,0); // 11
Output.create(49,12,14,15,12,12,12,12,12,63,0,0); //
do
do Output.create(50,30,51,48,24,12,6,3,51,63,0,0);
Output.create(50,30,51,48,24,12,6,3,51,63,0,0); // // 22
.. .. ..
do
do Output.create(65,0,0,0,0,0,0,0,0,0,0,0);
Output.create(65,0,0,0,0,0,0,0,0,0,0,0); //
// AA **
** TO
TO BE
BE FILLED
FILLED ****
do Output.create(66,31,51,51,51,31,51,51,51,31,0,0);
do Output.create(66,31,51,51,51,31,51,51,51,31,0,0); // B // B
do
do Output.create(67,28,54,35,3,3,3,35,54,28,0,0);
Output.create(67,28,54,35,3,3,3,35,54,28,0,0); //
// CC
.. .. ..
return;
return;
}} //
// Creates
Creates aa character
character mapmap array
array
function
function void create(int index, int
void create(int index, int a,
a, int
int b,
b, int
int c,
c, int
int d,
d, int
int e,
e,
int
int f,f, int
int g,
g, int
int h,
h, int
int i,
i, int
int j,
j, int
int k)
k) {{
var
var Array
Array map;
map;
let map = Array.new(11);
let map = Array.new(11);
let
let charMaps[index]
charMaps[index] == map;map;
let
let map[0] == a;
map[0] a;
let
let map[1]
map[1] == b;
b;
let
let map[2]
map[2] == c;
c;
...
...
let
let map[10]
map[10] == k;
k;
return; }
return; }

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 28
Keyboard primitives (in the Jack OS) class Math {
class Math {
class String {
class String {
class Array {
class Array {
class Output {
class Output {
class Screen {
class Screen {
class Memory {
class Memory {
class Keyboard {
class Keyboard {
class Sys {
class Sys {
function (…)
function (…)


}
}

Class
Class Keyboard
Keyboard {{

function
function char
char keyPressed()
keyPressed()

function
function char
char readChar()
readChar()

function
function String
String readLine(String
readLine(String message)
message)

function
function int
int readInt(String
readInt(String message)
message)

}}

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 29
Keyboard input

„ If the RAM address of the keyboard’s memory map is known,


can be implemented using a peek function
„ Problem I: the elapsed time between a “key press” and key release”
events is unpredictable
„ Problem II: when pressing a key, the user should get some visible
feedback (cursor, echo, ...).

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 30
Keyboard input (cont.)

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 31
Jack OS recap
Project 12:
class
class Math
Math {{
function
function
Class
void
void init()
String {init() Build it.
Class
function String {
function int abs(int x)
int abs(int x)
Class
Class Array
Array {{
function
function Array
Array new(int
new(int size)
size)
class
class Output
Output {{
method
method void
void dispose()
dispose()
Class
Class Screen
Screen {{
}}
class
class Memory
Memory {{
function
Class int
int peek(int address)
Class Keyboard
function {{
peek(int
Keyboard address)

Class
Class Sys
Sys {{
function
function void
void halt():
halt():
function
function void error(int errorCode)
void error(int errorCode)
function void wait(int duration)
function void wait(int duration)
}}

„ Implementation: similar to how GNU Unix and Linux were built:


„ Start with an existing system,
and gradually replace it with a new system,
one library at a time.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 32
Perspective
„ What we presented can be described as a:

z Mini OS

z Standard library

„ Many classical OS functions are missing

„ No separation between user mode and OS mode

„ Some algorithms (e.g. multiplication and division) are


standard

„ Other algorithms (e.g. line- and circle-drawing) can be


accelerated with special hardware

„ And, by the way, we’ve just finished building the computer.

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 33
The End

Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, www.idc.ac.il/tecs , Chapter 12: Operating System slide 34

Вам также может понравиться