Docsity

CPU Scheduling - Operating Systems - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download CPU Scheduling - Operating Systems - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

CPU Scheduling

Chapter 5

Recall Schedulers

  • Short term scheduler: Select a process in the ready

queue for CPU allocation.

  • Medium term scheduler (swapper): determine

which process to swap in/out of disk to/from

memory.

  • Long term scheduler: Determine which processes

are admitted into the system.

  • Scheduling is a fundamental O.S. function.
  • Schedulers affect the performance of the system.
  • We will discuss short-term schedulers (CPU

allocation).

Alternating Sequence of CPU And I/O

Bursts

Histogram of CPU-burst Times

CPU Scheduler

  • Selects from among the processes in memory that

are ready to execute, and allocates the CPU to one of them

  • CPU scheduling decisions may take place when a

process:

1. Switches from running to waiting state

2. Switches from running to ready state

3. Switches from waiting to ready

4. Terminates

  • Scheduling only under 1 and 4 is nonpreemptive
  • All other scheduling is preemptive

Costs of Preemptive Scheduling

  • Preemptive scheduling leads to some problems that the OS must deal with:
  • Problem 1: inconsistent data:
    • Suppose process 1 is updating data when preempted by process 2.
    • Process 2 may then try to read the data, which is in an inconsistent state.
    • The OS needs mechanisms to coordinate shared data.
  • Problem 2: Kernel preemption:
    • Suppose the kernel is preempted while updating data (e.g. I/O queues) used by other kernel functions. This could lead to chaos.
    • UNIX solution: Wait for the system call to complete or have an I/O block take place if in kernel mode.
    • Problem with UNIX solution: Not good for real time computing.

Scheduling Criteria

  • CPU utilization – keep the CPU as busy as possible
  • Throughput – # of processes that complete their

execution per time unit

  • Turnaround time – amount of time to execute a

particular process

  • Waiting time – amount of time a process has been

waiting in the ready queue

  • Response time – amount of time it takes from when

a request was submitted until the first response is

produced, not output (for time-sharing

environment)

Optimization Criteria

  • The scheduling criteria are optimization problems. We would like to maximize or minimize each.
  • Question: Maximize or Minimize?
    • CPU utilization:
    • throughput:
    • turnaround time:
    • waiting time:
    • response time:
  • Can all criteria be optimized simultaneously?
  • Usually try to optimize average times (although sometimes optimize minimum or maximum)

Recall Scheduling Criteria

  • CPU utilization –fraction of time the CPU is busy.
  • CPU efficiency – fraction of time the CPU is executing user code.
  • Throughput – # of processes completed per unit time
  • Average Turnaround time – average delay between job submission and job completion.
  • Waiting time – amount of time a process has been waiting in the ready queue
  • Response time – amount of time it takes from when a request was submitted until the first response is produced.

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:
  • Waiting time for P 1 = 0; P 2 = 24; P 3 = 27
  • Average waiting time: (0 + 24 + 27)/3 = 17

P 1 P 2 P (^3)

0 24 27 30

Shortest-Job-First (SJF) Scheduling

  • Associate with each process the length of its next CPU

burst. Use these lengths to schedule the process with the

shortest time

  • Two schemes:
    • nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst
    • preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is known as the Shortest-Remaining-Time-First (SRTF)
  • SJF is optimal – gives minimum average waiting time for a

given set of processes

Example of Non-Preemptive SJF

Process Arrival Time Burst Time P 1 0.0 7 P 2 2.0 4 P 3 4.0 1 P4 5.0 4

  • SJF (non-preemptive)
  • Average waiting time =

P 1 P3 P

0 3 7 16

P 4

8 12

More Example

  • Consider five processes arrived at time 0 in order

P1, P2, P3, P4, and P

  • P1 : t1 = 350
  • P2 : t2 = 125
  • P3 : t3 = 475
  • P4 : t4 = 250
  • P5 : t5 = 75
  • Compare FCFS and SJF scheduling algorithms
    • Draw the Gantt diagram.
    • Compute average turnaround time and waiting time.

Determining Length of Next CPU

Burst

  • Can only estimate the length.
  • Can be done by using the length of previous CPU

bursts, using exponential averaging.

  1. Define :
  2. , 0 1
  3. predictedvalue for thenextCPUburst
  4. actuallenght of CPUburst

≤ ≤

=

=

α α

τ (^) n 1

th tn n

τ n + 1 =α t n + ( 1 −α)τ n.