Operating Systems 2004 Project - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Operating Systems 2004 Project

Description:

For example: page fault. 10/5/09. 21. Project Assignment. 10/5/09. 22 ... The system uses Round-Robin scheduling: Swap current user program (thread) out ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 37
Provided by: newslabCs
Category:

less

Transcript and Presenter's Notes

Title: Operating Systems 2004 Project


1
Operating Systems 2004Project 4
  • Professor Tei-Wei Kuo
  • TAs Jen-Wei Hsieh
  • Yuan-Hao Chang
  • Yan-Cheng Lai

2
Agenda
  • Motivation
  • User Programs in Nachos
  • Relative Nachos Code for User Programs
  • Project Assignment
  • Project Implementation
  • Project Regulation and Submission

3
Motivation
  • Nachos keeps only one user program
  • Nachos does not provide paging

4
  • User Programs in Nachos

5
How Nachos Executes a User Program
If there is a user program
Object AddrSpace maps virtual page to physical
page in one to one (11) AddrSpaceAddrSpace()
pageTable new TranslationEntryNumPhysPages
Create a page table to run this program
Load program into physical memory
AddrSpaceLoad()
AddrSpaceExecute()1. Initial registers2. Load
page table registers3. Jump to the user program
Execute the program
kernel-gtmachine-gtRun()
For() OneInstruction(instr)//execute one
instruction kernel-gtinterrupt-gtOneTick()//Ad
vance timer tick
6
The Way to Map Address Space in Nachos
Physical page
Physical memory
Virtual page
Valid or not
User Program
0000
0
0
Offset 4
...
1
1
132
2
2
132128 4
3
3
132
4
4
132/128 1
5
5


127
127
Physical page size 128 Number of physical pages
128
7
Load User Program to Physical Memory
  • Read the header of the user program
  • The header format is NoffHeader defined in noff.h

Magic number
Magic number
Virtual address
The header of code segment
Offset in file
Segment size


Header
Vertual address
The header of read only data segment
User Program
Offset in file
Segment size
Code segment
Code and datasegments

8
Load User Program to Physical Memory (Cont.)
  • Load code and data segments into main memory
  • In Nachos, the segments are loaded into main
    memory with no offset.

Physical memory
0000
Code segment
Code segment
Initialized data segment
Initialized data segment
Read only data sement
Read only data segment

9
Execute One Instruction
  • MachineOneInstruction()
  • Use ReadMem() to read one instruction
  • It will call MachineTranslate() to get the
    physical address by checking the page talbe
    (AddrSpace table).
  • Read data from the address returned from
    MachineTranslate()
  • Parse the fetched instruction
  • Update PCReg and NextPCReg according the
    execution of the fetched instruction

10
  • Relative Nachos Code for User Programs

11
Nachos Page Table
  • The page table is
  • declared in machine/machine.h
  • Implemented as an array of TranslationEntry
    objects .
  • machine/translate.hclass TranslationEntry
  • public
  • int virtualPage // The page number in
    virtual memory. int physicalPage // The
    page number in real memory relative to the start
    of "mainMemory" bool valid bool
    readOnly bool use bool dirty

12
Related Files
  • The files you need to trace
  • /userprog/addrspace.cc, addrspace.h
  • /machine/translate.cc, translate.h
  • /machine/machine.cc, machine.h
  • /machine/mipssim.cc, mipssim.h
  • The files you may also need to modify while doing
    this projects
  • /threads/main.cc
  • /threads/thread.cc
  • /threads/scheduler.cc
  • /machine/interrupt.cc
  • /userprog/exception.cc

13
AddrSpace Class in translate.cc
  • AddrSpaceAddrSpace()
  • The AddrSpace constructor
  • Create an address space to run a user program
  • AddrSpaceAddrSpace()
  • The AddrSpace destructor
  • Deallocate the page table
  • bool AddrSpaceLoad (char fileName)
  • Load a user program into memory from a file
  • Nachos assumes virtual address equals to physical
    address
  • The user program is in NOFF format

14
AddrSpace Class in translate.cc (Cont.)
  • void AddrSpaceExecute()
  • Run a user program using the current thread
  • Initialize the register values of the simulated
    MIPS machine
  • this-gtInitRegisters()
  • Set page table register this-gtRestoreState()
  • Jump to the user program kernel-gtmachine-gtRun()

15
AddrSpace Class in translate.cc (Cont.)
  • void AddrSpaceInitRegisters()
  • Set the initial values for the user-level
    register set
  • void AddrSpaceRestoreState()
  • Tell the machine where to find the page table
    (pageTable)
  • Tell the machine number of pages needed by this
    user program (numPages)
  • void AddrSpaceSaveState()
  • Save any machine state for this address space on
    a context switch

16
Machine Class in translate.cc
  • bool MachineReadMem(int addr, int size, int
    value)
  • Read "size" (1, 2, or 4) bytes of virtual memory
    at "addr" into the location pointed to by
    "value".
  • If the address translation (from
    MachineTranslate()) is failed, it will raise an
    exception by invkoing RaiseException().
  • Arguments
  • add -- the virtual address to read from
  • size -- the number of bytes to read (1, 2, or 4)
  • value-- the place to write the result

17
Machine Class in translate.cc (Cont.)
  • bool MachineWriteMem(int addr, int size, int
    value)
  • Write "size" (1, 2, or 4) bytes of the contents
    of value into the virtual memory at location
    address "addr".
  • If the address translation (from
    MachineTranslate()) is failed, it will raise an
    exception by invkoing RaiseException().
  • Arguments
  • add -- the virtual address to to write to
  • size -- the number of bytes to written (1, 2, or
    4)
  • value-- the data to be written
  • Dirty bit would be set!!

18
Machine Class in translate.cc (Cont.)
  • ExceptionType
  • MachineTranslate(int virtAddr, int physAddr,
    int size, bool writing)
  • Translate a virtual address(virtAddr) into a
    physical address( physAddr), using current
    threads page table.
  • If error, return the type of the exception
  • Argument
  • "virtAddr" -- the virtual address to translate
  • physAddr" -- the place to store the physical
    address
  • "size" -- the amount of memory being read or
    written
  • "writing" -- if TRUE, check the "read-only" bit
    in the page table to see whether the process
    attempts to write a read only page.

19
Machine Class in Machine.h
  • Some important variable definitions
  • const int PageSize 128
  • The page size of the physical memory in Nachos
  • const int NumPhysPages 128
  • The number of pages in physical memory
  • char mainMemory
  • Physical memory to store the user program code
    and data, while it is executing.
  • TranslationEntry pageTable
  • Point to the page table of current thread

20
Machine Class in mipssim.cc
  • MachineOneInstruction(Instruction instr)
  • Execute one instruction from a user program
  • If there is any exception or interrupt, we return
    to kernel-gtmachine-gtRun() to invoke us again.
  • MachineRun() is a looping function
  • On a system call, the exception handler has to
    increment the program counter.
  • On an exception, we dont need to increment the
    program counter in order to restart this
    instruction again. For example page fault.

21
  • Project Assignment

22
Project Assignment
  • Multiprogramming and Virtual Memory (90)
  • Load multiple user programs to run on Nachos
    simultaneously
  • Implement a paging mechanism without considering
    page faults
  • Each user program starts at virtual address 0.
  • The system uses Round-Robin scheduling Swap
    current user program (thread) out in each timer
    interrupt, which occurs once every 100 ticks.
  • Bonus (10)
  • Consider the page fault and demand paging
  • Either use FIFO as the page-replacement strategy
  • Or design an arbitrary strategy (Remember to
    explain in your report)
  • When page fault occurs, dont need to swap out
    current user program.
  • It will be an extra bonus if you DO swap out
    current user program while handling demand
    paging.

23
Load Process without Page Faults
Physical page
Virtual page
0
0
1
1
2
2
3
3
Page table 1
4
5
6
7
User program 1
Physical page
Virtual page
0
4
1
5
2
6
User program 2
3
7
Main Memory
Page table 2
24
Load Process with Page Faults
Use FIFO strategy to replace page
Physical page
Virtual page
When program 2 fetch instruction at address 258,
which is located at page 2 (258 / 128 2 2)
0
0
i
1
1
2
2
Invalid
3
3

125
125
4
Page table 1
5
Page fault occurs- Start on-demand paging
6
User program 1

7

Physical page
Virtual page
125
0
126
1
127
126
2
i
0
User program 2
3
i
127
valid
Page table 2
Main Memory
Invalid
25
Loading Procedure
Program A
Program B
Nachos
Create thread A with its own page table
Create thread A with its own page table
Load program A into main memory and setup thread
As page table in AddrSpaceLoad()
Load program B into main memory and setup thread
Bs page table in AddrSpaceLoad()
Put thread A and B into readyList
System start kernel-gtmachine-gtRun()
Switch current thread
Load the demand page
run user programs (an infinitive loop)
Timer interrupt occurs
Page fault
26
  • Project Implementation

27
Enable Loading Multiple User Programs
  • Modify main.cc to let it enable loading multiple
    user programs from parameters.
  • Ex gt ./nachos x a b //two user programs a
    and b
  • Create one thread for each user program
  • Modify AddrSpaceLoad() to let it
  • Create a page table while loading a user program
  • Load a user program into memory
  • Configure its own page table for that user
    program in each invocation.

28
Handle Page Fault
  • In exception.cc, you have to add the
    PageFaultException case to load the demand page.
  • If the replaced page has been modified or is
    dirty, write it back.
  • In this project, you dont need to write any page
    back since no page will be modified in the test
    user programs.
  • Dont advance the program counter while handling
    page faults.
  • The system needs to execute the instruction that
    causes the page fault.

29
Save Necessary Info. on a Context Switch
  • You dont need to implement context switch by
    yourself and dont need to save or load the
    machines state, but need to load and store some
    extra information by yourself.
  • In ThreadRun() of scheduler.cc, the system
  • Calls oldThread-gtSaveUserState() and
    oldThread-gtSaveState() to save the state of the
    old thread, and
  • Calls oldThread-gtrestoreUserState() and
    oldThread-gtspace-gtRestoreState() to restore the
    resumed thread.
  • Therefore, you can load and store your extra
    information in the above four functions.

30
Sample Programs
a.c
b.c
int main() int i for(i1i)
printf("da000\n",i) printf("da001\n",i) p
rintf("da002\n",i) printf("da003\n",i) print
f("da004\n",i) printf("da005\n",i) printf("
da006\n",i) printf("da007\n",i) printf("da00
8\n",i) printf("da009\n",i) printf("da010\n"
,i)
int main() int i for(i1ilt10i)
printf("db000\n",i) printf("db001\n",i) p
rintf("db002\n",i) printf("db003\n",i) print
f("db004\n",i) printf("db005\n",i) printf("
db006\n",i) printf("db007\n",i) printf("db00
8\n",i) printf("db009\n",i) printf("db010\n"
,i) Halt()
31
Sample Execution and Sample Output
  • gt ./nachos x a b
  • Output

Start running user programs/threads ContextSwi
tch currentmain selecteda 1a000 1a001 1a002 1a00
3 1a004 1a005 1a006 1a007 1a008 ContextSwitch
currenta selectedb 1b000 1b001 1b002 1b003 1b004
1b005 1b006 1b007 1b008 ContextSwitch
currentb selecteda 1a009 1a010 ...
Loading program a Number of pages 14 Size of
code segment 656 Virtual address of code
segment 0 Size of readonly data segment
96 Virtual address of readonly data segment
656 Loading program b Number of pages 14 Size
of code segment 656 Virtual address of code
segment 0 Size of readonly data segment
96 Virtual address of readonly data segment
656
32
Another Example with Page Faults
a.c
b.c
int main() int i1 printf("da000\n",i)
printf("da001\n",i) printf("da002\n",i) prin
tf("da003\n",i) printf("da004\n",i) ... pr
intf("da445\n",i) printf("da446\n",i) printf
("da447\n",i) printf("da448\n",i) printf("d
a449\n",i) printf("da450\n",i) Halt()
int main() int i for(i1i)
printf("db000\n",i) printf("db001\n",i) p
rintf("db002\n",i) printf("db003\n",i) print
f("db004\n",i) printf("db005\n",i) printf
("db046\n",i) printf("db047\n",i) printf("d
b048\n",i) printf("db049\n",i) printf("db050
\n",i)
33
Sample Execution and Output with Page Faults
  • gt ./nachos x a b
  • Output

ContextSwitch currentmain nexta 1a000 1a001 1
a002 1a003 1a004 1a005 1a006 1a007 1a008 Contex
tSwitch currenta nextb PageFaultException at
virtual address2924, virtual page22 Loading
the demand page in physical page
0 1b000 1b001 1b002 1b003 1b004 1b005 1b006 1b007
1b008 ContextSwitch currentb
nexta 1a009 1a010 ...
Loading program a Number of pages 110 Size of
code segment 9408 Virtual address of code
segment 0 Size of readonly data segment
3616 Virtual address of readonly data segment
9408 Loading program b Number of pages 23 Size
of code segment 1424 Virtual address of code
segment 0 Size of readonly data segment
416 Virtual address of readonly data segment
1424 Invalid pages (pages not loaded) 18-22
34
  • Project Regulation and Submission

35
Notice
  • The sample user programs will be published on the
    course web page.
  • If you can pass the test for two user programs,
    you can get the standard score
  • If your program can pass the test of arbitrary
    user programs, you can get higher score.
  • Your output doesnt need to be exactly the same
    as the sample output, which is just for your
    reference.
  • Run your program
  • gt ./nachos x program1 program2
  • Handling page faults will get the 10 bonus.
  • You have implement the system call, printf, for
    the testing files

36
Submission
  • The files you should include
  • The whole nachos system without any object files.
  • Include the four sample test files, a.c, b.c,
    a_big.c, b_big.c
  • Your Makefile under /code/test directory should
    be able to compile the four sample files
  • All Nachos source files.
  • A report without page limitation
  • Pack the above files in a file named by your full
    student ID
  • Email your project to TA d93944006_at_ntu.edu.tw
    with the email title OS Project 4,
    YourName(FullStudentID)
  • For the delayed submission, deduct two points for
    each day
  • Deadline 2005/01/12 2400 The End
Write a Comment
User Comments (0)
About PowerShow.com