0% found this document useful (0 votes)
34 views59 pages

OS III Unit Notes

OS unit 3 handwritten notes

Uploaded by

arhamakhan024
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)
34 views59 pages

OS III Unit Notes

OS unit 3 handwritten notes

Uploaded by

arhamakhan024
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/ 59

Process Table and Process Control Block (PCB)

● While creating a process the operating system performs several operations.


● To identify the processes, it assigns a process identification number (PID) to each
process.
● As the operating system supports multi-programming, it needs to keep track of all the
processes. For this task,
● the process control block (PCB) is used to track the process’s execution status.
● Each block of memory contains information about
o the process state,
o program counter,
o stack pointer,
o status of opened files,
o scheduling algorithms, etc.
● All these information is required and must be saved when the process is switched from
one state to another.
● When the process makes a transition from one state to another, the operating system must
update information in the process’s PCB.
A process control block (PCB) contains information about the process, i.e. registers, quantum,
priority, etc.

The process table is an array of PCB’s, that means logically contains a PCB for all of the current
processes in the system.

● Pointer – It is a stack pointer which is required to be saved when the process is switched
from one state to another to retain the current position of the process.
● Process state – It stores the respective state of the process.
● Process number – Every process is assigned with a unique id known as process ID or
PID which stores the process identifier.
● Program counter – It stores the counter which contains the address of the next instruction
that is to be executed for the process.
● Register – These are the CPU registers which includes: accumulator, base, registers and
general purpose registers.
● Memory limits – This field contains the information about memory management system
used by operating system. This may include the page tables, segment tables etc.
● Open files list – This information includes the list of files opened for a process.

Miscellaneous accounting and status data –


● This field includes information about the amount of CPU used, time constraints, jobs or
process number, etc.
The process control block stores the register content also known as execution content of
the processor when it was blocked from running.
● This execution content architecture enables the operating system to restore a process’s
execution context when the process returns to the running state.
● When the process makes a transition from one state to another, the operating system
updates its information in the process’s PCB.
● The operating system maintains pointers to each process’s PCB in a process table so that
it can access the PCB quickly.
Process Schedulers in Operating System

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.
Process scheduling is an essential part of a Multiprogramming operating systems. Such operating
systems allow more than one process to be loaded into the executable memory at a time and the
loaded process shares the CPU using time multiplexing.
There are three types of process scheduler.

1. Long Term or job scheduler :


o It brings the new process to the ‘Ready State’.
o It controls Degree of Multi-programming, i.e., number of process present in ready
state at any point of time.
o It is important that the long-term scheduler make a careful selection of both IO and
CPU bound process.
o IO bound tasks are which use much of their time in input and output operations while
CPU bound processes are which spend their time on CPU.
o The job scheduler increases efficiency by maintaining a balance between the two.

2. Short term or CPU scheduler :


● It is responsible for selecting one process from ready state for scheduling it on the
running state.
● Note: Short-term scheduler only selects the process to schedule it doesn’t load the
process on running.
● Here is when all the scheduling algorithms are used.
● The CPU scheduler is responsible for ensuring there is no starvation owing to high
burst time processes.

● Dispatcher
o responsible for loading the process selected by Short-term scheduler on the
CPU (Ready to Running State)
o Context switching is done by dispatcher only.
o A dispatcher does the following:
1. Switching context.
2. Switching to user mode.
3. Jumping to the proper location in the newly loaded program.
● 3.Medium-term scheduler :
It is responsible for suspending and resuming the process.
o It mainly does swapping (moving processes from main memory to disk and vice
versa).
o Swapping may be necessary to improve the process mix or because a change in
memory requirements has overcommitted available memory, requiring memory to
be freed up.
o It is helpful in maintaining a perfect balance between the I/O bound and the CPU
bound.
o It reduces the degree of multiprogramming

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.
Process scheduling is an essential part of a Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at a
time and the loaded process shares the CPU using time multiplexing.
Process Scheduling Queues
The OS maintains all 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.
READY QUE HEADER CONTAIND THEPOINTER TO THE FIRST AND
THE FINAL PCB
● 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
Two-state process model refers to running and non-running states which are described below −
S.N State & Description
.

1 Running
When a new process is created, it enters into the system as in the running state.

2 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 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
Long Term Scheduler
It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing. It selects processes from the queue and loads them into
memory for execution. Process loads into the memory for CPU scheduling.
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.
Short Term Scheduler
It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.
Medium Term Scheduler
Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling
the swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled
out. Swapping may be necessary to improve the process mix.
Comparison among Scheduler
S.N Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler
.

1 It is a job scheduler It is a CPU scheduler It is a process swapping


scheduler.

2 Speed is lesser than short Speed is fastest among Speed is in between both short
term scheduler other two and long term scheduler.

3 It controls the degree of It provides lesser control It reduces the degree of


multiprogramming over degree of multiprogramming.
multiprogramming

4 It is almost absent or minimal It is also minimal in time It is a part of Time sharing


in time sharing system sharing system systems.
5 It selects processes from pool It selects those processes It can re-introduce the process
and loads them into memory which are ready to execute into memory and execution
for execution can be continued.

Context Switch
A context switch 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

More Detail

Process Scheduling

CPU-I/O BURST CYCLE


SCHEDULING CRITERIA:
CPU Scheduling Criteria
Different CPU scheduling algorithms have different properties and the choice of a particular
algorithm depends on the various factors. Many criteria have been suggested for comparing CPU
scheduling algorithms.
The criteria include the following:
1. CPU utilisation –
The main objective of any CPU scheduling algorithm is to keep the CPU as busy as possible.
Theoretically, CPU utilisation can range from 0 to 100 but in a real-time system, it varies
from 40 to 90 percent depending on the load upon the system.

2. Throughput –
A measure of the work done by CPU is the number of processes being executed and
completed per unit time. This is called throughput. The throughput may vary depending
upon the length or duration of processes.

3. Turnaround time –
For a particular process, an important criteria is how long it takes to execute that process.
The time elapsed from the time of submission of a process to the time of completion is
known as the turnaround time. Turn-around time is the sum of times spent waiting to get into
memory, waiting in ready queue, executing in CPU, and waiting for I/O.

4. Waiting time –
A scheduling algorithm does not affect the time required to complete the process once it
starts execution. It only affects the waiting time of a process i.e. time spent by a process
waiting in the ready queue.

5. Response time –
In an interactive system, turn-around time is not the best criteria. A process may produce
some output fairly early and continue computing new results while previous results are being
output to the user. Thus another criteria is the time taken from submission of the process of
request until the first response is produced. This measure is called response time.

There are various CPU Scheduling algorithms such as-


● First Come First Served (FCFS)
● Shortest Job First (SJF)
● Longest Job First (LJF)
● Priority Scheduling
● Round Robin (RR)
● Shortest Remaining Time First (SRTF)
● Longest Remaining Time First (LRTF)

More Detail

What is CPU Scheduling?


CPU Scheduling is a process of determining which process will own CPU for execution while
another process is on hold. The main task of CPU scheduling is to make sure that whenever the
CPU remains idle, the OS at least select one of the processes available in the ready queue for
execution. The selection process will be carried out by the CPU scheduler. It selects one of the
processes in memory that are ready for execution.

Types of CPU Scheduling


Here are two kinds of Scheduling methods:
Preemptive Scheduling
In Preemptive Scheduling, the tasks are mostly assigned with their priorities. Sometimes it is
important to run a task with a higher priority before another lower priority task, even if the lower
priority task is still running. The lower priority task holds for some time and resumes when the
higher priority task finishes its execution.

Non-Preemptive Scheduling
In this type of scheduling method, the CPU has been allocated to a specific process. The process
that keeps the CPU busy will release the CPU either by switching context or terminating. It is the
only method that can be used for various hardware platforms. That’s because it doesn’t need
special hardware (for example, a timer) like preemptive scheduling.

When scheduling is Preemptive or Non-Preemptive?


To determine if scheduling is preemptive or non-preemptive, consider these four parameters:

1. A process switches from the running to the waiting state.


2. Specific process switches from the running state to the ready state.
3. Specific process switches from the waiting state to the ready state.
4. Process finished its execution and terminated.

Only conditions 1 and 4 apply, the scheduling is called non- preemptive.


CPU Scheduling Criteria
A CPU scheduling algorithm tries to maximize and minimize the following:

Types of CPU scheduling Algorithm


There are mainly six types of process scheduling algorithms

1. First Come First Serve (FCFS)


2. Shortest-Job-First (SJF) Scheduling
3. Shortest Remaining Time
4. Priority Scheduling
5. Round Robin Scheduling
6. Multilevel Queue Scheduling
First Come First Serve
First Come First Serve is the full form of FCFS. It is the easiest and most simple CPU scheduling
algorithm. In this type of algorithm, the process which requests the CPU gets the CPU allocation
first. This scheduling method can be managed with a FIFO queue.
As the process enters the ready queue, its PCB (Process Control Block) is linked with the tail of
the queue. So, when CPU becomes free, it should be assigned to the process at the beginning of
the queue.

Implemented by using FIFO queue

Characteristics of FCFS method:

● It offers non-preemptive and pre-emptive scheduling algorithm.


● Jobs are always executed on a first-come, first-serve basis
● It is easy to implement and use.
● However, this method is poor in performance, and the general wait time is quite high.
1. Turn Around Time = Completion Time - Arrival Time
2. Waiting Time = Turn Around Time - Burst Time
In the First scenario, The Process P1 arrives at the first in the queue although; the burst time of
the process is the highest among all. Since, the Scheduling algorithm, we are following is FCFS
hence the CPU will execute the Process P1 first.

In this schedule, the average waiting time of the system will be very high. That is because of the
convoy effect. The other processes P2, P3 have to wait for their turn for 40 units of time although
their burst time is very low. This schedule suffers from starvation.

n the Second scenario, If Process P1 would have arrived at the last of the queue and the other
processes P2 and P3 at earlier then the problem of starvation would not be there.

Following example shows the deviation in the waiting times of both the scenarios. Although the
length of the schedule is same that is 44 units but the waiting time will be lesser in this schedule.
Example

In the Example, We have 3 processes named as P1, P2 and P3. The Burt Time of process P1 is
highest.

The Turnaround time and the waiting time in the following table, are calculated by the formula,

Shortest Job Next (SJN)


● This is also known as shortest job first, or SJF
● This is a non-preemptive, pre-emptive scheduling algorithm.
● Best approach to minimize waiting time.
● Easy to implement in Batch systems where required CPU time is known in advance.
● Impossible to implement in interactive systems where required CPU time is not known.
The processer should know in advance how much time process will take. Advantages of SJF
1. Maximum throughput
2. Minimum average waiting and turnaround time

Disadvantages of SJF
1. May suffer with the problem of starvation
2. It is not implementable because the exact Burst time for a process can't be known in
advance.

Example

In the following example, there are five jobs named as P1, P2, P3, P4 and P5. Their arrival time
and burst time are given in the table below.

Since, No Process arrives at time 0 hence; there will be an empty slot in the Gantt chart from
time 0 to 1 (the time at which the first process arrives).
According to the algorithm, the OS schedules the process which is having the lowest burst time
among the available processes in the ready queue.

Till now, we have only one process in the ready queue hence the scheduler will schedule this to
the processor no matter what is its burst time.

This will be executed till 8 units of time. Till then we have three more processes arrived in the
ready queue hence the scheduler will choose the process with the lowest burst time.

Among the processes given in the table, P3 will be executed next since it is having the lowest
burst time among all the available processes.

So that's how the procedure will go on in shortest job first (SJF) scheduling algorithm.

Shortest Remaining Time First (SRTF) Scheduling Algorithm

This Algorithm is the preemptive version of SJF scheduling. In SRTF, the execution of the
process can be stopped after certain amount of time. At the arrival of every process, the short
term scheduler schedules the process with the least remaining burst time among the list of
available processes and the running process.

Once all the processes are available in the ready queue, No preemption will be done and the
algorithm will work as SJF scheduling. The context of the process is saved in the Process
Control Block when the process is removed from the execution and the next process is
scheduled. This PCB is accessed on the next execution of this process.

Example

In this Example, there are five jobs P1, P2, P3, P4, P5 and P6. Their arrival time and burst time
are given below in the table.
The Gantt chart is prepared according to the arrival and burst time given in the table.
1. Since, at time 0, the only available process is P1 with CPU burst time 8. This is the only
available process in the list therefore it is scheduled.
2. The next process arrives at time unit 1. Since the algorithm we are using is SRTF which
is a preemptive one, the current execution is stopped and the scheduler checks for the
process with the least burst time.
Till now, there are two processes available in the ready queue. The OS has executed P1
for one unit of time till now; the remaining burst time of P1 is 7 units. The burst time of
Process P2 is 4 units. Hence Process P2 is scheduled on the CPU according to the
algorithm.
3. The next process P3 arrives at time unit 2. At this time, the execution of process P3 is
stopped and the process with the least remaining burst time is searched. Since the process
P3 has 2 unit of burst time hence it will be given priority over others.
4. The Next Process P4 arrives at time unit 3. At this arrival, the scheduler will stop the
execution of P4 and check which process is having least burst time among the available
processes (P1, P2, P3 and P4). P1 and P2 are having the remaining burst time 7 units and
3 units respectively.

P3 and P4 are having the remaining burst time 1 unit each. Since, both are equal hence
the scheduling will be done according to their arrival time. P3 arrives earlier than P4 and
therefore it will be scheduled again.
5. The Next Process P5 arrives at time unit 4. Till this time, the Process P3 has completed
its execution and it is no more in the list. The scheduler will compare the remaining burst
time of all the available processes. Since the burst time of process P4 is 1 which is least
among all hence this will be scheduled.
6. The Next Process P6 arrives at time unit 5, till this time, the Process P4 has completed its
execution. We have 4 available processes till now, that are P1 (7), P2 (3), P5 (3) and P6
(2). The Burst time of P6 is the least among all hence P6 is scheduled. Since, now, all the
processes are available hence the algorithm will now work same as SJF. P6 will be
executed till its completion and then the process with the least remaining time will be
scheduled.

Once all the processes arrive, No preemption is done and the algorithm will work as SJF.
Gate 2011
Priority Based Scheduling
● Priority scheduling is a non-preemptive algorithm and one of the most common
scheduling algorithms in batch systems.
● Each process is assigned a priority. Process with highest priority is to be executed first
and so on.
● Processes with same priority are executed on first come first served basis.
● Priority can be decided based on memory requirements, time requirements or any other
resource requirement.
Given: Table of processes, and their Arrival time, Execution time, and priority. Here we are
considering 1 is the lowest priority.

Process Arrival Time Execution Time Priority Service Time

P0 0 5 1 0

P1 1 3 2 11

P2 2 8 1 14

P3 3 6 3 5
Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 11 - 1 = 10

P2 14 - 2 = 12

P3 5-3=2

Average Wait Time: (0 + 10 + 12 + 2)/4 = 24 / 4 = 6

Round Robin Scheduling Algorithm

Round Robin scheduling algorithm is one of the most popular scheduling algorithm which can
actually be implemented in most of the operating systems. This is the preemptive version of
first come first serve scheduling. The Algorithm focuses on Time Sharing. In this algorithm,
every process gets executed in a cyclic way. A certain time slice is defined in the system which
is called time quantum. Each process present in the ready queue is assigned the CPU for that
time quantum, if the execution of the process is completed during that time then the process
will terminate else the process will go back to the ready queue and waits for the next turn to
complete the execution.
Advantages
1. It can be actually implementable in the system because it is not depending on the burst
time.
2. It doesn't suffer from the problem of starvation or convoy effect.
3. All the jobs get a fare allocation of CPU.

Disadvantages
1. The higher the time quantum, the higher the response time in the system.
2. The lower the time quantum, the higher the context switching overhead in the system.
3. Deciding a perfect time quantum is really a very difficult task in the system.
RR Scheduling Example

In the following example, there are six processes named as P1, P2, P3, P4, P5 and P6. Their
arrival time and burst time are given below in the table. The time quantum of the system is 4
units.

Process ID Arrival Time Burst Time

1 0 5

2 1 6

3 2 3

4 3 1

5 4 5

6 6 4

According to the algorithm, we have to maintain the ready queue and the Gantt chart. The
structure of both the data structures will be changed after every scheduling.

Ready Queue:

Initially, at time 0, process P1 arrives which will be scheduled for the time slice 4 units. Hence in
the ready queue, there will be only one process P1 at starting with CPU burst time 5 units.
P1

GANTT chart

The P1 will be executed for 4 units first.

45.2M

996

OOPs Concepts in Java

Ready Queue

Meanwhile the execution of P1, four more processes P2, P3, P4 and P5 arrives in the ready
queue. P1 has not completed yet, it needs another 1 unit of time hence it will also be added back
to the ready queue.

P2 P3 P4 P5 P1

6 3 1 5 1

GANTT chart

After P1, P2 will be executed for 4 units of time which is shown in the Gantt chart.
Ready Queue

During the execution of P2, one more process P6 is arrived in the ready queue. Since P2 has not
completed yet hence, P2 will also be added back to the ready queue with the remaining burst
time 2 units.

P3 P4 P5 P1 P6 P2

3 1 5 1 4 2

GANTT chart

After P1 and P2, P3 will get executed for 3 units of time since its CPU burst time is only 3
seconds.

Ready Queue

Since P3 has been completed, hence it will be terminated and not be added to the ready queue.
The next process will be executed is P4.

P4 P5 P1 P6 P2
1 5 1 4 2

GANTT chart

After, P1, P2 and P3, P4 will get executed. Its burst time is only 1 unit which is lesser then the
time quantum hence it will be completed.

Ready Queue

The next process in the ready queue is P5 with 5 units of burst time. Since P4 is completed hence
it will not be added back to the queue.

P5 P1 P6 P2

5 1 4 2

GANTT chart

P5 will be executed for the whole time slice because it requires 5 units of burst time which is
higher than the time slice.
Ready Queue

P5 has not been completed yet; it will be added back to the queue with the remaining burst time
of 1 unit.

P1 P6 P2 P5

1 4 2 1

GANTT Chart

The process P1 will be given the next turn to complete its execution. Since it only requires 1 unit
of burst time hence it will be completed.

Ready Queue

P1 is completed and will not be added back to the ready queue. The next process P6 requires
only 4 units of burst time and it will be executed next.

P6 P2 P5

4 2 1

GANTT chart

P6 will be executed for 4 units of time till completion.


Ready Queue

Since P6 is completed, hence it will not be added again to the queue. There are only two
processes present in the ready queue. The Next process P2 requires only 2 units of time.

P2 P5

2 1

GANTT Chart

P2 will get executed again, since it only requires only 2 units of time hence this will be
completed.

Ready Queue

Now, the only available process in the queue is P5 which requires 1 unit of burst time. Since the
time slice is of 4 units hence it will be completed in the next burst.

P5
1

GANTT chart

P5 will get executed till completion.

The completion time, Turnaround time and waiting time will be calculated as shown in the table
below.

As, we know,

1. Turn Around Time = Completion Time - Arrival Time


2. Waiting Time = Turn Around Time - Burst Time

Process Arrival Burst Completion Turn Around Waiting


ID Time Time Time Time Time

1 0 5 17 17 12

2 1 6 23 22 16

3 2 3 11 9 6

4 3 1 12 9 8

5 4 5 24 20 15

6 6 4 21 15 11
Avg Waiting Time = (12+16+6+8+15+11)/6 = 76/6 units

Multiple-Level Queues Scheduling


Multiple-level queues are not an independent scheduling algorithm. They make use of other
existing algorithms to group and schedule jobs with common characteristics.

● Multiple queues are maintained for processes with common characteristics.


● Each queue can have its own scheduling algorithms.
● Priorities are assigned to each queue.
For example, CPU-bound jobs can be scheduled in one queue and all I/O-bound jobs in another
queue. The Process Scheduler then alternately selects jobs from each queue and assigns them to
the CPU based on the algorithm assigned to the queue.
Types of system calls
The different system calls are as follows −
● System calls for Process management
● System calls for File management
● System calls for Directory management
Now let us discuss process management system calls.
System calls for Process management
A system is used to create a new process or a duplicate process called a fork.
The duplicate process consists of all data in the file description and registers common. The
original process is also called the parent process and the duplicate is called the child process.
The fork call returns a value, which is zero in the child and equal to the child’s PID (Process
Identifier) in the parent. The system calls like exit would request the services for terminating a
process.
Loading of programs or changing of the original image with duplicate needs execution of exec.
Pid would help to distinguish between child and parent processes.
Example
Process management system calls in Linux.
● fork − For creating a duplicate process from the parent process.
● wait − Processes are supposed to wait for other processes to complete their work.
● exec − Loads the selected program into the memory.
● exit − Terminates the process.
The pictorial representation of process management system calls is as follows −
fork() − A parent process always uses a fork for creating a new child process. The child process
is generally called a copy of the parent. After execution of fork, both parent and child execute the
same program in separate processes.
exec() − This function is used to replace the program executed by a process. The child
sometimes may use exec after a fork for replacing the process memory space with a new
program executable making the child execute a different program than the parent.
exit() − This function is used to terminate the process.
wait() − The parent uses a wait function to suspend execution till a child terminates. Using wait
the parent can obtain the exit status of a terminated child.

What is a Thread?
A thread is a path of execution within a process. A process can contain multiple threads.
Why Multithreading?
A thread is also known as 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
Process vs Thread?
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 process, a thread has its own program counter (PC), register set, and stack space.

Advantages of Thread over Process


1. Responsiveness: If the process is divided into multiple threads, if one thread completes its
execution, then its output can be immediately returned.
2. Faster context switch: Context switch time between threads is lower compared to process
context switch. Process context switching requires more overhead from the CPU.
3. Effective utilization of multiprocessor system: If we have multiple threads in a single process,
then we can schedule multiple threads on multiple processor. This will make process execution
faster.
4. Resource sharing: Resources like code, data, and files can be shared among all threads within
a process.
Note: stack and registers can’t be shared among the threads. Each thread has its own stack and
registers.
5. Communication: Communication between multiple threads is easier, as the threads shares
common address space. while in process we have to follow some specific communication
technique for communication between two process.
6. 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.
Types of Threads
There are two types of threads.
User Level Thread
Kernel Level Thread
Difference between User Level thread and Kernel Level thread
● Difficulty Level : Easy
● Last Updated : 02 Nov, 2021
User level thread Kernel level thread
User thread are implemented by users. kernel threads are implemented by OS.
OS doesn’t recognize user level threads. Kernel threads are recognized by OS.
Implementation of User threads is easy. Implementation of Kernel thread is complicated.
Context switch time is less. Context switch time is more.
Context switch requires no hardware support. Hardware support is needed.
If one user level thread perform blocking If one kernel thread perform blocking operation
operation then entire process will be blocked. then another thread can continue execution.
User level threads are designed as dependent Kernel level threads are designed as independent
threads. threads.
Example : Java thread, POSIX threads. Example : Window Solaris.
Memory Management in Operating System
● Difficulty Level : Medium
● Last Updated : 22 Nov, 2021
The term Memory can be defined as a collection of data in a specific format. It is used to store
instructions and processed data. The memory comprises a large array or group of words or bytes,
each with its own location. The primary motive of a computer system is to execute programs.
These programs, along with the information they access, should be in the main memory during
execution. The CPU fetches instructions from memory according to the value of the program
counter.
To achieve a degree of multiprogramming and proper utilization of memory, memory
management is important. Many memory management methods exist, reflecting various
approaches, and the effectiveness of each algorithm depends on the situation.
Here, we will cover the following memory management topics:
● What is Main Memory
● What is Memory Management
● Why memory Management is required
● Logical address space and Physical address space
● Static and dynamic loading
● Static and dynamic linking
● Swapping
● Contiguous Memory allocation
● Memory Allocation
● First Fit
● Best Fit
● Worst Fit
● Fragmentation
● Internal Fragmentation
● External Fragmentation
● Paging
Now before, We start memory management let us known about what is main memory.

What is Main Memory:

The main memory is central to the operation of a modern computer. Main Memory is a large
array of words or bytes, ranging in size from hundreds of thousands to billions. Main memory is
a repository of rapidly available information shared by the CPU and I/O devices. Main memory
is the place where programs and information are kept when the processor is effectively utilizing
them. Main memory is associated with the processor, so moving instructions and information
into and out of the processor is extremely fast. Main memory is also known as RAM(Random
Access Memory). This memory is a volatile memory.RAM lost its data when a power
interruption occurs.
Figure 1: Memory hierarchy

What is Memory Management :

In a multiprogramming computer, the operating system resides in a part of memory and the rest
is used by multiple processes. The task of subdividing the memory among different processes is
called memory management. Memory management is a method in the operating system to
manage operations between main memory and disk during process execution. The main aim of
memory management is to achieve efficient utilization of memory.

Why Memory Management is required:

● Allocate and de-allocate memory before and after process execution.


● To keep track of used memory space by processes.
● To minimize fragmentation issues.
● To proper utilization of main memory.
● To maintain data integrity while executing of process.

Now we are discussing the concept of logical address space and Physical address space:
Logical and Physical Address Space:

Logical Address space: An address generated by the CPU is known as “Logical Address”. It is
also known as a Virtual address. Logical address space can be defined as the size of the process.
A logical address can be changed.
Physical Address space: An address seen by the memory unit (i.e the one loaded into the
memory address register of the memory) is commonly known as a “Physical Address”. A
Physical address is also known as a Real address. The set of all physical addresses corresponding
to these logical addresses is known as Physical address space. A physical address is computed by
MMU. The run-time mapping from virtual to physical addresses is done by a hardware device
Memory Management Unit(MMU). The physical address always remains constant.

Static and Dynamic Loading:

To load a process into the main memory is done by a loader. There are two different types of
loading :
● Static loading:- loading the entire program into a fixed address. It requires more memory
space.
● Dynamic loading:- The entire program and all data of a process must be in physical memory
for the process to execute. So, the size of a process is limited to the size of physical memory.
To gain proper memory utilization, dynamic loading is used. In dynamic loading, a routine is
not loaded until it is called. All routines are residing on disk in a relocatable load format. One
of the advantages of dynamic loading is that unused routine is never loaded. This loading is
useful when a large amount of code is needed to handle it efficiently.

Static and Dynamic linking:

To perform a linking task a linker is used. A linker is a program that takes one or more object
files generated by a compiler and combines them into a single executable file.
● Static linking: In static linking, the linker combines all necessary program modules into a
single executable program. So there is no runtime dependency. Some operating systems
support only static linking, in which system language libraries are treated like any other
object module.
● Dynamic linking: The basic concept of dynamic linking is similar to dynamic loading. In
dynamic linking, “Stub” is included for each appropriate library routine reference. A stub is a
small piece of code. When the stub is executed, it checks whether the needed routine is
already in memory or not. If not available then the program loads the routine into memory.

Swapping :

When a process is executed it must have resided in memory. Swapping is a process of swap a
process temporarily into a secondary memory from the main memory, which is fast as compared
to secondary memory. A swapping allows more processes to be run and can be fit into memory at
one time. The main part of swapping is transferred time and the total time directly proportional
to the amount of memory swapped. Swapping is also known as roll-out, roll in, because if a
higher priority process arrives and wants service, the memory manager can swap out the lower
priority process and then load and execute the higher priority process. After finishing higher
priority work, the lower priority process swapped back in memory and continued
to the execution process.

Contiguous Memory Allocation :


The main memory should oblige both the operating system and the different client processes.
Therefore, the allocation of memory becomes an important task in the operating system. The
memory is usually divided into two partitions: one for the resident operating system and one for
the user processes. We normally need several user processes to reside in memory simultaneously.
Therefore, we need to consider how to allocate available memory to the processes that are in the
input queue waiting to be brought into memory. In adjacent memory allotment, each process is
contained in a single contiguous segment of memory.
Memory allocation:

To gain proper memory utilization, memory allocation must be allocated efficient manner. One
of the simplest methods for allocating memory is to divide memory into several fixed-sized
partitions and each partition contains exactly one process. Thus, the degree of multiprogramming
is obtained by the number of partitions.
Multiple partition allocation: In this method, a process is selected from the input queue and
loaded into the free partition. When the process terminates, the partition becomes available for
other processes.
Fixed partition allocation: In this method, the operating system maintains a table that indicates
which parts of memory are available and which are occupied by processes. Initially, all memory
is available for user processes and is considered one large block of available memory. This
available memory is known as “Hole”. When the process arrives and needs memory, we search
for a hole that is large enough to store this process. If the requirement fulfills then we allocate
memory to process, otherwise keeping the rest available to satisfy future requests. While
allocating a memory sometimes dynamic storage allocation problems occur, which concerns how
to satisfy a request of size n from a list of free holes. There are some solutions to this problem:

First fit:-
In the first fit, the first available free hole fulfills the requirement of the process allocated.

Here, in this diagram 40 KB memory block is the first available free hole that can store process
A (size of 25 KB), because the first two blocks did not have sufficient memory space.
Best fit:-
In the best fit, allocate the smallest hole that is big enough to process requirements. For this, we
search the entire list, unless the list is ordered by size.
Here in this example, first, we traverse the complete list and find the last hole 25KB is the best
suitable hole for Process A(size 25KB).
In this method memory utilization is maximum as compared to other memory allocation
techniques.
Worst fit:-In the worst fit, allocate the largest available hole to process. This method produces
the largest leftover hole.

Here in this example, Process A (Size 25 KB) is allocated to the largest available memory block
which is 60KB. Inefficient memory utilization is a major issue in the worst fit.

Fragmentation:

A Fragmentation is defined as when the process is loaded and removed after execution from
memory, it creates a small free hole. These holes can not be assigned to new processes because
holes are not combined or do not fulfill the memory requirement of the process. To achieve a
degree of multiprogramming, we must reduce the waste of memory or fragmentation problem. In
operating system two types of fragmentation:
Internal fragmentation:
Internal fragmentation occurs when memory blocks are allocated to the process more than their
requested size. Due to this some unused space is leftover and creates an internal fragmentation
problem.
Example: Suppose there is a fixed partitioning is used for memory allocation and the different
size of block 3MB, 6MB, and 7MB space in memory. Now a new process p4 of size 2MB comes
and demand for the block of memory. It gets a memory block of 3MB but 1MB block memory is
a waste, and it can not be allocated to other processes too. This is called internal fragmentation.
External fragmentation:
In external fragmentation, we have a free memory block, but we can not assign it to process
because blocks are not contiguous.
Example: Suppose (consider above example) three process p1, p2, p3 comes with size 2MB,
4MB, and 7MB respectively. Now they get memory blocks of size 3MB, 6MB, and 7MB
allocated respectively. After allocating process p1 process and p2 process left 1MB and 2MB.
Suppose a new process p4 comes and demands a 3MB block of memory, which is available, but
we can not assign it because free memory space is not contiguous. This is called external
fragmentation.
Both the first fit and best-fit systems for memory allocation affected by external
Paging in Operating System
● Difficulty Level : Medium
● Last Updated : 21 Sep, 2021
Paging is a memory management scheme that eliminates the need for contiguous allocation of
physical memory. This scheme permits the physical address space of a process to be non –
contiguous.
● Logical Address or Virtual Address (represented in bits): An address generated by the CPU
● Logical Address Space or Virtual Address Space( represented in words or bytes): The set of
all logical addresses generated by a program
● Physical Address (represented in bits): An address actually available on memory unit
● Physical Address Space (represented in words or bytes): The set of all physical addresses
corresponding to the logical addresses
Example:
● If Logical Address = 31 bit, then Logical Address Space = 231 words = 2 G words (1 G = 230)
● If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address = log2 227 =
27 bits
● If Physical Address = 22 bit, then Physical Address Space = 222 words = 4 M words (1 M =
220)
● If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address = log2 224 =
24 bits
The mapping from virtual to physical address is done by the memory management unit (MMU)
which is a hardware device and this mapping is known as paging technique.
● The Physical Address Space is conceptually divided into a number of fixed-size blocks,
called frames.
● The Logical address Space is also splitted into fixed-size blocks, called pages.
● Page Size = Frame Size
Let us consider an example:
● Physical Address = 12 bits, then Physical Address Space = 4 K words
● Logical Address = 13 bits, then Logical Address Space = 8 K words
● Page size = frame size = 1 K words (assumption)
Address generated by CPU is divided into
● Page number(p): Number of bits required to represent the pages in Logical Address Space
or Page number
● Page offset(d): Number of bits required to represent particular word in a page or page size
of Logical Address Space or word number of a page or page offset.
Physical Address is divided into
● Frame number(f): Number of bits required to represent the frame of Physical Address
Space or Frame number.
● Frame offset(d): Number of bits required to represent particular word in a frame or frame
size of Physical Address Space or word number of a frame or frame offset.

The hardware implementation of page table can be done by using dedicated registers. But the
usage of register for the page table is satisfactory only if page table is small. If page table contain
large number of entries then we can use TLB(translation Look-aside buffer), a special, small, fast
look up hardware cache.
● The TLB is associative, high speed memory.
● Each entry in TLB consists of two parts: a tag and a value.
● When this memory is used, then an item is compared with all tags simultaneously.If the item
is found, then corresponding value is returned.

Main memory access time = m


If page table are kept in main memory,
Effective access time = m(for page table) + m(for particular page in page table)
Page Replacement Algorithms in Operating Systems
● Difficulty Level : Easy
● Last Updated : 11 Apr, 2022
In an operating system that uses paging for memory management, a page replacement algorithm
is needed to decide which page needs to be replaced when a new page comes in.
Page Fault – A page fault happens when a running program accesses a memory page that is
mapped into the virtual address space but not loaded in physical memory.
Since actual physical memory is much smaller than virtual memory, page faults happen. In case
of a page fault, Operating System might have to replace one of the existing pages with the newly
needed page. Different page replacement algorithms suggest different ways to decide which page
to replace. The target for all algorithms is to reduce the number of page faults.
Page Replacement Algorithms :
1. First In First Out (FIFO) –
This is the simplest page replacement algorithm. In this algorithm, the operating system keeps
track of all pages in the memory in a queue, the oldest page is in the front of the queue. When a
page needs to be replaced page in the front of the queue is selected for removal.
Example-1Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the number
of page faults.

Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3
Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults.
Then 5 comes, it is not available in memory so it replaces the oldest page slot i.e 1. —>1 Page
Fault.
6 comes, it is also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page
Fault.
Finally, when 3 come it is not available so it replaces 0 1 page fault
Belady’s anomaly – Belady’s anomaly proves that it is possible to have more page faults when
increasing the number of page frames while using the First in First Out (FIFO) page replacement
algorithm. For example, if we consider reference string 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4 and 3 slots,
we get 9 total page faults, but if we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement –
In this algorithm, pages are replaced which would not be used for the longest duration of time in
the future.
Example-2:Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, with 4 page frame.
Find number of page fault.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already there so —> 0 Page fault.
when 3 came it will take the place of 7 because it is not used for the longest duration of time in
the future.—>1 Page fault.
0 is already there so —> 0 Page fault..
4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system cannot
know future requests. The use of Optimal Page replacement is to set up a benchmark so that
other replacement algorithms can be analyzed against it.
3. Least Recently Used –
In this algorithm, page will be replaced which is least recently used.
Example-3Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2 with 4 page
frames.Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page faults
0 is already their so —> 0 Page fault.
when 3 came it will take the place of 7 because it is least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available in
the memory.
GATE CS Corner Questions
Practicing the following questions will help you test your knowledge. All questions have been
asked in GATE in previous years or in GATE Mock Tests. It is highly recommended that you
practice them.

Virtual Memory in Operating System

Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as
though it were part of the main memory. The addresses a program may use to reference memory
are distinguished from the addresses the memory system uses to identify physical storage sites,
and program-generated addresses are translated automatically to the corresponding machine
addresses.
The size of virtual storage is limited by the addressing scheme of the computer system and the
amount of secondary memory is available not by the actual number of the main storage
locations.
It is a technique that is implemented using both hardware and software. It maps memory
addresses used by a program, called virtual addresses, into physical addresses in computer
memory.
1. All memory references within a process are logical addresses that are dynamically translated
into physical addresses at run time. This means that a process can be swapped in and out of
the main memory such that it occupies different places in the main memory at different times
during the course of execution.
2. A process may be broken into a number of pieces and these pieces need not be continuously
located in the main memory during execution. The combination of dynamic run-time address
translation and use of page or segment table permits this.
If these characteristics are present then, it is not necessary that all the pages or segments are
present in the main memory during execution. This means that the required pages need to be
loaded into memory whenever required. Virtual memory is implemented using Demand Paging
or Demand Segmentation.

Demand Paging :
The process of loading the page into memory on demand (whenever page fault occurs) is known
as demand paging.
The process includes the following steps :
1. If the CPU tries to refer to a page that is currently not available in the main memory, it
generates an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to proceed the OS
must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address space. The
page replacement algorithms are used for the decision-making of replacing the page in
physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will place the
process back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the
required page is brought into memory.
Advantages :
● More processes may be maintained in the main memory: Because we are going to load only
some of the pages of any particular process, there is room for more processes. This leads to
more efficient utilization of the processor because it is more likely that at least one of the
more numerous processes will be in the ready state at any particular time.
● A process may be larger than all of the main memory: One of the most fundamental
restrictions in programming is lifted. A process larger than the main memory can be
executed because of demand paging. The OS itself loads pages of a process in the main
memory as required.
● It allows greater multiprogramming levels by using less of the available (primary) memory
for each process.
Page Fault Service Time :
The time taken to service the page fault is called page fault service time. The page fault service
time includes the time taken to perform all the above six steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
Swapping:
Swapping a process out means removing all of its pages from memory, or marking them so that
they will be removed by the normal page replacement process. Suspending a process ensures that
it is not runnable while it is swapped out. At some later time, the system swaps back the process
from the secondary storage to the main memory. When a process is busy swapping pages in and
out then this situation is called thrashing.

Thrashing :
At any given time, only a few pages of any process are in the main memory and therefore more
processes can be maintained in memory. Furthermore, time is saved because unused pages are
not swapped in and out of memory. However, the OS must be clever about how it manages this
scheme. In the steady-state practically, all of the main memory will be occupied with process
pages, so that the processor and OS have direct access to as many processes as possible. Thus
when the OS brings one page in, it must throw another out. If it throws out a page just before it is
used, then it will just have to get that page again almost immediately. Too much of this leads to a
condition called Thrashing. The system spends most of its time swapping pages rather than
executing instructions. So a good page replacement algorithm is required.
In the given diagram, the initial degree of multiprogramming up to some extent of point(lambda),
the CPU utilization is very high and the system resources are utilized 100%. But if we further
increase the degree of multiprogramming the CPU utilization will drastically fall down and the
system will spend more time only on the page replacement and the time is taken to complete the
execution of the process will increase. This situation in the system is called thrashing.
Causes of Thrashing :
1. High degree of multiprogramming : If the number of processes keeps on increasing in the
memory then the number of frames allocated to each process will be decreased. So, fewer
frames will be available for each process. Due to this, a page fault will occur more frequently
and more CPU time will be wasted in just swapping in and out of pages and the utilization
will keep on decreasing.
For example:
Let free frames = 400
Case 1: Number of process = 100
Then, each process will get 4 frames.
Case 2: Number of processes = 400
Each process will get 1 frame.
Case 2 is a condition of thrashing, as the number of processes is increased, frames per
process are decreased. Hence CPU time will be consumed in just swapping pages.

2. Lacks of Frames: If a process has fewer frames then fewer pages of that process will be able
to reside in memory and hence more frequent swapping in and out will be required. This may
lead to thrashing. Hence sufficient amount of frames must be allocated to each process in
order to prevent thrashing.
Recovery of Thrashing :
● Do not allow the system to go into thrashing by instructing the long-term scheduler not to
bring the processes into memory after the threshold.
● If the system is already thrashing then instruct the mid-term schedular to suspend some of
the processes so that we can recover the system from thrashing.
Segmentation in Operating System
● Difficulty Level : Medium
● Last Updated : 21 Sep, 2021
A process is divided into Segments. The chunks that a program is divided into which are not
necessarily all of the same sizes are called segments. Segmentation gives user’s view of the
process which paging does not give. Here the user’s view is mapped to physical memory.
There are types of segmentation:
1. Virtual memory segmentation –
Each process is divided into a number of segments, not all of which are resident at any one
point in time.
2. Simple segmentation –
Each process is divided into a number of segments, all of which are loaded into memory at
run time, though not necessarily contiguously.
There is no simple relationship between logical addresses and physical addresses in
segmentation. A table stores the information about all such segments and is called Segment
Table.
Segment Table – It maps two-dimensional Logical address into one-dimensional Physical
address. It’s each table entry has:
● Base Address: It contains the starting physical address where the segments reside in
memory.
● Limit: It specifies the length of the segment.
Translation of Two dimensional Logical Address to one dimensional Physical Address.
Address generated by the CPU is divided into:
● Segment number (s): Number of bits required to represent the segment.
● Segment offset (d): Number of bits required to represent the size of the segment.
Advantages of Segmentation –
● No Internal fragmentation.
● Segment Table consumes less space in comparison to Page table in paging.
Disadvantage of Segmentation –
● As processes are loaded and removed from the memory, the free memory space is broken
into little pieces, causing External fragmentation.

You might also like