ECT426 M2 Ktunotes - in
ECT426 M2 Ktunotes - in
ECT426 M2 Ktunotes - in
Process Scheduling
• Each process alternates between using a processor and waiting for some event to occur
• The processor is kept busy by executing one process while the others wait.
• Process scheduling names suggest the relative time scales with which these functions are
performed
• Medium-term scheduling is a part of the swapping function. This is a decision whether to add
a process to those that are at least partially in main memory and therefore available for
execution.
• Short-term scheduling is the actual decision of which ready process to execute next
• Scheduling helps to manage queues and minimize queueing delay and to optimize
performance in a queueing environment
• The long-term scheduler creates processes from the queue when it can.
(i) The scheduler must decide when the OS can take on one or more additional processes.
(ii) The scheduler must decide which job or jobs to accept and turn into processes
• The more number of processes created, the smaller percentage of time that each process can
be executed
• Thus, the long-term scheduler may limit the degree of multiprogramming to provide
satisfactory service to the current set of processes.
• Long term scheduler may decide to add one or more new jobs
(ii) If the fraction of time that the processor is idle exceeds a certain threshold
• The criteria used may include priority, expected execution time, and I/O requirements.
• For interactive programs in a time-sharing system, a process is generated when the user attempts to
log in.
• Time-sharing users are not simply queued up and kept waiting until the system can accept them.
• OS will accept all authorized comers until the system is saturated, using some predefined measure of
saturation.
• At that point, a connection request is met with a message indicating that the system is full and the user
should try again later.
• The short-term scheduler, also known as the dispatcher, executes most frequently and makes
the fine-grained decision of which process to execute next.
• The short-term scheduler is invoked whenever an event occurs that may lead to the blocking
of the current process or that may provide an opportunity to preempt a currently running
process in favor of another.
• Clock interrupts, I/O interrupts, Operating system calls, Signals (e.g., semaphores)
• The main objective of short-term scheduling is to allocate processor time so that to optimize
system behavior.
• A set of criteria is established against which various scheduling policies may be evaluated.
• User oriented criteria relate to the behavior of the system as observed by the individual user
or process. Eg: Response time in an interactive system
• System oriented the focus is on effective and efficient utilization of the processor.
• Eg: Throughput, which is the rate at which processes are completed. This is a measure of
system performance and preferred to be maximized
• Good response time require a scheduling algorithm that switches between processes
frequently increasing the overhead of the system, reducing throughput.
• The relevance given to various requirements will depend on the nature and intended use of the
system.
• Lower-priority processes may suffer in a pure priority scheduling scheme, if there is always a
steady supply of higher-priority ready processes.
• If this behavior is not desirable, the priority of a process can be changed with its age or
execution history.
• If,
w = time spent in system so far, waiting
e = time spent in execution so far
s = total service time required by the process, including e ;
• Also, the cost of preemption may be kept relatively low by using efficient process-switching
mechanisms and by providing a large main memory to keep a high percentage of programs in
main memory.
• Turnaround time (TAT) is the residence time Tr , or total time that the item spends in the
system (waiting time plus service time).
• When the currently running process ceases to execute, the process that has been in the ready
queue the longest is selected for running.
• Process Z has a turnaround time that is almost double that of Y, but its normalized residence
time is under 2.0.
• Some of these may be in I/O queues (blocked state) but may move back to the ready queue
while the processor-bound process is executing.
• At this point, most or all of the I/O devices may be idle, even though there is potentially work
for them to do.
• When the currently running process leaves the Running state, the ready I/O-bound processes
quickly move through the Running state and become blocked on I/O events.
• Thus, FCFS may result in inefficient use of both the processor and the I/O devices.
• The scheduler may maintain a number of queues, one for each priority level, and dispatch
within each queue on a first-come-first-served basis
• When the interrupt occurs, the currently running process is placed in the ready queue, and the
next ready job is selected on a FCFS basis.
• This technique is also known as time slicing because each process is given a slice of time
before being preempted
• If the quantum is very short, then short processes will move through the system relatively
quickly.
• And, the processing involved in handling the clock interrupt and performing the scheduling
and dispatching function will increase. Thus, very short time quanta should be avoided.
• Best way is to design the time quantum such that it is slightly greater than the time required
for a typical interaction or process function.
• If it is less, then most processes will require at least two time quanta.
• One drawback to round robin is its relative treatment of processor-bound and I/O-bound
processes. Share of time for I/O bound process and processor bound process is a matter of
conflict
• Thus, a short process will jump to the head of the queue past longer jobs.
• One difficulty with the SPN policy is the need to know or at least estimate the required
processing time of each process.
• For batch jobs, the system may require the programmer to estimate the value and supply it to
the OS.
• The ready queue is divided into different queues according to different properties of the
process like memory size, process priority, or process type.
• All the different processes can be implemented in different ways, i.e., each process queue can
have a different scheduling algorithm.
• Multilevel Queue Scheduling distributes the processes into multiple queues based on the
properties of the processes.
• Each queue has its own priority level and scheduling algorithm to manage the processes
inside that queue.
• High-priority queues might use Round Robin scheduling. Low-priority queues might use
First Come, First Serve scheduling. (Optional)
• Processes cannot move between queues This algorithm prioritizes different types of
processes and ensures fair resource allocation
Time slicing.
When setting the priority of processes in a queue, every queue has an absolute priority over
that with a lower priority queue.
Time Slicing:
Each queue gets a slice or portion of the CPU time for scheduling its own processes.
For example, suppose Queue1 gets 40% 0f the CPU time then the remaining 60% of the CPU
time may be assigned as 40% to Queue2 and 20% to Queue3.
• If a process consumes too much processor time, it will be switched to the lowest priority
queue
• A process waiting in a lower priority queue for too long may be shifted to a higher priority
queue. This type of aging prevents starvation
• The method for determining when a queue should be demoted to a lower-priority queue.
• When a process is upgraded to a higher-priority queue, this process determines when it gets
upgraded.
• The method for determining which processes will enter the queue and when those processes
will require service
• This scheduling algorithm allows different processes to move between different queues.
• In this algorithm, a process that waits too long in a lower priority queue may be moved to a
higher priority queue which helps in preventing starvation.
Disadvantages of MFQS
• As processes are moving around different queues which leads to the production of more CPU
overheads.
• In order to select the best scheduler this algorithm requires some other means to select the
values
• The process can have states like new, ready, running, waiting, terminated, and suspended.
• Thread is the segment of a process which means a process can have multiple threads and these
multiple threads are contained within a process. A thread has three states: Running, Ready, and
Blocked.
• It takes less time to terminate as compared to the process but unlike the process, threads do not
isolate.
• These two characteristics are independent and could be treated independently by the OS.
• From time to time, a process may be allocated control or ownership of resources, such as
main memory, I/O channels, I/O devices, and files.
• Thus, a process has an execution state (Running, Ready, etc.) and a dispatching priority and
is the entity that is scheduled and dispatched by the OS.
• USER-LEVEL THREADS In a pure ULT facility, all of the work of thread management is
done by the application and the kernel is not aware of the existence of threads.
• The threads library contains code for creating and destroying threads, for passing messages
and data between threads, for scheduling thread execution, and for saving and restoring
thread contexts.
• This application and its thread are allocated to a single process managed by the kernel.
• At any time that the application in Running state, may create a new thread to run within the same
process.
• Spawning is done by invoking the spawn utility in the threads library which is invoked by a
procedure call.
• The threads library, creates a data structure for the new thread and then passes control to one of the
threads within this process that is in the Ready state, using some scheduling algorithm.
• When control is passed to the library, the context of the current thread is saved, and when control is
passed from the library to a thread, the context of that thread is restored.
• The context essentially consists of the contents of user registers, the program counter, and stack
pointers.
• The kernel continues to schedule the process as a unit and assigns a single execution state
(Ready, Running, Blocked, etc.) to that process
• A process can be interrupted, either by exhausting its time slice or by being preempted by a
higher priority process, while it is executing code in the threads library.
• Thus, a process may be in the midst of a thread switch from one thread to another when
interrupted.
• When that process is resumed, execution continues within the threads library, which
completes the thread switch and transfers control to another thread within that process
1. Kernel can simultaneously schedule multiple threads from the same process on multiple processors.
2. If one thread in a process is blocked, the kernel can schedule another thread of the same process.
• The principal disadvantage of the KLT approach compared to the ULT approach is that the transfer of
control from one thread to another within the same process requires a mode switch to the kernel.
• Multithreading can also handle multiple requests from the same user.
• The traditional approach was a single thread of execution per process where the concept of a
thread is not recognized, is referred to as a single-threaded approach.
• As programs work on behalf of the initial thread request and are interrupted by other
requests, the work status of the initial request is tracked until the work is completed
• Fast CPU speed and large memory capacities are needed for multithreading.
• The single processor executes pieces, or threads, of various programs so fast, it appears the
computer is handling multiple requests simultaneously.
2. unit of protection.
• Protected access to processors, other processes (for inter process communication), files,
and I/O resources (devices and channels)
• A saved thread context when not running; one way to view a thread is as an independent
• An execution stack
• Access to the memory and resources of its process, shared with all other threads in that
process
• They reside in the same address space and have access to the same data.
• When one thread alters an item of data in memory, other threads see the change when
accessed.
• If one thread opens a file with read privileges, other threads in the same process can also read
from that file.
• It takes less time to switch between two threads within the same process than to switch
between processes
Thus, if there is an application or function that should be implemented as a set of related units of
execution, it is far more efficient to do so as a collection of threads rather than a collection of
separate processes.
Finish When a thread completes, its register context and stacks are deallocated.
• Any alteration of a resource by one thread affects the environment of the other threads in the
same process.
• It is therefore necessary to synchronize the activities of the various threads so that they
• Multithreading allows the execution of multiple parts of a program at the same time.
• These parts are known as threads and are lightweight processes available within the process.
Therefore, multithreading leads to maximum utilization of the CPU by multitasking.
• Multiple CPUs share the load in multiprocessor scheduling so that various processes run
simultaneously
• The multiple CPUs in the system are in close communication, which shares a common bus,
memory, and other peripheral devices. So we can say that the system is tightly coupled.