42
1 Project 5 Discussion

Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

  • Upload
    lytruc

  • View
    216

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

1

Project 5���Discussion

Page 2: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

2

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: ptr to Thread

class Thread ... myProcess: ptr to ProcessControlBlock

Thread regs

stackTop name

status systemStack

isUserThread userRegs myProcess

PCB pid

parentsPid status

myThread name

exitStatus addrSpace

fileDescriptor

Page 3: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

3

pid parentsPid

status myThread

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: Thread

Thread regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

Page 4: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

4

pid parentsPid

status myThread

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: Thread

pcb = ... ... = pcb.myThread.stackTop

Thread regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

Page 5: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

5

pid parentsPid

status myThread

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: Thread

pcb = ... ... = pcb.myThread.stackTop

var th: Thread th = pcb.myThread ... = th.stackTop

Thread regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

Page 6: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

6

pid parentsPid

status myThread

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: Thread

pcb = ... ... = pcb.myThread.stackTop

var th: Thread th = pcb.myThread ... = th.stackTop

var th: ptr to Thread th = & pcb.myThread ... = th.stackTop

Thread regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

Page 7: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

7

pid parentsPid

status myThread

Threads and Processes - Design Options

class ProcessControlBlock ... myThread: Thread

pcb = ... ... = pcb.myThread.stackTop

var th: Thread th = pcb.myThread ... = th.stackTop

var th: ptr to Thread th = & pcb.myThread ... = th.stackTop

Thread regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

But th.stack = ... is dangerous!

Page 8: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

8

pid parentsPid

status

Threads and Processes - Design Options

Another Option: • Get rid of class Thread altogether • Include all Thread fields in PCB regs

stackTop name

status systemStack

isUserThread userRegs

PCB

name exitStatus addrSpace

fileDescriptor

Page 9: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

9

Threads and Processes - Design Options

Multiple Threads per Process PCB

xxx xxx xxx

threadList xxx

Thread next xxx xxx xxx

Thread next xxx xxx xxx

Thread next xxx xxx xxx

Thread next xxx xxx xxx

null

Page 10: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

10

Threads and Processes - Design Options

Multiple Threads per Process PCB

xxx xxx xxx

threadList xxx

Thread next

myPCB xxx xxx

Thread next

myPCB xxx xxx

Thread next

myPCB xxx xxx

Thread next

myPCB xxx xxx

null

Page 11: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

11

Class Thread

fields regs: array [13] of int -- Space for r2..r14 stackTop: ptr to void -- Current system stack top ptr name: ptr to array of char status: int -- JUST_CREATED, READY, -- RUNNING, BLOCKED, UNUSED

initialFunction: ptr to function (int) initialArgument: int systemStack: array [SYSTEM_STACK_SIZE] of int isUserThread: bool userRegs: array [15] of int -- Space for r1..r15 myProcess: ptr to ProcessControlBlock

Page 12: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

12

Class Thread

methods Init (n: ptr to array of char) Fork (fun: ptr to function (int), arg: int) Yield () Sleep () CheckOverflow () Print ()

Page 13: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

13

Class ProcessControlBlock

fields pid: int -- The process ID parentsPid: int -- The pid of the parent of this process status: int -- ACTIVE, ZOMBIE, or FREE myThread: ptr to Thread -- Each process has one thread exitStatus: int -- The value passed to Sys_Exit addrSpace: AddrSpace -- The logical address space fileDescriptor: array [MAX_FILES_PER_PROCESS] of ptr to OpenFile

Page 14: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

14

Class ProcessControlBlock

methods Init () Print ()

Page 15: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

15

Class ThreadManager

fields threadTable: array [MAX_NUMBER_OF_PROCESSES] of Thread freeList: List [Thread] threadManagerLock: Mutex -- These synchronization objects

aThreadBecameFree: Condition -- apply to the "freeList"

Page 16: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

16

Class ThreadManager

methods Init () Print () GetANewThread () returns ptr to Thread FreeThread (th: ptr to Thread)

Page 17: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

17

Class ProcessManager

fields processTable: array [MAX_NUMBER_OF_PROCESSES] of ProcessControlBlock

processManagerLock: Mutex -- These synchronization objects

aProcessBecameFree: Condition -- apply to the "freeList"

freeList: List [ProcessControlBlock] aProcessDied: Condition -- Signalled for new ZOMBIEs

nextPid: int

Page 18: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

18

Class ProcessManager

methods Init () Print () GetANewProcess () returns ptr to ProcessControlBlock FreeProcess (p: ptr to ProcessControlBlock) -- TurnIntoZombie (p: ptr to ProcessControlBlock)

-- WaitForZombie (proc: ptr to ProcessControlBlock) returns int

Page 19: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

19

The “Stub” File System

Model of disk Sequence of sectors

The File System Sector zero contains the directory Only one directory (a “flat” file system)

Files: Name Starting sector Size (number of bytes) All sectors for a file are contiguous

Page 20: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

20

The “Stub” File System

directory sector

0 1 2 3 4 5 6 7

MyFile file1 UserProgram SwapFile

Page 21: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

21

The “diskUtil” Utility

Another BLITZ tool

Initialize the file system, create empty directory... diskUtil -i

List the directory of the BLITZ “DISK”... diskUtil -l

Copy a file from Unix to the BLITZ “DISK”... diskUtil -a UnixFileName BlitzFileName

Get help info... diskUtil -h

Page 22: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

22

File-Related Classes

Class “FileControlBlock” (FCB) Contains info kernel needs to read/write to a file Where on disk A buffer area to use Length of file Must have only one FCB per file

Class “FileManager” A monitor Where all the methods are

Page 23: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

23

Class FileControlBlock

fields fcbID: int numberOfUsers: int -- count of OpenFiles pointing here

startingSectorOfFile: int -- or -1 if FCB not in use sizeOfFileInBytes: int bufferPtr: ptr to void -- ptr to a page frame relativeSectorInBuffer: int -- or -1 if none bufferIsDirty: bool -- Set to true when buffer is modified

Page 24: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

24

Class FileControlBlock

methods Init () Print ()

Page 25: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

25

Class OpenFile

Contains: Ptr to the FCB for this file The “current position” Processes point to “OpenFile”s, not “FCB”s

Page 26: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

26

The Unix Open-File Model

PCB

PCB

PCB

PCB

PCB

PCB

PCB

PCB

OpenFile pos=52

OpenFile pos=17

OpenFile pos=350

FCB

Disk FCB

Page 27: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

27

File Descriptors

pid parentsPid

status

0 1 2 3

...

MAX-1

PCB

fileDescriptor

OpenFile pos=17

OpenFile pos=52

OpenFile pos=350

null

null

FCB

FCB

FCB

Page 28: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

28

Class OpenFile

fields currentPos: int -- 0 = first byte of file fcb: ptr to FileControlBlock -- null = not open numberOfUsers: int -- count of Processes pointing here

Page 29: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

29

Class OpenFile

methods Print () ReadBytes (targetAddr, numBytes: int) returns bool -- returns true if all okay ReadInt () returns int LoadExecutable (addrSpace: ptr to AddrSpace) returns int -- returns -1 if problems

Page 30: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

30

Class FileManager

fields fileManagerLock: Mutex

fcbTable: array [MAX_NUM_FILE_CONTROL_BLKS] of FileControlBlock

anFCBBecameFree: Condition fcbFreeList: List [FileControlBlock]

openFileTable: array [MAX_NUM_OPEN_FILES] of OpenFile

anOpenFileBecameFree: Condition openFileFreeList: List [OpenFile]

directoryFrame: ptr to void

Page 31: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

31

Class FileManager

methods Init () Print () FindFCB (filename: String) returns ptr to FileControlBlock -- null if errors

Open (filename: String) returns ptr to OpenFile -- null if errors

Close (open: ptr to OpenFile) Flush (open: ptr to OpenFile) SynchRead (open: ptr to OpenFile, targetAddr, bytePos, numBytes: int) returns bool SynchWrite (open: ptr to OpenFile, targetAddr, bytePos, numBytes: int) returns bool

Page 32: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

32

Class DiskDriver

fields DISK_STATUS_WORD_ADDRESS: ptr to int DISK_COMMAND_WORD_ADDRESS: ptr to int DISK_MEMORY_ADDRESS_REGISTER: ptr to int DISK_SECTOR_NUMBER_REGISTER: ptr to int DISK_SECTOR_COUNT_REGISTER: ptr to int semToSignalOnCompletion: ptr to Semaphore semUsedInSynchMethods: Semaphore diskBusy: Mutex

Page 33: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

DiskInterruptHandler

currentInterruptStatus = DISABLED!!-- print ("DiskInterruptHandler invoked!\n")!!if diskDriver.semToSignalOnCompletion!! diskDriver.semToSignalOnCompletion.Up()!!endIf !

33

Page 34: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

34

Class DiskDriver

methods Init () SynchReadSector (sectorAddr, numberOfSectors, memoryAddr: int)

StartReadSector (sectorAddr, numberOfSectors, memoryAddr: int,

whoCares: ptr to Semaphore) SynchWriteSector (sectorAddr, numberOfSectors, memoryAddr: int)

StartWriteSector (sectorAddr, numberOfSectors, memoryAddr: int, whoCares: ptr to Semaphore)

Page 35: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

Building a User-level Program

UserSystem.h UserSystem.c UserRuntime.s MyProgram.h MyProgram.c

%asm UserRuntime.s!%kpl UserSystem -unsafe!%asm UserSystem.s!%kpl MyProgram -unsafe!%asm MyProgram.s!%lddd UserRuntime.o UserSystem.o MyProgram.o

! ! ! ! ! ! -o MyProgram!

35

Page 36: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

Put Executable on the BLITZ Disk

% diskUtil –i!% diskUtil -a temp1 MyFileA!% diskUtil -a temp2 MyFileB!% diskUtil -a MyProgram MyProgram!% diskUtil –l! StartingSector SizeInSectors SizeInBytes FileName! ============== ============= =========== =====================! 0 1 8192 < directory >! 1 1 8192 MyFileA! 2 3 17000 MyFileB! 5 8 60264 MyProgram!

36 The disk is emulated with a Unix file. The filename (“DISK”) is implicit.

Page 37: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

The First User-Level Program

MyProgram.h header MyProgram!! uses UserSystem!! functions!! main ()!!endHeader!

MyProgram.c !code MyProgram!! function main ()!! print ("My user-level program is!! ! ! ! ! ! ! running!\n")!! Sys_Shutdown ()!! endFunction!!endCode! 37

Page 38: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

System Calls (Wrapper functions)

function Sys_Exit (returnStatus: int) function Sys_Shutdown () function Sys_Yield () function Sys_Fork () returns int function Sys_Join (processID: int) returns int function Sys_Exec (filename: String) returns int function Sys_Create (filename: String) returns int function Sys_Open (filename: String) returns int function Sys_Read (fileDesc: int, buffer: ptr to char, sizeInBytes: int)

returns int function Sys_Write (fileDesc: int, buffer: ptr to char, sizeInBytes: int)

returns int function Sys_Seek (fileDesc: int, newCurrentPosition: int) returns int function Sys_Close (fileDesc: int)

38

Page 39: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

Wrapper Function

function Sys_Read (fileDesc: int, buffer: ptr to char, sizeInBytes: int) returns int return DoSyscall (SYSCALL_READ, fileDesc, buffer asInteger, sizeInBytes, 0) endFunction

39

Page 40: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

DoSyscall

external DoSyscall (funCode, arg1, arg2, arg3, arg4: int) returns int

DoSyscall:!! !load ![r15+8],r1 !! Move arg1 into r1!! !load ![r15+12],r2!! Move arg2 into r2!! !load ![r15+16],r3!! Move arg3 into r3!! !load ![r15+20],r4!! Move arg4 into r4!! !load ![r15+4],r5 !! Move funcCode into r5!! !syscall r5! !! Do the syscall!! !store!r1,[r15+4] !! Move result from r1 onto stack!! !ret ! ! !! Return!

40

Page 41: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

Two copies (User, Kernel) must match!

enum SYSCALL_EXIT = 1, SYSCALL_SHUTDOWN, SYSCALL_YIELD, SYSCALL_FORK, SYSCALL_JOIN, SYSCALL_EXEC, SYSCALL_CREATE, SYSCALL_OPEN, SYSCALL_READ, SYSCALL_WRITE, SYSCALL_SEEK, SYSCALL_CLOSE 41

Page 42: Project 5 Discussion - Computer Action Teamweb.cecs.pdx.edu/~harry/os/slides/Project5.pdfxxx xxx xxx threadList xxx Thread next xxx xxx ... ptr to void "-- Current system stack top

Virtual Address Space

• A Page for “environment” data • Pages for the text segment • Pages for the data segment • Pages for the BSS segment • Pages for the user’s stack

Typical: 0 environment pages 2 text pages 1 data page 0 BSS pages 1 stack page

4 pages ! 32Kbyte Virtual Address Space 42