CPU Switch From Process to Process Context switch





















- Slides: 21

CPU Switch From Process to Process Context switch

Scheduling queues • Maintains scheduling queues of processes – Job queue – set of all processes in the system – Ready queue – set of all processes residing in main memory, ready and waiting to execute – Device queues – set of processes waiting for an I/O device • Processes migrate among the various queues

Scheduling queues Job queue Ready queue Device queue

Process Scheduling • Scheduler – Selects a process from a set of processes • Two kinds of schedulers 1. Long term schedulers, job scheduler – A large number of processes are submitted (more than memory capacity) – Stored in disk – Long term scheduler selects process from job pool and loads in memory 2. Short term scheduler, CPU scheduler – Selects one process among the processes in the memory (ready queue) – Allocates to CPU

Representation of Process Scheduling CPU scheduler selects a process Dispatched (task of Dispatcher) Parent at wait()

Scheduling algorithms • Scheduling in batch systems(FCFS, SJf, Shortest remaining time next). • Scheduling in interactive systems(Round robin, Priority, multiple queues, Shortest process next, Guaranteed, lottery) • Scheduling in Realtime(Hard real time, Soft realtime)

First-Come, First-Served (FCFS) Scheduling • • Process that requests CPU first, is allocated the CPU first Ready queue=>FIFO queue Non preemptive Simple to implement Performance evaluation • Ideally many processes with several CPU and I/O bursts • Here we consider only one CPU burst per process

First-Come, First-Served (FCFS) Scheduling Process Burst Time P 1 24 P 2 3 P 3 3 • Suppose that the processes arrive in the order: P 1 , P 2 , P 3 The Gantt Chart for the schedule is: P 1 0 P 2 24 P 3 27 30 • Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17

Shortest-Job-First (SJF) Scheduling • Nonpreemptive algorithm • Runtime of a process is known in advance • When several equally important jobs are waiting the scheduler picks the shortest job first

Example of SJF Process. Arrival Time P 1 0. 0 P 2 2. 0 P 3 4. 0 P 4 5. 0 • SJF scheduling chart P 4 0 P 3 P 1 3 Burst Time 6 8 7 3 9 P 2 16 • Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 Avg waiting time for FCFS? 24

Preemptive version Shortest-remaining-time-first • Preemptive version called shortest-remainingtime-first • The processor is allocated to the job but it can be preempted by a newer ready job with shorter time to completion. • Impossible to implement in interactive systems where required CPU time is not known. • It is often used in batch environments where short jobs need to give preference.

Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority (smallest integer highest priority) • Set priority value nice – Internal (time limit, memory req. , ratio of I/O Vs CPU burst) – External (importance, fund etc) • SJF is priority scheduling where priority is the inverse of predicted next CPU burst time • Two types – Preemptive – Nonpreemptive • Problem Starvation – low priority processes may never execute • Solution Aging – as time progresses increase the priority of the process

Scheduling in interactive systems Round Robin Scheduling • Round Robin is the preemptive process scheduling algorithm. • Each process is provided a fix time to execute, it is called a quantum. • Once a process is executed for a given time period, it is preempted and other process executes for a given time period.

Example of RR with Time Quantum = 4 Process Burst Time P 1 24 P 2 3 P 3 3 • The Gantt chart is: P 1 0 P 2 4 P 3 7 P 1 10 P 1 14 P 1 18 22 P 1 26 P 1 30 • Avg waiting time = ((10 -4)+4+7)/3=5. 66

Priority Schedulding • Each process is assigned a priority • The process with highest priority is allowed to run first • Processes with same priority are executed on first come first served basis.

Process Arrival Time Execution Time Priority Service Time P 0 0 5 1 0 P 1 1 3 2 11 P 2 2 8 1 14 P 3 3 6 3 5

Process Waiting Time P 0 0 -0=0 P 1 11 - 1 = 10 P 2 14 - 2 = 12 P 3 5 -3=2 Average Wait Time: (0 + 12 + 2)/4 = 24 / 4 = 6

Multiple-Level Queues Scheduling • Multiple queues are maintained for processes with common characteristics. • Each queue can have its own scheduling algorithms. • Priorities are assigned to each queue. • For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another queue.

Shortest process next • Shortest process next is an algorithm in which the process having the smallest execution time is chosen for the next execution. • This scheduling method can be preemptive or non-preemptive. • It significantly reduces the average waiting time for other processes awaiting execution.

Guaranteed Scheduling • Make real promises to the users about performance. • • If there are n users logged in while you are working, you will receive about 1 /n of the CPU power. • To make good on this promise, the system must keep track of how much CPU each process had since its creation

Lottery Scheduling • Processes are each assigned some number of lottery tickets • lottery tickets are chosen at random and the process holding the ticket gets the resource • Giving each process at least one lottery ticket