Unit 2
Unit 2
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 –
Stack
1 The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
Heap
2
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.
Data
4
This section contains the global and static variables.
Program:A program is a piece of code which may be a single line
or millions of lines. A computer program is usually written by a
computer programmer in a programming language. For example,
here is a simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
Ready
The process is waiting to be assigned to a processor. Ready processes are
2 waiting to 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
Once the process finishes its execution, or it is terminated by the operating
5 system, it is moved to the terminated state where it waits to be removed
from main memory.
The PCB is maintained for a process throughout its lifetime, and is deleted once the
process terminates.
Thread in Operating System
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.
All threads belonging to the same process share – code section, data section, and OS
resources (e.g. open files and signals)
But each thread has its own (thread control block) – thread ID, program counter,
register set, and a stack
Any operating system process can execute a thread. we can say that single process can
have multiple threads.
Why Do We Need Thread?
Threads run in concurrent manner that improves 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. For example, when we work on Microsoft
Word or Google Docs, we notice that while we are typing, multiple things happen
together (formatting is applied, page is changed and auto save happens).
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
These are the basic components of the Operating System.
Stack Space: Stores local variables, function calls, and return addresses specific to the
thread.
Register Set: Hold temporary data and intermediate results for the thread’s execution.
Program Counter: Tracks the current instruction being executed by the thread.
Types of Thread in Operating System
Threads are of two types. These are described below.
User Level Thread
Kernel Level Thread
Threads
The primary difference is that threads within the same process run in a shared memory
space, while processes run in separate memory spaces. Threads are not independent of
one another like processes are, and as a result, threads share with other threads their code
section, data section, and OS resources (like open files and signals). But, like a process, a
thread has its own program counter (PC), register set, and stack space.
For more, refer to Difference Between Process and 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.
Single Threaded vs Multi-threaded Process
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.
Responsiveness: If the process is divided into multiple threads, if one thread completes its
execution, then its output can be immediately returned.
Faster context switch: Context switch time between threads is lower compared to the
process context switch. Process context switching requires more overhead from the CPU.
Effective utilization of multiprocessor system: If we have multiple threads in a single
process, then we can schedule multiple threads on multiple processors. This will make
process execution faster.
Resource sharing: Resources like code, data, and files can be shared among all threads
within a process. Note: Stacks and registers can’t be shared among the threads. Each thread
has its own stack and registers.
Communication: Communication between multiple threads is easier, as the threads share a
common address space. while in the process we have to follow some specific communication
techniques for communication between the two processes.
Enhanced throughput of the system: If a process is divided into multiple threads, and each
thread function is considered as one job, then the number of jobs completed per unit of time is
increased, thus increasing the throughput of the system.
Definition
.Categories of Scheduling
There are two categories of scheduling:
1. Non-preemptive: Here the resource can’t be taken from a process until the process completes
execution. The switching of resources occurs when the running process terminates and moves
to a waiting state.
2. Preemptive: Here the OS allocates the resources to a process for a fixed amount of time.
During resource allocation, the process switches from running state to ready state or from
waiting state to ready state. This switching occurs as the CPU may give priority to other
processes and replace the process with higher priority with the running process.
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.
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.
2 Queue is implemented 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 provides lesser
It controls the degree of It reduces the degree of
3 control over degree of
multiprogramming multiprogramming.
multiprogramming
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
CPU Scheduling
processes,
the CPU is
allocated.
Based on
More Minimum
the lowest Smaller
complex Average
CPU burst than FCFS No Yes
than FCFS Waiting
SJF time (BT). Time
Same as SJF
the
Depending
allocation
on some
of the CPU The
More measures
is based on preference
complex e.g., arrival Yes Yes is given to
the lowest
than FCFS time, the short
CPU burst
process jobs
time (BT).
size, etc
But it is
SRTF preemptive.
According
to the order The
Large as
of the complexity Each
compared
process depends on process has
to SJF and
arrives with Time Yes No given a
Priority fairly fixed
fixed time Quantum
scheduling. time
quantum size
RR (TQ)
Average
Algorithm waiting time
Allocation Complexity (AWT) Preemption Starvation Performance
According
to the
priority. This type is Well
Smaller performance
The bigger less Yes Yes but contain
than FCFS
priority task complex a starvation
Priority Pre- executes problem
emptive first
According
to the
This type is
priority
less
with Preemptive Most
complex
monitoring Smaller beneficial
than No Yes
the new than FCFS with batch
Priority systems
incoming
preemptive
Priority non- higher
preemptive priority jobs
According
to the More
process complex Good
that resides than the Smaller performance
in the priority than FCFS No Yes but contain
bigger scheduling a starvation
problem
queue algorithms
MLQ priority
P0 0 ms 9 ms
P1 1 ms 4 ms
P2 2 ms 9 ms
The pre-emptive shortest job first scheduling algorithm is used. Scheduling is carried out
only at arrival or completion of processes. What is the average waiting time for the three
processes?
(A) 5.0 ms
(B) 4.33 ms
(C) 6.33
(D) 7.33
Solution: (A)
Process P0 is allocated processor at 0 ms as there is no other process in the ready queue. P0 is
preempted after 1 ms as P1 arrives at 1 ms and burst time for P1 is less than remaining time of
P0. P1 runs for 4ms. P2 arrived at 2 ms but P1 continued as burst time of P2 is longer than P1.
After P1 completes, P0 is scheduled again as the remaining time for P0 is less than the burst time
of P2. P0 waits for 4 ms, P1 waits for 0 ms and P2 waits for 11 ms. So average waiting time is
(0+4+11)/3 = 5.
Question: Consider the following set of processes, with the arrival times and the CPU-burst times
given in milliseconds.
P1 0 ms 5 ms
P2 1 ms 3 ms
P3 2 ms 3 ms
P4 4 ms 1 ms
What is the average turnaround time for these processes with the preemptive Shortest
Remaining Processing Time First algorithm ?
(A) 5.50
(B) 5.75
(C) 6.00
(D) 6.25
Answer: (A)
Solution: The following is Gantt Chart of execution
P1 P2 P4 P3 P1
1 4 5 8 12
Turn Around Time = Completion Time – Arrival Time Avg Turn Around Time = (12 + 3 + 6+
1)/4 = 5.50
Question: An operating system uses the Shortest Remaining Time First (SRTF) process scheduling
algorithm. Consider the arrival times and execution times for the following processes:
P1 20 ms 0 ms
P2 25 ms 15 ms
P3 10 ms 30 ms
P4 15 ms 45 ms
Preemptive Scheduling
PREEMPTIVE NON-PREEMPTIVE
Parameter SCHEDULING SCHEDULING
It has overheads of
It does not have overheads
Overhead scheduling the processes