lecture5_scheduling
lecture5_scheduling
Lecture 5
Process Scheduling
(chapter 5)
• The slides here are adapted/modified from the textbook and its slides:
Operating System Concepts, Silberschatz et al., 7th & 8th editions,
Wiley.
REFERENCES
• Operating System Concepts, 7th and 8th editions, Silberschatz et al.
Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009.
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
• Operating Systems Examples
• Algorithm Evaluation
ready running
waiting
P1 P2 P3
0 24 27 30
P2 P3 P1
0 3 6 30
• Associate with each process the length of its next CPU burst. Use
these lengths to schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a given set
of processes
– The difficulty is knowing the length of the next CPU request
P4 P1 P3 P2
0 3 9 16 24
=0
n+1 = n
– Recent history does not count
=1
– n+1 = tn
– Only the actual last CPU burst counts
• If we expand the formula, we get:
n+1 = tn+(1 - ) tn-1 + …
+(1 - )j tn -j + …
+(1 - )n +1 0
0 1 5 10 17 26
• Each process gets a small unit of CPU time (time quantum), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and 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 than (n-1)q time units.
• Performance
– q large FIFO
– q small q must be large with respect to context switch, otherwise
overhead is too high
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
• Three queues:
– Q0 – RR with time quantum 8 milliseconds
– Q1 – RR time quantum 16 milliseconds
– Q2 – FCFS
• Scheduling
– A new job enters queue Q0 which is served RR (q=8). When it
gains CPU, job receives 8 milliseconds. If it does not finish in 8
milliseconds, job is moved to queue Q1.
– At Q1 job is again served RR and receives 16 additional
milliseconds. If it still does not complete, it is preempted and
moved to queue Q2.
#include <pthread.h>
#include <stdio.h> This means kernel
#define NUM THREADS 5 will create threads and
int main(int argc, char *argv[]) will do scheduling
{ Treat it as a
int i; normal process
pthread t tid[NUM THREADS]; (not real-time, etc.)
pthread attr t attr;
/* get the default attributes */
pthread attr init(&attr);
/* set the scheduling algorithm to PROCESS or SYSTEM
*/
pthread attr setscope(&attr, PTHREAD SCOPE SYSTEM);
/* set the scheduling policy - FIFO, RT, or OTHER */
pthread attr setschedpolicy(&attr, SCHED OTHER);
/* create the threads */
for (i = 0; i < NUM THREADS; i++)
pthread create(&tid[i],&attr,runner,NULL);
CS342 Operating Systems 33 İbrahim Körpeoğlu, Bilkent University
Pthread Scheduling API
• Solaris scheduling
• Windows XP scheduling
• Linux scheduling