Rudy Harjanto cs162-az Notes for 2/2 Lecture preemptive scheduling scheme: allows a process to be temporarily halted so another process can be run, with the intention of returning to the halted process. Overhead of context switching should be considered when choosing scheduling algorithm. starvation: when a process that is ready to run but has to wait indefinitely in a scheduling queue and therefore remains Scheduling Algorithms 1. Non-Preemptive Scheduling Algorithms a. First come first serve (FCFS) - the process that requests the CPU first is allocated the CPU first and run until completion. Implemented with a FIFO queue. ADVANTAGES: simple to implement, low context switching overhead, works well if each process takes roughly same amount of time DISADVANTAGES: average wait time is far from optimal when process run time distribution is highly skewed (eg. a really long process in the front of queue severly delays all other processes) b. Shortest job first/shortest process time (SJF/SPT) - Little's Algorithm states: N = lambda * F, where N = # of processes, lambda = arrival rate of users, F = average flow time. Therefore to minimize the flow time, you should minimize the number of jobs. Therefore a logical improvement over FCFS would be to allocate CPU to the process that has the smallest processing time, thus leading to a decrease in the number of processes as fast as possible. ADVANTAGES: has minimum average flow time DISADVANTAGES: requires knowledge about how long process will take. has high variance of (flow/service) -- long jobs have to wait a long time 2. Preemptive Scheduling Algorithms Round Robin (RR) - FCFS scheduling with preemption. Processes are stored in a priority queue based on process time. First process is run for a quantum (length of time slice), then it is placed at the back of the queue and so on. RR performance is highly correlated to the quantum size. If quantum is very large, RR is FCFS. If quantum is very small, performance suffers because of more overhead due to context switching. Modern OSs have a quantum under 0.1 seconds. ADVANTAGES: short jobs get through quickly, has low variance of (flow/service) DISADVANTAGES: high overhead due to context switching Shortest remaining processing time (SRPT) - preemptive version of SJT/SPT. Take for example two jobs 1 and 2, with job 1 being longer but arriving first in the queue. Job 1 is run and then temporarily halted when job 2 arrives in the queue. Job 1 is allocated the CPU once job 2 finishes ADVANTAGES: minimizes average flow time DISADVANTAGES: requires future knowledge about how long each job will run for -- can be done by using past statistics. high variance of flow (long jobs have to wait a long time and starve) Shortest elapsed time (SET) - run job that has shortest amount of time so far. Approximate by time slicing between equally long processes. ADVANTAGES: short jobs get out fast, variance is low DISADVANTAGES: high overhead espeically when process run times are highly skewed. Multilevel Queue Scheduling - separate processes into multiple priority queues. Internally, each queue is scheduled by one of the algorithms above (eg. foreground queue uses RR, while background uses FCFS). There must also be scheduling between each queue. Fixed priority (higher priority queues must be empty for processes in lower to run) or time slicing (eg. foreground queue given 80% of CPU, while background is given 20%) are common. Some ways to classify processes into different groups/queues include [foreground, background], [interactive, batch], [I/O, computational], [newly arrived, long running], [deadline, non deadline] A special case of Multilevel queue scheduling is called Multilevel feedback-queue (aka exponential queue) - one adaptive version of Multilevel queue scheduling. Processes can change priority queue during run time. Give new processes high priority and short time slices. As time progresses, decrease the process priority by 1 and double the quantum. To prevent starvation, allow long running processes to move up to higher priority queues. Increasing quantum decreases the amount of overhead and BSD uses a multilevel feedback scheduling algorithm with 128 priority levels handled by 32 queues. Quantum is 0.1 seconds. Priority of user based on type of job, load factor of job Ways to cheat the queueing system 1. make long jobs look like small jobs. dissect one long process into many small ones because short jobs get higher priority 2. do a lot of fake I/O (to /dev/null) to get a higher priority adaptive scheduling scheme: those that can change priority of a job depending on its run time behavior (MLFB, SET, exponential queue) Most modern and high performing scheduling algorithms are preemptive and adaptive. _________ Process is independent if - it cannot affect or be affected by other processes executing in the system - its state is not shared in any way by any other process - it is deterministic; input alone determines results - files not shared with other, does not use input from another process or generate output for another. cannot conflict in resources with another process. cooperating process - - can affect or be affected by other processes. - can share state with another. Macro behavior should be reproducible. Benefits of allowing process cooperation: - sharing of resources. allow for concurrent access to information - speed up computation by running processes i nparallel - allows for a more modular system