Review Preview Mutual Exclusion Solutions with Busy Waiting Test and Set Lock Priority Inversion problem with busy waiting Mutual Exclusion with Sleep and Wakeup The Producer-Consumer Problem Race Condition in Producer-Consumer Problem Semaphore The producer-consumer problem with semaphore Dining Philosopher's Problem Mutexes Monitor Scheduling Schedulers Scheduling Algorithms Shortest Job First Shortest Remaining Time Round Robin Priority Queue Guaranteed Scheduling Lottery Scheduling Scheduler Part of operating system that choose the next process for executing its job based on the rule (scheduling algorithm) is called process scheduler. scheduler is designed for making efficient use of the CPU. Long-Term Scheduler Selects a process from the pool of job and load into memory for execution Short-term scheduler selects a process from the ready queue and allocates the CPU. Memory Scheduler schedule which process is in memory and in disk. Three Level Scheduler Effective CPU Scheduler is essential The effective process scheduling algorithm is essential since process switch is very expensive.. All information for the blocked (become ready state or block state) must be saved (state, register, stack pointer, PC,..). The memory map (information regarding memory management) must be saved.. A new process is selected by the scheduler. Information for the new process must be loaded. start run new process. behavior CPU-bounded process a process spend most of their time computing (using CPU) I/O-bounded process a process spend most of their time waiting for I/O. 6
When to make scheduling decision?. When a process switches from the running state to the blocking state (short term scheduler). When a process switches from the running state to the ready state (short term scheduler). When a process terminate its job (long term scheduler). When a process switches from the blocked state to the ready state (short term scheduler) When scheduling takes place only under circumstances,, we say nonpreemptive scheduling scheme; otherwise, the scheduling scheme is called preemptive scheduling scheme. Categories of Scheduling Algorithm Since different system environments have different goals, different scheduling algorithms are needed. Batch System Nonpreemptive or preemptive with long CPU time periods for each process are acceptable. Interactive System CPU times are shared with several users sit on terminals for quick response. Preemptive scheduling scheme is necessary. Real Time System Preemptive scheduling scheme is not necessary sometimes 7 8 Scheduling in the Batch System First Come First Serve (Batch) Goal of batch system Throughput maximize number of process completed per time unit Turnaround time minimize the interval from the time of submission to the time of completion of a process. CPU utilization It is simplest CPU-scheduling algorithm and nonpreemptive. The implementation of the FCFS is easily managed with FIFO queue. When a process enter the ready queue, its PCB (process control block or process table) is linked onto the tail of the queue. When the CPU is free, scheduler chose a process from the head of ready queue. The average waiting time might be long with FCFS. 9 0 First Come First Serve (Batch) Shortest Job First (Batch) CPU time three processes are in the job queue to run. > > The algorithm associated with each process the length of next CPU time FCFS Optimal 0 7 0 Average waiting time = (0++7)/=7 0 6 0 t t CPU run time needed 6 (msec) 8(msec) 7(msec) (msec) Average waiting time = (0++6)/=
Shortest Job First (Batch) FCFS SJF CPU run time needed 6 (msec) 8(msec) 7(msec) (msec) Lets assume arriving times as (for FCFS) > > > 0 6 Average waiting time = (0+6++)/=0. 0 9 6 Average waiting time = (0++9+6)/=7 Shortest Job First (Batch) Shortest Job First algorithm is provably optimal in that it gives the minimal average waiting time for a given set of processes. But real difficulty with the SJF algorithm is knowing the length of CPU request. Since there is no way to know the length of next CPU time for a process The SJF algorithm can not be used for short-term scheduler (CPU scheduler). It can be used for long-term scheduler for the batch system. Since, CPU time for each batch job can be estimated. Shortest Remaining Time (Batch) Shortest Remaining Time (Batch) Preemptive version of the SJF algorithm is Shortest Remaining Time Next algorithm. When a new process is arrives at the ready queue while previous process is executing. The newly arrived process might be shorter than what is left of the current executing process. Arrival Time 0 CPU time 8 9 P 0 0 7 6 Average waiting time = ((0 ) + 0 + (7 ) + ( ))/ = 6. Average Turnaround time = ((7-0) + (-) + (6-)+(0-))/ = 6 Scheduling in Interactive System Round-Robin (Interactive) OS design Goal in Interactive System Fast response time The Round-Robin Scheduling algorithm is designed especially for the timesharing systems. A small unit of time called time quantum is defined (between 0 to 0 millisecond). Ready queue is treated as a circular queue. 7 8
Round-Robin (Interactive) Round-Robin (Interactive) The main issue with round-robin algorithm is the length of the quantum. If quantum is too short, many portion of CPU time are wasted for process switching low CPU utilization. If quantum is too long, slow response time. 9 0 A priority is assigned to each process CPU is allocated to a process based on the priority. Equal priority processes are scheduled in FCFS or Round-Robin based on nonpreemptive or preemptive. How to define a priority to each process? Internal way based on the measurable quantity or quantities to compute the priority of a process. ex) CPU time limits, memory requirements, the number of open files, average I/O bound and CPU/bound ratio. External way based on the importance of the process. Ex) school: faculty have high priority than student. company: A customer who pay more money can get high priority. A major problem with priority scheduling algorithm is the starvation of lower priority process. A solution to the problem of starvation of low priority process is aging. A aging is a technique of gradually increasing the priorities of processes that wait in the system for a long time.
Guaranteed Scheduling (Interactive) Guaranteed scheduling algorithm guarantees fairness by monitoring the amount of CPU time spent by each user and allocating resources (CPU) accordingly. To make fairness, system must keep track of how much CPU each process has had since its creation. If there are n processes in the system, each process will receive /n of the CPU power. Since the amount of CPU time each process has actually used is known, it is fairly easy to compute the ratio of actual CPU time consumed. Based ratio, scheduler select a process for CPU. Lottery Scheduling (Interactive) Lottery ticket is given to each process for various system resources. Whenever a scheduling decision has to be made, a lottery ticket is selected at random and the process holding that ticket gets the resources. 6