cpu scheduling
play

CPU Scheduling Schedulers in the OS Structure of a CPU Scheduler - PDF document

CPSC 410 / 611 : Operating Systems CPU Scheduling Schedulers in the OS Structure of a CPU Scheduler Scheduling = Selection + Dispatching Criteria for scheduling Scheduling Algorithms FIFO/FCFS SPF / SRTF


  1. CPSC 410 / 611 : Operating Systems CPU Scheduling • Schedulers in the OS • Structure of a CPU Scheduler – Scheduling = Selection + Dispatching • Criteria for scheduling • Scheduling Algorithms – FIFO/FCFS – SPF / SRTF – Priority / MLFQ • Thread Dispatching (hands-on!) Schedulers start long-term scheduler short-term scheduler suspended ready running ready suspended blocked blocked medium-term scheduler 1

  2. CPSC 410 / 611 : Operating Systems Short-Term Scheduling • Recall: Motivation for multiprogramming -- have multiple processes in memory to keep CPU busy. • Typical execution pro fi le of a process/thread: start terminate wait for I/O wait for I/O wait for 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 to use the CPU next?!” 4 2 ready running 3 1 waiting non-preemptive Scheduling decision points: – 1 1. The running process changes from running to waiting preemptive (current CPU burst of that process is over). – 2 2. The running process terminates. – 3 3. A waiting process becomes ready (new CPU burst of that process begins). – 4 4. The current process switches from running to ready . 2

  3. CPSC 410 / 611 : Operating Systems Structure of a Scheduler ready queue PCB scheduler dispatcher CPU select process start new process ? ? What Is a Good Scheduler? Criteria • User oriented: – T Turnaround time : time interval from submission of job until its completion – W Waiting time : sum of periods spent waiting in ready queue – R Response time : time interval from submission of job to fi rst response – N Normalized turnaround time: ratio of turnaround time to service time • System oriented: – C CPU utilization : percentage of time CPU is busy – T Throughput : number of jobs completed per time unit • Any good scheduler should: – maximize CPU utilization and throughput – minimize turnaround time, waiting time, response time • Huh? – maximum/minimum values vs. average values vs. variance 3

  4. CPSC 410 / 611 : Operating Systems Scheduling Algorithms • F FCFS : First-come- fi rst-served • SPN: Shortest Process Next S • S SRT: Shortest Remaining Time • priority scheduling • R RR : Round-robin • MLFQ: Multilevel feedback queue scheduling M • Multiprocessor scheduling First-Come-First-Served (FCFS/FIFO) append at the end of queue head tail CPU PCB • Advantages: – very simple • Disadvantages: – long average and worst-case waiting times – poor dynamic behavior (convoy effect) 4

  5. CPSC 410 / 611 : Operating Systems Waiting Times for FCFS/FIFO • Example: P 1 = 24, P 2 = 6, P 3 = 6 W awg = (24+30)/3 = 18 P 1 P 2 P 3 W wc = 30 Different arrival order: W awg = (6+12)/3 = 6 P 2 P 3 P 1 W wc = 12 • Average waiting times is not minimal. • Waiting times may substantially vary over time. • Worst-case waiting times can be very long. Convoy Effects CPU I/O empty! CPU-bound I/O-bound CPU empty! I/O 5

  6. CPSC 410 / 611 : Operating Systems Shortest Process Next determine location in queue (compare next CPU burst lengths) CPU long jobs short jobs • Whenever CPU is idle, picks process with shortest next CPU burst . • Advantages: minimizes average waiting times. • Problem: How to determine length of next CPU burst?! • Problem: Starvation of jobs with long CPU bursts. SJF Minimizes Average Waiting Time • Provably optimal: Proof: swapping of jobs P long P short P short P long dW = t short - t long < 0 • Example: 6 12 8 4 W = 6+18+26 = 50 6 8 12 4 W = 6+14+26 = 46 6 8 4 12 W = 6+14+18 = 38 6 4 8 12 W = 6+10+18 = 34 4 6 8 12 W = 4+10+18 = 32 6

  7. CPSC 410 / 611 : Operating Systems SJF in Practice ? How to determine execution time of next CPU burst ?! – wild guess? – code inspection? • Forecasting (i.e. estimation) S n+1 = F(T n , T n-1 , T n-2 , T n-3 , T n-4 , ...) • Simple forecasting function: exponential average: S n+1 = a T n + (1-a) S n • Example: a = 0.8 S n+1 = 0.8T n + 0.16T n-1 + 0.032T n-2 + 0.0064T n-3 + ... Exponential Averaging: Example 16 14 a = 0.2 12 a = 0.5 10 a = 0.8 8 6 4 2 1 7

  8. CPSC 410 / 611 : Operating Systems Preemptive SPN: Shortest-Remaining-Time-First • SPN: P 1 and P 3 arrive here P 2 arrives here P 1 P 3 P 2 nil P 3 P 2 P 3 ready queue P 3 • SRT: P 1 and P 3 arrive here P 2 arrives here P 1 P 2 P 1 P 3 nil P 3 P 1 P 3 P 3 P 1 is preempted P 1 resumes execution (Fixed) Priority Scheduling Selector (compare priorities) CPU low priority high priority • Whenever CPU is idle, picks process with highest priority. • Priority: – process class, urgency, pocket depth. • Unbounded blocking: Starvation – Increase priority over time: aging 8

  9. CPSC 410 / 611 : Operating Systems • Conceptually • Priority Queues priority queue low priority priority low priority q=f(p) (compare priorities) (compare priorities) Selector Selector high priority high priority CPU CPU Round-Robin • FIFO with preemption after time quantum • Method for time sharing • Choice of time quantum: – large: FCFS – small: Processor sharing • Time quantum also de fi nes context-switching overhead FIFO queue end of time quantum CPU terminate 9

  10. CPSC 410 / 611 : Operating Systems Multilevel Queue Scheduling low priority batch processes (compare priorities) user processes Selector high-priority user processes kernel processes high priority separate queues, perhaps CPU with different scheduling policies Multilevel Feedback Queue Scheduling (conceptually) low priority FCFS (quantum = in fi nity) (compare priorities) quantum = 16 ms Selector aging quantum = 4ms quantum = 2 ms demotion high priority 10

  11. CPSC 410 / 611 : Operating Systems CPU Scheduling • Schedulers in the OS • Structure of a CPU Scheduler – Scheduling = Selection + Dispatching ready queue • Criteria for scheduling PCB scheduler dispatcher CPU • Scheduling Algorithms select process start new process – FIFO/FCFS ? ? – SPF / SRTF – Priority / MLFQ • Thread Dispatching (hands-on!) Managing and Dispatching Threads (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(); } }; 11

Recommend


More recommend