Académique Documents
Professionnel Documents
Culture Documents
Spring 2007
• CPU Scheduling
– Basic Concepts
Lecture - V
– Scheduling Criteria
CPU Scheduling – Different Scheduling Algorithms
Tevfik Koşar
3 4
5 6
1
Dispatcher Scheduling Criteria
• Dispatcher module gives control of the CPU to the • CPU utilization – keep the CPU as busy as possible
process selected by the short-term scheduler; • Throughput – # of processes that complete their
Its function involves: execution per time unit
– switching context • Turnaround time – amount of time to execute a
– switching to user mode particular process
– jumping to the proper location in the user program to • Waiting time – amount of time a process has been
restart that program
waiting in the ready queue
• Dispatch latency – time it takes for the dispatcher
• Response time – amount of time it takes from
to stop one process and start another running
when a request was submitted until the first
response is produced, not output (for time-
sharing environment)
7 8
P1 P2 P3
0 24 27 30
Suppose that the processes arrive in the order • Associate with each process the length of its next CPU
P2 , P3 , P1 burst. Use these lengths to schedule the process with
• The Gantt chart for the schedule is: the shortest time
• Two schemes:
P2 P3 P1 – nonpreemptive – once CPU given to the process it cannot be
preempted until completes its CPU burst
0 3 6 30
– preemptive – if a new process arrives with CPU burst length less
than remaining time of current executing process, preempt.
This scheme is know as the
• Waiting time for P1 = 6; P2 = 0; P3 = 3 Shortest-Remaining-Time-First (SRTF)
• Average waiting time: (6 + 0 + 3)/3 = 3 • SJF is optimal – gives minimum average waiting time for
• Much better than previous case a given set of processes
• Convoy effect short process behind long process
11 12
2
Example of Non-Preemptive SJF Example of Preemptive SJF
Process Arrival Time Burst Time Process Arrival Time Burst Time
P1 0.0 7 P1 0.0 7
P2 2.0 4 P2 2.0 4
P3 4.0 1 P3 4.0 1
P4 5.0 4 P4 5.0 4
• SJF (non-preemptive) • SJF (preemptive)
P1 P3 P2 P4 P1 P2 P3 P2 P4 P1
0 3 7 8 12 16 0 2 4 5 7 11 16
Determining Length of Next CPU Burst Prediction of the Length of the Next CPU Burst
15 16
3
Round Robin (RR) Example of RR with Time Quantum = 20
• Each process gets a small unit of CPU time Process Burst Time
(time quantum), usually 10-100 milliseconds. P1 53
After this time has elapsed, the process is
P2 17
preempted and added to the end of the ready
P3 68
queue.
P4 24
• If there are n processes in the ready queue and
• The Gantt chart is:
the time quantum is q, then each process gets
1/n of the CPU time in chunks of at most q
time units at once. No process waits more P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Time Quantum and Context Switch Time Turnaround Time Varies With The Time Quantum
21 22
4
Multilevel Feedback Queue Example of Multilevel Feedback Queue
25 26
27 28
29 30
5
Pthread Scheduling API Pthread Scheduling API
#include <pthread.h>
#include <stdio.h>
#define NUM THREADS 5
int main(int argc, char *argv[]) /* now join on each thread */
{ for (i = 0; i < NUM THREADS; i++)
int i;
pthread join(tid[i], NULL);
pthread t tid[NUM THREADS];
pthread attr t attr; }
/* get the default attributes */ /* Each thread will begin control in this function */
pthread attr init(&attr);
void *runner(void *param)
/* set the scheduling algorithm to PROCESS or SYSTEM */
pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM); {
/* set the scheduling policy - FIFO, RT, or OTHER */ printf("I am a thread\n");
pthread attr setschedpolicy(&attr, SCHED OTHER);
pthread exit(0);
/* create the threads */
for (i = 0; i < NUM THREADS; i++) }
pthread create(&tid[i],&attr,runner,NULL);
31 32
• Solaris scheduling
• Windows XP scheduling
• Linux scheduling
33 34
35 36
6
Linux Scheduling The Relationship Between Priorities and Time-slice length
37 38
39 40
Hmm..
Hmm..
41 42
7
Reading Assignment Acknowledgements
• Read chapter 5 from Silberschatz. • “Operating Systems Concepts” book and supplementary
material by Silberschatz, Galvin and Gagne.
43 44