0% found this document useful (0 votes)
25 views14 pages

Operating System

Uploaded by

tanujkk996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views14 pages

Operating System

Uploaded by

tanujkk996
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COURSE PLAN

OPERATING SYSTEMS
UNIT II - CPU SCHEDULING

Objectives:
 Describe various CPU scheduling algorithms
 Assess CPU scheduling algorithms based on scheduling criteria
 Explain the issues related to multiprocessor and multi core scheduling
 Describe various real-time scheduling algorithms
 Describe the scheduling algorithms used in the Windows, Linux, and Solaris operating systems
 Apply modeling and simulations to evaluate CPU scheduling algorithms

Outcomes :
 Understands the use of different process scheduling algorithm
 Understand the role of the scheduler, and how its behaviour influences the performance of the
system.
 Know the difference between I/O-bound and CPU-bound tasks, and how they relate to
scheduling.
 Understand typical interactive and real time scheduling approaches.

Prerequisites:
 Knowledge about various tasks performed by Operating System.
 Knowledge about process, threads, multiprocessing and basic scheduling techniques.

Plan for the lecture delivery


 1. Teaching aid both Blackboard and Presentation Via LCD
 2. Topic should be started with Definition, Types of Scheduling, Major Scheduling Criteria
as follows:

What is CPU Scheduling?

 CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold.
 The main task of CPU scheduling is to make sure that whenever the CPU remains idle, the OS at
least select one of the processes available in the ready queue for execution.
 The selection process will be carried out by the CPU scheduler. It selects one of the processes in
memory that are ready for execution.

Types of CPU Scheduling

1
Preemptive Scheduling

In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is important to
run a task with a higher priority before another lower priority task, even if the lower priority task is still
running. The lower priority task holds for some time and resumes when the higher priority task finishes
its execution.

Non-Preemptive Scheduling

In this type of scheduling method, the CPU has been allocated to a specific process. The process that
keeps the CPU busy will release the CPU either by switching context or terminating. It is the only method
that can be used for various hardware platforms. That's because it doesn't need special hardware (for
example, a timer) like preemptive scheduling.

When scheduling is Preemptive or Non-Preemptive?

To determine if scheduling is preemptive or non-preemptive, consider these four parameters:

1. A process switches from the running to the waiting state.


2. Specific process switches from the running state to the ready state.
3. Specific process switches from the waiting state to the ready state.
4. Process finished its execution and terminated.
Only conditions 1 and 4 apply, the scheduling is called non- preemptive.
All other scheduling are preemptive.

Important CPU scheduling Terminologies

 Burst Time/Execution Time: It is a time required by the process to complete execution. It is also
called running time.
 Arrival Time: when a process enters in a ready state
 Finish Time: when process complete and exit from a system
 Multiprogramming: A number of programs which can be present in memory at the same time.
 Jobs: It is a type of program without any kind of user interaction.
 User: It is a kind of program having user interaction.
 Process: It is the reference that is used for both job and user.
 CPU/IO burst cycle: Characterizes process execution, which alternates between CPU and I/O
activity. CPU times are usually shorter than the time of I/O.

2
What is Dispatcher?

It is a module that provides control of the CPU to the process. The Dispatcher should be fast so that it can
run on every context switch. Dispatch latency is the amount of time needed by the CPU scheduler to stop
one process and start another.

Functions performed by Dispatcher:

 Context Switching
 Switching to user mode
 Moving to the correct location in the newly loaded program.

Types of CPU scheduling Algorithm

There are mainly six types of process scheduling algorithms

1. First Come First Serve (FCFS)


2. Shortest-Job-First (SJF) Scheduling
3. Shortest Remaining Time
4. Priority Scheduling
5. Round Robin Scheduling
6. Multilevel Queue Scheduling

1. First Come First Serve

 First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU scheduling
algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation
first.
 This scheduling method can be managed with a FIFO queue. As the process enters the ready
queue, its PCB (Process Control Block) is linked with the tail of the queue. So, when CPU
becomes free, it should be assigned to the process at the beginning of the queue.

Characteristics of FCFS method:

 It offers non-preemptive and pre-emptive scheduling algorithm.


 Jobs are always executed on a first-come, first-serve basis
 It is easy to implement and use.

3
 However, this method is poor in performance, and the general wait time is quite high.
Convoy Effect in FCFS

FCFS may suffer from the convoy effect if the burst time of the first job is the highest among all. As in
the real life, if a convoy is passing through the road then the other persons may get blocked until it passes
completely. This can be simulated in the Operating System also.

If the CPU gets the processes of the higher burst time at the front end of the ready queue then the
processes of lower burst time may get blocked which means they may never get the CPU if the job in the
execution has a very high burst time. This is called convoy effect or starvation.

2. Shortest Remaining Time

The full form of SRT is Shortest remaining time. It is also known as SJF preemptive scheduling. In this
method, the process will be allocated to the task, which is closest to its completion. This method prevents
a newer ready state process from holding the completion of an older process.

Characteristics of SRT scheduling method:

 This method is mostly applied in batch environments where short jobs are required to be given
preference.
 This is not an ideal method to implement it in a shared system where the required CPU time is
unknown.
 Associate with each process as the length of its next CPU burst. So that operating system uses
these lengths, which helps to schedule the process with the shortest possible time.

3. Priority Based Scheduling

 Priority scheduling is a method of scheduling processes based on priority. In this method, the
scheduler selects the tasks to work as per the priority.
 Priority scheduling also helps OS to involve priority assignments. The processes with higher
priority should be carried out first, whereas jobs with equal priorities are carried out on a round-
robin or FCFS basis. Priority can be decided based on memory requirements, time requirements,
etc.

4. Round-Robin Scheduling

4
Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm comes from the
round-robin principle, where each person gets an equal share of something in turn. It is mostly used for
scheduling algorithms in multitasking. This algorithm method helps for starvation free execution of
processes.

Characteristics of Round-Robin Scheduling

 Round robin is a hybrid model which is clock-driven


 Time slice should be minimum, which is assigned for a specific task to be processed. However, it
may vary for different processes.
 It is a real time system which responds to the event within a specific time limit.

5. Shortest Job First

SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process with the shortest
execution time should be selected for execution next. This scheduling method can be preemptive or non-
preemptive. It significantly reduces the average waiting time for other processes awaiting execution.

Characteristics of SJF Scheduling

 It is associated with each job as a unit of time to complete.


 In this method, when the CPU is available, the next process or job with the shortest completion
time will be executed first.
 It is Implemented with non-preemptive policy.
 This algorithm method is useful for batch-type processing, where waiting for jobs to complete is
not critical.
 It improves job output by offering shorter jobs, which should be executed first, which mostly
have a shorter turnaround time.

6. Multiple-Level Queues Scheduling

This algorithm separates the ready queue into various separate queues. In this method, processes are
assigned to a queue based on a specific property of the process, like the process priority, size of the
memory, etc.

However, this is not an independent scheduling OS algorithm as it needs to use other types of algorithms
in order to schedule the jobs.

Characteristic of Multiple-Level Queues Scheduling:

 Multiple queues should be maintained for processes with some characteristics.


 Every queue may have its separate scheduling algorithms.
 Priorities are given for each queue.

Example Demonstration

5
Round Robin Scheduling Example

In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their arrival time
and burst time are given below in the table. The time quantum of the system is 4 units.

Process ID Arrival Time Burst Time

1 0 5

2 1 6

3 2 3

4 3 1

5 4 5

6 6 4

According to the algorithm, we have to maintain the ready queue and the Gantt chart. The structure of
both the data structures will be changed after every scheduling.

Ready Queue:

Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units. Hence in the
ready queue, there will be only one process P1 at starting with CPU burst time 5 units.

P1

GANTT chart

The P1 will be executed for 4 units first.

Ready Queue

Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready queue. P1 has
not completed yet, it needs another 1 unit of time hence it will also be added back to the ready queue.

6
P2 P3 P4 P5 P1

6 3 1 5 1

GANTT chart

After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.

Ready Queue

During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has not
completed yet hence, P2 will also be added back to the ready queue with the remaining burst time 2 units.

P3 P4 P5 P1 P6 P2

3 1 5 1 4 2

GANTT chart

After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3 seconds.

Ready Queue

Since P3 has been completed, hence it will be terminated and not be added to the ready queue. The next
process will be executed is P4.

P4 P5 P1 P6 P2

1 5 1 4 2

7
GANTT chart

After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then the time
quantum hence it will be completed.

Ready Queue

The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence it will
not be added back to the queue.

P5 P1 P6 P2

5 1 4 2

GANTT chart

P5 will be executed for the whole time slice because it requires 5 units of burst time which is higher than
the time slice.

Ready Queue

P5 has not been completed yet; it will be added back to the queue with the remaining burst time of 1 unit.

P1 P6 P2 P5

1 4 2 1

GANTT Chart

The process P1 will be given the next turn to complete its execution. Since it only requires 1 unit of burst
time hence it will be completed.

8
Ready Queue

P1 is completed and will not be added back to the ready queue. The next process P6 requires only 4 units
of burst time and it will be executed next.

P6 P2 P5

4 2 1

GANTT chart

P6 will be executed for 4 units of time till completion.

Ready Queue

Since P6 is completed, hence it will not be added again to the queue. There are only two processes present
in the ready queue. The Next process P2 requires only 2 units of time.

P2 P5

2 1

GANTT Chart

P2 will get executed again, since it only requires only 2 units of time hence this will be completed.

9
Ready Queue

Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since the time
slice is of 4 units hence it will be completed in the next burst.

P5

GANTT chart

P5 will get executed till completion.

The completion time, Turnaround time and waiting time will be calculated as shown in the table below.

As, we know,
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turn Around Time - Burst Time

Process ID Arrival Time Burst Time Completion Time Turn Around Time Waiting Time

1 0 5 17 17 12

2 1 6 23 22 16

3 2 3 11 9 6

4 3 1 12 9 8

5 4 5 24 20 15

10
6 6 4 21 15 11

Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units

Conclusion:

 Scheduling algorithms should not affect the behavior of the system (same results regardless of schedule).
 However, the algorithms do impact the system's efficiency and response time.
 The best schemes are adaptive. To do absolutely best, we'd have to be able to predict the future.

Points in nutshell:

 Scheduling performance criteria and goals are dependent on environment


 There exists several different algorithms targeted for various systems
 Traditional OSes like Windows, UniX, Linux,usually uses a priority-based algorithm
 The right time slice can improve overall utilization

Points for understanding


 CPU scheduling is a process of determining which process will own CPU for execution while
another process is on hold.

 In Preemptive Scheduling, the tasks are mostly assigned with their priorities.

 In the Non-preemptive scheduling method, the CPU has been allocated to a specific process.

 Burst time is a time required for the process to complete execution. It is also called running time.

 The number of processes that finish their execution per unit time is known Throughput.

 Waiting time is an amount that specific process needs to wait in the ready queue.It is an amount
to time in which the request was submitted until the first response is produced.

 Turnaround time is an amount of time to execute a specific process.

 A dispatcher is a module that provides control of the CPU to the process.

 In the First Come First Serve method, the process which requests the CPU gets the CPU
allocation first.

 In the Shortest Remaining time, the process will be allocated to the task, which is closest to its
completion.

 In, Priority Scheduling the scheduler selects the tasks to work as per the priority.

11
 Round robin scheduling works on principle, where each person gets an equal share of
something in turn

 In Shortest job first the shortest execution time should be selected for execution next

 In Multilevel scheduling, method separates the ready queue into various separate queues. In this
method, processes are assigned to a queue based on a specific property

 The CPU uses scheduling to improve its efficiency.

Sample Question & Answer:


1. The processes that are residing in main memory and are ready and waiting to execute are kept
on a list called _____________
a) job queue
b) ready queue
c) execution queue
d) process queue
Answer: b

2. The interval from the time of submission of a process to the time of completion is termed as
____________
a) waiting time
b) turnaround time
c) response time
d) throughput
Answer: b

3. Which scheduling algorithm allocates the CPU first to the process that requests the CPU first?
a) first-come, first-served scheduling
b) shortest job scheduling
c) priority scheduling
d) none of the mentioned
Answer: a

4. In priority scheduling algorithm, when a process arrives at the ready queue, its priority is
compared with the priority of ____________
a) all process
b) currently running process
c) parent process
d) init process
Answer: b

5. Which algorithm is defined in Time quantum?


a) shortest job scheduling algorithm

12
b) round robin scheduling algorithm
c) priority scheduling algorithm
d) multilevel queue scheduling algorithm
Answer: b

6. Round robin scheduling falls under the category of ____________


a) Preemptive scheduling
b) Non-preemptive scheduling
c) All of the mentioned
d) None of the mentioned
Answer: a

7. There are 10 different processes running on a workstation. Idle processes are waiting for an
input event in the input queue. Busy processes are scheduled with the Round-Robin time
sharing method. Which out of the following quantum times is the best value for small
response times, if the processes have a short runtime, e.g. less than 10ms?
a) tQ = 15ms
b) tQ = 40ms
c) tQ = 45ms
d) tQ = 50ms
Answer: a

8. Which of the following algorithms tends to minimize the process flow time?
a) First come First served
b) Shortest Job First
c) Earliest Deadline First
d) Longest Job First
Answer: b

9. Which of the following statements are true? (GATE 2010)

I. Shortest remaining time first scheduling may cause starvation

II. Preemptive scheduling may cause starvation

III. Round robin is better than FCFS in terms of response time

a) I only
b) I and III only
c) II and III only

13
d) I, II and III

Answer: d

Final Conclusion :

For fixed priority scheduling algorithms, due to its minimum average waiting time and average
turnaround time, SJF scheduling algorithm serves all types of job with optimum scheduling criteria.
But long process will never been served.

To improve time management performance, we will deal with the advantages of the hardware
like parallel and dependent execution. An operating system where hardware and software model
coexist lead to raising several topics as communication overhead and resource optimization,
portability, suitable interface and mechanisms of synchronization.

14

You might also like