cpu schedulinghaggerty/teaching/cisc361/09... · 2009. 3. 18. · cpu scheduling remember process...

25
CPU Scheduling Scheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions. The cpu is an expensive resource so utilize it well. May have more than 1. With multiprogramming will have many processes (threads) to pick from. The scheduler runs an algorithm to decide which to pick. Want to make efficient use of cpu(s).

Upload: others

Post on 04-Feb-2021

0 views

Category:

Documents


0 download

TRANSCRIPT

  • CPU Scheduling

    Scheduling processes (or kernel-level threads) onto the cpu is one of the most important OS functions.The cpu is an expensive resource so utilize it well. May have more than 1.With multiprogramming will have many processes (threads) to pick from.The scheduler runs an algorithm to decide which to pick. Want to make efficient use of cpu(s).

  • CPU Scheduling

    Remember process switching is expensive. Need to save state.*I/O Bound and cpu bound processes *A cpu bound process will use the cpu for long periods of time between I/O use.An I/O bound process will use the cpu for short periods of time, spending a lot of time doing I/O.When a process is doing I/O take it off of the cpu and let a process in ready state run.

  • CPU Scheduling

    Non-preemptive algorithms let processes run until they give up the cpu or do I/O.

    Preemptive algorithms will interrupt a process after it has used up its time quantum (limit on cpu) and let another one run.

  • CPU Scheduling GoalsFairness – give all processes a chance to have cpu.Balance – keep all parts of system that can be used busy.Throughput – maximize jobs per time.Turnaround time – average time for job to finish – minimize it.CPU utilization – keep cpu busy.Response time – be responsive to interactive users.Enforce policy and meet user expectations

  • CPU Scheduling GoalsSome of the goals contradict each other.For example, better utilization would lead to worse response time. A better response time would decrease utilization

    Also for real time systems: Meet deadlines Predictability

    We'll look at some algorithms next.

  • First-Come First-Served (FCFS)Simple algorithm. Let first job in run first.Nonpreemptive.One queue of ready processes.Take off cpu when blocks and let next in line run. When process done blocking it goes to the end of the line.

    Like lines of people being served.

    Very simple to understand and code.

  • Problems with FCFSFavors cpu-bound over I/O bound process

    Penalizes short and I/O bound processes

    turnaround time = execution + wait time

    Waiting in line at the store or bank analogies.

  • Shortest Job First (SJF)Assumes know run times in advance.Nonpreemptive.Pick the shortest job in queue first and run

    Turnaround time is optimal.

    Penalizes long jobs.Good response time for short processes.

  • Shortest Remaining Time NextPreemptive version of SJF.Pick process with shortest remaining time.If a new job needs less time than the current running one, give it the cpu.Good response time for short jobs.

    Need to know time required.May starve long processes.

  • Round-Robin SchedulingWidely used, fair, simple, old, preemptiveTime-slicing

    Define a time quantum – the amount of time a process can stay on cpu. Once used up take it off. Also taken off when blocks. Take next job on list.

    Overhead (waste of cpu) from context switch – make quantum long enough.

  • Round-Robin Scheduling (cont)Too long of a quantum can give bad interactive response.

    Better performance if preemption is rare and process switch on I/O mostly.

    All jobs of equal importance.

  • Priority Scheduling Assign priorities and let highest priority process have cpu.Priority may change as time goes by.May also use a quantum.Priorities may be static or dynamic.Group processes into priority classes and use round-robin on each class.Starvation of low-priority jobs possible.

    Unix nice values.

  • Multiple QueuesIdea: Give cpu-bound process a large quantum and schedule them infrequentlyHave priority classes.Highest class run for one quantum. Next class two quantum, next 4 quantum and so on.

    If a process uses up its quantum (cpu-bound) it gets put in lower class.Interactive use could raise priority.

  • Fair share scheduling (FSS)Take into account who owns the process and be fair to the user rather than the processes.

    Added to Solaris 9.Very useful with zones (covered soon)

    Example: Give group A a share of 50, group B a share of 30 and group C a share of 20.

  • Some other algorithmsShortest Process Next - Run process with shortest estimated time based on past use.Guaranteed Scheduling - For n users (or processes) let each get 1/n of the cpu.Lottery Scheduling - Have lottery tickets. The winning prize is time on the cpu. Give higher priority processes more tickets.

  • Real-Time SchedulingMust get something done within a fixed period of time.

    Hard real time – absolute deadlines must be met.

    Soft real time – would rather not miss a deadline, but once in a while a miss is ok.

  • Thread SchedulingWith only user-level threads the kernel schedules the process and the thread scheduler of the process schedules the threads.

    With kernel-level threads the kernel picks a thread to run.

  • Solaris Process SchedulingKernel threads are scheduled.

    Derived from SVR4 and inherited 3 scheduling classes from it. - Time Share (TS) - Real Time (RT) - System (SYS)

    Later interactive (IA) was added.

  • Solaris Process ClassesTime Share (TS) – as threads run, their priority gets worse; as they wait, it gets better. Designed to fairly allocate cpu. Run thread until gives up cpu (blocking) or use up the time quantum.Real Time (RT) – for processes that need real-time requirements. Stay on cpu as long as need it.System (SYS) – Used by the OS.Interactive (IA) – To give better response to GUI desktop processes.

  • Solaris Process ClassesThe Fixed priority class (FX) was added more recently. It gives processes a fixed priority.

    TS, IA, RT and FX are dynamically loadable kernel modules.SYS is integral to the kernel.priocntl -l

    Solaris has global priorities where higher priorities are better.

  • Solaris Process Classes

    169

    160159

    10099

    60

    59

    0

    highInterrupts

    Real time

    System

    Time Shareand

    Interactivelow

    If RT is notloaded theninterrupts arefrom 100-109

    ps -eLclook at classand priorityof LWPs

  • More Solaris Process Classes

    The IA class is not much different than TS They share the same dispatch table and global priority range.Lower priority threads get a larger time quantum. The quantum gets smaller as priority increases as higher priority threads get scheduled more often.Time quantum is typically tens of ms.Priority of thread changes as time goes byEvery thread will be within 50-59 every second.

  • More Solaris Process Classes

    CPU hogs will end up in the low priority range as they keep using up quantum.If they do not use all of time quantum priority will increase.An IA class process has priority increased when it is the process with the current window focus.The SYS class has no dispatch table or time quantum. No time-slicing and no re-prioritization. Run until voluntarily give up processor.

  • Priority Inversion

    Happens when a lower priority thread prevents a higher priority thread from running as it is holding a critical resource needed. Solved by letting the lower priority thread inherit a higher priority and continue to run.

    Done to prevent a deadlock.

  • Linux Scheduling

    Schedules threads.

    1. Real-Time FIFO- is highest priority with no preemption except by new ones in this class.2. Real-Time RR – are preemptable with no deadlines.3. Timesharing – standard class