Operating System Notes
Basic ->
OS
Video Resource -> Introduction to Operating System
Theory ->
Operating Systems: It is the interface between the user and the computer hardware.
The main purpose of an OS is to execute user programs and make it easier for users to
understand and interact with computers as well as run applications. It is specially designed
to ensure that the computer system performs better by managing all computational activities.
It also manages computer memory, processes, and operation of all hardware and software.
Process
Video Resource -> Process in operating system | Lec-35 | Bhanu Priya
Process Management (Processes and Threads)
Theory ->
A process is a program under execution.
A thread is a lightweight process and forms the basic unit of CPU utilisation. A process can
perform more than one task at the same time by including multiple threads. A thread has its
own program counter, register set, and stack.
Video Resource -> L-1.11: Process Vs Threads in Operating System
Theory ->
Comparison Basis Process Thread
Definition A process is a program under A thread is a lightweight process
execution i.e an active that can be managed
program. independently by a scheduler.
Context switching Processes require more time Threads require less time for
time for context switching as they context switching as they are
are more heavy. lighter than processes.
Memory Sharing Processes are totally A thread may share some
independent and don’t share memory with its peer threads.
memory.
Communication Communication between Communication between threads
processes requires more time requires less time than between
than between threads. processes .
Blocked If a process gets blocked, If a user level thread gets
remaining processes can blocked, all of its peer threads
continue execution. also get blocked.
Resource Processes require more Threads generally need less
Consumption resources than threads. resources than processes.
Dependency Individual processes are Threads are parts of a process
independent of each other. and so are dependent.
Data and Code Processes have independent A thread shares the data
sharing data and code segments. segment, code segment, files
etc. with its peer threads.
Treatment by OS All the different processes are All user level peer threads are
treated separately by the treated as a single task by the
operating system. operating system.
Time for creation Processes require more time Threads require less time for
for creation. creation.
Time for Processes require more time Threads require less time for
termination for termination. termination.
MultiProgramming vs MultiProcessing vs Multithreading
Video Resource -> Multiprogramming Vs Multitasking Vs Multiprocessing
L-1.3: Multiprogramming and Multitasking Operating System in Hindi wit…
Theory ->
Multiprogramming – Multiprogramming is known as keeping multiple programs in the main
memory at the same time ready for execution.
Multiprocessing – A computer using more than one CPU at a time.
Multitasking – Multitasking is nothing but multiprogramming with a Round-robin scheduling
algorithm.
Multithreading is an extension of multitasking.
Scheduling
Video Resource -> CPU Scheduling | Chapter-5 | Operating System - YouTube
L-2.1: Process Scheduling Algorithms (Preemption Vs Non-Preemption) | CPU Schedu…
Theory ->
Why do we need scheduling?
A typical process involves both I/O time and CPU time. In a uniprogramming system like
MS-DOS, time spent waiting for I/O is wasted and CPU is free during this time. In
multiprogramming systems, one process can use CPU while another is waiting for I/O. This
is possible only with process scheduling.
Scheduling of processes/work is done to finish the work on time. CPU Scheduling is a
process that allows one process to use the CPU while another process is delayed (in
standby) due to unavailability of any resources such as I / O etc, thus making full use of the
CPU
Arrival Time – Time at which the process arrives in the ready queue.
Completion Time – Time at which process completes its execution.
Burst Time – Time required by a process for CPU execution.
Turn Around Time – Time Difference between completion time and arrival time.
Turn Around Time = Completion Time - Arrival Time
Waiting Time (WT) – Time Difference between turn around time and burst time.
Waiting Time = Turnaround Time - Burst Time
Scheduling Algorithms
● First Come First Serve (FCFS) : Simplest scheduling algorithm that schedules
according to arrival times of processes.
● Shortest Job First (SJF): Processes which have the shortest burst time are
scheduled first.
● Shortest Remaining Time First (SRTF): It is a preemptive mode of SJF algorithm in
which jobs are scheduled according to the shortest remaining time.
● Round Robin (RR) Scheduling: Each process is assigned a fixed time, in a cyclic
way.
● Priority Based scheduling (Non Preemptive): In this scheduling, processes are
scheduled according to their priorities, i.e., highest priority process is scheduled first.
If priorities of two processes match, then scheduling is according to the arrival time.
● Highest Response Ratio Next (HRRN): In this scheduling, processes with the highest
response ratio are scheduled. This algorithm avoids starvation.
Response Ratio = (Waiting Time + Burst time) / Burst time
● Multilevel Queue Scheduling (MLQ): According to the priority of the process,
processes are placed in the different queues. Generally high priority processes are
placed in the top level queue. Only after completion of processes from the top level
queue, lower level queued processes are scheduled.
In preemptive scheduling, the CPU is allocated to the processes for a limited time whereas,
in Non-preemptive scheduling, the CPU is allocated to the process till it terminates or
switches to the waiting state.
The executing process in preemptive scheduling is interrupted in the middle of execution
when higher priority one comes whereas the executing process in non-preemptive
scheduling is not interrupted in the middle of execution and waits till its execution.
Starvation is the problem that occurs when high priority processes keep executing and low
priority processes get blocked for indefinite time. In a heavily loaded computer system, a
steady stream of higher-priority processes can prevent a low-priority process from ever
getting the CPU.
● FCFS can cause long waiting times, especially when the first job takes too much
CPU time.
● If the time quantum for Round Robin scheduling is very large, then it behaves the
same as FCFS scheduling.
● SJF is optimal in terms of average waiting time for a given set of processes. SJF
gives a minimum average waiting time, but the problem with SJF is how to
know/predict the time of the next job.
Other Resources -> OS Scheduling Algorithms - javatpoint
Critical Section Problem
Video Resource -> L-3.4: Critical Section Problem | Mutual Exclusion, Progress and …
Critical Section problem Solution | OS | Lec-55 | Bhanu Priya
Theory ->
Critical Section – The portion of the code in the program where shared variables are
accessed and/or updated.
Remainder Section – The remaining portion of the program excluding the Critical Section.
Race around Condition – The final output of the code depends on the order in which the
variables are accessed. This is termed as the race around condition.
The critical section cannot be executed by more than one process at the same time; the
operating system faces the difficulties in allowing and disallowing the processes from
entering the critical section.
A solution for the critical section problem must satisfy the following three conditions:
Mutual Exclusion – If a process Pi is executing in its critical section, then no other process is
allowed to enter into the critical section.
Progress – If no process is executing in the critical section, then the decision of a process to
enter a critical section cannot be made by any other process that is executing in its
remainder section. The selection of the process cannot be postponed indefinitely.
Bounded Waiting – There exists a bound on the number of times other processes can enter
into the critical section after a process has made a request to access the critical section and
before the request is granted.
Other Resource -> OS Critical Section Problem - javatpoint
Semaphore
Video Resource -> L-3.8: Semaphores | Wait, Signal Operation | Counting Semaphor…
Semaphore | Counting & Binary | OS | Lec-56 | Bhanu Priya
Theory ->
Semaphore is the variable which stores the entire wake up calls that are being transferred
from producer to consumer. It is a variable on which read, modify and update happens
automatically in kernel mode.
According to the demand of the situation, Semaphore can be divided into two categories.
1. Counting Semaphore
2. Binary Semaphore or Mutex
Counting Semaphore
There are scenarios in which more than one process needs to execute in a critical section
simultaneously. However, counting semaphore can be used when we need to have more
than one process in the critical section at the same [Link] this mechanism, the entry and
exit in the critical section are performed on the basis of the value of counting semaphore.
The value of counting semaphore at any point of time indicates the maximum number of
processes that can enter in the critical section at the same time.
Down(S)
Critical Section
Up(S)
Down -> S (Decrement the value of integer variable)
-> If one process enter into critical condition then another will not be allowed
1-> critical section is empty so process can enter into critical section
0-> Some process it is present in the critical section
S = 3;
// Never mutual Exclusion
Down(S)
{
S = S-1;
if(s<0)
{
// When we wake process we don't send it to do part
// We will send it to Critical section
// Sleep in process
}
else
{
// allow process
}
}
Critical section
Up -> S (Increment the value of integer variable)
-> It will allow other process to enter into critical section
Up (S)
{
S = S+1;
if(s<=0)
{
// Wake the process
// Wake();
}
In counting semaphore, Mutual exclusion was not provided because we had the set of
processes which were required to execute in the critical section simultaneously. However,
Binary Semaphore strictly provides mutual exclusion. Here, instead of having more than 1
slot available in the critical section, we can only have at most 1 process in the critical
section. The semaphore can have only two values, 0 or 1.
Down -> S (Decrement the value of integer variable)
-> If one process enter into critical condition then another will not be allowed
1-> critical section is empty so process can enter into critical section
0-> Some process it is present in the critical section
Down(S)
{
if(s == 1)
{
s = 0; // those process which can enter critical section
}
else
{
// It will only do blocking
// Block the process (We will make it sleep)
}
}
Critical section
Up -> S (Increment the value of integer variable)
-> It will allow other process to enter into critical section
Up (S)
{
if(s == 0)
{
// it will allow other process to enter
s = 1;
// Waking up
Wake();
}
}
Video -> What is difference between Semaphore and Mutex
Other Resource -> OS Semaphore Introduction - javatpoint
Deadlock
Video Resource -> What is deadlock | Necessary Conditions | OS | Lec-63 | Bhanu Pr…
L-4.1: DEADLOCK concept | Example | Necessary condition | Operating System
Theory ->
Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same track and
there is only one track, none of the trains can move once they are in front of each other. A
similar situation occurs in operating systems when there are two or more processes that hold
some resources and wait for resources held by other(s).
Other Resource -> OS Deadlocks Introduction - javatpoint
Context Switching
Video Resource -> Context Switch
Theory ->
Context switching is basically a process of saving the context of one process and loading the
context of another process. It is one of the cost-effective and time-saving measures
executed by CPU because it allows multiple processes to share a single CPU. Therefore, it
is considered an important part of a modern OS. This technique is used by the OS to switch
a process from one state to another i.e., from running state to ready state. It also allows a
single CPU to handle and control various different processes or threads without even the
need for additional resources
Fixed Partitioning
The earliest and one of the simplest techniques which can be used to load more than one
processes into the main memory is Fixed partitioning or Contiguous memory allocation.
In this technique, the main memory is divided into partitions of equal or different sizes. The
operating system always resides in the first partition while the other partitions can be used to
store user processes. The memory is assigned to the processes in a contiguous way.
In fixed partitioning,
1. The partitions cannot overlap.
2. A process must be contiguously present in a partition for the execution.
Fragmentation
Video Resource -> Fragmentation | Internal & External | OS | Lec-17 | Bhanu Priya
Theory ->
1. Internal Fragmentation
If the size of the process is less than the total size of the partition then some size of the
partition gets wasted and remains unused. This is wastage of the memory and called internal
fragmentation.
2. External Fragmentation
The total unused space of various partitions cannot be used to load the processes even
though there is space available but not in the contiguous form.
Dynamic Partitioning
Dynamic partitioning tries to overcome the problems caused by fixed partitioning. In this
technique, the partition size is not declared initially. It is declared at the time of process
loading.
The first partition is reserved for the operating system. The remaining space is divided into
parts. The size of each partition will be equal to the size of the process. The partition size
varies according to the need of the process so that the internal fragmentation can be
avoided.
Other Resource -> Fragmentation in Operating System - javatpoint
Paging
Video Resource -> L-5.9: What is Paging | Memory management | Operating System
Theory ->
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
Other Resource -> OS Paging with Example - javatpoint
Page Replacement Algorithms
Video Resource -> Paging & Page Replacement Algorithms - Operating Systems -
YouTube
Theory ->
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, the page in the
front of the queue is selected for removal.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be used
for the longest duration of time in the future.
3. Least Recently Used: In this algorithm, a page will be replaced which is least recently
used.
Demand Paging : The process of loading the page into memory on demand (whenever page
fault occurs) is known as demand paging.
Thrashing is when the page fault and swapping happens very frequently at a higher rate,
and then the operating system has to spend more time swapping these pages. This state in
the operating system is known as thrashing.
Video Resource -> What is thrashing | OS | Lec-34 | Bhanu Priya
Other Resource -> OS Page Replacement Algorithms - javatpoint
Page Replacement Algorithms in Operating Systems - GeeksforGeeks