0% found this document useful (0 votes)
70 views31 pages

Os Unit 2 Lecture Notes 2

n/a

Uploaded by

ksach225
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)
70 views31 pages

Os Unit 2 Lecture Notes 2

n/a

Uploaded by

ksach225
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/ 31

lOMoARcPSD|45334098

OS Unit-2 - Lecture notes 2

Operating Systems (Jawaharlal Nehru Technological University, Hyderabad)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Sachin ([email protected])
lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

UNIT-II
PROCESS MANAGEMENT
PROCESS CONCEPT
The process concept includes the following:
1. Process
2. Process state
3. Process Control Block
4. Threads
Process
A process can be thought of as a program in execution (or) A process is the unit of work in a
modern time-sharing system.
A process will need certain resources such as CPU time, memory, files and I/O devices to
accomplish its task.
These resources are allocated to the process either when it is created or while it is executing.
The below figure shows the structure of process in memory:

The process contains several sections: Text, Data, Heap and Stack.
 Text Section contains the program code. It also includes the current activity, as
represented by the value of the program counter and the contents of the processor’s
registers.
 Process stack contains temporary data such as function parameters, return addresses and
local variables.
 Data section contains global variables.
 Heap is memory that is dynamically allocated during process run time.
Difference between Program and Process:
 A program is a passive entity, such as a file containing a list of instructions stored on disk
often called an executable file.
 A process is an active entity with a program counter specifying the next instruction to
execute and a set of associated resources.
 A program becomes a process when an executable file is loaded into memory.
Two common techniques for loading executable files are double-clicking an icon
representing the executable file and entering the name of the executable file on the command
line as in prog.exe or a.out.
Although two processes may be associated with the same program, they are considered as
two separate execution sequences.

32

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

For instance, several users may be running different copies of the mail program or the same
user may invoke many copies of the web browser program. Each of these is considered as a
separate process.
Process State
As a process executes, it changes state. The process state defines the current activity of that
process.
A process may be in one of the following states:
 New: The process is being created.
 Ready: The process is waiting to be assigned to a processor.
 Running: Instructions are being executed.
 Waiting: The process is waiting for some event to occur such as an I/O completion or
reception of a signal.
 Terminated: The process has finished execution.
Note: Only one process can be running on any processor at any instant of time.

Process Control Block


Each process is represented in the operating system by a Process Control Block (PCB). It is
also called a Task Control Block.
PCB serves as the repository for any information that may vary from process to process.

The PCB contains information related to process such as:


 Process state: The state may be new, ready, running, waiting and terminated.
 Program counter: The counter indicates the address of the next instruction to be
executed for this process.
 CPU registers: The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers and general-
purpose registers etc. Along with the program counter, this state information must be
saved when an interrupt occurs, to allow the process to be continued correctly afterward.

33

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 CPU-scheduling information: This information includes a process priority, pointers to


scheduling queues and any other scheduling parameters.
 Memory-management information: This information includes the base and limit
registers values, the page tables or the segment tables depending on the memory system
used by the operating system.
 Accounting information: This information includes the amount of CPU and real time
used, time limits, account numbers, job or process numbers and so on.
 I/O status information: This information includes the list of I/O devices allocated to the
process, a list of open files and so on.
Threads
In a single processor system a process is a program that performs a single thread of
execution.
 Example: When a process is running a word-processor program, a single thread of
instructions is being executed.
 This single thread of control allows the process to perform only one task at a time.
 The user cannot simultaneously type in characters and run the spell checker within the
same process.
In multicore system or multi-processor system allows a process to run multiple threads of
execution in parallel.
On a system that supports threads, the PCB is expanded to include information for each
thread.
Process Scheduling
The objective of multiprogramming is to have some process running at all times, to maximize
CPU utilization.
The objective of time sharing is to switch the CPU among processes so frequently that users
can interact with each program while it is running.
To meet these objectives, the Process Scheduler selects an available process for program
execution on the CPU.
Process scheduling involves three things:
1. Scheduling Queues
2. Schedulers
3. Context Switch
Scheduling Queues
There are several queues are implemented in operating system such as Job Queue, Ready
Queue, Device Queue.
 Job Queue: It consists of all processes in the system. As processes enter the system, they
are put into a job queue.
 Ready Queue: The processes that are residing in main memory and they are ready and
waiting to execute are kept on a list called the Ready Queue. Ready queue is generally
stored as a linked list. A ready-queue header contains pointers to the first and final PCBs
in the list. Each PCB includes a pointer field that points to the next PCB in the ready
queue.

34

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 Device Queue: Each device has its own device queue. It contains the list of processes
waiting for a particular I/O device.

Consider the above Queuing Diagram:


 Two types of queues are present: the Ready Queue and a set of Device Queues. CPU
and I/O are the resources that serve the queues.
 A new process is initially put in the ready queue. It waits there until it is selected for
execution or dispatched.
Once the process is allocated the CPU and is executing, one of several events could occur:
 The process could issue an I/O request and then be placed in an I/O queue.
 The process could create a new child process and wait for the child’s termination.
 The process could be removed forcibly from the CPU, as a result of an interrupt and be
put back in the ready queue.
Schedulers
A process migrates among the various scheduling queues throughout its lifetime. For
scheduling purpose, the operating system must select processes from these queues. The
selection process is carried out by the Scheduler.
There are three types of Schedulers are used:
1. Long Term Scheduler
2. Short Term Scheduler
3. Medium Term Scheduler
Long Term Scheduler (New to ready state)
 Initially processes are spooled to a mass-storage device (i.e Hard disk), where they are
kept for later execution.
 Long-term scheduler or job scheduler selects processes from this pool and loads them into
main memory for execution. (i.e. from Hard disk to Main memory).
 The long-term scheduler executes much less frequently, there may be minutes of time
between creation of one new process to another process.
 The long-term scheduler controls the degree of multiprogramming (the number of
processes in memory).
Short Term Scheduler (Ready to Running)

35

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 Short-term scheduler or CPU scheduler selects from among the processes that are ready
to execute and allocates the CPU to one of them. (i.e. a process that resides in main
memory will be taken by CPU for execution).
 The short-term scheduler must select a new process for the CPU frequently.
 The short term scheduler must be very fast because of the short time between executions
of processes.
Midium Term Scheduler
Medium Term Scheduler does two tasks:
1. Swapping: Medium-term scheduler removes a process from main memory and stores it
into the secondary storage. After some time, the process can be reintroduced into main
memory and its execution can be continued where it left off. This procedure is called
Swapping.
2. Medium Term Scheduler moves a process from CPU to I/O waiting queue and I/O queue
to ready queue.

The processes can be described as two types:


1. I/O bound process is one that spends more of its time doing I/O than it spends doing
computations.
2. CPU Bound process using more of its time doing computations and generates I/O
requests infrequently.
The long-term scheduler selects a good process mix of I/O-bound and CPU-bound processes.
 If all processes are I/O bound, the ready queue will almost always be empty and the CPU
will remain idle for long time because I/O device processing takes a lot of time.
 If all processes are CPU bound, the I/O waiting queue will almost always be empty. I/O
devices will be idle and CPU is busy for most of the time.
 Thus if the system maintains the combination of CPU bound and I/O bound processes
then the system performance will be increased.
Note: Time-sharing systems such as UNIX and Microsoft Windows systems often have no
long-term scheduler but simply put every new process in memory for the short-term
scheduler.
Context Switching
 Switching the CPU from one process to another process requires performing a state save
of the current process and a state restore of a different process. This task is known as a
Context Switch.
 The context is represented in the PCB of the process. It includes the value of the CPU
registers, the process state and memory-management information.

36

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 When a context switch occurs, the kernel saves the context of the old process in its PCB
and loads the saved context of the new process scheduled to run.
 Context-switch time is pure overhead, because the system does no useful work while
switching. Context switch time may be in few milliseconds.
Operations on Processes
1. Process Creation
2. Process Termination
Process Creation
 During the execution of a process in its life time, a process may create several new
processes.
 The creating process is called a parent process and the new processes are called children
process.
 Each of these new processes may create other processes forming a tree of processes.
 Operating system identifies processes according to process identifier (pid).
 Pid provides an unique integer number for each process in the system.
 Pid can be used as an index to access various attributes of a process within the kernel.
The below figure shows the process tree for the Linux OS that shows the name of each
process and its pid. In Linux process is called task.

 The init process always has a pid of 1. The init process serves as the root parent process
for all user processes.
 Once the system has booted, the init process can also create various user processes, such
as a web or print server, an ssh server etc.
 kthreadd and sshd are child processes of init.
 The kthreadd process is responsible for creating additional processes that perform tasks
on behalf of the kernel.
 The sshd process is responsible for managing clients that connect to the system by using
secure shell (ssh).
ps command is used to obtain a list of processes:
ps –el

37

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

The command will list complete information for all processes currently active in the system.
 When a process creates a child process, that child process will need certain resources such
as CPU time, memory, files, I/O devices to accomplish its task.
 A child process may be able to obtain its resources directly from the operating system or
it may be constrained to a subset of the resources of the parent process.
 The parent may have to partition its resources among its children or it may be able to
share some resources such as memory or files among several of its children.
 Restricting a child process to a subset of the parent’s resources prevents any process from
overloading the system by creating too many child processes.
When a process creates a new process there exist two possibilities for execution:
1. The parent continues to execute concurrently with its children.
2. The parent waits until some or all of its children have terminated.
There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (i.e) it has the same program and
data as the parent.
2. The child process has a new program loaded into it.
Process System calls in Unix/ Linux: fork( ), exec( ), wait( ), exit( )
 fork( ): In UNIX OS a new process is created by the fork( ) system call.
 The new process consists of a copy of the address space of the original process. This
mechanism allows the parent process to communicate easily with its child process.
 Both the parent and the child processes continue execution at the instruction after the
fork( ).
 For the new child process (i.e. Child Process) the return code for the fork( ) is zero.
 The nonzero process identifier of the child is returned to the parent.
 exec( ): After a fork( ) system call, one of the two processes typically uses the exec( )
system call to replace the process’s memory space with a new program.
 The exec( ) system call loads a binary file into memory and starts its execution.
 In this way, the two processes are able to communicate and then go their separate ways.
 wait( ): The parent can create more children or if the parent has nothing else to do while
the child process is running then the parent process can issue a wait( ) system call to
move itself out of the Ready Queue until the child process terminates.
 The call to exec( ) overlays the process’s address space with a new program or the call to
exec( ) does not return control unless an error occurs.

Program for Creating a separate process using the UNIX fork( ) system call
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

38

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

int main( )
{
pid_t pid;
/* fork a child process */
pid = fork( );
if (pid < 0)
{ /* error occurred */
fprintf(stderr, "Fork Failed");
return 1;
}
else if (pid == 0)
{ /* child process */
execlp("/bin/ls","ls",NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait(NULL);
printf("Child Complete");
}
return 0;
}
The above C program shows the UNIX system calls fork, exec, wait. Two different processes
are running copies of the same program.
 The only difference is that the value of pid for the child process is zero, while the value of
pid for the parent is an integer value greater than zero (i.e. the actual pid of the child
process).
 The child process inherits privileges and scheduling attributes from the parent, as well as
certain resources such as open files.
 The child process then overlays its address space with the UNIX command /bin/ls (used
to get a directory listing) using the execlp( ) system call (execlp( ) is a version of the
exec( ) system call).
 The parent waits for the child process to complete with the wait( ) system call.
 When the child process completes by either implicitly or explicitly invoking exit( ), the
parent process resumes from the call to wait( ), where it completes using the exit( )
system call.
Process Termination: exit( )
 A process terminates when it finishes executing its final statement and asks the operating
system to delete it by using the exit( ) system call.
 The process may return a status value to its parent process via the wait( ) system call.
 All the resources of the process including physical and virtual memory, open files and I/O
buffers are deallocated by the operating system.
A parent may terminate the execution of one of its children for a variety of reasons such as:

39

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

1. The child has exceeded its usage of some of the resources that it has been allocated.
2. The task assigned to the child is no longer required.
3. The parent is exiting and the operating system does not allow a child to continue if its
parent terminates.
Cascading Termination
If a parent process terminates either normally or abnormally then all its children must also be
terminated is referred as Cascading Termination. It is normally initiated by operating system.
In Linux and UNIX systems, a process can be terminate by using the exit( ) system call
providing an exit status as a parameter:
/* exit with status 1 */
exit(1);
Under normal termination, exit( ) may be called either directly (i.e. exit(1)) or indirectly (i.e.
by a return statement in main( ) ).
A parent process may wait for the termination of a child process by using the wait( ) system
call. The wait( ) system call is passed a parameter that allows the parent to obtain the exit
status of the child. This system call also returns the process identifier of the terminated child
so that the parent can tell which of its children has terminated:
pid_t pid;
int status;
pid = wait(&status);
Zombie process
A process that has terminated but whose parent has not yet called wait( ) is known as a
zombie process.
 When a process terminates, its resources are deallocated by the operating system. Its entry
in the process table must remain there until the parent calls wait( ), because the process
table contains the process’s exit status.
 Once the parent calls wait( ), the process identifier of the zombie process and its entry in
the process table are released.
Orphan Processes
If a parent did not invoke wait( ) and instead terminated, thereby leaving its child processes
as orphans are called Orphan processes.
 Linux and UNIX address this scenario by assigning the init process as the new parent to
orphan processes.
 The init process periodically invokes wait( ), thereby allowing the exit status of any
orphaned process to be collected and releasing the orphan’s process identifier and
process-table entry.
CPU SCHEDULING
CPU scheduling is the basis of Multi-programmed operating systems. By switching the CPU
among processes, the operating system can make the computer more productive.
 In a single-processor system, only one process can run at a time. Others must wait until
the CPU is free and can be rescheduled.

40

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 The CPU will sit idle and waiting for a process that needs an I/O operation to complete. If
the I/O operation completes then only the CPU will start executing the process. A lot of
CPU time has been wasted with this procedure.
 The objective of multiprogramming is to have some process running at all times to
maximize CPU utilization.
 When several processes are in main memory, if one processes is waiting for I/O then the
operating system takes the CPU away from that process and gives the CPU to another
process. Hence there will be no wastage of CPU time.
Concepts of CPU Scheduling
1. CPU–I/O Burst Cycle
2. CPU Scheduler
3. Pre-emptive Scheduling
4. Dispatcher
CPU–I/O Burst Cycle
Process execution consists of a cycle of CPU execution and I/O wait.
 Process execution begins with a CPU burst. That is followed by an I/O burst. Processes
alternate between these two states.
 The final CPU burst ends with a system request to terminate execution.
 Hence the First cycle and Last cycle of execution must be CPU burst.

CPU Scheduler
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.
Preemptive Scheduling
CPU-scheduling decisions may take place under the following four cases:
1. When a process switches from the running state to the waiting state.
Example: as the result of an I/O request or an invocation of wait( ) for the termination of a
child process.
2. When a process switches from the running state to the ready state.
Example: when an interrupt occurs
3. When a process switches from the waiting state to the ready state.
Example: at completion of I/O.
4. When a process terminates.
For situations 2 and 4 are considered as Pre-emptive scheduling situations. Mach OS X,
WINDOWS 95 and all subsequent versions of WINDOWS are using Preemptive scheduling.
41

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler. Dispatcher function involves:
1. Switching context
2. Switching to user mode
3. Jumping to the proper location in the user program to restart that program.
The dispatcher should be as fast as possible, since it is invoked during every process switch.
The time it takes for the dispatcher to stop one process and start another process running is
known as the Dispatch Latency.
SCHEDULING CRITERIA
Different CPU-scheduling algorithms have different properties and the choice of a particular
algorithm may favor one class of processes over another.
Many criteria have been suggested for comparing CPU-scheduling algorithms:
 CPU utilization: CPU must be kept as busy as possible. CPU utilization can range from
0 to 100 percent. In a real system, it should range from 40 to 90 percent.
 Throughput: The number of processes that are completed per time unit.
 Turn-Around Time: It is the interval from the time of submission of a process to the
time of completion. Turnaround time is the sum of the periods spent waiting to get into
memory, waiting in the ready queue, executing on the CPU and doing I/O.
 Waiting time: It is the amount of time that a process spends waiting in the ready queue.
 Response time: It is the time from the submission of a request until the first response is
produced. Interactive systems use response time as its measure.
Note: It is desirable to maximize CPU utilization and Throughput and to minimize Turn-
Around Time, Waiting time and Response time.
CPU SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the processes in the ready
queue is to be allocated the CPU. Different CPU-scheduling algorithms are:
1. First-Come, First-Served Scheduling (FCFS)
2. Shortest-Job-First Scheduling (SJF)
3. Priority Scheduling
4. Round Robin Scheduling
5. Multilevel Queue Scheduling
6. Multilevel Feedback Queue Scheduling
Gantt Chart is a bar chart that is used to illustrates a particular schedule including the start
and finish times of each of the participating processes.
First-Come, First-Served Scheduling (FCFS)
In FCFS, the process that requests the CPU first is allocated the CPU first.
 FCFS scheduling algorithm is Non-preemptive.
 Once the CPU has been allocated to a process, it keeps the CPU until it releases the CPU.
 FCFS can be implemented by using FIFO queues.
 When a process enters the ready queue, its PCB is linked onto the tail of the queue.
 When the CPU is free, it is allocated to the process at the head of the queue.

42

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 The running process is then removed from the queue.


Example:1 Consider the following set of processes that arrive at time 0. The processes are
arrived in the order P1, P2, P3, with the length of the CPU burst given in milliseconds.
Process Burst Time
P1 24
P2 3
P3 3
Gantt Chart for FCFS is:

The average waiting time under the FCFS policy is often quite long.
 The waiting time is 0 milliseconds for process P1, 24 milliseconds for process P2 and 27
milliseconds for process P3.
 Thus, the average waiting time is (0 + 24 + 27)/3 = 17 milliseconds.
Convoy Effect in FCFS
Convoy effect means, when a big process is executing in CPU, all the smaller processes must
have to wait until the big process execution completes. This will effect the performance of the
system.
Example:2 Let us consider same example above but with the processes arrived in the order
P2, P3, P1.

The processes coming at P2, P3, P1 the average waiting time (6 + 0 + 3)/3 = 3 milliseconds
whereas the processes are came in the order P1, P2, P3 the average waiting time is 17
milliseconds.
Disadvantage of FCFS:
FCFS scheduling algorithm is Non-preemptive, it allows one process to keep CPU for long
time. Hence it is not suitable for time sharing systems.

Shortest-Job-First Scheduling (SJF)


SJF algorithm is defined as “when the CPU is available, it is assigned to the process that has
the smallest next CPU burst”. If the next CPU bursts of two processes are the same, FCFS
scheduling is used between two processes.
SJF is also called as Shortest-Next CPU-Burst algorithm, because scheduling depends on
the length of the next CPU burst of a process, rather than its total length.
Example: Consider the following processes and CPU burst in milliseconds:
Process Burst Time
P1 6
P2 8
P3 7
P4 3

43

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Gantt Chart of SJF algorithm:

Waiting Time for Processes:


Process Burst Time (ms) Waiting Time
P1 6 3
P2 8 16
P3 7 9
P4 3 0
Average Waiting Time 7 ms
 By looking at the above table the average waiting time by using SJF algorithm is 7ms.
 SJF gives the minimum average waiting time for a given set of processes. SJF is optimal.
 The average waiting time decreases because moving a short process before long process
decrease the waiting time of the short process more than it increases the waiting time of
the long process.
Difficulty with SJF
The difficulty with the SJF algorithm is ―knowing the length of the next CPU request‖. With
Short-Term Scheduling, there is no way to know the length of the next CPU burst. It is not
implemented practically.
Solution for the difficulty
One approach to this problem is to try to approximate SJF scheduling.
 We may not know the length of the next CPU burst, but we may be able to predict its
value. We expect that the next CPU burst will be similar in length to the previous ones.
 By computing an approximation of the length of the next CPU burst, we can pick the
process with the shortest predicted CPU burst.
 The next CPU burst is generally predicted as an Exponential Average of the measured
lengths of previous CPU bursts.
The following formula defines the Exponential average:

tn be the length of the nth CPU burst (i.e. contains the most recent information).
stores the past history.
be our predicted value for the next CPU burst.
α controls the relative weight of recent and past history in our prediction (0 ≤ α ≤1)
 If α=0, then , recent history has no effect
 If α=1 then , only the most recent CPU burst matters.
 If α = 1/2, so recent history and past history are equally weighted.
Shortest Remaining Time First Scheduling (SRTF)
SRTF is the pre-emptive SJF algorithm.
 A new process arrives at the ready queue, while a previous process is still executing.

44

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 The next CPU burst of the newly arrived process may be shorter than the currently
executing process.
 SRTF will preempt the currently executing process and executes the shortest job.
Consider the four processes with arrival times and burst times in milliseconds:
Process Arrival time Burst Time (ms)
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Gantt Chart for SRTF

 Process P1 is started at time 0, since it is the only process in the queue.


 Process P2 arrives at time 1. The remaining time for process P1 (7 milliseconds) is larger
than the time required by process P2 (4 milliseconds), so process P1 is preempted and
process P2 is scheduled.
 The average waiting time = 26/4 = 6.5 milliseconds.
Priority Scheduling
A priority is associated with each process and the CPU is allocated to the process with the
highest priority. Equal-priority processes are scheduled in FCFS order.
 An SJF algorithm is special kind of priority scheduling algorithm where small CPU burst
will have higher priority.
 Priorities can be defined based on time limits, memory requirements, the number of open
files etc.
Example: Consider the following processes with CPU burst and Priorities. All the processes
are arrived at time t=0 in the same order. Low numbers are having higher priority.
Process Burst time (ms) Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Gantt chart for Priority Scheduling:

Process Burst time (ms) Waiting Time


P1 10 6
P2 1 0
P3 2 16
P4 1 18
P5 5 1
Average Waiting Time 8.2 ms

45

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Priority scheduling can be either Preemptive or Non-preemptive.


A Preemptive Priority Scheduling algorithm will preempt the CPU if the priority of the
newly arrived process is higher than the priority of the currently running process.
Problem: Starvation or Indefinite Blocking
 In priority Scheduling when there is a continuous flow of higher priority processes has
come to ready queue then all the lower priority processes must have to wait for the CPU
until the all the higher priority processes execution completes.
 This leads to lower priority processes blocked from getting CPU for long period of time.
This situation is called Starvation or Indefinite blocking.
 In worst case indefinite blocking may take years to execute the process.
Solution: Aging
Aging involves gradually increasing the priority of processes that wait in the system for a
long time.
Round-Robin Scheduling (RR)
Round-Robin (RR) scheduling algorithm is designed especially for Timesharing systems.
 RR is similar to FCFS scheduling, but preemption is added to enable the system to switch
between processes.
 A small unit of time called a Time Quantum or Time Slice is defined. A time quantum is
generally from 10 to 100 milliseconds in length.
 The ready queue is treated as a Circular queue. New processes are added to the tail of
the ready queue.
 The CPU scheduler goes around the ready queue by allocating the CPU to each process
for a time interval of up to 1 time quantum and dispatches the process.
 If a process CPU burst exceeds 1 time quantum, that process is preempted and is put back
in the ready queue.
In RR scheduling one of two things will then happen.
1. The process may have a CPU burst of less than 1 time quantum. The process itself will
release the CPU voluntarily. The scheduler will then proceed to the next process in the
ready queue.
2. 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.
Consider the following set of processes that arrive at time 0 and the processes are arrived in
the order P1, P2, P3 and Time Quanta=4.
Process Burst Time
P1 24
P2 3
P3 3
Gantt chart of Round Robin Scheduling

46

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 If we use a time quantum of 4 milliseconds, then process P1 gets the first 4 milliseconds.
 Since it requires another 20 milliseconds, it is preempted after the first time quantum and
the CPU is given to the next process in the queue, process P2.
 CPU burst of Process P2 is 3, so it does not need 4 milliseconds then it quits before its
time quantum expires. The CPU is then given to the next process P3.
 Once each process has received 1 time quantum, the CPU is returned to process P1 for an
additional time quantum.
The average waiting time under the RR policy is often long.
 P1 waits for 6 milliseconds (10 - 4), P2 waits for 4 milliseconds and P3 waits for 7
milliseconds. Thus, the average waiting time is 17/3 = 5.66 milliseconds.
The performance of the RR algorithm depends on the size of the Time Quantum.
 If the time quantum is extremely large, the RR policy is the same as the FCFS policy.
 If the time quantum is extremely small (i.e. 1 millisecond) the RR approach can result in a
large number of context switches.
 The time taken for context switch value should be a small fraction of Time quanta then
the performance of the RR will be increased.
Note: A rule of thumb is that 80 percent of the CPU bursts should be shorter than the time
quantum.
Multi-Level Queue Scheduling (MLQ)
In Multilevel Queue Scheduling algorithm the processes are classified into different groups.
 A Multilevel queue scheduling partitions the ready queue into several separate queues.
 The processes are permanently assigned to one queue based on memory size, process
priority or process type. Each queue has its own scheduling algorithm.
Example: Foreground processes have highest priority over background processes and these
processes have different response times hence it needs different scheduling.

The above figure shows Multi-level queue scheduling algorithm with five queues, listed
below in order of priority:
1. System processes
2. Interactive processes
3. Interactive editing processes
4. Batch processes
5. Student processes

47

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Each queue has absolute priority over lower-priority queues.


 No lower level queue processes will start executing unless all the processes in higher
level queue are empty.
Example: The interactive processes start executing only when all the processes in system
queue are empty.
 If a lower priority process is executing and an higher priority process entered into the
queue then lower priority process will be preempted and starts executing a higher priority
process.
Example: If a system process entered the ready queue while an interactive process was
running, the interactive process would be preempted.
Disadvantage: Starvation of Lower level queue
In multilevel queue scheduling algorithm is inflexible.
 The processes are permanently assigned to a queue when they enter the system. Process
are not allowed to move from one queue to other queue.
 There is a chance that lower level queues will be in starvation because unless the higher
level queues are empty no lower level queues will be executing.
 If at any instant of time if there is a process in higher priority queue then there is no
chance that lower level process can be executed eternally.
Multilevel Feedback Queue Scheduling is used to overcome the problem of Multi-level queue
scheduling.
Multilevel Feedback Queue Scheduling (MLFQ)
Multilevel feedback queue scheduling algorithm allows a process to move between queues.
 Processes are separated according to the characteristics of their CPU bursts.
 If a process uses too much CPU time, it will be moved to a lower-priority queue.
 A process that waits too long in a lower-priority queue moved to a higher-priority queue.
 This form of aging prevents starvation.
Consider a multilevel feedback queue scheduler with three queues: queue0, queue1, queue2.
 The scheduler first executes all processes in queue0 then queue1 and then queue2.
 Only when queue0 and queue1 is empty, scheduler will execute processes in queue2.
 A process that arrives for queue1 will preempt a process in queue2. A process in queue1
will in turn be preempted by a process arriving for queue0.

48

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 A process entering the ready queue is put in queue0. A process in queue 0 is given a time
quantum of 8ms. If it does not finish within this time, it is moved to the tail of queue 1.
 If queue 0 is empty, the process at the head of queue1 is given a quantum of 16ms. If it
does not complete, it is preempted and is put into queue2.
 Processes in queue 2 are run on an FCFS basis but are run only when queues 0 and 1 are
empty.
 This scheduling algorithm gives highest priority to any process with a CPU burst of 8ms
or less. Such a process will quickly get the CPU and finish its CPU burst and go off to its
next I/O burst.
 Processes that need more than 8ms but less than 24ms are also served quickly, although
with lower priority than shorter processes.
 Long processes automatically sink to queue2 and are served in FCFS order with any CPU
cycles left over from queues0 and queue1.
A Multi-Level Feedback queue scheduler is defined by the following parameters:
 The number of queues.
 The scheduling algorithm for each queue.
 The method used to determine when to upgrade a process to a higher priority queue.
 The method used to determine when to demote a process to a lower priority queue.
 The method used to determine which queue a process will enter when that process needs
service.
THREAD SCHEDULING
Threads can be divided into two types: Kernel level threads and User level threads.
 Kernel-level threads are being scheduled by the operating system.
 User-level threads are managed by a Thread Library and the kernel is unaware of them.
 User-level threads must be mapped to an associated kernel-level thread to run on a CPU
Contention scope
 The thread library schedules user-level threads to run on an available Light Weight
Process. This scheme is known as Process-Contention Scope (PCS). There will be a
competition for the CPU among threads belonging to the same process.
 PCS is done according to priority. The scheduler selects the runnable thread with the
highest priority to run. User-level thread priorities are set by the programmer and are not
adjusted by the thread library.
 PCS will preempt the thread currently running in favor of a higher-priority thread.
 To decide which kernel-level thread to schedule onto a CPU, the kernel uses System-
Contention Scope (SCS). Competition for the CPU with SCS scheduling takes place
among all threads in the system.
 Windows, Linux and Solaris are the systems schedule threads using only SCS.
MULTIPLE-PROCESSOR SCHEDULING
Scheduling process will become complex with multiple CPU structure but Load Sharing is
possible with multiple CPU structure.
In multiple processor system, we can use any available processor to run any process in the
queue.

49

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Multiprocessor Scheduling Approaches


There are two approaches of multiprocessing: Asymmetric and Symmetric Multiprocessing.
 In Asymmetric Multiprocessing, Master and Worker relationship exist.
The master server is a single processor that handles the activities related to all scheduling
decisions, I/O processing and other system activities. The other processors execute only
user code.
 Asymmetric multiprocessing is simple because only one processor accesses the system
data structures, reducing the need for data sharing.
 In Symmetric Multiprocessing (SMP) each processor is self-scheduling. All processes
may be in a common ready queue or each processor may have its own private ready
queue processes.
 Scheduling proceeds by having the scheduler for each processor examine the ready queue
and select a process to execute.
 When multiple processors trying to access and update a common data structure, the
scheduler must be programmed carefully.
 Two separate processors do not choose to schedule the same process and that processes
are not lost from the queue.
Processor Affinity
The Process Affinity is “to make a process run on the same CPU it ran on last time”.
 The processor cache contains the data that is most recently accessed by the process.
 If the process migrates from one processor to another processor, the contents of cache
memory must be invalidated for the first processor and the cache for the second processor
must be entered again.
 Because of the high cost of invalidating and re-entering of caches, most SMP systems try
to avoid migration of processes from one processor to another and instead attempt to keep
a process running on the same processor.
Processor affinity can be implemented in two ways: Soft affinity and Hard affinity.
 Soft affinity: The operating system will attempt to keep a process on a single processor,
but it is possible for a process to migrate between processors.
 Hard affinity: Some systems provide system calls that support Hard Affinity, thereby
allowing a process to specify a subset of processors on which it may run.
Load Balancing
Load balancing attempts to keep the workload evenly distributed across all processors in an
SMP system.
Load balancing is necessary only on systems where each processor has its own private queue
of processes to execute.
There are two approaches to load balancing: Push Migration and Pull Migration.
 Push migration: A specific process periodically checks the load on each processor. If the
task finds an imbalance then it evenly distributes the load by moving (pushing) processes
from overloaded processor to idle or less-busy processors.
 Pull migration: It occurs when an idle processor pulls a waiting process from a busy
processor.

50

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Multicore Processors
In multicore processor each core acts as a separate processor. Multicore processors may
complicate scheduling issues.
 When a processor accesses memory, it spends a significant amount of time waiting for the
data to become available. This waiting time is called Memory Stall.
 Memory Stall may occur for several reasons for example Cache miss.
 In order to avoid memory stall many recent hardware designs have implemented
multithreaded processor cores in which two or more hardware threads are assigned to
each core. That way, if one thread stalls while waiting for memory, the core can switch to
another thread.

 The execution of thread0 and the execution of thread 1 are interleaved on a dual-threaded
processor core.
 From an operating-system perspective, each Hardware Thread appears as a Logical
Processor that is available to run a software thread.
 Thus, on a Dual-threaded, Dual-core system, Four logical processors are presented to the
operating system.
There are two ways to multithread a processing core: Coarse-grained and Fine-grained
Coarse-Grained Multithreading:
 A thread executes on a processor until a long-latency event such as a memory stall occurs.
 The processor must switch to another thread to begin execution, because of the delay
caused by the long-latency event.
 The cost of switching between threads is high, since the instruction pipeline must be
flushed before the other thread can begin execution on the processor core.
 Once this new thread begins execution, it begins filling the pipeline with its instructions.
Fine-grained (or) Interleaved Multithreading:
 Thread switching done at the boundary of an instruction cycle.
 The architectural design of fine-grained systems includes logic for thread switching. As a
result, the cost of switching between threads is small.
REAL-TIME CPU SCHEDULING
Real Time systems are categorized into two types: Soft and Hard real time systems.
 Soft Real Time System: It provides no guarantee as to when a critical real-time process
will be scheduled. They guarantee only that the process will be given preference over
noncritical processes.
 Hard real-time systems: A task must be serviced by its deadline. Service after the
deadline has expired is the same as no service at all.

51

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Issues related to Real Time CPU scheduling:


1. Minimizing Latency
2. Priority Based Scheduling
3. Rate-Monotonic Scheduling
4. Earliest-Deadline-First Scheduling
5. Proportional Share Scheduling
6. POSIX Real-Time Scheduling
Minimizing Latency
In an Event-Driven nature of Real Time System, the system is waiting for an event to occur.
Event may arise in software or hardware. When an event occurs the system must respond to
that event and service the event as quickly as possible.
Event Latency is the amount of time that elapses from when an event occurs to when it is
serviced.
Two types of latencies that affect the performance of the real time systems:
1. Interrupt latency
Interrupt latency refers to the period of time from the arrival of an interrupt at the CPU to
the start of the routine that services the interrupt.
Interrupt latency must be minimized to ensure that real time tasks receive immediate
action.
2. Dispatch latency
The amount of time required for the scheduling dispatcher to stop one process and start
another is known as dispatch latency.
Real-time operating systems minimize dispatch latency to provide real-time tasks with
immediate access to the CPU.
Priority Based Scheduling
In real time operating system each process is having a deadline. Hence the scheduler for a
real-time operating system must support a priority-based algorithm with preemption.
 The process which is having lowest deadline time will be given highest priority.
 Preemptive-priority-based scheduler only guarantees soft real-time functionality.
 Hard real-time systems must further guarantee that real-time tasks will be serviced in
accord with their deadline requirements. Hence it uses Admission control technique. In
this type of scheduling a process may have to announce its deadline requirements to the
scheduler.
 The Admission-control algorithm scheduler either admits the process or rejects it.
If process is admitted then there is a guarantee that the process will complete on time.
If a process is rejected then the task will be not serviced by its deadline.
Rate Monotonic Scheduling
The rate-monotonic scheduling algorithm schedules periodic tasks using a static priority
policy with preemption.
 Upon entering the system, each periodic task is assigned a priority inversely based on its
period.
 Shorter period will be given higher priority. Longer period will be given lower priority.

52

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 Rate-monotonic scheduling assumes that the processing time of a periodic process is the
same for each CPU burst (i.e.) every time a process acquires the CPU, the duration of its
CPU burst is the same.
Scheduling Problem 1:
P1 and P2 are two processes and Periodic Times (i.e. Deadlines) and processing times as
given in the table. The deadline for each process requires that it complete its CPU burst by
the start of its next period.
Process Periodic Time (p) Processing Time (t)
P1 p1 =50 t1=20
P2 p2=100 t2=35
Condition for Scheduling possibility:
We must check whether it is possible to schedule the tasks so that each process meets its
deadlines.
If we measure the CPU utilization of a process Pi as the ratio of its burst to its period:

 CPU utilization of P1 is 20/50 = 0.40 = 40%


 CPU utilization of P2 is 35/100 = 0.35 = 35%
 Total CPU utilization = 40 + 35= 75%.
We can schedule these tasks in such a way that both meet their deadlines and still leave the
CPU with available cycles.
Case 1: By using Rate monotonic Scheduling we assign P1 a higher priority than P2 because
the period of P1 is shorter than that of P2.

 P1 starts first and completes its CPU burst at time 20, thereby meeting its first deadline.
 P2 starts running at this point and runs until time 50.
 At this time, P2 is preempted by P1, although it still has 5 milliseconds remaining in its
CPU burst.
 P1 completes its CPU burst at time 70, at which point the scheduler resumes P2.
 P2 completes its CPU burst at time 75, also meeting its first deadline.
 The system is idle until time 100, when P1 is scheduled again.
Case 2: We assign P2 a higher priority than P1 (Not using Rate Monotonic Scheduling)

 P2 starts execution first and completes at time 35.

53

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 At this point the process P1 starts and P1 completes its CPU burst at time 55.
 The first deadline for P1 is at time 50, so the scheduler has caused P1 to miss its deadline.
Scheduling Problem 2:
Process Periodic Time (p) Processing Time (t)
P1 p1 =50 t1=25
P2 p2=80 t2=35
P1 having highest priority than P2.
The total CPU utilization of the two processes is (25/50)+(35/80) = 0.94.
Hence by the formula, the two processes could be scheduled and still leave the CPU with 6
percent available time.

 P1 runs until it completes its CPU burst at time 25.


 Process P2 then begins running and runs until time 50, when it is preempted by P1.
 At this point, P2 still has 10 milliseconds remaining in its CPU burst.
 Process P1 runs until time 75; consequently, P2 misses the deadline for completion of its
CPU burst at time 80.
Limitations of Rate monotonic Scheduling: CPU utilization is bounded and it is not always
possible fully to maximize CPU resources. The worst-case CPU utilization for scheduling N
processes is N(21/N − 1).
Earliest-Deadline-First Scheduling (EDF)
EDF scheduling dynamically assigns priorities according to deadline.
 Earlier deadline have higher priority. When a process becomes runnable, it must
announce its deadline requirements to the system.
 Priorities may have to be adjusted to reflect the deadline of the newly runnable process.
Scheduling Problem
Process Periodic Time (p) Processing Time (t)
P1 p1 =50 t1=25
P2 p2=80 t2=35

 Process P1 has the earliest deadline, so its initial priority is higher than that of process P2.
 Process P2 begins running at the end of the CPU burst for P1. However, whereas rate-
monotonic scheduling allows P1 to preempt P2 at the beginning of its next period at time
50, EDF scheduling allows process P2 to continue running.
 P2 now has a higher priority than P1 because its next deadline (at time 80) is earlier than
that of P1 (at time 100). Thus, both P1 and P2 meet their first deadlines.

54

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 Process P1 again begins running at time 60 and completes its second CPU burst at time
85, also meeting its second deadline at time 100.
 P2 begins running at this point, only to be preempted by P1 at the start of its next period
at time 100.
 P2 is preempted because P1 has an earlier deadline (time 150) than P2 (time 160).
 At time 125, P1 completes its CPU burst and P2 resumes execution, finishing at time 145
and meeting its deadline as well.
 The system is idle until time 150, when P1 is scheduled to run once again.
Note: EDF scheduling is theoretically optimal but practically it is impossible.
 Theoretically, it can schedule processes so that each process can meet its deadline
requirements and CPU utilization will be 100 percent.
 In practice it is impossible to achieve this level of CPU utilization due to the cost of
context switching between processes and interrupt handling.
Proportional Share Scheduling
Proportional share schedulers operate by allocating T shares among all applications.
An application can receive N shares of time, thus ensuring that the application will have N/T
of the total processor time.
Example: Assume total share T = 100 is to be divided among three processes A, B and C.
 A is assigned 50 shares, B is assigned 15 shares and C is assigned 20 shares.
 This scheme ensures that A will have 50 percent of total processor time, B will have 15
percent and C will have 20 percent.
 Proportional share schedulers must work in conjunction with an admission-control policy
to guarantee that an application receives its allocated shares of time.
 An admission-control policy will admit a client requesting a particular number of shares
only if sufficient shares are available.
 Scheduler allocated 50 + 15 + 20 = 85 shares of the total of 100 shares.
 If a new process D requested 30 shares, the admission controller would deny D entry into
the system.
POSIX Real-Time Scheduling
The POSIX standard provides extensions for real-time computing.
POSIX defines two scheduling classes for real-time threads: SCHED_FIFO, SCHED_RR.
 SCHED_FIFO schedules threads according to a FCFS policy using a FIFO queue. There
is no time slicing among threads of equal priority (i.e.) highest-priority real-time thread at
the front of the FIFO queue will be granted the CPU until it terminates or blocks.
 SCHED_RR uses a Round-Robin policy. It provides time slicing among threads of equal
priority..
OPERATING-SYSTEM EXAMPLES: LINUX & WINDOWS
Linux Scheduling
From Linux Kernel Version 2.6.23 the Completely Fair Scheduler (CFS) became the
default Linux scheduling algorithm.
 Scheduling in the Linux system is based on scheduling classes.

55

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 Each class is assigned a specific priority. By using different scheduling classes, the kernel
can accommodate different scheduling algorithms based on the needs of the system and
its processes.
 The scheduling criteria for a Linux server may be different from those for a mobile device
running Linux.
 To decide which task to run next, the scheduler selects the highest-priority task belonging
to the highest-priority scheduling class.
Standard Linux kernels implement two scheduling classes:
1. Completely Fair Scheduler (CFS) Class
2. Real-time scheduling class
CFS Scheduling
The CFS scheduler assigns a proportion of CPU processing time to each task.
 This proportion is calculated based on the Nice value assigned to each task.
 Nice values range from −20 to +19 and the default nice value is 0.
 Lower nice value indicates a higher priority. (i.e.) Tasks with lower nice values receive a
higher proportion of CPU processing time.
 CFS doesn’t use discrete values of time slices and instead identifies Targeted Latency.
 Targeted Latency is an interval of time during which every runnable task should run at
least once. Proportions of CPU time are allocated from the value of targeted latency.
 In addition to having default and minimum values, targeted latency can increase if the
number of active tasks in the system grows beyond a certain threshold.
Assigning priority in CFS scheduling
 The CFS scheduler doesn’t directly assign priorities. It records how long each task has
run by maintaining the virtual run time of each task using the per-task variable
vruntime.
 The virtual run time is associated with a decay factor based on the priority of a task:
lower-priority tasks have higher rates of decay than higher-priority tasks.
 For tasks at normal priority (i.e. nice values of 0), virtual run time is identical to actual
physical run time.
 If a task with default priority runs for 200 milliseconds, its vruntime = 200 milliseconds.
 Similarly a lower-priority task vruntime >200 ms and higher-priority task vruntime <
200 ms.
 The scheduler selects the task that has the smallest vruntime value for running the next on
CPU.
 A higher-priority task that becomes available to run can preempt a lower-priority task.
 The Linux CFS scheduler provides an efficient algorithm for selecting which task to run
next. Each runnable task is placed in a red-black tree. It is a balanced binary search tree
whose key is based on the value of vruntime.
Example: Consider CPU bound task and I/O bound tasks have same nice value.
I/O bound task have shorter vrutime value because it is having less burst time as compared to
CPU bound task.
At any point of time if the CPU-bound task is executing when the I/O-bound task becomes
eligible to run, the I/O-bound task will preempt the CPU-bound task.

56

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Real time scheduling in LINUX


Linux implements real-time scheduling using the POSIX standard.
 Any task scheduled using either the SCHED FIFO or the SCHED RR real-time policy
runs at a higher priority than normal tasks.
 Linux uses two separate priority ranges: Real-time tasks and Normal tasks.
 Real-time tasks are assigned static priorities within the range of 0 to 99 and normal (i.e.
non real-time) tasks are assigned priorities from 100 to 139.
 Numerically lower values indicate higher relative priorities.
Windows Scheduling
Windows schedules threads using a preemptive priority scheduling algorithm.
 The Windows scheduler ensures that the highest-priority thread will always run.
 The portion of the Windows kernel that handles scheduling is called the dispatcher.
 The dispatcher uses a 32-level priority scheme to determine the order of thread execution.
Priorities are divided into classes:
1. Variable class contains threads having priorities from 1 to 15.
2. Real-time class contains threads with priorities ranging from 16 to 31.
3. A thread running at priority 0 that is used for memory management.
The dispatcher uses a queue for each scheduling priority and traverses the set of queues from
highest to lowest until it finds a thread that is ready to run. If no ready thread is found, the
dispatcher will execute a special thread called the idle thread.
Windows API identifies the following six priority classes to which a process can belong:
 IDLE PRIORITY CLASS
 BELOW NORMAL PRIORITY CLASS
 NORMAL PRIORITY CLASS
 ABOVE NORMAL PRIORITY CLASS
 HIGH PRIORITY CLASS
 REALTIME PRIORITY CLASS
A thread within a given priority classes also has a relative priority. The values for relative
priorities include:
 IDLE
 LOWEST
 BELOW NORMAL
 NORMAL
 ABOVE NORMAL
 HIGHEST
 TIME CRITICAL
The priority of each thread is based on both the priority class it belongs to and its relative
priority within that class. Each thread has a base priority representing a value in the priority
range for the class to which the thread belongs. The base priorities for each priority class are
as follows:
 REALTIME PRIORITY CLASS—24
 HIGH PRIORITY CLASS—13

57

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

 ABOVE NORMAL PRIORITY CLASS—10


 NORMAL PRIORITY CLASS—8
 BELOW NORMAL PRIORITY CLASS—6
 IDLE PRIORITY CLASS—4
When a thread’s time quantum runs out, that thread is interrupted. If the thread is in the
variable-priority class, its priority is lowered.
 The priority is never lowered below the base priority.
 Lowering the priority tends to limit the CPU consumption of compute-bound threads.
 When a variable priority thread is released from a wait operation, the dispatcher boosts
the priority.
 The amount of the boost depends on what the thread was waiting for.
Windows distinguishes between the foreground process that is currently selected on the
screen and the background processes that are not currently selected.
 When a process moves into the foreground, Windows increases the scheduling quantum
by some factor (i.e. by 3).
 This increase gives the foreground process three times longer to run before a time-sharing
preemption occurs.
Windows 7 introduced user-mode scheduling (UMS) , which allows applications can create
and schedule multiple threads without involving the Windows kernel scheduler.
 Microsoft Windows provides Concurrency Runtime (ConcRT), a concurrent
programming framework for C++ designed for task-based parallelism on multicore
processors.
 ConcRT provides a user-mode scheduler together with facilities for decomposing
programs into tasks, which can then be scheduled on the available processing cores.
THREADS
A thread is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a
register set, and a stack.
 It shares with other threads belonging to the same process its code section, data section,
and other operating-system resources, such as open files and signals.
 A traditional process is called “heavyweight process” has a single thread of control.
 If a process has multiple threads of control, it can perform more than one task at a time.
 The below figure illustrates the difference between a traditional single-threaded process
and a multithreaded process.

58

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Most operating-system kernels are now multithreaded. Several threads operate in the kernel,
and each thread performs a specific task, such as managing devices, managing memory, or
interrupt handling.
Examples:
1. A web browser might have one thread display images or text while another thread
retrieves data from the network.
2. A word processor may have a thread for displaying graphics, another thread for
responding to keystrokes from the user, and a third thread for performing spelling and
grammar checking in the background.
3. Solaris OS has a set of threads in the kernel specifically for interrupt handling.
4. Linux OS uses a kernel thread for managing the amount of free memory in the system.

Need for Multi-Threading


Consider the scenario; a web server accepts client requests for web pages, images, sound etc.
A busy web server may have thousands of clients concurrently accessing it. If the web server
ran as a traditional single-threaded process, it would be able to service only one client at a
time, and a client might have to wait a very long time for its request to be serviced.
One solution is to have the server run as a single process that accepts requests. When the
server receives a request, it creates a separate process to service that request. Process creation
is time consuming and resource intensive.
If the new process will perform the same tasks as the existing process, why incur all that
overhead?
Hence it is more efficient to use one process that contains multiple threads. If the web-server
process is multi-threaded, the server will create a separate thread that listens for client
requests. When a request is made, rather than creating another process, the server creates a
new thread to service the request and resume listening for additional requests.

Benefits of Multi-Threading
1. Responsiveness: Multithreading an interactive application may allow a program to
continue running even if part of it is blocked or is performing a lengthy operation, thereby
increasing responsiveness to the user.
2. Resource sharing. Processes can only share resources through shared memory and
message passing must be explicitly arranged by the programmer. Whereas threads share
the memory and the resources of the process. The benefit of sharing code and data is that
it allows an application to have several different threads of activity within the same
address space.
3. Economy. Allocating memory and resources for process creation is costly. Because
threads share the resources of the process to which they belong, it is more economical to
create and context-switch threads. In Solaris, creating a process is about thirty times
slower than is creating a thread, and context switching is about five times slower.
4. Scalability. The benefits of multithreading can be even greater in a multiprocessor
architecture, where threads may be running in parallel on different processing cores.
59

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Types of Threads
There are two types of threads: 1. Kernel threads 2. User threads
 Kernel threads are supported and managed directly by the operating system.
 User threads are supported above the kernel and are managed without kernel support.
Multithreading Models
A relationship must exist between user threads and kernel threads. Three common ways of
establishing such a relationship are:
1. Many-to-One model
2. One-to-One model
3. Many-to-Many model.
Many-to-One model
 The many-to-one model maps many user-level threads to one kernel thread.
 Thread management is done by the thread library in user space, so it is efficient.
 The entire process will block if a thread makes a blocking system call.
 Only one thread can access the kernel at a time, multiple threads are unable to run in
parallel on multicore systems. Hence very few systems continue to use the model because
of its inability to take advantage of multiple processing cores.
One-to-One Model
 The one-to-one model maps each user thread to a kernel thread.
 It provides more concurrency than the many-to-one model by allowing another thread to
run when a thread makes a blocking system call.
 It also allows multiple threads to run in parallel on multiprocessors.
 The only drawback to this model is that creating a user thread requires creating the
corresponding kernel thread.
 Because the overhead of creating kernel threads can burden the performance of an
application, most implementations of this model restrict the number of threads supported
by the system.
 Linux, along with the family of Windows operating systems, implement the one-to-one
model.
Many-to-Many Model
The many-to-many model multiplexes many user-level threads to a smaller or equal number
of kernel threads. The number of kernel threads may be specific to either a particular
application or a particular machine.
Many-to-One Model One-to-One Model Many-to-Many Model

60

Downloaded by Sachin ([email protected])


lOMoARcPSD|45334098

KMIT/CSE/II-II/OS-2 P.VAMSHI KRISHNA

Advantage of Many-to-Many Model


 The many-to-one model allows the developer to create as many user threads as they
wishes; it does not result in true concurrency, because the kernel can schedule only one
thread at a time.
 The one-to-one model allows greater concurrency, but the developer has to be careful not
to create too many threads within an application.
 The many-to-many model suffers from neither of these shortcomings: developers can
create as many user threads as necessary, and the corresponding kernel threads can run in
parallel on a multiprocessor. when a thread performs a blocking system call, the kernel
can schedule another thread for execution.

61

Downloaded by Sachin ([email protected])

You might also like