Você está na página 1de 31

Scheduling

Simma Rai

Basic Concept
In multiprogramming, multiple processes and threads competing for CPU at the same time

Scheduling
Scheduling
Set of policies and mechanisms that an OS supports for determining the order of execution of the pending jobs or processes

Scheduler
The part of the operating that makes the choice of which ready process to run next.

Scheduling algorithm
the algorithm Scheduler uses

Process Behavior
CPUI/O Burst Cycle Process execution consists of a cycle of CPU execution and I/O wait I/0 burst : when a process enters the blocked state waiting for an external device to complete its work.

compute-bound processes:
spend most of their time computing long CPU bursts and thus infrequent I/0 waits

I/O-bound processes:
spend most of their time waiting for I/O short CPU bursts and thus frequent I/O waits key factor is the length of the CPU burst i.e. the duration of computing

Alternating Sequence of CPU and I/O Bursts

Process Behavior

Levels/Types of Scheduler
1. long-term scheduler 2. mid-term or medium-term scheduler 3. short-term scheduler

Long-term scheduler
admission scheduler or high-level scheduler decides which jobs or processes are to be admitted to the ready queue (in the Main Memory) Admission of a process to the set of currently executing processes is either authorized or delayed by the long-term scheduler responsible for controlling the degree of multiprogramming long term queue exists in the Hard Disk or the "Virtual Memory"

Medium-term scheduler
Intermediate level Swapping in/out
removes processes from main memory and places them on secondary memory (such as a disk drive) or vice versa process which has not been active for some time, or has a low priority, or taking up a large amount of memory..

Short-term scheduler
Low level CPU scheduler decides which of the ready, in-memory processes are to be executed (allocated a CPU)

Scheduling Criteria
CPU utilization keep the CPU as busy as possible Throughput # of processes that complete their execution per time unit Turnaround time amount of time to execute a particular process

Waiting time amount of time a process has been waiting in the ready queue
Response time amount of time it takes from when a request was submitted until the first response is produced

Scheduling Algorithm Optimization Criteria


Max CPU utilization Max throughput Min turnaround time Min waiting time Min response time

When to Schedule
1. New process created
Choice between parent process or the child process

2. Process exit
Which ready process need to be run

3. Process block
Select another process to run

4. I/O interrupt
decide whether to run the newly ready process or the process that was running at the time of the interrupt, or some third process

Types of scheduling algorithm


1. non preemptive ("voluntary" or "co-operative")
not forcibly suspended
scheduler is unable to "force" processes off the CPU picks a process to run and then just lets it run until it blocks (either on I/O or waiting for another process) or until it voluntarily releases the CPU

2. preemptive (preempt: interrupt)


forcibly suspended
capable of forcibly removing processes from a CPU when it decides to allocate that CPU to another process picks a process and lets it run for a maximum of some fixed time

Scheduling Algorithm Goals


All systems
Fairness
giving each process a fair share of the CPU

Batch systems
Throughput
maximize jobs per hour Eg: system that can perform 30 jobs per hr is better than one that completes 20 per hr

Policy enforcement
seeing that stated policy is carried out

Balance
keeping all parts of the system busy (balance between I/O bound and CPU bound processes)

Turnaround time
minimize time between submission and termination

CPU utilization
keep the CPU busy all the time

Scheduling Algorithm Goals


Interactive systems
Response time
respond to requests quickly Eg: On a PC a user request to start a program or open a file should take precedence over the background processes

Real-time systems
Meeting deadlines
avoid losing data

Predictability

Proportionality
meet users' expectations

Scheduling in Batch System


First come First Served Shortest job first Shortest remaining time next

First come First Served


simplest algorithm non preemptive processes are assigned the CPU in the order they request processes have same priority How it works?
Basically, there is a single queue of ready processes. When the first job enters the system, it is started immediately and allowed to run as long as it wants to. It is not interrupted because it has run too long. As other jobs come in, they are put onto the end of the queue. When the running process blocks, the first process on the queue is run next. When a blocked process becomes ready, like a newly arrived job, it is put on the end of the queue

First-Come, First-Served (FCFS) Scheduling

Process Burst Time P1 24 P2 3 P3 3 Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is:
P1 P2 P3

24

27

30

Waiting time for P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

FCFS Scheduling (Cont.)


Suppose that the processes arrive in the order: P2 , P3 , P1 The Gantt chart for the schedule is:
P2 0 3 P3 6 P1 30

Waiting time for P1 = 6; P2 = 0; P3 = 3 Average waiting time: (6 + 0 + 3)/3 = 3 Much better than previous case

First come First Served


Benefits:
Easy to understand and implement
linked list can keep track of all ready process

Fair (like people waiting on queue to take tickets)

Disadvantage:
unfair for short job

Shortest Job First


non preemptive Example:

four jobs A, B, C, and D with run times of 8, 4, 4, 4 min respectively Turnaround time for A is 8 minutes, for B is 12 minutes, for C is 16 minutes, and for D is 20 minutes for an average of 14 minutes. With SJF algorithm, turnaround times are now 4, 8, 12, and 20 minutes for an average of 11 minutes

Shortest Job First


Consider the case of four jobs, with run times of a, b, c, and d, respectively.
Job 1 Turnaround time (min) a

2
3 4 Mean

a+b
a+b+c a+b+c+d (4a+3b+2c+d)/4

Conclusion:
a contributes more to the average than the other times, so it should be the shortest job, with b next, then c, and finally d as the longest as it affects only its own turnaround time.

Example of SJF
ProcessArrival Time Burst Time P1 0.0 6 P2 2.0 8 P3 4.0 7 P4 5.0 3 P P2 P4 3 P 1 SJF scheduling chart
0 3 9 16 24

Average waiting time = (3 + 16 + 9 + 0) / 4 = 7

Shortest Job First


Disadvantage:
only optimal when all the jobs are available simultaneously. Example: consider five jobs, A through E, with run times of 2, 4, 1, 1, and 1, respectively. Their arrival times are 0, 0, 3, 3, and 3. Using shortest job first we will run the jobs in the order A, B, C, D, E, for an average wait of 4.6. However, running them in the order B, C, D, E, A has an average wait of 4.4.

Shortest remaining time next


preemptive version of shortest job first process with shortest remaining run time picked by scheduler run time has to be known in advance How it works?
When a new job arrives, its total time is compared to the current process' remaining time. If the new job needs less time to finish than the current process, the current process is suspended and the new job started.

This scheme allows new short jobs to get good service.

Example of Shortest-remaining-time-first

ProcessA arri Arrival TimeT P1 0 P2 1 P3 2 P4 3 Preemptive SJF Gantt Chart


P1 P2 1 P4 P1 10

Burst Time 8 4 9 5

P3 17 26

Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec

Scheduling in Interactive systems


common on personal computers, servers
Round Robin Scheduling Priority Scheduling Shortest process next scheduling

Round Robin Scheduling


One of the oldest, simplest, fairest, and most widely used Each process is assigned a time interval, quantum, during which it is allowed to run If the process is still running at the end of the quantum, the CPU is preempted and given to another process. If the process has blocked or finished before the quantum has elapsed, the CPU switching is done when the process blocks All the scheduler needs to do is maintain a list of runnable processes When the process uses up its quantum, it is put on the end of the list

Round Robin Scheduling


Quantum size
too short
causes too many process switches and lowers the CPU efficiency Eg: If process switch- 1ms & quantum- 4ms, 20% CPU wastage on pre-emption overhead

too long
may cause poor response to short interactive requests Eg: If process switch- 1ms & quantum- 100ms, only 1% CPU wastage on pre-emption overhead but if there are 50 requests, the last request will get CPU only after 5s i.e. sluggish

Priority Scheduling
all processes may not be equally important. RR does not considers this PS algorithm does
Eg: On PC, a daemon process sending electronic mail in the background should be assigned a lower priority than a process displaying a video film on the screen in real time.

each process is assigned a priority the runnable process with the highest priority is allowed to run Priority Adjustment needed to prevent high-priority processes from running indefinitely. Two ways:
priority decreased along with time. Once the running process priority is less than the other, process switch occurs processes are allocated quantum. Highest priority processes allowed to run first

Priority assignment
Static Dynamic

Você também pode gostar