Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scheduling.

Similar presentations


Presentation on theme: "Scheduling."— Presentation transcript:

1 Scheduling

2 Content of This Lecture
Why CPU scheduling? Basic scheduling algorithms FIFO (FCFS) Shortest job first Round Robin Priority Scheduling Goals: Understand how your program is executed on the machine together with other programs

3 Review: State Process Model

4 OS Representation of Process via Process Control Block (PCB)

5 Process Scheduling Deciding which process/thread should occupy the resource (CPU, disk, etc) CPU I want to play Whose turn is it? Process 2 Process 3 Process 1

6 Context Switch Switch CPU from one process to another
Performed by scheduler It includes: save PCB state of the old process; load PCB state of the new process; Flush memory cache; Change memory mapping (TLB); Context switch is expensive ( microseconds) No useful work is done (pure overhead) Can become a bottleneck Need hardware support

7 When should the scheduler be called?
A new process is admitted The running process exits The running process is blocked I/O interrupt (some processes will be ready) Clock interrupt (every 10 milliseconds)

8 Preemptive vs. Non-preemptive scheduling
The running process keeps the CPU until it voluntarily gives up the CPU process exits switches to blocked state Transition 3 is only voluntary Preemptive scheduling: The running process can be interrupted and must release the CPU (can be forced to give up CPU) 4 Terminated Running 1 3 Ready Blocked

9 Scheduling Objectives
Fairness (equitable shares of CPU) Priority (highest priority first) Efficiency (make best use of equipment) Encouraging good behavior (can’t take advantage of the system) Support for heavy loads (degrade gracefully) Adapting to different environments (interactive, real-time, multi-media)

10 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Performance Criteria Efficiency: keep resources as busy as possible Throughput: # of processes that complete in unit time Waiting Time Total amount of time spent by the process waiting in ready queue Initial Waiting Time Amount of time spent by the process waiting in ready queue before it starts executing Response Time amount of time from when a job is admitted until it completes Proportionality: Assign CPU proportionally to given application weight Meeting Deadlines: example, complete by 5pm Dec. 15th 2009!

11 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Process Profiles I/O – Bound Does too much I/O to keep CPU busy CPU – Bound Does too much computation to keep I/O busy Process Mix Scheduling should load balance between I/O bound and CPU-bound processes Ideal would be to run all equipment at 100% utilization but that would not necessarily be good for response time

12 Simple Processor Scheduling Algorithms
Copyright ©: Nahrstedt, Angrave, Abdelzaher Simple Processor Scheduling Algorithms Batch systems First Come First Serve (FCFS) Shortest Job First Interactive Systems Round Robin Priority Scheduling

13 First Come First Serve (FCFS)
Copyright ©: Nahrstedt, Angrave, Abdelzaher First Come First Serve (FCFS) Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Used in Batch Systems Implementation FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue. Quiz: Is it Preemptive or Non-preemptive?

14 First Come First Serve (FCFS)
Copyright ©: Nahrstedt, Angrave, Abdelzaher First Come First Serve (FCFS) Process that requests the CPU FIRST is allocated the CPU FIRST. Also called FIFO Used in Batch Systems Implementation FIFO queue A new process enters the tail of the queue The scheduler selects the process at the head of the queue. Quiz: Is it Preemptive or Non-preemptive?  once a process has CPU, it runs to completion (non-preemptive)!

15 Copyright ©: Nahrstedt, Angrave, Abdelzaher
FCFS Example Process Duration Order Arrival Time P1 24 1 P2 3 2 P3 4 The final schedule: P1 (24) P2 (3) P3 (4) 24 27 P1 waiting time: 0 P2 waiting time: 24 P3 waiting time: 27 The average waiting time: ( )/3 = 17 Does the execution order affect the average waiting time?

16 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Problems with FCFS Non-preemptive Does not minimize AWT Cannot utilize resources in parallel: Assume 1 process CPU bounded and many I/O bounded processes result: Convoy effect, low CPU and I/O Device utilization Why?

17 Why Convoy Effects with FCFS?
Copyright ©: Nahrstedt, Angrave, Abdelzaher Why Convoy Effects with FCFS? Consider n-1 jobs in system that are I/O bound and 1 job that is CPU bound: I/O bound jobs pass quickly through the ready queue and suspend themselves waiting for I/O. CPU bound job arrives at head of queue and executes until completion. I/O bound jobs re-join ready queue and wait for CPU bound job to complete. I/O devices idle until CPU bound job completes. When CPU bound job completes, the ready I/O-bounded processes quickly move through the running state and become blocked on I/O events again. CPU becomes idle.

18 Interactive Scheduling Algorithms
Copyright ©: Nahrstedt, Angrave, Abdelzaher Interactive Scheduling Algorithms Usually preemptive Time is sliced into quanta (time intervals) Scheduling decision is also made at the beginning of each quantum Performance Criteria Average response time Average initial waiting time Average waiting time Fairness (or proportional resource allocation) Representative algorithms: Round-robin Priority-based

19 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Round-robin One of the oldest, simplest, most commonly used scheduling algorithm Select process/thread from ready queue in a round-robin fashion (take turns) Problems: Does not consider priority Context switch overhead Preemption after quantum expiration

20 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Round-robin: Example Process Duration Order Arrival Time P1 3 1 P2 4 2 P3 Suppose time quantum is: 1 unit, P1, P2 & P3 never block P1 P2 P3 P1 P2 P3 P1 P2 P3 P2 10 P1 waiting time: 4 P2 waiting time: 6 P3 waiting time: 6 The average waiting time (AWT): (4+6+6)/3 = 5.33 Note that Initial Waiting Time of P1 is zero, P2’s IWT is 1, and P3’s IWT is 2

21 Choosing the Time Quantum
Copyright ©: Nahrstedt, Angrave, Abdelzaher Choosing the Time Quantum Time slice too large FIFO behavior Poor initial waiting time Time slice too small Too many context switches (overheads) Inefficient CPU utilization Heuristic: 70-80% of jobs block within time-slice Typical time-slice ms (depends on job priority)

22 Shortest Job First (SJF)
Copyright ©: Nahrstedt, Angrave, Abdelzaher Shortest Job First (SJF) Schedule the job with the shortest computation time first Scheduling in Batch Systems Two types: Non-preemptive Preemptive Optimal if all jobs are available simultaneously: Gives the best possible AWT (average waiting time)

23 Non-preemptive SJF: Example
Copyright ©: Nahrstedt, Angrave, Abdelzaher Non-preemptive SJF: Example Process Duration Order Arrival Time P1 6 1 P2 8 2 P3 7 3 P4 4 P4 (3) P1 (6) P3 (7) P2 (8) 3 9 16 24 P4 waiting time: 0 P1 waiting time: 3 P3 waiting time: 9 P2 waiting time: 16 The total time is: 24 The average waiting time (AWT): ( )/4 = 7

24 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Comparing to FCFS Process Duration Order Arrival Time P1 6 1 P2 8 2 P3 7 3 P4 4

25 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Comparing to FCFS Process Duration Order Arrival Time P1 6 1 P2 8 2 P3 7 3 P4 4 P1 (6) P2 (8) P3 (7) P4 (3) 14 21 24 6 The total time is the same. The average waiting time (AWT): ( )/4 = 10.25 (comparing to 7) P1 waiting time: 0 P2 waiting time: 6 P3 waiting time: 14 P4 waiting time: 21

26 Copyright ©: Nahrstedt, Angrave, Abdelzaher
Preemptive SJF Shortest job runs first. A job that arrives and is shorter than the running job will preempt it

27 A Problem with Preemptive SJF
Copyright ©: Nahrstedt, Angrave, Abdelzaher A Problem with Preemptive SJF Starvation A job may keep getting preempted by shorter ones Example Process A with computation time of 1 hour arrives at time 0 But every 1 minute, a short process with computation time of 2 minutes arrives Result of SJF: A never gets to run What’s the difference between starvation and a deadlock?


Download ppt "Scheduling."

Similar presentations


Ads by Google