Operating Systems Nachos Project 2 Thread Scheduling Motivation

  • Slides: 21
Download presentation
Operating Systems Nachos Project 2 Thread Scheduling

Operating Systems Nachos Project 2 Thread Scheduling

Motivation & Objective Modern operating systems should have the ability to schedule multiple threads.

Motivation & Objective Modern operating systems should have the ability to schedule multiple threads. n Implement round-robin scheduling. n

Round-Robin Scheduling Process Burst Time A 5 B 10 time slice : 2 (ticks)

Round-Robin Scheduling Process Burst Time A 5 B 10 time slice : 2 (ticks) A B 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Thread Life Cycle in Nachos Kthread() new Kthread. fork() ready Note : Kthread. yield()

Thread Life Cycle in Nachos Kthread() new Kthread. fork() ready Note : Kthread. yield() invokes run. Next. Thread() to select next thread to run. Kthready() blocked (use a ready. Queue) Kthread. yield() Kthread. sleep () running Kthread. finish () finished

Preparation n Some files need to be modified. n Use proj 1 directory. n

Preparation n Some files need to be modified. n Use proj 1 directory. n In machine/Interrupt. java n Line 99, 100: ignore context-switch time //if (old. Status == false && status == true) // tick(true); n n Line 123: public void schedule() Line 136: public void tick()

n In threads/KThread. java n n n Line 400: Add Simple. Thread class Line

n In threads/KThread. java n n n Line 400: Add Simple. Thread class Line 428: Add self. Test 2() function In threads/Threaded. Kernel. java n Line 50: Add KThread. self. Test 2(); n Line 51: //Semaphore. self. Test(); n Line 52: //Synch. List. self. Test(); n You could download these form course website.

Interrupt Controller n nachos. machine. Interrupt class emulates low-level interrupt hardware. It maintains an

Interrupt Controller n nachos. machine. Interrupt class emulates low-level interrupt hardware. It maintains an event queue, clock. Clock ticks when tick() excutes. n tick() takes a boolean (false: 1 or true: 10 ticks) After any tick, the event queue is examined any pending interrupt events are serviced by invoking the device event handler associated with the event.

Threaded. Kernel. java n Initialize() n set scheduler n n start threading n n

Threaded. Kernel. java n Initialize() n set scheduler n n start threading n n nachos. threads. Round. Robin. Scheduler new KThread(null); self. Test() n n KThread. self. Test() KThread. self. Test 2() // added by yourself run() n terminate() n // create main thread

KThread. java All Nachos threads are instances of nachos. thread. Kthread. n If you

KThread. java All Nachos threads are instances of nachos. thread. Kthread. n If you want to create more threads. Create a java. lang. Runnable(), then make a Kthread, and call fork(). n

KThread. self. Test() n the Runnable class is Ping. Test private static class Ping.

KThread. self. Test() n the Runnable class is Ping. Test private static class Ping. Test implements Runnable { Ping. Test(int which) { this. which = which; } public void run() { for (int i=0; i<5; i++) { System. out. println( "*** thread " + which + looped “ + i + " times“ ); current. Thread. yield(); } } private int which;

public static void self. Test() { Lib. debug(dbg. Thread, "Enter KThread. self. Test"); new

public static void self. Test() { Lib. debug(dbg. Thread, "Enter KThread. self. Test"); new KThread(new Ping. Test(1)). set. Name("forked thread"). fork(); new Ping. Test(0). run(); } running thread: ready queue: main thread forked thread 1. 2. 3. 4. 5. 6. 7. run main thread create forked thread main thread run Ping. Test main thread yield next thread is forked thread run forked thread … Hint : You can trace this functions as the starting point.

public static void self. Test() { Lib. debug(dbg. Thread, "Enter KThread. self. Test"); new

public static void self. Test() { Lib. debug(dbg. Thread, "Enter KThread. self. Test"); new KThread(new Ping. Test(1)). set. Name("forked thread"). fork(); new Ping. Test(0). run(); }

KThread. self. Test 2() n the Runnable class is Simple. Thread private static class

KThread. self. Test 2() n the Runnable class is Simple. Thread private static class Simple. Thread implements Runnable { Simple. Thread( int burst_time) { this. burst_time = burst_time; } public void run() { int remaining_time=burst_time; long current_tick; while(remaining_time>0) { current_tick=Machine. timer(). get. Time(); remaining_time--; System. out. println( current_tick+ " running: " + current. Thread. get. Name() + " , remaining time: " + remaining_time); Machine. interrupt(). tick(false); //advacnce the time } } private int burst_time; }

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test 2"); new KThread(new Simple. Thread(5)). set. Name("forked thread 1"). fork(); new KThread(new Simple. Thread(10)). set. Name("forked thread 2"). fork(); } running thread: ready queue: main thread forked thread 1 forked thread 2 1. 2. 3. 4. 5. run main thread create forked thread 1 create forked thread 2 forked thread 1 and forked thread 2 haven't be executed yet!!

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test 2"); new KThread(new Simple. Thread(5)). set. Name("forked thread 1"). fork(); new KThread(new Simple. Thread(10)). set. Name("forked thread 2"). fork(); yield(); } running thread: ready queue: main thread forked thread 1 forked thread 2 Hint : How to design main thread is a key point in this project. 1. 2. 3. 4. 5. 6. 7. 8. run main thread create forked thread 1 create forked thread 2 main thread yield next thread is forked thread run forked thread 1

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test

public static void self. Test 2() { Lib. debug(dbg. Thread, "Enter KThread. self. Test 2"); new KThread(new Simple. Thread(5)). set. Name("forked thread 1"). fork(); new KThread(new Simple. Thread(10)). set. Name("forked thread 2"). fork(); yield(); }

Basic requirement Your Nachos system have to execute threads by using round-robin scheduling and

Basic requirement Your Nachos system have to execute threads by using round-robin scheduling and show them. n Please use Machine. interrupt(). schedule( n long when, //Time slot 多久 String type, Runnable handler //interrupt 通知哪個hanlder );

Basic requirement Each thread runs Simple. Thread. n All settings of threads have to

Basic requirement Each thread runs Simple. Thread. n All settings of threads have to be read from outer files. n

 Input file: 2 2 // Time slice // Number of threads //burst time

Input file: 2 2 // Time slice // Number of threads //burst time 5 10 Output file:

Note Do not modify any classes in the nachos. machine package. n Do not

Note Do not modify any classes in the nachos. machine package. n Do not directly use Java threads (the java. lang. Thread class). The Nachos security manager will not permit it. n You could directly use Java File objects (in the java. io package) to read files. n

Grading Policy n Code n correctness 60% You should provide four test cases to

Grading Policy n Code n correctness 60% You should provide four test cases to verify your Nachos system. Every test cases is 15% n Report 30% n Bonus 15%