A Browser-based Operating Systems Project

sixmileugliestInternet και Εφαρμογές Web

24 Ιουν 2012 (πριν από 5 χρόνια και 2 μήνες)

375 εμφανίσεις

A Browser-based
Operating Systems
Project
by Alan G. Labouseur
and the terrific
Fall 2009 OS students
Ready Queue
Goals
Planning the Work
Working the Plan
The Future
2
Ready Queue
Goals
Planning the Work
Working the Plan
The Future
3
Goals
Teach
classic
OS
concepts
User vs. Kernel [code | mode]
Memory Allocation, Management, and
Access Control
Context Switching for Multiprogramming
CPU Scheduling (preemptive and non-)
I/O (screen / keyboard, disk and file
system)
4
Goals
Teach
classic
OS
constructs
Mode bit
Base and Limit Registers
Process Control Blocks
Interrupts
System Queues
Device Drivers
5
Goals
Develop a
modern
OS
implementation
Latest Platform: Web Browsers
(and mobile devices, but they run Web
browsers too)
Web browsers’ de facto language:
Javascript
An OS written in Javascript, seriously?
6
Javascript?
Seriously, an OS written in Javascript.
RIA (Rich Internet Applications) are common today.
GMail, Yahoo Maps, LinkedIn, Facebook, Twitter, ...
Javascript is the backbone of cross-browser RIA
There are browsers for every platform
PC, Mac, Cell phones, Smart phones, Wii, Kindle, ...
Javascript apps can run anywhere, with no software to
install or configure, ever.
What about all the Systems Programming?
7
Javascript!
The ubiquitous “system language” of browsers
Smells like Java and C#, but...
Weakly typed (sadly)
Very (but not purely) Object-oriented in nature
prototypical inheritance (based on Self, from PARC)
polymorphic, some encapsulation
Somewhat (but not purely) Functional in nature
supports higher order functions as values,
parameters, and traditional subprograms
supports expressions
8
Javascript
Base and Limit Registers
Process Control Blocks
Interrupts
function

ProcessControlBlock
()
{
this.pid = _nextProcessID++;
this.state = PCB_NEW;
this.baseAddress =
0x0000
;
this.limitAddress =
0x0000
;
this.regPC =
0x00
;
this.regIR =
0x00
;
this.regAcc =
0x00
;
this.regX =
0x00
;
this.regY =
0x00
;
this.zero =
0
;

this.pages =
new

Array()
;

this.priority =
0
;
this.inMemory =
false
;
this.numBytes =
0
;

this.updateState = pcbUpdateState
this.updateRegs = pcbUpdateRegs;
this.givePage = pcbGivePage;

this.toString = pcbToString;
}
_hardwareClockId =

setInterval
(krnOnCPUClockPulse, CLOCK_INTERVAL);
function

simEnableKeyboardInterrupt
()
{
// Listen for key presses in the document
// and call the simulation processor, which
// will in turn call the os interrupt handler.
document.addEventListener("keydown",
simOnKeypress, false);
}
9
Javascript
System Queues
Device Drivers
function

krnBootstrap
() // Page 8.
{
// Initialize some global variables.
_KernelBuffers =
new

Array
();
_KernelInputQueue =
new
Queue();
_KernelInterruptQueue =
new
Queue();
_Console =
new
Console(); // HTML5 Canvas
_StdIn = _Console;
_StdOut = _Console;

// Create the process queues.
_KernelJobQueue =
new
Queue();
_KernelReadyQueue =
new
Queue();
_KernelWaitingQueue =
new
Queue();
function

Queue
()
{
// Properties
this.q =
new

Array
();
// Methods
this.getSize = qSize;
this.enqueue = qEnqueue;
this.dequeue = qDequeue;
this.toString = qToString;
}
. . .
function

DeviceDriverBase
()
{
// Base Attributes
this.version = "0.07";
this.status = "unloaded";
this.preemptable = false;
// Base Method pointers.
// Override in subclass.
this.driverEntry = null;
this.isr = null;
}
DeviceDriverKeyboard.prototype =
new
DeviceDriverBase;
function

DeviceDriverKeyboard
()
{
// Override the base method pointers.
this.driverEntry = krnKbdDriverEntry;
this.isr = krnKbdDispatchKeyPress;
}
10
Competitive Advantage
Platform differences kill products and
companies
WordPerfect: DOS, OS/2, or Windows? OS/2. Oops.
Lotus: Late with ‘1-2-3 for Windows’, gave market share
to Excel. OS/2 version was main attraction for IBM.
Mobile today: Symbian, J2ME, BREW, Objective-C, Palm,
or Windows Mobile?
There are
still
no games for the Mac.
11
Competitive Advantage
Fewer differences among various browsers’
implementation of Javascript than
Windows NT, 2000, XP, Vista, 7
Solaris, AIX, BSD, Linux (Red Hat, Ubuntu, Debian...)
Symbian, J2ME, BREW, Objective-C, Palm, or Windows
Mobile
12
Javascript . . .
provides all of the tools we need for systems
programming
with none of the headaches of
platform problems
installation
configuration
in a manner that’s supported on virtually
every modern device.
13
Ready Queue
Goals
Planning the Work
Working the Plan
The Future
14
Planning the Work
Issues
Javascript may be new to some students.
This is an Operating Systems class, NOT a Web
Programming class.
Browser-based programming may be new to some
students unfamiliar with “webby” issues like
event handling
style sheets
HTML5 Canvas element
No Eclipse or Visual Studio-like tool for Javascript
15
Planning the Work
Solution - The Initial Project: ALOSS
“Webby” issues handled
Canvas element in place
Javascript example code, cross-referenced to pages in
our text book.
Keyboard interrupt implemented
Canvas drawing (letter patterns) done
Class / Prototype pattern in use
Provides a resource and reference for the
entire semester.
16
Planning the Work
Solution - External Support
Class web site
Javascript syntax and Canvas resources
Software Engineering Radio podcast links
Project blog
The students can help each other
17
Planning the Work
Solution - Software Development Tools
Interactive debugging:
FireBug
Documentation:
JSDoc
Static Code Analysis:
JSLint
,
JSMeter
Google has an internal Javascript-to-Javascript
compiler that does type inference and type
checking, and emits minimized JS code. I want it.
Unit Testing:
FireUnit
,
YUI Test
,
JsUnit
Acceptance Testing:
Selenium
18
Planning the Work
Issue: This a HUGE development project
(That’s a good thing!)
Solution: Incremental Projects (iProjects)
Spread over the entire semester.
Each will lead into the next.
Mistakes made and shortcuts taken in one project will
continue to haunt them in all future projects. (Just
like in real life.)
19
Planning the Work
Given the initial project . . .
iP1 - Add some Kernel-level shell commands
iP2 - Run one user program in memory
iP3 - Run many user programs in memory (round-robin)
iP4 - Disk I/O with MS-DOS
*
Final Project - Many user programs, disk-based virtual
memory, FCFS and Priority CPU scheduling options
* No, not THAT one.
M
arist
S
tudent -
D
atabase-backed
O
perating
S
ystem
20
Planning the Work
Issue: Machine Language
What CPU instruction set?
Solution: Look to the classic 6502
Virtual 6502
for testing already available
Needs slight modification to support System Calls
Worked great in these (and other) machines:
21
6502a Instruction Set
22
Example Example
Description Op Code Mnemonic Assembly Disassembly
------------------------------------ ------- -------- ------------ -----------
Load the accumulator with a constant A9 LDA LDA #$07 A9 07
Load the accumulator from memory AD LDA LDA $0010 AD 10 00
Store the accumulator in memory 8D STA STA $0010 8D 10 00
Add (carry not implemented) 6D ADC ADC $0010 6D 10 00
Adds contents of an address to
the contents of the accumulator and
keeps the result in the accumulator
Load the X register with a constant A2 LDX LDX #$01 A2 01
Load the X register from memory AE LDX LDX $0010 AE 10 00
Load the Y register with a constant A0 LDY LDY #$04 A0 04
Load the Y register from memory AC LDY LDY $0010 AC 10 00
No Operation EA NOP EA EA
Break (which is really a system call) 00 BRK 00 00
Compare a byte in memory to the X reg EC CPX EC $0010 EC 10 00
Sets the Z (zero) flag if equal

Branch X bytes if Z flag = 0 D0 BNE D0 EF D0 EF
Increment the value of a byte EE INC EE $0021 EE 21 00
System Call FF SYS FF
#$01 in X reg = print the integer stored in the Y register.
#$02 in X reg = print the 00-terminated string stored at the address in
the Y register.
ML Sample Program
23
ML only
24
A9 03 8D 41
00 A9 01 8D
40 00 AC 40
00 A2 01 FF
EE 40 00 AE
40 00 EC 41
00 D0 EF A9
44 8D 42 00
A9 4F 8D 43
00 A9 4E 8D
44 00 A9 45
8D 45 00 A9
00 8D 46 00
A2 02 A0 42
FF 00
Entered into OS
and run
Ready Queue
Goals
Planning the Work
Working the Plan
The Future
25
Working the Plan - iP1
Goals
Familiarization with the initial project.
Practice Javascript programming by adding some
kernel-level commands to the OS.
Results
Everyone did well.
I saw some creative and unusual commands.
WhatsForDinner, BondJamesBond, Dungeon
Enhanced ‘sarcastic mode’ for cursing users
26
Working the Plan - iP2
Goal
Run one user program in memory
Results - Joe Bedell
Process Control Blocks (PCB)
CPU Simulation
27
Working the Plan - iP3
Goal
Run many user programs in memory (round-robin)
Results - Joe Casey
Memory management
Context switching
28
Working the Plan - iP4
Goal
Disk I/O with MS-DOS
Wait a second . . .
You can’t do that with Javascript, right?
Right. Well... sort of.
Web Services / JSONP
Google Gears
29
MS-DOS: Architecture
Web-based API for Disk Operations
(username, track, sector, block) --> data
Each student/user gets
1 disk with
4 tracks of
8 sectors of
8 blocks of
512 bytes =
65,536 bytes per student/user.
30
MS-DOS: Backing Store
SQL-Server on my Web Server
31
USE [3nfconsultingcom]
GO;
-- "Disk" table for the Operating Systems class.
-- 1 disk with 4 tracks of 8 sectors of 8 blocks of 512 bytes = 65,536 per username.
CREATE TABLE msdos
(
username nvarchar(20) NOT NULL,
track smallint NOT NULL,
sector smallint NOT NULL,
block smallint NOT NULL,
data nvarchar(512) NULL,
PRIMARY KEY CLUSTERED (username ASC, track ASC, sector ASC, block ASC)
)
-- Insert all the students' disk data.
insert into msdos (username, track, sector, block, data) values('bedell', 0, 0, 0, '');
insert into msdos (username, track, sector, block, data) values('bedell', 0, 0, 1, '');
insert into msdos (username, track, sector, block, data) values('bedell', 0, 0, 2, '');
.
.
.
MS-DOS: Web API
XML Web Service:
MSDOSmain
JSONP -
example code
on class web site
32
GetBlock -
GET /msdos.asmx/GetBlock?username=string&track=string&sector=string&block=string HTTP/1.1
SetBlock
GET /msdos.asmx/SetBlock?username=string&track=string&sector=string&block=string&data=string HTTP/1.1
UserDisk
GET /msdos.asmx/UserDisk?username=string HTTP/1.1
GetBlock:
http://www.3NFconsulting.com/msdos/GetBlock.aspx?u=username&t=track&s=sector&b=block
SetBlock:
http://www.3NFconsulting.com/msdos/SetBlock.aspx?u=username&t=track&s=sector&b=block&d=data
MS-DOS: Server C#
33
[WebMethod(Description = "Write raw block data given a username, track, sector, block number, and a string
of data <= 512 bytes long")]
public

string
SetBlock(
string
username,

int
track,

int
sector,

int
block,

string
data)
{

string
retCode = "0 SetBlock Failure";

string
query =
String
.
Format
("update MSDOS " +
"set data = '{0}' " +
"where username = '{1}' " +
" and track = {2} " +
" and sector = {3} " +
" and block = {4}", data, username, track, sector, block);

using
(SqlConnection conn =
new
SqlConnection(CONNECTION_STRING))

using
(SqlCommand sql =
new
SqlCommand(query, conn))
{
conn.Open();

int
rowsAffected = sql.ExecuteNonQuery();

if
(rowsAffected > 0)
{
retCode = "1 SetBlock Success";
}
else
{
// TODO: Note the SQL Error Message and return that.
}
}

return
retCode;
}
Working the Plan - iP4
Goal
Disk I/O with MS-DOS
Results - Rob Fama
I/O with JSONP
Synchronization
JSONP is asynchronous, but we need synchronous
reads and writes.
What will he do? What WILL he do?
34
Working the Plan - iP4
Goal
Disk I/O with MS-DOS
Results - Neal Tanner
I/O with Google Gears
35
Working the Plan - Final
Goals
Many user programs
Disk-based virtual memory
FCFS and Priority CPU scheduling options
Results - CJ McGregor
Swapping
ls
cp
36
Accomplishments
Flat file system
Character output
Load one program
no
no
no
no
37
PC-DOS and CP/M
Flat file system
Character output
Load one program
Load many programs
Process scheduling
Device drivers
Platform independent
OS Class
Ready Queue
Goals
Planning the Work
Working the Plan
The Future
38
The Future
Build an entire browser-based Systems
Programming Stack
Browser-based compiler
Working on that with my Compiler Design students
this semester.
Could explore software-isolated processes in the
future
Browser-based hardware simulation
Currently too general, needs details
To do: Perhaps I’ll teach Computer Organization
and Architecture some semester
39
The Future
Parallel and Concurrent Programming
The CPU simulation is a Javascript object
_cpu = new CPU();
We can easily create multiple CPU instances
_cpu1 = new CPU();
_cpu2 = new CPU();
Enhance the scheduler to handle processor affinity
(asymmetric) or load balancing (symmetric)
Easier said than done, but the point is that it’s a
very friendly environment in which to explore
multi-core programming on many levels.
40
The Future
Transactional Memory (TM)
Enhance the Memory Manager to provide for
concurrency via Transactional Memory
Provide for high-level code like
and enforce ACID (or at least ACI) properties in
memory (and enhance the file system for the
“D”urability.)
41
function

KeyManager
()
{

atomic
{

int
retVal = this.nextKeyVal;
this.nextKeyVal++;
}

return
retVal;
}
The Future
Talking to Beth Golub, the OS editor at John
Wiley & Sons, about inclusion as a project in
their Operating Systems Concepts textbook.
There is currently a simulation of some algorithms, but
nothing like I have presented here.
Reaching out to other OS text book
publishers/editors. (Pearson)
Maybe something Open Source or Creative
Commons?
Maybe something for SIG CSE?
42
Thank You
Questions?
Comments?
Advice?