Introduction to Nachos Scheduling - KU ITTC · 3 Nachos Overview Nachos is an operating system...

Post on 25-Mar-2020

13 views 0 download

Transcript of Introduction to Nachos Scheduling - KU ITTC · 3 Nachos Overview Nachos is an operating system...

1

Introduction to Nachos Scheduling

Michael Jantz

Dr. Prasad Kulkarni

2

Introduction● In this lab, we will become acquainted with the

Nachos simulator, and make a slight change to its scheduling semantics.

● Untar the Nachos tar file that came with this lab. Also, make it, and tag the source:– tar xvzf nachos.tar.gz

– cd nachos; make

– ctags -R

3

Nachos Overview● Nachos is an operating system simulation that runs in a

single Unix process.

● It implements a multi-process model using POSIX PThreads.

● The OS level activities are implemented as C++ code and execute as a normal process.

● User-level processes are each allocated a PThread and are interpreted and executed by the MIPS machine simulator.

– Unlike a real OS, Nachos doesn't run on bare hardware.

– To simulate the low-level function of interrupts, clocks, and registers, Nachos simulates a MIPS microprocessor.

4

Nachos Overview (cont.)

5

Executing a User Program● Use -x argument to specify a user program for Nachos to simulate, e.g:

– ./userprog/nachos -x ./test/nice_free

● When you start Nachos in this way, a thread for nice_free is allocated, the executable instructions of nice_free are loaded into its (simulated) address space, the MIPS simulator's program counter is initialized to the start address, and the process is simulated (one instruction at a time) using Machine::Run().

6

Switching to Kernel Mode● Main interpreter loop in Machine::Run():

for (;;) {OneInstruction(instr);interrupt->OneTick();

}

● Once a process has been loaded and is being simulated, its execution may be halted by a trap or interrupt.

● If instr is a Nachos system call, OneInstruction() will raise an exception to execute the system call code defined in Nachos, essentially simulating a trap into “kernel” mode.

● interrupt->OneTick() will advance the simulated clock by one tick and also call HandleIfDue() to handle all pending interrupts at that point.

7

A Multitasking Simulator● Nachos is a multitasking simulator. That is, it has a scheduler that allows

it to manage multiple processes at once.

● When a user program issues the Nachos-defined Fork() system call, the loop in Machine::Run will raise an exception, Nachos will create another PThread (through use of the host operating system's ABI), populate its address space appropriately, and update its scheduler's data structures appropriately (e.g. add the newly created process to the run queue).

● With multiple processes being simulated by Nachos, we need some way of determining which process gets to use simulated MIPS processor.

● Therefore, Nachos needs a scheduling algorithm to determine when the running process will be switched from and which ready process will be switched to.

8

Round Robin Scheduling Algorithm● By default, Nachos uses a Round Robin scheduling algorithm with a

fixed, unvarying quantum for each process.

● Before describing its implementation in detail, a few notes on some of the data structures Nachos is using:

– On initialization, Nachos creates a SchedTimerInterrupt interrupt, which is scheduled to become due every 100 simulated timer ticks (i.e. HandleIfDue(), called by interrupt->OneTick(), in the main interpreter loop returns true when the simulated clock has advanced 100 ticks since the last SchedTimerInterrupt). This is how Nachos implements its quantum.

– The scheduler maintains a FIFO queue of all processes in the ready list (not surprisingly, called the readyList). When processes become ready (e.g. when an I/O request completes, or when a new process has just been forked), they are appended to the end of the ready list.

– The thread currently using the CPU can be accessed anywhere in the Nachos “kernel” using the global currentThread pointer.

9

Round Robin Control Flow

Main Interpreter Loop

OneInstruction(instr);OneTick();

SchedTimerInterrupt

yieldOnReturn = true;yieldOnReturn

currentThread->yield();yieldOnReturn = false;

Thread::Yield()

scheduler->readyToRun(this)next = scheduler->FindNextToRun()

scheduler->Run(next)

if SchedTimerInterrupt due

if yieldOnReturn is true

return branch

return

10

Round Robin Details● When the SchedTimerInterrupt is handled, it will set the global

yieldOnReturn to true and return to the main loop.

● Next, currentThread invokes Yield() to rescind control of the MIPS simulator. In Yield():

– scheduler->readyToRun(this) places the currentThread on the readyList. For Round Robin, this function appends the currentThread to the end of the readyList.

– next = scheduler->findNextToRun() returns the next process to run as chosen by the scheduling algorithm. For Round Robin, this function simply chooses the thread at the front of the readyList.

– scheduler->run(next) actually performs the context switch. When this returns, the running process is no longer this, but the process chosen by the scheduler (next).

● Now, control returns to the main loop. The registers for the simulated MIPS processor will all point to values for the newly chosen process, and the next execution of OneInstruction(instr) simulates the next instruction of the newly chosen process.

11

Testing Round Robin● We can use the nice_free program in the test/ directory to test the behavior of the

Nachos scheduler.

● nice_free forks four processes and each child process prints a different character to stdout. Each process prints until some maximum limit is reached.

● The order of the characters printed to the screen tells us which process was chosen to run and a rough estimate of how long each process ran between context switches.

● Two important notes:

– The Nice() system call used in nice_free currently does nothing. You will implement this system call in the first Nachos assignment.

– nice_free uses a special Nachos system call - Echo() - to print values to the screen. Echo() is different than Write() in that it will not cause its calling process to block (system calls involving I/O usually do). Therefore, every process in this test will always either be running or on the readyList. This forces the scheduler to choose its next process from a readyList of length more than one.

12

Round Robin Sample Output

13

FCFS Scheduler● In today's lab, we will change Nachos to implement a First

Come, First Serve scheduler.

● If you don't remember the semantics of FCFS, review the slides from class.

● A few notes:

– FCFS is actually simpler than Round Robin and there are not very many good reasons to actually implement a FCFS scheduler in a real operating system.

– However, the purpose of this lab is to get you acquainted with the Nachos simulator and to start you thinking about how a real scheduler might work.

14

Suggestions● Much of the time in real operating systems programming is spent trying to

understand how components of the system work together and where a change should be made.

● In this sense, this lab and your assignments in class will be very similar to “real” operating systems programming.

● Use gdb, grep, find and your tags file to navigate the source tree. e.g., running the following commands will print the stack trace of every function (with filename and line number of declaration) that was called before Nachos entered the Scheduler::Run routine:

-bash-3.2$ gdb ./userprog/nachos(gdb) b Scheduler::Run(gdb) r -x ./test/nice_free(gdb) bt

● REMEMBER: This should not be a very large change. If you find yourself changing a lot of Nachos code, rethink your approach.

15

FCFS Sample Output