Operating System
Operating System
OS
Os is the interface between the user and the computer hardware. ( Jab bhi kisi program ko kisi
hardware ki zarurat hoti hai like for input ke liye ya memory k liye to vo uska OS provide krvata
h....OS basically ek resource manger hai jo computer hardware or system programs k beech me
as an interface kaam krta hai or act krta hai ).
* Types of OS by GFG
Batch OS – In this type of OS, every user prepares his or her job on an offline device like a
punch card and submit it to the computer operator( operator ek admi hai ). A job with a similar
type of needs are batched together and run as a group by an operator to save the time. The
user of a batch operating system never directly interacts with the computer. Examples are :-
Payroll System, Bank Statements, etc.
Multiprogramming OS – The main memory consists of jobs waiting for CPU time. The OS selects
one of the processes and assigns it to the CPU. Whenever the executing process needs to wait
for any other operation (like I/O), the OS selects another process from the job queue and
assigns it to the CPU. This way, the CPU is never kept idle and the user gets the flavor of getting
multiple tasks done at once.
Multi-tasking/ Timesharing OS – In a Multi-tasking System, more than one processes are being
executed at a particular time with the help of the time-sharing concept. So, in the time-sharing
environment, we decide a time that is called time quantum and when the process starts its
execution then the execution continues for only that amount of time and after that, other
processes will be given chance for that amount of time only. In the next cycle, the first process
will again come for its execution and it will be executed for that time quantum only and again
next process will come. This process will continue. Examples of Time-Sharing OSs are: Multics,
Unix, etc.
Real Time OS – Real-Time OS are usually built for dedicated systems to accomplish a specific set
of tasks within deadlines. Real-time systems are used when there are time requirements that
are very strict like missile systems, air traffic control systems, robots, etc. There are two types
of Real-time Operating System:
Hard Real-time: In this type, a small delay can lead to drastic change. So, when the time
constraint is very important then we use the Hard Real-time.
Soft Real-time: Here, the time constraint is not that important but here also we are dealing
with some real-time data.
Network Operating System – These systems run on a server and provide the capability to
manage data, users, groups, security, applications, and other networking functions. These types
of operating systems allow shared access of files, printers, security, applications, and other
networking functions over a small private network. These computers are popularly known as
tightly coupled systems. Examples are: Microsoft Windows Server 2003, Microsoft Windows
Server 2008, UNIX, Linux, Mac OS X.
THREADS
A thread is a lightweight process which has its own program counter, register set, and stack. A
process is made up of multiple threads. ( thread basically process ka ek part hota hai jiska apna
program counter, system register or stack hoti hai. Thread ko hum light weight process bhi
bolte hai kyu ki yeh apne app me ek process hoti hai. Ek process ki multiple threads hoti hai.)
• Threads are designed to assist each other, processes may or may not do it.
* Types of threads :-
User level :- User thread are implemented by users. Implementation of User threads is easy.
User level threads are designed as dependent threads. No hardware support needed . Ex :- Java
thread, POSIX threads.
Kernel level :- kernel threads are implemented by OS. Implementation of Kernel thread is
complicated. Kernel level threads are designed as independent threads. Hardware support is
needed. Ex :- Window Solaris.
PROCESS MANAGEMENT
vo Program jo chl rha hai mtlb execution me hai...that is called process.
* Process in memory :-
* Modes of OS :-
User Mode - The system is in user mode when the operating system is running a user
application such as handling a text editor. The transition from user mode to kernel mode occurs
when the application requests the help of operating system or an interrupt or a system call
occurs. The mode bit is set to 1 in the user mode. It is changed from 1 to 0 when switching from
user mode to kernel mode.
Kernel Mode - The system starts in kernel mode when it boots and after the operating system
is loaded, it executes applications in user mode. There are some privileged instructions that can
only be executed in kernel mode. These are interrupt instructions, input output management
etc. If the privileged instructions are executed in user mode, it is illegal and a trap is
generated.The mode bit is set to 0 in the kernel mode. It is changed from 0 to 1 when switching
from kernel mode to user mode.
( To basically os ke do mode hote hai ya to "Kernel" ya to "user". Kernel mode jo hota vo thoda
khatarnaak hota hai kyu ki usme os direct access kr skta hai memory or hardware ko to agr kuch
bhi fault hua to poora system fail ho skta hai mtlb english me bole bole to whole system can
hault pr user mode me sirf prgram fail hota hai jaise kai br likha aa jata h not responding uss
type se to isme only program will hault. User mode zyada safe hota hai or isliye yehi mode use
hota hai most of the OS me. )
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.
* Schedulers in OS :-
( To pehle aata hai long term scheduler or Job Scheduler - yeh decide krta hai ki new state se
ready state me kin kin process ko leke jana hai.....or yeh sab ko isliye nhi bhej skta ready me kyu
ki yeh degree of multiprogramming bhi control krta hai i.e., number of process present in ready
state at any point of time..
Fir ata hai short term scheduler or CPU scheduler - yeh bhai ka kaam hota hai koi bhi ek
process ko select krna jisko yeh ready se running state me bhejna chahta hai...yeh bs select krta
hai process ko baki bhejna ya load krne ka kaam dispatcher ka hota hai.
Fir ata hai medium term scheduler - yeh bhai ka kaam hota hai kisi bhi process ko suspend krke
fir resume krne ka mtlb agr 2 process hai p1 & p2 jisme se p1 chlri hai pr acha se usko input
output lag gya to yeh bhai usko remove krke p2 ko memory de dega or jab p1 lautega to usko
vhi se kaam chalu kradega to basically yeh swapping krta hau process ki. It also reduces the
degree of multiprogramming.
Swapping is a memory management scheme in which any process can be temporarily swapped
from main memory to secondary memory so that the main memory can be made available for
other processes. It is used to improve main memory utilization. In secondary memory, the place
where the swapped-out process is stored is called swap space.
The concept of swapping has divided into two more concepts: Swap-in and Swap-out.
• Swap-out is a method of removing a process from RAM and adding it to the hard disk.
• Swap-in is a method of removing a program from a hard disk and putting it back into the main
memory or RAM.
Completion time :- mtlb jis time pe process execute ho gya ho mtlb khtm ho gyi ho.
Burst time :- mtlb process cpu me execute hone ke liye kitna time leti hai.
Turn around time :- difference hota completion or arrival time ke beech me.
Waiting time :- btata hai ki process ne kitna time wait kiya hai. Iska formala hota hai turn-
around - burst time = total time taken by process - time taken by process for execution =
waiting time.
Throughput :- yeh terminology no. Of processes ke liye use hoti hai or btati hai total no. Of
process com completed per unit time .
Non Preemptive and preemptive algorithms :- vo algorithms jinme hum process ko cpu se
remove nhi kr skte mtlb vo puri execute hogi hi unhe hum non preemptive algorithm bolte but
vo algorithms jisme process ki kuch priority hoti hai ya vo priority wise execute hoti ki pehle yeh
chli gyi fir koi or fir pehli vali baad me execute hori hai iss type ki algorithms preemptive hoti hai.
• First Come First Serve (FCFS): First come first serve scheduling algorithm states that the
process that requests the CPU first is allocated the CPU first. It is implemented by using the
FIFO queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue.
When the CPU is free, it is allocated to the process at the head of the queue. The running
process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.
• 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. It is a non-preemptive
scheduling algorithm.Shortest Remaining Time First (SRTF): It is preemptive mode of SJF
algorithm in which jobs are schedule according to shortest remaining time.
• Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling
algorithm, we give priority to the process having the longest burst time. This is non-preemptive
in nature i.e., when any process starts executing, can’t be interrupted before complete
execution. Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in
which we give priority to the process having largest burst time remaining.
• Round Robin Scheduling: Each process is assigned a fixed time(Time Quantum/Time Slice) in
cyclic way.It is designed especially for the time-sharing system. The ready queue is treated as a
circular queue. To implement Round Robin scheduling, we keep the ready queue as a FIFO
queue of processes. New processes are added to the tail of the ready queue. The CPU scheduler
picks the first process from the ready queue, sets a timer to interrupt after 1-time quantum,
and dispatches the process. One of two things will then happen. The process may have a CPU
burst of less than 1-time quantum. In this case, the process itself will release the CPU
voluntarily. The scheduler will then proceed to the next process in the ready queue. Otherwise,
if the CPU burst of the currently running process is longer than 1-time quantum, the timer will
go off and will cause an interrupt to the operating system. A context switch will be executed,
and the process will be put at the tail of the ready queue. The CPU scheduler will then select
the next process in the ready queue.
Aging is a technique of gradually increasing the priority of processes that wait in the system for
a long time.For example, if priority range from 127(low) to 0(high), we could increase the
priority of a waiting process by 1 Every 15 minutes. Eventually even a process with an initial
priority of 127 would take no more than 32 hours for priority 127 process to age to a priority-0
process.
• Highest Response Ratio Next (HRRN): In this scheduling, processes with highest response
ratio is scheduled. This algorithm avoids starvation. Response Ratio = (Waiting Time + Burst
time) / Burst time.
1.) Maximum optimization :- mtlb cpu ka full use krna chayie yeh na ho ki process pending padi
hui hai or cpu khali hai aaram kr rha hai .
2.) Fair allocation of Cpu :- mtlb cpu me jitni jagah h utni hi do processes ko .
3.) Max throughput :- mtlb 1 sec me ya 1 ghnte me zyada se zyada process complete hone
chahiye.
4.) Min turn-around time :- mtlb process ko complete hone me km se km time lgna chayie.
On the basis of synchronization, processes are categorized as one of the following two types:
Independent Process : Execution of one process does not affects the execution of other
processes. Here Synchronization is not needed.
Cooperative Process : Execution of a process that can affect or are affected by other processes.
These process need to be synchronised so that order of the execution could be guaranteed
otherwise it can lead to conflicting situations such as race condition.
Race condition :- A race condition occurs when two process access a shared variable at the
same time & try to change it which results in inconsistency of the output. For example in
Producer - consumer problem count++ and count-- trying to change the value of count
simultaneously.
Critical section :- The critical section is a code segment where the shared( common )
resources( data structure, cpu, memory etc.) can be accessed by a process. ( Basically critical
section vo code hota hai jisko execute krke process shared(common) resource ko access krti
hai.) The entry to the critical section is mainly handled by wait() function while the exit from the
critical section is controlled by the signal() function.
Critical section problem :- only one process can execute in its critical section at a time. If
multiple process will access critical section then it will lead to the situation such as race
condition. So, Any solution to the critical section problem must satisfy three requirements:
Mutual Exclusion : By Mutual Exclusion, we mean that if one process is executing inside critical
section then the other process must not enter in the critical section.
Progress : Progress means that if one process doesn't need to execute into critical section then
it should not stop other processes to get into the critical section.
Bounded Waiting : We should be able to predict the waiting time for every process to get into
the critical section. The process must not be endlessly waiting for getting into the critical
section.
1.) Peterson’s Solution is a classical software based solution to the critical section problem. In
Peterson’s solution, we have two shared variables:
- boolean flag[i] :Initialized to FALSE, initially no one is interested in entering the critical section
- int turn : The process whose turn is to enter the critical section.
2.) Test And Set is a hardware solution to the synchronization problem. In TestAndSet, we have
a shared lock variable which can take either of the two values, 0 or 1.
0 Unlock
1 Lock
Before entering into the critical section, a process inquires about the lock. If it is locked, it keeps
on waiting until it becomes free and if it is not locked, it takes the lock and executes the critical
section. In TestAndSet, Mutual exclusion and progress are preserved but bounded waiting
cannot be preserved.
3.) Semaphore is simply a variable that is non-negative and shared between threads. This
variable is used to solve the critical section problem and to achieve process synchronization in
the multiprocessing environment.
Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and 1. Its
value is initialized to 1. It is used to implement the solution of critical section problems with
multiple processes.
Counting Semaphore – Its value can range over an unrestricted domain. It is used to control
access to a resource that has multiple instances.
Producer & Consumer problem and their solution :- The Producer & Consumer problem by
AFTER ACADEMY.
DEADLOCK
Introduction to deadlock in OS by GFG
Symbolic addresses :- The addresses used in a source code. The variable names for ex. are
symbolic addresses.
Relative addresses:- At the time of compilation, a compiler converts symbolic addresses into
relative addresses.
Physical addresses :- The loader generates these addresses at the time when a program is
loaded into main memory.
Logical Address is generated by CPU while a program is running. The logical address is virtual
address as it does not exist physically, therefore, it is also known as Virtual Address. This
address is used as a reference to access the physical memory location by CPU. The term Logical
Address Space is used for the set of all logical addresses generated by a program’s perspective.
Physical Address identifies a physical location of required data in a memory. The user never
directly deals with the physical address but can access by its corresponding logical address. The
user program generates the logical address and thinks that the program is running in this logical
address but the program needs physical memory for its execution, therefore, the logical
address must be mapped to the physical address by MMU before they are used. The term
Physical Address Space is used for all physical addresses corresponding to the logical addresses
in a Logical address space.
Note :- Virtual and physical addresses are the same in compile-time and load-time address-
binding schemes. Virtual and physical addresses differ in execution-time address-binding
scheme.
The hardware device called Memory-Management Unit( MMU ) is used for mapping logical
address to its corresponding physical address during runtime.
• Base Register (Relocation Register)– contains the starting physical address of the process.
• Limit Register -mentions the limit relative to the base address on the region occupied by the
process.
The logical address generated by the CPU is first checked by the limit register, If the value of the
logical address generated is less than the value of the limit register, the base address stored in
the relocation register is added to the logical address to get the physical address of the memory
location.
2. MMU will generate a relocation register (base register) for eg: 14000
If the logical address value is greater than the limit register, then the CPU traps to the OS, and
the OS terminates the program by giving fatal error.
Address binding : is the process of mapping from one address space to another address space.
Logical address is an address generated by the CPU during execution, whereas Physical Address
refers to the location in the memory unit(the one that is loaded into memory).The logical
address undergoes translation by the MMU or address translation unit in particular. The output
of this process is the appropriate physical address or the location of code/data in RAM.
Compile Time – If you know that during compile time, where process will reside in memory,
then an absolute address is generated. i.e The physical address is embedded to the executable
of the program during compilation. Loading the executable as a process in memory is very fast.
But if the generated address space is preoccupied by other processes, then the program
crashes and it becomes necessary to recompile the program to change the address space.
Load time – If it is not known at the compile time where the process will reside, then a
relocatable address will be generated. The loader translates the relocatable address to an
absolute address. The base address of the process in main memory is added to all logical
addresses by the loader to generate an absolute address. In this, if the base address of the
process changes, then we need to reload the process again.
Execution time – The instructions are in memory and are being processed by the CPU.
Additional memory may be allocated and/or deallocated at this time. This is used if a process
can be moved from one memory to another during execution(dynamic linking-Linking that is
done during load or run time). e.g – Compaction.
Static loading :- The complete program is linked and complied without dependency of an
external program. If the static loading is used then accordingly static linking is applied.
Dynamic loading :- All the modules are loaded dynamically. The developer provides a reference
to all of them and the rest of the work is done at execution time. If the dynamic loading is used
then accordingly dynamic linking is applied.
In this technique, memory is allotted in a continuous way to the processes. Contiguous memory
allocation can be categorized into two ways :
In the fixed partition scheme, memory is divided into fixed number of partitions. Fixed means
number of partitions are fixed in the memory. In the fixed partition, in every partition only one
process will be accommodated. Degree of multi-programming is restricted by number of
partitions in the memory( Obviously jab ek partition me ek hi process kaam kr skti to total jitne
partition honge utni hi process kaam kr skti hai )Maximum size of the process is restricted by
maximum size of the partition( haan to jitna bada partition utni hi badi process ki size ).
Internal Fragmentation is found in fixed partition scheme i.e., alloted memory is more than the
requested memory. Internal fragmentation is the difference between allotted and requested
memory.
To overcome the problem of internal fragmentation, instead of fixed partition scheme, variable
partition scheme is used.
In the variable partition scheme, initially memory will be single continuous free block.
Whenever the request by the process arrives, accordingly partition will be made in the memory.
If the smaller processes keep on coming then the larger partitions will be made into smaller
partitions.
External Fragmentation is found in variable partition scheme i.e there’s a sufficient quantity of
area within the memory to satisfy the memory request of a method. however the process’s
memory request cannot be fulfilled because the memory offered is during a non-contiguous
manner. This happen in a such a way in which suppose two allocated process have completed
their execution, now their vaccant space will cause the irregularity or inconstancy in the
memory block which will lead to the external fragmentation.
Compaction : Moving all the processes toward the top or towards the bottom to make free
available memory in a single continuous place is called as compaction. Compaction is harmful to
implement because it interrupts all the running processes in the memory as it moves them.
First Fit: In this type fit, the partition is allocated, which is the first sufficient block from the
beginning of the main memory.
Best Fit: It allocates the process to the partition that is the first smallest partition among the
free partitions.
Worst Fit: It allocates the process to the partition, which is the largest sufficient freely available
partition in the main memory.
Next Fit: It is mostly similar to the first Fit, but this Fit, searches for the first sufficient partition
from the last allocation point.
Paging is a memory management technique in which process address space is broken into
blocks of the same size called pages. Similarly, main memory is divided into small fixed-sized
blocks of (physical) memory called frames and the size of a frame is kept the same as that of a
page to have optimum utilization of the main memory and to avoid external fragmentation.
Address Translation
• 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.
• 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.
A data structure called page map table is used by MMU to keep track of the relation between a
page of a process to a frame in physical memory.
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.
Note :- Paging can also cause page fault. A page fault occurs when a program attempts to
access data or code that is in its address space, but is not currently located in the system RAM.
Ex :- add() function whose code get divided into 2 pages. If we only execute one page, we won't
be able to complete the funct. add().
Segmentation is a memory management technique in which each job is divided into several
segments of different sizes, one for each module that contains pieces that perform related
functions. Each segment is actually a different logical address space of the program.
Segmentation memory management works very similar to paging but here segments are of
variable-length where as in paging pages are of fixed size.
The operating system maintains a segment map table for every process and a list of free
memory blocks along with segment numbers, their size and corresponding memory locations in
main memory. For each segment, the table stores the starting address of the segment and the
length of the segment. A reference to a memory location includes a value that identifies a
segment and an offset.
3. Virtual Memory
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as
though it were part of main memory. 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.
Demand paging
The process of loading the page into memory on demand (whenever page fault occurs) is
known as demand paging.
1. If CPU try to refer a page that is currently not available in the main memory, it generates an
interrupt indicating 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.
6. The signal will be sent to the CPU to continue the program execution and it will place the
process back into ready state.
Page Fault Service Time : The time taken to service the page fault is called as page fault service
time. The page fault service time includes the time taken to perform all the above six steps.