CHAPTER2
CHAPTER2
Process
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 −
S.N. Component & Description
Stack
1 The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
Text
3 This includes the current activity represented by the value of Program Counter and
the contents of the processor's registers.
4 Data
This section contains the global and static variables.
When a process executes, it passes through different states. These stages may
differ in different operating systems, and the names of these states are also not
standardized.
In general, a process can have one of the following five states at a time.
S.N. State & Description
1 Start
This is the initial state when a process is first started/created.
Ready
The process is waiting to be assigned to a processor. Ready processes are waiting to
2 have the processor allocated to them by the operating system so that they can run.
Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
Running
3 Once the process has been assigned to a processor by the OS scheduler, the process
state is set to running and the processor executes its instructions.
Waiting
4 Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.
Terminated or Exit
5 Once the process finishes its execution, or it is terminated by the operating system,
it is moved to the terminated state where it waits to be removed from main memory.
A Process Control Block is a data structure maintained by the Operating System for
every process. The PCB is identified by an integer process ID (PID). A PCB keeps all
the information needed to keep track of a process as listed below in the table −
Process State
1 The current state of the process i.e., whether it is ready, running, waiting, or
whatever.
2 Process privileges
This is required to allow/disallow access to system resources.
3 Process ID
Unique identification for each of the process in the operating system.
4 Pointer
A pointer to parent process.
Program Counter
5 Program Counter is a pointer to the address of the next instruction to be executed
for this process.
CPU registers
6 Various CPU registers where process need to be stored for execution for running
state.
Accounting information
9 This includes the amount of CPU used for process execution, time limits, execution
ID etc.
10 IO status information
This includes a list of I/O devices allocated to the process.
Definition
The process scheduling is the activity of the process manager that handles the
removal of the running process from the CPU and the selection of another process
on the basis of a particular strategy.
Categories of Scheduling
Explore our latest online courses and learn new skills at your own pace. Enroll
and become a certified expert to boost your career.
The OS maintains all Process Control Blocks (PCBs) in Process Scheduling Queues.
The OS maintains a separate queue for each of the process states and PCBs of all
processes in the same execution state are placed in the same queue. When the
state of a process is changed, its PCB is unlinked from its current queue and moved
to its new state queue.
The Operating System maintains the following important process scheduling queues
−
Job queue − This queue keeps all the processes in the system.
Ready queue − This queue keeps a set of all processes residing in main memory,
ready and waiting to execute. A new process is always put in this queue.
Device queues − The processes which are blocked due to unavailability of an I/O
device constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin,
Priority, etc.). The OS scheduler determines how to move processes between the
ready and run queues which can only have one entry per processor core on the
system; in the above diagram, it has been merged with the CPU.
Two-state process model refers to running and non-running states which are
described below −
Running
1
When a new process is created, it enters into the system as in the running state.
Not Running
Processes that are not running are kept in queue, waiting for their turn to execute.
Each entry in the queue is a pointer to a particular process. Queue is implemented
2 by using linked list. Use of dispatcher is as follows. When a process is interrupted,
that process is transferred in the waiting queue. If the process has completed or
aborted, the process is discarded. In either case, the dispatcher then selects a
process from the queue to execute.
Schedulers
Schedulers are special system software which handle process scheduling in various
ways. Their main task is to select the jobs to be submitted into the system and to
decide which process to run. Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
The primary objective of the job scheduler is to provide a balanced mix of jobs, such
as I/O bound and processor bound. It also controls the degree of multiprogramming.
If the degree of multiprogramming is stable, then the average rate of process
creation must be equal to the average departure rate of processes leaving the
system.
On some systems, the long-term scheduler may not be available or minimal. Time-
sharing operating systems have no long term scheduler. When a process changes
the state from new to ready, then there is use of long-term scheduler.
It is a process swapping
1 It is a job scheduler It is a CPU scheduler
scheduler.
It is almost absent or
It is also minimal in time It is a part of Time sharing
4 minimal in time sharing
sharing system systems.
system
It selects processes from It selects those It can re-introduce the
5 pool and loads them into processes which are process into memory and
memory for execution ready to execute execution can be continued.
Context Switching
A context switching is the mechanism to store and restore the state or context of a
CPU in Process Control block so that a process execution can be resumed from the
same point at a later time. Using this technique, a context switcher enables multiple
processes to share a single CPU. Context switching is an essential part of a
multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute
another, the state from the current running process is stored into the process
control block. After this, the state for the process to run next is loaded from its own
PCB and used to set the PC, registers, etc. At that point, the second process can
start executing.
Context switches are computationally intensive since register and memory state
must be saved and restored. To avoid the amount of context switching time, some
hardware systems employ two or more sets of processor registers. When the
process is switched, the following information is stored for later use.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Motivation
Threads are very useful in modern programming whenever a process has
multiple tasks to perform independently of the others.
This is particularly true when one of the tasks may block, and it is desired to
allow the other tasks to proceed without blocking.
For example in a word processor, a background thread may check spelling and
grammar while a foreground thread processes user input ( keystrokes ), while a
third thread loads images from the hard drive, and a fourth does periodic
automatic backups of the file being edited.
Another example is a web server - Multiple threads allow for multiple requests to
be satisfied simultaneously, without having to service requests sequentially or to
fork off separate processes for every incoming request. ( The latter is how this
sort of thing was done before the concept of threads was developed. A daemon
would listen at a port, fork off a child for every incoming request to be
processed, and then go back to listening to the port. )
Benefits
A thread is a single sequence stream within a process. Threads are also called
lightweight processes as they possess some of the properties of processes. Each
thread belongs to exactly one process. In an operating system that supports
multithreading, the process can consist of many threads. But threads can be
effective only if the CPU is more than 1 otherwise two threads have to context
switch for that single CPU.
What is Thread in Operating Systems?
In a process, a thread refers to a single sequential activity being executed. these
activities are also known as thread of execution or thread control. Now, any
operating system process can execute a thread. we can say, that a process can
have multiple threads.
Why Do We Need Thread?
Threads run in parallel improving the application performance. Each such thread
has its own CPU state and stack, but they share the address space of the process
and the environment.
Threads can share common data so they do not need to use inter-process
communication. Like the processes, threads also have states like ready,
executing, blocked, etc.
Priority can be assigned to the threads just like the process, and the highest
priority thread is scheduled first.
Each thread has its own Thread Control Block (TCB). Like the process, a context
switch occurs for the thread, and register contents are saved in (TCB). As
threads share the same address space and resources, synchronization is also
required for the various activities of the thread.
Components of Threads
No hardware support is
Hardware Hardware support is
required for context
support needed.
switching.
Multithreaded applications
Kernels can be
Multithreading cannot take full advantage
multithreaded.
of multiprocessing.
Multithreading Models
Some operating system provide a combined user level thread and Kernel level
thread facility. Solaris is a good example of this combined approach. In a combined
system, multiple threads within the same application can run in parallel on multiple
processors and a blocking system call need not block the entire process.
Multithreading models are three types
The many-to-many model multiplexes any number of user threads onto an equal or
smaller number of kernel threads.
The following diagram shows the many-to-many threading model where 6 user level
threads are multiplexing with 6 kernel level threads. In this model, developers can
create as many user threads as necessary and the corresponding Kernel threads
can run in parallel on a multiprocessor machine. This model provides the best
accuracy on concurrency and when a thread performs a blocking system call, the
kernel can schedule another thread for execution.
Many to One Model
Many-to-one model maps many user level threads to one Kernel-level thread.
Thread management is done in user space by the thread library. When thread
makes a blocking system call, the entire process will be blocked. Only one thread
can access the Kernel at a time, so multiple threads are unable to run in parallel on
multiprocessors.
If the user-level thread libraries are implemented in the operating system in such a
way that the system does not support them, then the Kernel threads use the many-
to-one relationship modes.
One to One Model
Disadvantage of this model is that creating user thread requires the corresponding
Kernel thread. OS/2, windows NT and windows 2000 use one to one relationship
model.
The table below represents the difference between process and thread.
Process Thread
The process has its own Thread has Parents’ PCB, its own Thread
Process Control Block, Stack, Control Block, and Stack and common Address
and Address Space. space.
The process does not share Threads share data with each other.
Process Thread
What is Multi-Threading?
A thread is also known as a lightweight process. The idea is to achieve parallelism
by dividing a process into multiple threads. For example, in a browser, multiple tabs
can be different threads. MS Word uses multiple threads: one thread to format the
text, another thread to process inputs, etc. More advantages of multithreading are
discussed below.
Multithreading is a technique used in operating systems to improve the
performance and responsiveness of computer systems. Multithreading allows
multiple threads (i.e., lightweight processes) to share the same resources of a single
process, such as the CPU, memory, and I/O devices.
Categories of Scheduling
Scheduling falls into one of two categories:
Non-Preemptive: In this case, a process’s resource cannot be taken before the
process has finished running. When a running process finishes and transitions to
a waiting state, resources are switched.
Preemptive: In this case, the OS assigns resources to a process for a
predetermined period. The process switches from running state to ready state or
from waiting state to ready state during resource allocation. This switching
happens because the CPU may give other processes priority and substitute the
currently active process for the higher priority process.
Operating system uses various schedulers for the process scheduling described
below.
1. Long term scheduler
Long term scheduler is also known as job scheduler. It chooses the processes from
the pool (secondary memory) and keeps them in the ready queue maintained in the
primary memory.
Long Term scheduler mainly controls the degree of Multiprogramming. The purpose
of long term scheduler is to choose a perfect mix of IO bound and CPU bound
processes among the jobs present in the pool.
If the job scheduler chooses more IO bound processes then all of the jobs may
reside in the blocked state all the time and the CPU will remain idle most of the
time. This will reduce the degree of Multiprogramming. Therefore, the Job of long
term scheduler is very critical and may affect the system for a very long time.
Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from
the ready queue and dispatch to the CPU for the execution.
A scheduling algorithm is used to select which job is going to be dispatched for the
execution. The Job of the short term scheduler can be very critical in the sense that
if it selects job whose CPU burst time is very high then all the jobs after that, will
have to wait in the ready queue for a very long time.
This problem is called starvation which may arise if the short term scheduler makes
some mistakes while selecting the job.
Medium term scheduler takes care of the swapped out processes.If the running
state processes needs some IO time for the completion then there is a need to
change its state from running to waiting.
Medium term scheduler is used for this purpose. It removes the process from the
running state to make room for the other processes. Such processes are the
swapped out processes and this procedure is called swapping. The medium term
scheduler is responsible for suspending and resuming the processes.
It is a process-swapping
It is a job scheduler It is a CPU scheduler
scheduler.
Process Queues
The Operating system manages various types of queues for each of the process
states. The PCB related to the process is also stored in the queue of the same state.
If the Process is moved from one state to another state then its PCB is also unlinked
from the corresponding queue and added to the other state queue in which the
transition is made.
1. Job Queue
In starting, all the processes get stored in the job queue. It is maintained in the
secondary memory. The long term scheduler (Job scheduler) picks some of the jobs
and put them in the primary memory.
2. Ready Queue
Ready queue is maintained in primary memory. The short term scheduler picks the
job from the ready queue and dispatch to the CPU for the execution.
3. Waiting Queue
When the process needs some IO operation in order to complete its execution, OS
changes the state of the process from running to waiting. The context (PCB)
associated with the process gets stored on the waiting queue which will be used by
the Processor when the process finishes the IO.
1. Arrival Time
The time at which the process enters into the ready queue is called the arrival time.
2. Burst Time
The total amount of time required by the CPU to execute the whole process is called
the Burst Time. This does not include the waiting time. It is confusing to calculate
the execution time for a process even before executing it hence the scheduling
problems based on the burst time cannot be implemented in reality.
3. Completion Time
The Time at which the process enters into the completion state or the time at which
the process completes its execution, is called completion time.
4. Turnaround time
The total amount of time spent by the process from its arrival to its completion, is
called Turnaround time.
5. Waiting Time
The Total amount of time for which the process waits for the CPU to be assigned is
called waiting time.
6. Response Time
The difference between the arrival time and the time at which the process first gets
the CPU is called Response Time.
CPU Scheduling
In the uniprogrammming systems like MS DOS, when a process waits for any I/O
operation to be done, the CPU remains idol. This is an overhead since it wastes the
time and causes the problem of starvation. However, In Multiprogramming systems,
the CPU doesn't remain idle during the waiting time of the Process and it starts
executing other processes. Operating System has to define which process the CPU
will be given.
This is a task of the short term scheduler to schedule the CPU for the number of
processes present in the Job Pool. Whenever the running process requests some IO
operation then the short term scheduler saves the current context of the process
(also called PCB) and changes its state from running to waiting. During the time,
process is in waiting state; the Short term scheduler picks another process from the
ready queue and assigns the CPU to this process. This procedure is called context
switching.
The Operating system maintains a process control block during the lifetime of the
process. The Process control block is deleted when the process is terminated or
killed. There is the following information which is saved in the process control block
and is changing with the state of the process.
Why do we need Scheduling?
In Multiprogramming, if the long term scheduler picks more I/O bound processes
then most of the time, the CPU remains idol. The task of Operating system is to
optimize the utilization of resources.
If most of the running processes change their state from running to waiting then
there may always be a possibility of deadlock in the system. Hence to reduce this
overhead, the OS needs to schedule the jobs to get the optimal utilization of CPU
and to avoid the possibility to deadlock.
There are various algorithms which are used by the Operating System to schedule
the processes on the processor in an efficient way.
There are the following algorithms which can be used to schedule the jobs.
1. First Come First Serve
It is the simplest algorithm to implement. The process with the minimal arrival time
will get the CPU first. The lesser the arrival time, the sooner will the process gets
the CPU. It is the non-preemptive type of scheduling.
2. Round Robin
In the Round Robin scheduling algorithm, the OS defines a time quantum (slice). All
the processes will get executed in the cyclic way. Each of the process will get the
CPU for a small amount of time (called time quantum) and then get back to the
ready queue to wait for its next turn. It is a preemptive type of scheduling.
The job with the shortest burst time will get the CPU first. The lesser the burst time,
the sooner will the process get the CPU. It is the non-preemptive type of scheduling.
It is the preemptive form of SJF. In this algorithm, the OS schedules the Job
according to the remaining time of the execution.
In this algorithm, the priority will be assigned to each of the processes. The higher
the priority, the sooner will the process get the CPU. If the priority of the two
processes is same then they will be scheduled according to their arrival time.
In this scheduling Algorithm, the process with highest response ratio will be
scheduled next. This reduces the starvation in the system.
First Come First Serve paves the way for understanding of other algorithms. This
algorithm may have many disadvantages. But these disadvantages created very
new and efficient algorithms. So, it is our responsibility to learn about First Come
First Serve CPU Process Scheduling Algorithms.
Important Abbreviations
First Come First Serve CPU Scheduling Algorithm shortly known as FCFS is the first
algorithm of CPU Process Scheduling Algorithm. In First Come First Serve Algorithm
what we do is to allow the process to execute in linear manner.
This means that whichever process enters process enters the ready queue first is
executed first. This shows that First Come First Serve Algorithm follows First In First
Out (FIFO) principle.
The First Come First Serve Algorithm can be executed in Pre Emptive and Non Pre
Emptive manner. Before, going into examples, let us understand what is Pre
Emptive and Non Pre Emptive Approach in CPU Process Scheduling.
In this instance of Pre Emptive Process Scheduling, the OS allots the resources to a
Process for a predetermined period of time. The process transitions from running
state to ready state or from waiting state to ready state during resource allocation.
This switching happens because the CPU may assign other processes precedence
and substitute the currently active process for the higher priority process.
In this case of Non Pre Emptive Process Scheduling, the resource cannot be
withdrawn from a process before the process has finished running. When a running
process finishes and transitions to the waiting state, resources are switched.
Scheduling Criteria There are several different criteria to consider when trying to
select the "best" scheduling algorithm for a particular situation and environment,
including:
CPU utilization - Ideally the CPU would be busy 100% of the time, so as to
waste 0 CPU cycles. On a real system CPU usage should range from 40%
( lightly loaded ) to 90% ( heavily loaded. )
Throughput - Number of processes completed per unit time. May range from
10 / second to 1 / hour depending on the specific processes.
Waiting time - How much time processes spend in the ready queue waiting
their turn to get on the CPU.
Response time - The time taken in an interactive program from the issuance
of a command to the commence of a response to that command.
In FCFS Scheduling
The process which arrives first in the ready queue is firstly assigned the CPU.
In case of a tie, process with smaller process id is executed first.
Advantages-
Disadvantages-
It suffers from convoy effect i.e. processes with higher burst time arrived before
the processes with smaller burst time.
(b) 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.
The processer should know in advance how much time process will take.
Pre-emptive mode of Shortest Job First is called as Shortest Remaining Time First
(SRTF).
Advantages-
Disadvantages-
It can not be implemented practically since burst time of the processes can not be
known in advance.
CPU is assigned to the process on the basis of FCFS for a fixed amount of time.
After the time quantum expires, the running process is preempted and sent to the
ready queue.
It is best suited for time sharing system, client server architecture and interactive
system. Disadvantages-
It leads to starvation for processes with larger burst time as they have to repeat
the cycle many times.
With increasing value of time quantum, Round Robin Scheduling tends to become
FCFS Scheduling.
When time quantum tends to infinity, Round Robin Scheduling becomes FCFS
Scheduling.
The performance of Round Robin scheduling heavily depends on the value of time
quantum.
The value of time quantum should be such that it is neither too big nor too small.
Convoy Effect In First Come First Serve (FCFS )
The Non preemptive way means that if a process or job is started execution, then
the operating system must complete its process or job. Until, the process or job is
zero the new or next process or job does not start its execution. The definition of
Non Preemptive Scheduling in terms of Operating System means that the Central
Processing Unit (CPU) will be completely dedicated till the end of the process or job
started first and the new process or job is executed only after finishing of the older
process or job.
There may be a few cases, which might cause the Central Processing Unit (CPU) to
allot a too much time. This is because in the First Come First Serve Scheduling
Algorithm Non Preemptive Approach, the Processes or the jobs are chosen in serial
order. Due, to this shorter jobs or processes behind the larger processes or jobs
takes too much time to complete its execution. Due, to this the Waiting Time, Turn
Around Time, Completion Time is very high.
So, here as the first process is large or completion time is too high, then this Convoy
effect in the First Come First Serve Algorithm is occurred.
Let us assume that Longer Job takes infinite time to complete. Then, the remaining
processes have to wait for the same infinite time. Due to this Convoy Effect created
by the Longer Job the Starvation of the waiting processes increases very rapidly.
This is the biggest disadvantage of FCFS CPU Process Scheduling.
1. Implementation is simple.
2. Does not cause any causalities while using
3. It adopts a non pre emptive and pre emptive strategy.
4. It runs each procedure in the order that they are received.
5. Arrival time is used as a selection criterion for procedures.
Example
Now, let us solve this problem with the help of the Scheduling Algorithm named
First Come First Serve in a Non Preemptive Approach.
Average CT = ( 9 + 12 + 14 + 18 + 21 + 23 ) / 6
Average CT = 97 / 6
Average CT = 16.16667
Average WT = ( 0 + 8 + 11 + 13 + 16 + 18 ) /6
Average WT = 66 / 6
Average WT = 11
Average TAT = 89 / 6
Now, let us understand how they can be solved in Pre Emptive Approach
In Pre Emptive Approach we search for the best process which is available