CMSC 421 Operating Systems Lecture Notes (c) 1994 Howard E. Motteler Process Scheduling ================== Consider the process state diagram: _________ RUNNING / | A | | | | | | V V WAITING ------> READY The scheduler moves a process between the running and ready states. Schedulers are either - Preemptive: a running process can be `kicked out' by the scheduler, or - Non-preemptive: a process keeps the CPU as long as it wants (`Multiprogramming' means running one job when another is waiting; all schedulers we consider are multiprogramming.) Criteria for a good scheduling algorithm ----------------------------------------- For an interactive system - fairness - efficiency - response time For a batch system - turnaround (time waiting for job to finish) - throughput (number of jobs completed per unit of time) Fairness: - each user gets a `fair share' of system resources - there are tradeoffs, e.g., between interactive and CPU-bound jobs Efficiency: usually refers to how effectively system resources, such as CPU and hard disk, are used For example, _multiprogramming_ (running one job when another is doing, say, an i/o wait) increases CPU efficiency : Suppose an average process would spend 20% of the time active and 80% waiting, if it had a system to itself. Then CPU utilization is then 20% If 5 such processes are multiprogrammed, the probability of all waiting at the same time is .8^5 = .33, and CPU utilization becomes 1 - .33 = .67, or 67%. Example: Round Robin Scheduler ------------------------------- A pre-emptive scheduler is used in most timesharing systems In RR, the READY state is a FIFO queue The scheduler receives regular interrupts from a clock Each clock tick (interrupt) is a `time slice' This is often somewhere between 1 ms and 100 ms When the scheduler receives a clock interrupt, it - moves the current process to the tail of the ready queue - runs the process at the head of the ready queue Length of time slice - too long: interactive response is lousy - too short: too much time spent context switching A long time slice tends to favor jobs that have a long CPU burst Suppose: - N processes - time slice is t ms Then any process must wait at must t * (N-1) ms before it runs again. Example: Shortest Job First Scheduling --------------------------------------- A non-preemptive scheduler used in some batch systems, as well as for other sorts of scheduling problems Suppose - we have a queue of jobs, and - we know job run-times beforehand The shortest-job-first scheduler does what it says This scheduler minimizes average turnaround time (the average wait until a job is completed) Example arrival (arbitrary) order: job A B C D runtime 10 4 2 8 waiting time 10 14 16 24 average wait 16 shortest job first: job C B D A runtime 2 4 8 10 waiting time 2 6 16 24 average wait 12 Q: Can we do this with interactive jobs? A: Interactive jobs may have a run burst (time until i/o or other wait) that can be estimated. If we knew (or had an approximation) of the run burst, then in theory this could be done. Priority Scheduling -------------------- Basic idea: - assign a priority to each job; - run job with highest priority (process queue may be sorted by priorities) Problem: a high priority process can _starve_ other processes One solution: _age_ the running process - periodically decrease priority of running process - or periodically increase priority of waiting processes Pure priority scheduling means keeping a sorted queue, - it may be more efficient to have _priority classes_ - each class has its own queue (typically RR) - only run a process in queue n if higher priority queues are empty Multiple queues can also use different schedulers, e.g. - low priority queue RR - high priority queue FIFO (no clock interrupts) - low pri. queue only runs when high pri. queue is empty (INMOS Transputer microcoded scheduler used this algorithm) Example: CTSS scheduling algorithm ----------------------------------- (pre-emptive scheduling with varying priorities) Intended for a system where the context switch was very expensive Basic idea: if a job remains CPU bound, - decrease its priority - increase its time slice CPU-intensive jobs run less often, but for longer bursts of time Note: the job is assumed to be CPU bound if it leave the RUN state due to a scheduler interrupt rather than an i/o wait Problem: the CTSS implementation would increase a job's priority on user input, and user's would type periodic s to keep their priority up (DEC PDP-lls running RSTS used a similar scheduler, and things like chess programs could completely stall the system) Two level schedulers --------------------- (similar to some priority schemes) Suppose some processes are kept in memory, while others are swapped out to disk level 1: schedule processes in memory level 2: move processes between memory and disk (i.e., decide who gets swapped out)