38
Discussion Week 5 TA: Kyle Dewey

Discussion Week 5

Embed Size (px)

DESCRIPTION

Discussion Week 5. TA: Kyle Dewey. Overview. HW 3.10 and 6.2 review Binary formats System call execution in NACHOS Memory management in NACHOS I/O in NACHOS. Homework 3.10. - PowerPoint PPT Presentation

Citation preview

Page 1: Discussion Week 5

Discussion Week 5TA: Kyle Dewey

Page 2: Discussion Week 5

Overview

•HW 3.10 and 6.2 review

•Binary formats

•System call execution in NACHOS

•Memory management in NACHOS

•I/O in NACHOS

Page 3: Discussion Week 5

Homework 3.10

•“Identify the values of pid at lines A, B, C, and D. Assume that the actual pids of the parent and child are 2600 and 2603, respectively.”

Page 4: Discussion Week 5

Homework 6.2•The Cigarette-Smokers Problem

AgentAgent

Match Match SmokerSmoker

Paper Paper SmokerSmoker

Tobacco Tobacco SmokerSmoker

TableTable(holds two of three (holds two of three

items)items)

Page 5: Discussion Week 5

Problem Specifics

•Agent places two items

•Smoker with remaining item grabs the two and smokes

•The process repeats

Page 6: Discussion Week 5

Java Implementation

Page 7: Discussion Week 5

Binary Formats

Page 8: Discussion Week 5

NOFF

•NACHOS Object File Format

Magic Number Magic Number (0xBADFAD)(0xBADFAD)

Code (Text)Code (Text)

Initialized Data Initialized Data (Data)(Data)

Uninitialized Uninitialized Data (BSS)Data (BSS)

Page 9: Discussion Week 5

Why Bother?

•CPU sees only a stream of instructions

•All gets loaded into memory anyway

Page 10: Discussion Week 5

Advantage

•Tells OS roughly how portions will be used

•Optimizations possible

•Share (reentrant) code and constant data

•Prevent execution of non-code regions

Page 11: Discussion Week 5

System Calls Revisited

Page 12: Discussion Week 5

System Call Execution

read()read()

User Space

Kernel Space

read()read() readFromDisreadFromDisk()k()

read()read()

Page 13: Discussion Week 5

User -> Kernel

•Problem: kernel space and user space are enforced by hardware

•Hardware must be informed of jump

Page 14: Discussion Week 5

Solution?

•Instruction to specify the level

•By necessity, it is privileged

•Need kernel space to tell the system we’re in kernel space - catch 22

Page 15: Discussion Week 5

Existing Machinery•Interrupts are serviced by the kernel

•Generated from other devices, often I/O

•Preempt all else and enter the kernel

•The routines that service interrupts are called “interrupt service routines” - ISRs

Page 16: Discussion Week 5

Interrupts

Hard Hard DriveDrive

ISR 1ISR 1

ISR 2ISR 2

ISR 3ISR 3

Int 1

Memory

CPUCPU

Page 17: Discussion Week 5

Using Interrupts•Trigger a “software interrupt”

•Kernel mode entered synchronously

•Parameters can be passed in registers, in a specific memory location, etc.

•Note that the actual mechanism and lingo is hardware dependent

Page 18: Discussion Week 5

MIPS System Calls

•MIPS has the “syscall” instruction

•Processor throws a system call exception, triggering the OS’ system call service routine

•By convention, the syscall ID is in $v0, and arguments are passed in $a0 and $a1

Page 19: Discussion Week 5

MIPS System Calls•Assume we want the system call with ID

5

•This call takes no arguments

addi $v0, $zero, 5syscall CPUCPU Syscall Syscall

ISRISR

Syscall 5 Syscall 5 HandlerHandler

Page 20: Discussion Week 5

•code/userprog/exception.cc

•code/userprog/syscall.h

•code/test/start.s

Page 21: Discussion Week 5

Memory Management

Page 22: Discussion Week 5

Project #2 Memory

•Physical = virtual (until Project #3)

•Must using paging

•Need to allocate and free pages as requested

Page 23: Discussion Week 5

NACHOS Memory

•Does not have much

•128 byte pages

•32 pages total

•8 pages for each process’ stack + data + code

•Simple bitmap is sufficient to record what is and is not used

Page 24: Discussion Week 5

Contiguous Memory

•Since physical = virtual, served memory requests must be contiguous

•I.e. if a process requests 5 pages, they must be contiguous

•*Could* do compaction, but this is a terrible idea

Page 25: Discussion Week 5

Fork() Example

Used - P1Used - P1

FreeFree

FreeFree

FreeFree

Used - P2Used - P2

Used - P1Used - P1

FreeFree

FreeFree

Used - P2Used - P2

Used - P3Used - P3P1 fork()s P3

Memory by page Memory by page

Page 26: Discussion Week 5

Exit() Example

Used - P1Used - P1

FreeFree

FreeFree

Used - P2Used - P2

Used - P3Used - P3P2 exit()s

Memory by page Memory by page

Used - P1Used - P1

FreeFree

FreeFree

Used - P3Used - P3

FreeFree

Page 27: Discussion Week 5

Getting Pages

•Memory is available through:

•machine->mainMemory

•Merely array of 1 byte characters

•Need to split into pages on your own

Page 28: Discussion Week 5

Memory and Concurrency

•Multiple processes may request pages at the same time

•Only one may get any given page

•Synchronization primitives from Project #1 will have to be used

•Make sure they work correctly!

Page 29: Discussion Week 5

I/O Syscalls

Page 30: Discussion Week 5

NACHOS Disk

•Do not need to worry about this until Project 3

•I/O syscalls for Project 2 utilize Linux’s existing syscalls for file I/O directly

Page 31: Discussion Week 5

I/O Syscalls

•Actually implement Read() and Write(), NOT readAt() and writeAt()

•readAt() and writeAt()’s provided implementations are sufficient to implement Read() and Write()

Page 32: Discussion Week 5

Files and Concurrency

•Process A prints “Hello world!”

•Process B prints “Goodbye cruel world!”

Hello woGoodbye crld!ruel world!

Page 33: Discussion Week 5

Files and Concurrency

•Determining what needs to be locked may be difficult

•May have separate things that need locking

•May need multiple locks for distinct resources

•Concurrent reads are OK, but not concurrent writes

Page 34: Discussion Week 5

Open File Semantics

•Semantics of Fork() are that child processes inherit open files

•Read() and Write() can only manipulate open files

•If a process will not close its files upon Exit(), then the OS must do so

Page 35: Discussion Week 5

Open Files

•Which files are opened must be recorded in the PCB

•This allows for all aforementioned behaviors

•Also allows for an offset for subsequent Read() and Write() requests

Page 36: Discussion Week 5

Console

•Read() and Write() may also manipulate the console

•Console is not opened or closed

•Constants specifying console usage are in syscall.h

Page 37: Discussion Week 5

Caveats

•The given code is really getting buggy

•Provided code is also getting really ugly

Page 38: Discussion Week 5

How-To Implement

•Project #2 has a step-by-step implementation guide at http://www.cs.ucsb.edu/~cs170/projects/homework_2guide.html

•Please read carefully