CPSC 410 / 611 : Operating Systems CPSC 410/ 611: Week 3: CPU Scheduling • Schedulers in t he OS • St ruct ure of a CPU Scheduler – Scheduling = Select ion + Dispat ching • Thread Dispat ching (hands-on!) • Crit eria f or scheduling • Scheduling Algorit hms – FI FO/ FCFS – SPF / SRTF – Priorit y / MLFQ Scheduler s start long-term scheduler short-term scheduler suspended ready running ready suspended blocked blocked medium-term scheduler 1
CPSC 410 / 611 : Operating Systems Shor t -Term Scheduling • Recall: Mot ivat ion f or mult iprogramming -- have mult iple processes in memory t o keep CP U busy. • Typical execut ion prof ile of a process/ t hread: st ar t t er minat e wait f or I / O wait f or I / O wait f or I / O CPU burst CPU burst CPU burst CPU burst • CPU scheduler is managing the execution of CPU bursts, represented by processes in ready or running state. Scheduling Decisions “Who is going t o use t he CPU next ?!” 4 2 ready running ready running 3 1 waiting waiting non- preemptive Scheduling decision point s: – 1 . The running process changes f rom running t o wait ing preemptive (current CPU burst of t hat process is over). – 2 . The running process t erminat es. – 3 . A wait ing process becomes r eady (new CPU bur st of t hat process begins). – 4 . The current process swit ches f rom running t o r eady . 2
CPSC 410 / 611 : Operating Systems St ruct ure of a Scheduler ready queue ready queue P CB P CB scheduler dispat cher CPU scheduler dispat cher CPU select process st art new process ? ? CPU Scheduling • Schedulers in t he OS • St ruct ure of a CPU Scheduler – Scheduling = Select ion + Dispat ching • Thread Dispat ching (hands-on!) ready queue ready queue • Crit eria f or scheduling P CB scheduler dispat cher CP U P CB scheduler dispat cher CP U • Scheduling Algorit hms select process st art new process – FI FO/ FCFS ? ? – SPF / SRTF – Priorit y / MLFQ 3
CPSC 410 / 611 : Operating Systems Managing and Dispat ching Thr eads (1) typedef enum {THRD_INIT, THRD_READY, THRD_SUSPENDED, THRD_RUNNING, THRD_EXIT, THRD_STOPPED} THREAD_STATE; typedef struct thread_context { class Thread : public PObject { reg_t s0, s1, s2, s3; protected: reg_t s4, s5, s6, s7; char name[15]; reg_t gp; Addr stack_pointer; reg_t ra; friend class Scheduler; reg_t fp; THREAD_CONTEXT thread_context; reg_t sp; THREAD_STATE thread_state; reg_t pc; Scheduler * sched; /* pointer to global scheduler */ } THREAD_CONTEXT; public: Thread(char _name[], int (*_thread_func_addr)(), int _stack_size, Scheduler * _s); ~Thread(); /* -- THREAD EXECUTION CONTROL */ virtual int start() { /* Start thread and toss it on the ready queue. */ sched->resume(); } virtual int kill() { /* Terminate the execution of the thread. */ sched->terminate(); } }; Managing and Dispat ching Thr eads (2) class Scheduler { private: int yield_to(Thread * new_thread); /* Calls low-level dispatching mechanisms. */ protected: Thread * current_thread; /* -- MANAGEMENT OF THE READY QUEUE */ virtual int remove_thread(Thread * _thr) {}; /* = NULL; */ /* Remove the Thread from any scheduler queues. */ virtual Thread * first_ready() {}; /* = NULL;*/ /* Removes first thread from ready queue and returns it. This method is used in 'yield'. */ virtual int enqueue(Thread * _thr) {}; /* = NULL; */ /* Puts given thread in ready queue. This method is used in 'resume'. */ public: Scheduler(); /* Instantiate a new scheduler. This is done during OS startup. */ /* -- START THE EXECUTION OF THREADS . */ virtual int start(); /* Start the execution of threads by yielding to first thread in ready queue. Has to be called AFTER at least one thread has been started (typically the idle thread). */ /* -- SCHEDULING OPERATIONS */ virtual int yield(); /* Give up the CPU. If another process is ready, make that process have the CPU. Returns 0 if ok. */ int terminate_thread(Thread * _thr); /* Terminate given thread. The thread must be eliminated from any ready queue and its execution must be stopped. Special care must be taken if this is the currently executing thread. */ int resume(Thread * _thr); /* Indicate that the process is ready to execute again. The process is put on the ready queue.*/ }; 4
CPSC 410 / 611 : Operating Systems Managing and Dispat ching Thr eads (2) class Scheduler { private: int yield_to(Thread * new_thread); /* Calls low-level dispatching mechanisms. */ protected: int Scheduler:: yield () { int return_code = 0; Thread * current_thread; /* -- MANAGEMENT OF THE READY QUEUE */ /* -- GET NEXT THREAD FROM READY QUEUE. */ virtual int remove_thread(Thread * _thr) {}; /* = NULL; */ Thread * new_thread = first_ready(); /* Remove the Thread from any scheduler queues. */ if (!new_thread) { virtual Thread * first_ready() {}; /* = NULL;*/ /* --- THERE IS NO OTHER THREAD READY */ /* Removes first thread from ready queue and returns it. This method is used in 'yield'. */ /* (THIS MUST BE THE IDLE THREAD, THEN) */ virtual int enqueue(Thread * _thr) {}; /* = NULL; */ return return_code; /* Puts given thread in ready queue. This method is used in 'resume'. */ } public: else { Scheduler(); /* Instantiate a new scheduler. This is done during OS startup. */ /* --- GIVE CONTROL TO new_thread */ return_code = yield_to(new_thread); /* -- START THE EXECUTION OF THREADS . */ virtual int start(); /* THIS CODE IS EXECUTED AFTER A resume OPERATION. */ /* Start the execution of threads by yielding to first thread in ready queue. return return_code; Has to be called AFTER at least one thread has been started (typically the idle thread). */ } } /* of Scheduler::yield() */ /* -- SCHEDULING OPERATIONS */ virtual int yield(); /* Give up the CPU. If another process is ready, make that process have the CPU. Returns 0 if ok. */ int terminate_thread(Thread * _thr); /* Terminate given thread. The thread must be eliminated from any ready queue and its execution must be stopped. Special care must be taken if this is the currently executing thread. */ int resume(Thread * _thr); /* Indicate that the process is ready to execute again. The process is put on the ready queue.*/ }; Managing and Dispat ching Thr eads (2) class Scheduler { private: int yield_to(Thread * new_thread); /* Calls low-level dispatching mechanisms. */ protected: Thread * current_thread; /* -- MANAGEMENT OF THE READY QUEUE */ virtual int remove_thread(Thread * _thr) {}; /* = NULL; */ /* Remove the Thread from any scheduler queues. */ virtual Thread * first_ready() {}; /* = NULL;*/ /* Removes first thread from ready queue and returns it. This method is used in 'yield'. */ virtual int enqueue(Thread * _thr) {}; /* = NULL; */ /* Puts given thread in ready queue. This method is used in 'resume'. */ public: int Scheduler:: resume (Thread * _thr) { Scheduler(); /* Instantiate a new scheduler. This is done during OS startup. */ /* -- START THE EXECUTION OF THREADS . */ /* This thread better not be on the ready queue. */ virtual int start(); assert(_thr->thread_state != THRD_READY); /* Start the execution of threads by yielding to first thread in ready queue. enqueue(_thr); Has to be called AFTER at least one thread has been started (typically the idle thread). */ /* -- SCHEDULING OPERATIONS */ return 0; virtual int yield(); /* Give up the CPU. If another process is ready, make that process have the CPU. Returns 0 if ok. */ } /* Scheduler::resume() */ int terminate_thread(Thread * _thr); /* Terminate given thread. The thread must be eliminated from any ready queue and its execution must be stopped. Special care must be taken if this is the currently executing thread. */ int resume(Thread * _thr); /* Indicate that the process is ready to execute again. The process is put on the ready queue.*/ }; 5
Recommend
More recommend