0% found this document useful (0 votes)
4 views

CPU Scheduling 1

CPU scheduling is a method that allows one process to utilize the CPU while others wait for resources, aiming for efficiency and fairness. It involves a short-term scheduler that selects processes from the ready queue and a dispatcher that controls CPU allocation. Various scheduling algorithms, such as FCFS, SJF, and Round Robin, are used to optimize CPU utilization, minimize waiting time, and ensure fair process execution.

Uploaded by

Jashan Saggu
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)
4 views

CPU Scheduling 1

CPU scheduling is a method that allows one process to utilize the CPU while others wait for resources, aiming for efficiency and fairness. It involves a short-term scheduler that selects processes from the ready queue and a dispatcher that controls CPU allocation. Various scheduling algorithms, such as FCFS, SJF, and Round Robin, are used to optimize CPU utilization, minimize waiting time, and ensure fair process execution.

Uploaded by

Jashan Saggu
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/ 15

What is CPU Scheduling?

CPU scheduling is a process which allows one process to use the CPU while the
execution of another process is on hold(in waiting state) due to unavailability of
any resource like I/O etc, thereby making full use of CPU. The aim of CPU
scheduling is to make the system efficient, fast and fair.

Whenever the CPU becomes idle, the operating system must select one of the
processes in the ready queue to be executed. The selection process is carried out by
the short-term scheduler (or CPU scheduler). The scheduler selects from among
the processes in memory that are ready to execute, and allocates the CPU to one of
them.

CPU Scheduling: Dispatcher


Another component involved in the CPU scheduling function is the Dispatcher.
The dispatcher is the module that gives control of the CPU to the process selected
by the short-term scheduler. This function involves:

 Switching context
 Switching to user mode

Types of CPU Scheduling


CPU scheduling decisions may take place under the following four circumstances:

 When a process switches from the running state to the waiting state(for I/O
request or invocation of wait for the termination of one of the child
processes).
 When a process switches from the running state to the ready state (for
example, when an interrupt occurs).
 When a process switches from the waiting state to the ready state(for
example, completion of I/O).
 When a process terminates.
In circumstances 1 and 4, there is no choice in terms of scheduling. A new
process(if one exists in the ready queue) must be selected for execution. There is a
choice, however in circumstances 2 and 3.When Scheduling takes place only under
circumstances 1 and 4, we say the scheduling scheme is non-preemptive;
otherwise the scheduling scheme is preemptive.

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.This scheduling method is used by the Microsoft
Windows 3.1 and by the Apple Macintosh operating systems.

Preemptive Scheduling
In this type of Scheduling, the tasks are usually assigned with priorities. At times it
is necessary to run a certain task that has a higher priority before another task
although it is running. Therefore, the running task is interrupted for some time and
resumed later when the priority task has finished its execution.

CPU Scheduling: Scheduling Criteria


There are many different criterias to check when considering the "best" scheduling
algorithm, they are:

CPU Utilization:To make out the best use of CPU and not to waste any CPU
cycle, CPU would be working most of the time(Ideally 100% of the time).
Considering a real system, CPU usage should range from 40% (lightly loaded) to
90% (heavily loaded.)

Throughput:It is the total number of processes completed per unit time or


rather say total amount of work done in a unit of time. This may range from
10/second to 1/hour depending on the specific processes.
Turnaround Time: It is the amount of time taken to execute a particular
process, i.e. The interval from time of submission of the process to the time of
completion of the process(Wall clock time).

Waiting Time: The sum of the periods spent waiting in the ready queue
amount of time a process has been waiting in the ready queue to acquire get control
on the CPU.

Load Average: It is the average number of processes residing in the ready


queue waiting for their turn to get into the CPU.

Response Time:Amount of time it takes from when a request was submitted


until the first response is produced. Remember, it is the time till the first response
and not the completion of process execution(final response).

In general CPU utilization and Throughput are maximized and other factors are
reduced for proper optimization.

Scheduling Algorithms
To decide which process to execute first and which process to execute last to
achieve maximum CPU utilisation, computer scientists have defined some
algorithms, they are:

 First Come First Serve(FCFS) Scheduling


 Shortest-Job-First(SJF) Scheduling
 Priority Scheduling
 Round Robin(RR) Scheduling
 Multilevel Queue Scheduling
First Come First Serve (FCFS): Simplest scheduling algorithm that
schedules according to arrival times of processes. First come first serve scheduling
algorithm states that the process that requests the CPU first is allocated the CPU
first. When the CPU is free, it is allocated to the process at the head of the queue.
The running process is then removed from the queue. FCFS is a non-preemptive
scheduling algorithm.

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.
 However, this method is poor in performance, and the general wait time is
quite high.

Note:First come first serve suffers from convoy effect.

Shortest Job First (SJF): Process which have the shortest burst time are
scheduled first.If two processes have the same bust time then FCFS is used to
break the tie. It is a non-preemptive scheduling algorithm.

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.
Shortest Remaining Time First (SRTF): It is preemptive mode of
SJF algorithm in which jobs are schedule according to shortest remaining time.

Round Robin Scheduling: Each process is assigned a fixed time(Time


Quantum/Time Slice) in cyclic way. It is designed especially for the time-sharing
system. The ready queue is treated as a circular queue. The CPU scheduler goes
around the ready queue, allocating the CPU to each process for a time interval of
up to 1-time quantum. To implement Round Robin scheduling, we keep the ready
queue as a FIFO queue of processes. New processes are added to the tail of the
ready queue. The CPU scheduler picks the first process from the ready queue, sets
a timer to interrupt after 1-time quantum, and dispatches the process. One of two
things will then happen. The process may have a CPU burst of less than 1-time
quantum. In this case, the process itself will release the CPU voluntarily. The
scheduler will then proceed to the next process in the ready queue. Otherwise, if
the CPU burst of the currently running process is longer than 1-time quantum, the
timer will go off and will cause an interrupt to the operating system. A context
switch will be executed, and the process will be put at the tail of the ready queue.
The CPU scheduler will then select the next process in the ready queue.

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.

Priority Based scheduling (Non-Preemptive): In this scheduling,


processes are scheduled according to their priorities, i.e., highest priority process
is scheduled first. If priorities of two processes match, then schedule according to
arrival time. Here starvation of process is possible.

Multilevel Queue Scheduling: According to the priority of process,


processes are placed in the different queues. Generally high priority process are
placed in the top level queue. Only after completion of processes from top level
queue, lower level queued processes are scheduled. It can suffer from starvation.

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.

Below are different time with respect to a process


Arrival Time: Time at which the process arrives in the ready queue.

Completion Time: Time at which process completes its execution.

Burst Time: Time required by a process for CPU 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

Why do we need scheduling?


A typical process involves both I/O time and CPU time. In a uni programming
system like MS-DOS, time spent waiting for I/O is wasted and CPU is free during
this time. In multi programming systems, one process can use CPU while another
is waiting for I/O. This is possible only with process scheduling.

Max throughput [Number of processes that complete their execution per time unit]

Min turnaround time [Time taken by a process to finish execution]

Min waiting time [Time a process waits in ready queue] Objectives of Process
Scheduling Algorithm

Max CPU utilization [Keep CPU as busy as possible]


Fair allocation of CPU.

Min response time [Time when a process produces first response]

Some useful facts about Scheduling Algorithms: FCFS can cause long waiting
times, especially when the first job takes too much CPU time.

Both SJF and Shortest Remaining time first algorithms may cause starvation.
Consider a situation when the long process.

Topic Thread
A thread is a single sequence stream within in a process. Because threads have
some of the properties of processes, they are sometimes called lightweight
processes. In a process, threads allow multiple executions of streams. In many
respect, threads are popular way to improve application through parallelism. a
thread can be in any of several states (Running, Blocked, Ready or Terminated).
Each thread has its own stack. Since thread will generally call different procedures
and thus a different execution history. This is why thread needs its own stack. An
operating system that has thread facility, the basic unit of CPU utilization is a
thread. A thread has or consists of a program counter (PC), a register set, and a
stack space. Threads are not independent of one other like processes as a result
threads shares with other threads their code section, data section, OS resources
also known as task, such as open files and signals.

Processes Vs Threads

As we mentioned earlier that in many respect threads operate in the same way as
that of processes. Some of the similarities and differences are:

Similarities:Like processes threads share CPU and only one thread active
(running) at a time.

Like processes, threads within a processes, threads within a processes execute


sequentially.

Like processes, thread can create children.

And like process, if one thread is blocked, another thread can run.
Differences: Unlike processes, threads are not independent of one another.

Unlike processes, all threads can access every address in the task .

Unlike processes, thread are design to assist one other. Note that processes might
or might not assist one another because processes may originate from different
users.

Why Threads?
Following are some reasons why we use threads in designing operating systems.A
process with multiple threads make a great server for example printer
server.Because threads can share common data, they do not need to use
interprocess communication.Because of the very nature, threads can take
advantage of multiprocessors. Threads are cheap in the sense that They only need a
stack and storage for registers therefore, threads are cheap to create. Threads use
very little resources of an operating system in which they are working. That is,
threads do not need new address space, global data, program code or operating
system resources. Context switching are fast when working with threads. The
reason is that we only have to save and/or restore PC, SP and registers. But this
cheapness does not come free - the biggest drawback is that there is no protection
between threads.

Difference between Process and Thread

S.N PROCESS THREAD


1 Process is heavy weight or Thread is light weight, taking
resource intensive. lesser resources than a process.
2 Process switching needs Thread switching does not need
interaction with operating to interact with operating
system system.
3 In multiple processing All threads can share same set of
environments, each process open files, child processes.
executes the same code but has
its own memory and file
resources.
4 If one process is blocked, then no While one thread is blocked and
other process can execute until waiting, a second thread in the
the first process is unblocked same task can run.

5 Multiple processes without using Multiple threaded processes use


threads use more resources. fewer resources.

6 In multiple processes each One thread can read, write or


process operates independently change another thread's data.
of the others.

Advantages of Thread
 Threads minimize the context switching time.
 Use of threads provides concurrency within a process.
 Efficient communication.
 It is more economical to create and context switch threads.
 Threads allow utilization of multiprocessor architectures to a greater scale
and efficiency.

Difference between User-Level & Kernel-


Level Thread
S.N. User-Level Threads Kernel-Level Thread

1 User-level threads are faster to Kernel-level threads are slower


create and manage to create and manage.

2 Implementation is by a thread Operating system supports


library at the user level. creation of Kernel threads.

3 User-level thread is generic and Kernel-level thread is specific to


can run on any operating system. the operating system.
4 Multi-threaded applications Kernel routines themselves can
cannot take advantage of be multithreaded.
multiprocessing.

Topic Process Management


Process:A process is basically a program in execution. The execution of a
process must progress in a sequential fashion. A process is defined as an entity
which represents the basic unit of work to be implemented in the system. To put it
in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned
in the program. When a program is loaded into the memory and it becomes a
process, it can be divided into four sections - stack, heap, text and data. The
following image shows a simplified layout of a process inside main memory -

Process Components
s.no Component Description
The process Stack
1 stack contains the
temporary data
such as
method/function
parameters, return
address and local
variables.

This is dynamically
2 Heap allocated memory
to a process during
its run time.

This includes the


3 Text current activity
represented by the
value of Program
Counter and the
contents of the
processor's
registers.

Program

A program is a piece of code which may be a single line or millions of lines. A


computer program is usually written by a computer programmer in a programming
language. For example, here is a simple program written in C programming
language -

#include <stdio.h>

int main() {

printf("Hello, World! \n");

return 0;

A computer program is a collection of instructions that performs a specific task


when executed by a computer. When we compare a program with a process, we
can conclude that a process is a dynamic instance of a computer program. A part of
a computer program that performs a well-defined task is known as an algorithm. A
collection of computer programs, libraries and related data are referred to as a
software.

Topic: process synchronization


Process Synchronization means sharing system resources by processes in a such a
way that, Concurrent access to shared data is handled thereby minimizing the
chance of inconsistent data. Maintaining data consistency demands mechanisms to
ensure synchronized execution of cooperating processes. Process Synchronization
was introduced to handle problems that arose while multiple process executions.
Some of the problems are discussed below.

Critical Section Problem

A Critical Section is a code segment that accesses shared variables and has to be
executed as an atomic action. It means that in a group of cooperating processes,at a
given point of time, only one process must be executing its critical section. If any
other process also wants to execute its critical section, it must wait until the first
one finishes.

Solution to Critical Section Problem

A solution to the critical section problem must satisfy the following three
conditions:

1. Mutual Exclusion:Out of a group of cooperating processes, only one process


can be in its critical section at a given point of time.

2. Progress:If no process is in its critical section, and if one or more threads want
to execute their critical section then any one of these threads must be allowed to
get into its critical section.

3. Bounded Waiting:After a process makes a request for getting into its critical
section, there is a limit for how many other processes can get into their critical
section, before this process's request is granted.So after the limit is reached, system
must grant the process permission to get into its critical section.

On the basis of synchronization, processes are categorized as one of the following


two types:
Independent Process : Execution of one process does not affects the execution of
other processes.

Cooperative Process : Execution of one process affects the execution of other


processes.

Process synchronization problem arises in the case of Cooperative process also


because resources are shared in Cooperative processes.

Race Condition When more than one processes are executing the same code or
accessing the same memory or any shared variable in that condition there is a
possibility that the output or the value of the shared variable is wrong so for that all
the processes doing race to say that my output is correct this condition known as
race condition. Several processes access and process the manipulations over the
same data concurrently, then the outcome depends on the particular order in which
the access takes place.

Peterson’s Solution
Peterson’s Solution is a classical software based solution to the critical section
problem.In Peterson’s solution, we have two shared variables: boolean flag[i]
:Initialized to FALSE, initially no one is interested in entering the critical section
int turn : The process whose turn is to enter the critical section.Peterson’s Solution
preserves all three conditions :Mutual Exclusion is assured as only one process can
access the critical section at any time. Progress is also assured, as a process outside
the critical section does not block other processes from entering the critical section.
Bounded Waiting is preserved as every process gets a fair chance.

Disadvantages of Peterson’s Solution

It involves Busy waiting

It is limited to 2 processes.

Synchronization Hardware
Many systems provide hardware support for critical section code. The critical
section problem could be solved easily in a single-processor environment if we
could disallow interrupts to occur while a shared variable or resource is being
modified. In this manner, we could be sure that the current sequence of instructions
would be allowed to execute in order without pre-emption. Unfortunately, this
solution is not feasible in a multiprocessor environment. Disabling interrupt on a
multiprocessor environment can be time consuming as the message is passed to all
the processors. This message transmission lag, delays entry of threads into critical
section and the system efficiency decreases.

Mutex Locks
As the synchronization hardware solution is not easy to implement for everyone, a
strict software approach called Mutex Locks was introduced. In this approach, in
the entry section of code, a LOCK is acquired over the critical resources modified
and used inside critical section, and in the exit section that LOCK is released.As
the resource is locked while a process executes its critical section hence no other
process can access it.

TOPIC Semaphores
A Semaphore is an integer variable, which can be accessed only through two
operations wait() and signal(). There are two types of semaphores : Binary
Semaphores and Counting Semaphores.

Binary Semaphores : They can only be either 0 or 1. They are also known as
mutex locks, as the locks can provide mutual exclusion. All the processes can share
the same mutex semaphore that is initialized to 1. Then, a process has to wait until
the lock becomes 0. Then, the process can make the mutex semaphore 1 and start
its critical section. When it completes its critical section, it can reset the value of
mutex semaphore to 0 and some other process can enter its critical section.

Counting Semaphores: They can have any value and are not restricted over a
certain domain. They can be used to control access to a resource that has a
limitation on the number of simultaneous accesses. The semaphore can be
initialized to the number of instances of the resource. Whenever a process wants to
use that resource, it checks if the number of remaining instances is more than zero,
i.e., the process has an instance available. Then, the process can enter its critical
section thereby decreasing the value of the counting semaphore by 1. After the
process is over with the use of the instance of the resource, it can leave the critical
section thereby adding 1 to the number of available instances of the resource.

You might also like