0% found this document useful (0 votes)
20 views

3 Process Scheduling

Uploaded by

moin latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

3 Process Scheduling

Uploaded by

moin latif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CPU Scheduling

• How is the OS to decide which of several tasks to take off a queue?


• Scheduling: deciding which threads are given access to resources
from moment to moment.

Lec 3 Operating Systems 1


Assumptions about Scheduling

• CPU scheduling big area of research in early ‘70s


• Many implicit assumptions for CPU scheduling:
• One program per user
• One thread per program
• Programs are independent
• These are unrealistic but simplify the problem
• Does “fair” mean fairness among users or programs?
• If I run one compilation job and you run five, do you get five times as much
CPU?
• Often times, yes!
• Goal: dole out CPU time to optimize some desired parameters of the
system.
• What parameters?

Lec 3 Operating Systems 2


Assumption: CPU Bursts

Lec 3 Operating Systems 3


Assumption: CPU Bursts

• Execution model: programs alternate between bursts of CPU and I/O


• Program typically uses the CPU for some period of time, then does I/O, then uses CPU
again
• Each scheduling decision is about which job to give to the CPU for use by its next CPU
burst
• With timeslicing, thread may be forced to give up CPU before finishing current CPU
burst.

Lec 3 Operating Systems 4


What is Important in a Scheduling
Algorithm?
• Minimize Response Time
• Elapsed time to do an operation (job)
• Response time is what the user sees
• Time to echo keystroke in editor
• Time to compile a program
• Real-time Tasks: Must meet deadlines imposed by World
• Maximize Throughput
• Jobs per second
• Throughput related to response time, but not identical
• Minimizing response time will lead to more context switching than if you maximized only
throughput
• Minimize overhead (context switch time) as well as efficient use of resources (CPU,
disk, memory, etc.)
• Fairness
• Share CPU among users in some equitable way
• Not just minimizing average response time

5
Scheduling Algorithms: First-Come, First-
Served (FCFS)

• “Run until Done:” FIFO algorithm


• In the beginning, this meant one program runs non-preemtively until
it is finished (including any blocking for I/O operations)
• Now, FCFS means that a process keeps the CPU until one or more
threads block
• Example: Three processes arrive in order P1, P2, P3.
• P1 burst time: 24
• P2 burst time: 3
• P3 burst time: 3
• Draw the Gantt Chart and compute Average Waiting Time and
Average Completion Time.

Lec 3 Operating Systems 6


Scheduling Algorithms: First-Come, First-
Served (FCFS)
• Example: Three processes arrive in order P1, P2, P3.
• P1 burst time: 24
• P2 burst time: 3 P1 P2 P3
• P3 burst time: 3
0 24 27 30
• Waiting Time
• P1: 0
• P2: 24
• P3: 27
• Completion Time:
• P1: 24
• P2: 27
• P3: 30
• Average Waiting Time: (0+24+27)/3 = 17
• Average Completion Time: (24+27+30)/3 = 27

Lec 3 Operating Systems 7


Scheduling Algorithms: First-Come, First-
Served (FCFS)
• What if their order had been P2, P3, P1?
• P1 burst time: 24
• P2 burst time: 3
• P3 burst time: 3

Lec 3 Operating Systems 8


Scheduling Algorithms: First-Come, First-
Served (FCFS)
• What if their order had been P2, P3, P1?
• P1 burst time: 24
• P2 burst time: 3 P2 P3 P1
• P3 burst time: 3
0 3 6 30
• Waiting Time
• P1: 0
• P2: 3
• P3: 6
• Completion Time:
• P1: 3
• P2: 6
• P3: 30
• Average Waiting Time: (0+3+6)/3 = 3 (compared to 17)
• Average Completion Time: (3+6+30)/3 = 13 (compared to 27)

Lec 3 Operating Systems 9


Scheduling Algorithms: First-Come, First-
Served (FCFS)
• Average Waiting Time: (0+3+6)/3 = 3 (compared to 17)
• Average Completion Time: (3+6+30)/3 = 13 (compared to 27)
• FIFO Pros and Cons:
• Simple (+)
• Short jobs get stuck behind long ones (-)
• If all you’re buying is milk, doesn’t it always seem like you are stuck behind a cart full of
many items
• Performance is highly dependent on the order in which jobs arrive (-)

Lec 3 Operating Systems 10


Round Robin (RR) Scheduling

• FCFS Scheme: Potentially bad for short jobs!


• Depends on submit order
• If you are first in line at the supermarket with milk, you don’t care who is
behind you; on the other hand…
• Round Robin Scheme
• Each process gets a small unit of CPU time (time quantum)
• Usually 10-100 ms
• After quantum expires, the process is preempted and added to the end of the
ready queue
• Suppose N processes in ready queue and time quantum is Q ms:
• Each process gets 1/N of the CPU time
• In chunks of at most Q ms
• What is the maximum wait time for each process?
• No process waits more than (n-1)q time units

Lec 3 Operating Systems 11


Round Robin (RR) Scheduling

• Round Robin Scheme


• Each process gets a small unit of CPU time (time quantum)
• Usually 10-100 ms
• After quantum expires, the process is preempted and added to the end of the
ready queue
• Suppose N processes in ready queue and time quantum is Q ms:
• Each process gets 1/N of the CPU time
• In chunks of at most Q ms
• What is the maximum wait time for each process?
• No process waits more than (n-1)q time units

• Performance Depends on Size of Q


• Small Q => interleaved
• Large Q is like FCFS
• Q must be large with respect to context switch time, otherwise overhead is
too high (spending most of your time context switching!)

Lec 3 Operating Systems 12


Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3

• The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 1822 2630

Lec 3 Operating Systems 13


Example of RR with Time Quantum = 4
Process Burst Time
P1 24 P1 P2 P3 P1 P1 P1 P1 P1
P2 3
P3 3 0 4 7 10 14 1822 2630
• Waiting Time:
• P1: (10-4) = 6
• P2: (4-0) = 4
• P3: (7-0) = 7
• Completion Time:
• P1: 30
• P2: 7
• P3: 10
• Average Waiting Time: (6 + 4 + 7)/3= 5.67
• Average Completion Time: (30+7+10)/3=15.67

Lec 3 Operating Systems 14


Turnaround Time Varies With The Time Quantum

Lec 3 Operating Systems 15


Example of RR with Time Quantum = 20

• Waiting Time: A process can finish before the time quantum expires, and release the CPU.
• P1: (68-20)+(112-88) = 72
• P2: (20-0) = 20
• P3: (28-0)+(88-48)+(125-108) = 85
• P4: (48-0)+(108-68) = 88
• Completion Time:
• P1: 125
• P2: 28
• P3: 153
• P4: 112
• Average Waiting Time: (72+20+85+88)/4 = 66.25
• Average Completion Time: (125+28+153+112)/4 = 104.5

Lec 3 Operating Systems 16


RR Summary
• Pros and Cons:
• Better for short jobs (+)
• Fair (+)
• Context-switching time adds up for long jobs (-)
• The previous examples assumed no additional time was needed for context switching – in reality, this would add to wait
and completion time without actually progressing a process towards completion.
• Remember: the OS consumes resources, too!
• If the chosen quantum is
• too large, response time suffers
• infinite, performance is the same as FIFO
• too small, throughput suffers and percentage overhead grows
• Actual choices of timeslice:
• UNIX: initially 1 second:
• Worked when only 1-2 users
• If there were 3 compilations going on, it took 3
seconds to echo each keystroke!
• In practice, need to balance short-job
performance and long-job throughput:
• Typical timeslice 10ms-100ms
• Typical context-switch overhead 0.1ms – 1ms (about 1%)

Lec 3 Operating Systems 17


Comparing FCFS and RR
• Assuming zero-cost context Job # FCFS CT RR CT
switching time, is RR always 1 100 991
better than FCFS? 2 200 992
• Assume 10 jobs, all start at the … … …
same time, and each require
100 seconds of CPU time 9 900 999

• RR scheduler quantum of 1 10 1000 1000


second
• Completion Times (CT)
• Both FCFS and RR finish at the same time
• But average response time is much worse under RR!
• Bad when all jobs are same length
• Also: cache state must be shared between all jobs with RR but can be
devoted to each job with FIFO
• Total time for RR longer even for zero-cost context switch!

Lec 3 Operating Systems 18

You might also like