designed and presented by dr. ayman elshenawy elsefy · central to operating-system design. •in a...
TRANSCRIPT
Designed and Presented by
Dr. Ayman Elshenawy Elsefy
Dept. of Systems & Computer Eng..
AL-AZHAR University
Website : eaymanelshenawy.wordpress.com
Email : [email protected]
Reference
Operating System Concepts, ABRAHAM SILBERSCHATZ
Operating Systems
Chapter 5: CPU Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
• Thread Scheduling
• Multiple-Processor Scheduling
• Operating Systems Examples
• Algorithm Evaluation
Basic Concepts• The CPU is one of the primary computer resources, its scheduling is
central to operating-system design.
• In a single-processor system:
• only one process can run at a time;
• any others must wait until the CPU is free and can be rescheduled.
• Maximum CPU utilization obtained with multiprogramming
• A process is executed until it must wait for some I/O request execution.
• Several processes are kept in memory at one time.
• When one process has to wait, the operating system takes the CPU away from that process and gives the CPU to another process.
• This pattern continues.
• Every time one process has to wait, another process can take over use of the CPU.
CPU–I/O Burst Cycle• Process execution consists of a cycle of CPU (CPU burst) execution and
wait for I/O (I/O burst).
• Processes alternate between these two states.
• Process execution begins with a CPU burst, then an I/O burst, thenanother CPU burst, then another I/O burst, and so on.
• The final CPU burst ends with a system request to terminate execution.
• The durations of CPU bursts have been measured.
• An I/O-bound program has many short CPU bursts, A CPU-boundprogram might have a few long CPU bursts.
CPU–I/O Burst Cycle
CPU Scheduler• Selects a process from among the processes in ready queue, and allocates the
CPU to one of them
• Queue may be ordered in various ways
• CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state (the result of an I/O request or an invocation of wait for the termination of one of the child processes (non preemptive scheduling )
2. Switches from running to ready state (an Interrupt occurs) (preemptive scheduling )
3. Switches from waiting to ready (I/O Terminates)(preemptive scheduling )
4. Terminates(non preemptive scheduling )
• Under non-preemptive scheduling, once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU, either by terminating or by switching to the waiting state.
• All other scheduling is preemptive
• Consider access to shared data
• Consider preemption while in kernel mode
• Consider interrupts occurring during crucial OS activities
Dispatcher
• Dispatcher module gives control of the CPU to the process selected by the short-term scheduler; this involves:
• switching context
• switching to user mode
• jumping to the proper location in the user program to restart that program
• Dispatch latency – time it takes for the dispatcher to stop one process and start another running
Scheduling Criteria• Many criteria have been suggested for comparing CPU-scheduling algorithms.
• CPU utilization: keep the CPU as busy as possible ( rang from 0% to 100%).
• Throughput: # of processes that complete their execution per time unit
• Turnaround time: amount of time to execute a particular process (Turnaroundtime is the sum of the periods spent waiting to get into memory, waiting in theready queue, executing on the CPU, and doing I/O).
• Waiting time – amount of time a process has been waiting in the ready queue (thesum of the periods spent waiting in the ready queue).
• Response time – amount of time it takes from when a request was submitted untilthe first response is produced, not output (for time-sharing environment)
• Scheduling Requirements:• Max CPU utilization• Max throughput• Min turnaround time• Min waiting time• Min response time
Scheduling Algorithms
1. First-Come, First-Served Scheduling (FCFS)
• The process that requests the CPU first is allocated the CPU first.
• Can be Implemented using a FIFO queue.
• When a process enters the ready queue, its PCB is placed on the tail of the queue.
• CPU is allocated to the process at the head of the queue.
• The running process is then removed from the queue.
• The code for FCFS scheduling is simple to write and understand.
• Disadvantages:
• The average waiting time is often quite long.
• There is a convoy effect as all the other processes wait for the one big process to get off the CPU. This effect results in lower CPU and device utilization than might be possible if the shorter processes were allowed to go first.
Scheduling Algorithms
1. First-Come, First-Served Scheduling (FCFS)
Scheduling Algorithms
2. Shortest Job First (SJF)
• 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
• Could ask the user
• Disadvantage:
• How to know the length of the next CPU request.
• For long-term (job) scheduling in a batch system,
we can use as the length the process time limit that
a user specifies when he submits the job.
• Users must estimate the process time limit
accurately, since a lower value may mean faster
response.
• SJF scheduling is used frequently in long-term
scheduling.
Determining Length of Next CPU Burst• We may not know the length of the next CPU burst, but we may be able to predict its value.
• the next CPU burst will be similar in length to the previous ones.
• Computing an approximation of the length of the next CPU burst, we can pick the process
with the shortest predicted CPU burst.
• an exponential average of the measured lengths of previous CPU bursts.
Let tnbe the length of the nth CPU burst, and let n+1
be our predicted value for the next CPU burst. Then,
for , 0 ≤ ≤ 1, define
Example of Shortest-remaining-time-first
Priority Scheduling• A priority number (integer) is associated with each process
• Equal Priorities are served using FCFS algorithm
• The CPU is allocated to the process with the highest priority (smallest integer)
• SJF is priority scheduling, priority is the inverse of predicted next CPU burst time
• Problem Starvation – low priority processes may never execute
• Solution Aging – as time progresses increase the priority of the process
[P1(6)+P2(0)+P3(16)+P4(18)+P5(1)]/5=41/5=8.2
Round Robin (RR)• Each process gets a small unit of CPU time (time quantum q), usually 10-100ms.
• 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.
• Timer interrupts every quantum
to schedule next process
• Performance• q large FIFO
• q small q must be large with respect
• to context switch, otherwise
• overhead is too high
Time Quantum and Context Switch Time
80% of CPU bursts should be shorter than q
Turnaround Time Varies With The Time Quantum
Multilevel Queue• processes are easily classified into different groups
• Ready queue is partitioned into separate queues, eg:• foreground (interactive)
• background (batch)
• Process permanently in a given queue
• such as memory size, process priority, or process type
• Each queue has its own scheduling algorithm:• foreground – RR
• background – FCFS
• Scheduling must be done between the queues:• Fixed priority scheduling; (i.e., serve all from
foreground then from background).
Possibility of starvation.
• Time slice – each queue gets a certain amount of
CPU time which it can schedule amongst its
processes; i.e., 80% to foreground in RR
• 20% to background in FCFS
Multilevel Queue Scheduling
Multilevel Feedback Queue• A process can move between the various queues; aging can be implemented this
way
• Multilevel-feedback-queue scheduler defined by the following parameters:
• number of queues
• scheduling algorithms for each queue
• method used to determine when to upgrade a process
• method used to determine when to demote a process
• method used to determine which queue a process will enter when that process needs service
Example of Multilevel Feedback Queue• 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 FCFS
• 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 FCFS and receives 16 additional milliseconds
• If it still does not complete, it is preempted and moved to queue Q2
Thread Scheduling• Threads may be user-level and kernel-level threads.
• User-level threads:
• Managed by a thread library, the kernel is unaware of them.
• must be mapped to an associated kernel-level thread and may use alightweight process (LWP).
• Is known as process contention scope (PCS), competition for the CPU takesplace among threads belonging to the same process.
• PCS is done according to priority the scheduler selects the runnable threadwith the highest priority to run.
• Priorities are set by the programmer and are not adjusted by the thread library
• kernel-level thread:
• Uses system-contention scope (SCS).
• Competition for the CPU with SCS scheduling takes place among all threadsin the system.
Multiprocessor Scheduling• load sharing becomes possible but scheduling problems become more complex.
• Asymmetric multiprocessing
• all scheduling decisions, I/O processing, and other system activities handled by a single processor the master server.
• The other processors execute only user code.
• is simple because only one processor accesses the system data structures
• reducing the need for data sharing.
• Symmetric multiprocessing (SMP)
• Each processor is self-scheduling.
• All processes may be in a common ready queue, or each processor have its own queue.
• The scheduler for each processor examine the ready queue and select a process to execute.
• do not choose to schedule the same process and that processes
• avoid migration of processes from one processor to another (Processor affinity)
• Difficult to load process data from one cash and reload it into another.
• Sched_setaffinity()
Load Balancing• Attempts to keep the workload evenly distributed across all processors in an SMP
system.
• each processor has its own private queue of eligible processes to execute.
• push migration:
• a specific task periodically checks the load on each processor.
• If it finds an imbalance, it distributes the load by moving (or pushing) processes fromoverloaded to idle or less-busy processors.
• pull migration.
• Pull migration occurs when an idle processor pulls a waiting task from a busy processor.
Algorithm Evolution• How do we select a CPU-scheduling algorithm for a particular system?
• Maximizing CPU utilization under the constraint that the maximum responsetime is 1 second
• Maximizing throughput such that turnaround time is (on average) linearlyproportional to total execution time
Deterministic Model (Analytical)• Takes a particular predetermined workload and defines the performance of each
algorithm for that workload
Simulation• To get a more accurate evaluation of scheduling algorithms, we can use simulations.