Você está na página 1de 29

CS223 Operating Systems: Scheduling

D EPA RTM EN T O F C O M P U TER S C I EN C E A N D EN G I N EER IN G


Syllabus
Scheduling: Types of Scheduling, Scheduling Algorithms: FCFS, SJF, Priority, Round Robin.

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


CPU Scheduler
•Selects from among the processes in memory that are ready to execute, and
allocates the CPU to one of them
•CPU scheduling decisions may take place when a process:
1.Switches from running to waiting state
2.Switches from running to ready state
3.Switches from waiting to ready
4.Terminates
•Scheduling under 1 and 4 is nonpreemptive
•All other scheduling is preemptive
SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY
Scheduling Criteria
•By switching the CPU among processes, the Operating System can make the
computer more productive.

•Throughput – Number of processes that complete their execution per in unit time
•Turnaround time – Total amount of time needed to execute a particular process
•Waiting time – amount of time a process has been waiting in the ready queue prior to
its execution
•Response time – amount of time it takes from when a request was submitted until the
first response is produced, not output (for time-sharing environment)

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Types of processor scheduling
•Aim of the processor scheduling is to assign processor to processes over time, in a
way that meets system objectives i.e. throughput, response time and processor
efficiency.
•Scheduling is broken down into three categories:

◦ Long term scheduling: is performed when a new process is created.

◦ Medium term scheduling: determines which process shall be allowed to compete for the CPU

◦ Short term scheduling: determines which ready process will be assigned the CPU when it next becomes
available and actually assign the CPU to this process

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


New

Long term Long term

scheduling scheduling

short term
Suspended Ready Running Exit
medium term scheduling
scheduling

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Preemptive & Nonpreemptive Scheduling
There are four circumstances under CPU scheduling decisions will be performed:

1. When a process switches from running state to the waiting state.

2. When a process switches from running state to ready state. (interrupt)

3. When a process switches from waiting state to ready state.

4. When a process terminates.

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Scheduling Algorithms

•FCFS (First Come First Serve)


•SJF (Shortest Job First)
•Priority scheduling
•Round Robin scheduling

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


FCFS Scheduling: Characteristics

Selection Function: max(w), selects the process which is waiting in the ready
queue for maximum time.
Decision Mode : Non_preemptive
Throughput: Not emphasized
Response Time: May be high, especially if there is a large variance in the process
execution times.
Overhead: Minimum
Effect on Processes: Penalizes short and I/O bound processes.
Starvation: No
SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY
Computations :-
Completion Time: Time at which process completes its execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.

Waiting Time = Turn Around Time – Burst Time

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Process Burst Time
P1 24
P2 3
P3 3

•Suppose that the processes arrive in the order: P1 , P2 , P3

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

Waiting time for P1 = 0; P2 = 24; P3 = 27

Average waiting time: (0 + 24 + 27)/3 = 17

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order : P2 , P3 , P1

P2 P3 P1

0 3 6 30

Waiting time for P1 = 6; P2 = 0; P3 = 3


Average waiting time: (6 + 0 + 3)/3 = 3

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Example-2 of FCFS
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

P1 P2 P3 P4
0 7 11 12 16
Average waiting time = (0 + 5 + 7 + 7)/4 = 4.75 TAT : P1 : 7,P2 :9 , P3 :8, P4 : 11
Average Turnaround Time = (7+9+8+11)/4=8.75
WT
, : P1 :0 , P2 :5, P3 :7, P4 :7

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Shortest-Job-First (SJR) Scheduling
Associate with each process the length of its next CPU burst. Use these lengths to schedule the
process with the shortest time

Two schemes:
• nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU
burst
• preemptive – if a new process arrives with CPU burst length less than remaining time of current
executing process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF)

SJF is optimal – gives minimum average waiting time for a given set of processes

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Non-preemptive SJF

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Example of Non-Preemptive SJF

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

P1 P3 P2 P4

0 3 7 8 12 16

Waiting time :
P1 : 0, P3 : 3, P2 :6, P4:7 avg WT : 4
Turn around time :
P1 : 7, P3: 4, P2: 10, P4 :11 avg TAT :8

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Shortest Job First Preemptive or Shortest Remaining
Time
• It is a preemptive version of SJF. In this policy, scheduler always chooses the process that has the shortest
expected remaining processing time.
• When a new process arrives in the ready queue, it may in fact have a shorter remaining time than the
currently running process.
• Accordingly, the scheduler may preempt whenever a new process becomes ready.
• Scheduler must have an estimate of processing time to perform the selection function.

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Shortest Job First Preemptive or Shortest Remaining Time:
characteristics
• Selection Function: minimum total service time required by the process, minus time spent in
execution so far.
• Decision Mode : Preemptive ( At arrival time)
• Throughput: High
• Response Time: Provides good response time
• Overhead: Can be high
• Effect on Processes: Penalizes long processes.
• Starvation: Possible

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Example of Preemptive SJF (SRTN)
Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

TAT : P1 : 16 , P2 :5, P3 : 1, P4 :6 avg : TAT : 7


WT : P1 :9, P2: 1, P3:0, P4 :2 avg WT : 3

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Round Robin (RR)
Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds.
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.

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Example of RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Typically, higher average turnaround than SJF, but better response


SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY
Example: Round Robin (By Default preemptive: Time Quantum 2)

Process Arrival Time Burst Time


P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4

P1 P2 P1 P3 P2 P4 P1 P4 P1

0 2 4 6 7 9 11 13 15 16

Average waiting time = (9 + 3 + 2 +6)/4 =5


Average turnaround time =(16+7+3+10)/4=9

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Priority Scheduling
A priority number (integer) is associated with each process

The CPU is allocated to the process with the highest priority (smallest integer  highest
priority)
Problem  Starvation – low priority processes may never execute
Solution  Aging – as time progresses increase the priority of the process

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Difference Between Preemptive and Non Preemptive Priority Scheduling

In non-preemptive priority scheduling, once all the available processes are in the ready
queue, the scheduled process will run till the completion with no preemption.
In the preemptive priority scheduling, the process which is being executed can be
stopped at the arrival of a high priority process.
Some Formulas used to calculate turn around time and waiting time –
Turn Around Time = Completion Time – Arrival Time
Waiting Time = Turn Around Time – Burst Time

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Non Preemptive Priority Scheduling

Consider 4 processes P1, P2, P3 and P4

32 32 26
26 26 24
We can represent this example using GANTT chart as below –

26 32

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Preemptive Priority Scheduling

We can also represent this example using GANTT chart as below

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Some points to remember

Advantages of Priority Scheduling

1. Simplicity
2. Suitable for application with varying time and resource requirement
3. Process with high priority will execute first. It results in better response time.

Disadvantages of Priority Scheduling

1. Indefinite blocking or starvation.


2. Priority scheduling can leave some low priority waiting processes indefinitely for CPU.
3. If the system eventually crashes, then unfinished low priority process are lost.

Starvation

It is a situation in which the continuous arrival of higher priority process keeps the lowest priority process always in waiting state. The
waiting process will starve (in other words, the deadline of the waiting process will never meet). We can resolve the starvation problem in
the priority scheduling with the help of Aging technique.

Aging Technique

In Aging technique, the priority of every lower priority processes has to be increased after a fixed interval of time

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY


Exec system call
exec is used when the user wants to launch a new file or program in the same process.

•Current process image is overwritten with a new process image.

•New process image is the one you passed as exec argument

•The currently running process is ended

•New process image has same process ID, same environment, and same file descriptor (because process is not replaced process image is replaced)

•The CPU stat and virtual memory is affected. Virtual memory mapping of the current process image is replaced by virtual memory of new process
image.

execl
execle
execlp
execv
execve
execvp

SCHOOL OF COMPUTER ENGINEERING & TECHNOLOGY

Você também pode gostar