Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Operating System

What is the main purpose of an operating system? Discuss different types?


An operating system (OS) is system software that manages computer hardware,
software resources, and provides common services for computer programs. So
it manages the computer’s memory, processes, devices, files, and security
aspects of the system. It also allows us to communicate with the computer
without knowing how to speak the computer’s language. Without an operating
system, a computer is not useful.
Types of operating system:
• Batch Operating System
• Multi-Programming System
• Multi-Processing System
• Multi-Tasking Operating System
• Time-Sharing Operating System
• Distributed Operating System
• Network Operating System
• Real-Time Operating System
What is a socket, kernel and monolithic kernel ?
A socket is defined as an endpoint for communication, A pair of processes
communicating over a network employ a pair of sockets ,one for each process.
A socket is identified by an IP address concatenated with a port number.
The server waits for incoming client requests by listening to a specified port.
Once a request is received, the server accepts a connection from the client
socket to complete the connection.
Kernel is the central core component of an operating system that manages
operations of computer and hardware. Kernel Establishes communication
between user level application and hardware. Manages memory and CPU time.
Decides state of incoming processes. Controls Disk, Memory, Task Management
Monolithic Kernel (provides good performance but lots of lines of code)
It is one of the types of kernel where all operating system services operate in
kernel space. It has dependencies between system components. It has huge
lines of code which is complex.
Example : Unix, Linux, Open VMS, XTS-400 etc.
Difference between process and program and thread? Different types of
process.
Process is an instance of an executing program. For example, we write our
computer programs in a text file and when we execute this program, it
becomes a process which performs all the tasks mentioned in the program.
Process States in Operating System

Program is a set of instructions to perform a certain task.


Eg: chrome.exe, notepad.exe

Thread:
Thread is a path of execution within a process. A thread is also known as a
lightweight process. The idea is to achieve parallelism by dividing a process into
multiple threads.
For example,Word processor uses multiple threads: one thread to format the
text, another thread to process inputs.
Define virtual memory, thrashing, threads.
Virtual Memory:
A computer can address more memory than the amount physically installed on
the system. This extra memory is actually called virtual memory and it is a
section of a hard disk that’s set up to emulate the computer’s RAM.
The main visible advantage of this scheme is that programs can be larger than
physical memory. Virtual memory serves two purposes. First, it allows us to
extend the use of physical memory by using a disk. Second, it allows us to have
memory protection, because each virtual address is translated to a physical
address.
Thrashing:
Thrashing is a condition or a situation when the system is spending a major
portion of its time in servicing the page faults, but the actual processing done is
very negligible. High degree of multiprogramming(if number of processes
keeps on increasing in the memory), lack of frames (if a process is allocated too
few frames, then there will be too many and too frequent page faults) causes
Thrashing.
Thread:
A thread is a single sequential flow of execution of tasks of a process so it is
also known as thread of execution or thread of control.
What is RAID ? Different types.
RAID, or “Redundant Arrays of Independent Disks” is a technique which makes
use of a combination of multiple disks instead of using a single disk for
increased performance, data redundancy or both.
Data redundancy, although taking up extra space, adds to disk reliability. This
means, in case of disk failure, if the same data is also backed up onto another
disk, we can retrieve the data and go on with the operation.
Key Evaluation Points for a RAID System
• Reliability
• Availability
• Performance
• Capacity
Different RAID Levels – (see in GFG)
• RAID-0 (Stripping)
• RAID-1 (Mirroring)
• RAID-2 (Bit-Level Stripping with Dedicated Parity)
• RAID-3 (Byte-Level Stripping with Dedicated Parity)
• RAID-4 (Block-Level Stripping with Dedicated Parity)
• RAID-5 (Block-Level Stripping with Distributed Parity)
• RAID-6 (Block-Level Stripping with two Parity Bits)

What is a deadlock? Different conditions to achieve a deadlock.


A Deadlock is a situation where each of the computer processes waits for a
resource which is being assigned to some other process. In this situation, none
of the processes gets executed since the resource it needs is held by some
other process which is also waiting for some other resource to be released.
Necessary Conditions for deadlock: (occurs simultaneously)
• Mutual Exclusion
• Hold and Wait
• No preemption
• Circular Wait
What is fragmentation? Types of fragmentation.
An unwanted problem in the operating system in which the processes are
loaded and unloaded from memory, and free memory space is fragmented.
Processes can’t be assigned to memory blocks due to their small size, and the
memory blocks stay unused. It is also necessary to understand that as
programs are loaded and deleted from memory, they generate free space or a
hole in the memory. These small blocks cannot be allotted to new arriving
processes, resulting in inefficient memory use.
The conditions of fragmentation depend on the memory allocation system. As
the process is loaded and unloaded from memory, these areas are fragmented
into small pieces of memory that cannot be allocated to incoming processes. It
is called fragmentation.
Types of fragmentation:
1. Internal
2. External
What is spooling ?
SPOOL is an acronym for simultaneous peripheral operations online. Spooling is
a process in which data is temporarily held to be used and executed by a
device, program, or system.
In spooling, there is no interaction between the I/O devices and the CPU. That
means there is no need for the CPU to wait for the I/O operations to take place.
Such operations take a long time to finish executing, so the CPU will not wait
for them to finish.
The biggest example of Spooling is printing. The documents which are to be
printed are stored in the SPOOL and then added to the queue for printing.
During this time, many processes can perform their operations and use the
CPU without waiting while the printer executes the printing process on the
documents one-by-one.

Mutex vs Semaphore
Mutex and Semaphore both provide synchronization services but they are not the
same. Details about both Mutex and Semaphore are given below −

Mutex
Mutex is a mutual exclusion object that synchronizes access to a resource. It is
created with a unique name at the start of a program. The Mutex is a locking
mechanism that makes sure only one thread can acquire the Mutex at a time and
enter the critical section. This thread only releases the Mutex when it exits the critical
section.
This is shown with the help of the following example −

wait (mutex);
…..
Critical Section
…..
signal (mutex);

A Mutex is different than a semaphore as it is a locking mechanism while a


semaphore is a signalling mechanism. A binary semaphore can be used as a Mutex
but a Mutex can never be used as a semaphore.
Semaphore
A semaphore is a signalling mechanism and a thread that is waiting on a semaphore
can be signaled by another thread. This is different than a mutex as the mutex can
be signaled only by the thread that called the wait function.
A semaphore uses two atomic operations, wait and signal for process
synchronization.
The wait operation decrements the value of its argument S, if it is positive. If S is
negative or zero, then no operation is performed.

wait(S)
{
while (S<=0);
S--;
}

The signal operation increments the value of its argument S.

signal(S)
{
S++;
}

Two types of semaphores:


1- Counting Semaphores are integer value semaphores and have an unrestricted
value domain. These semaphores are used to coordinate the resource access, where
the semaphore count is the number of available resources.
2- Binary semaphores are like counting semaphores but their value is restricted to
0 and 1. The wait operation only works when the semaphore is 1 and the signal
operation succeeds when semaphore is 0.

Belady’s Anomaly
A phenomenon where increasing the number of page frames results in an
increase in the number of page faults for a given memory access pattern.
Solution to fix Belady’s Anomaly:
Implementing alternative page replacement algo helps eliminate Belady’s
Anomaly. Use of stack based algorithms, such as Optimal Page Replacement
Algorithm and Least Recently Used (LRU) algorithm, can eliminate the issue of
increased page faults as these algorithms assign priority to pages.
Starving and Aging in OS
Starving/Starvation(also called Lived lock): Starvation is the problem that
occurs when low priority processes get jammed for an unspecified time as the
high priority processes keep executing. So starvation happens if a method is
indefinitely delayed.
Solution to Starvation : Ageing is a technique of gradually increasing the
priority of processes that wait in the system for a long time.

Why does trashing occur?


High degree of multiprogramming (if number of processes keeps on increasing
in the memory) , lack of frames(if a process is allocated too few frames, then
there will be too many and too frequent page faults.) causes Thrashing.

What is paging and why do we need it?


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.
Paging is used for faster access to data. When a program needs a page, it is
available in the main memory(RAM) as the OS copies a certain number of
pages from your storage device to main memory. Paging allows the physical
address space of a process to be noncontiguous.

Demand Paging, Segmentation


Demand paging is a method of virtual memory management which is based on
the principle that pages should only be brought into memory if the executing
process demands them. This is often referred to as lazy evaluation as only
those pages demanded by the process are swapped from secondary storage to
main memory.
So demand paging works opposite to the principle of loading all pages
immediately.
Segmentation is a memory management technique in which the memory is
divided into the variable size parts. Each part is known as a segment which can
be allocated to a process.
The details about each segment are stored in a table called a segment table.
Segment table is stored in one (or many) of the segments.
Segment table contains mainly two information about segment:
Base: It is the base address of the segment
Limit: It is the length of the segment.

Real Time Operating System, types of RTOS.


A real-time operating system (RTOS) is a special-purpose operating system used
in computers that has strict time constraints for any job to be performed and is
intended to serve real time applications that possess data as it comes in,
typically without buffer delays.
Types of RTOS:
• Hard RTOS
• Firm RTOS
• Soft RTOS
Static binding happens when the code is compiled, while Dynamic binding
happens when the code is executed at run time.
Static Binding:
When a compiler acknowledges all the information required to call a function
or all the values of the variables during compile time, it is called “static
binding”. As all the required information is known before runtime, it increases
the program efficiency and it also enhances the speed of execution of a
program. Static Binding makes a program very efficient, but it declines the
program flexibility, as ‘values of variable’ and ‘function calling’ are predefined
in the program. Static binding is implemented in a program at the time of
coding. Overloading a function or an operator is the example of compile time
polymorphism i.e. static binding.
Dynamic Binding:
Dynamic Binding Calling a function or assigning a value to a variable, at run-
time is called “Dynamic Binding”. Dynamic binding can be associated with run
time ‘polymorphism’ and ‘inheritance’ in OOP. Dynamic binding makes the
execution of a program flexible as it can decide what value should be assigned
to the variable and which function should be called, at the time of program
execution. But as this information is provided at run time it makes the
execution slower as compared to static binding.

Operating System Scheduling algorithms


A Process Scheduler schedules different processes to be assigned to the CPU based
on particular scheduling algorithms. Diff types of scheduling algorithms are -
• First-Come, First-Served (FCFS) Scheduling
• Shortest-Job-Next (SJN) Scheduling
• Priority Scheduling
• Shortest Remaining Time
• Round Robin(RR) Scheduling
• Multiple-Level Queues Scheduling
These algorithms are either non-preemptive or preemptive. Non-preemptive
algorithms are designed so that once a process enters the running state, it cannot be
preempted until it completes its allotted time, whereas the preemptive scheduling is
based on priority where a scheduler may preempt a low priority running process
anytime when a high priority process enters into a ready state.
First Come First Serve (FCFS)
• Jobs are executed on first come, first serve basis.
• It is a non-preemptive, pre-emptive scheduling algorithm.
• Easy to understand and implement.
• Its implementation is based on FIFO queue.
• Poor in performance as average wait time is high.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 0-0=0

P1 5-1=4

P2 8-2=6

P3 16 - 3 = 13

Average Wait Time: (0+4+6+13) / 4 = 5.75

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.
Given: Table of processes, and their Arrival time, Execution time

Process Arrival Time Execution Time Service Time

P0 0 5 0

P1 1 3 5

P2 2 8 14

P3 3 6 8

Waiting time of each process is as follows −

Process Waiting Time

P0 0-0=0

P1 5-1=4

P2 14 - 2 = 12

P3 8-3=5

Average Wait Time: (0 + 4 + 12 + 5)/4 = 21 / 4 = 5.25

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

Shortest Remaining Time


• Shortest remaining time (SRT) is the preemptive version of the SJN
algorithm.
• The processor is allocated to the job closest to completion but it can be
preempted by a newer ready job with shorter time to completion.
• Impossible to implement in interactive systems where required CPU
time is not known.
• It is often used in batch environments where short jobs need to give
preference.

Round Robin Scheduling


• Round Robin is the preemptive process scheduling algorithm.
• Each process is provided a fix time to execute, it is called a quantum.
• Once a process is executed for a given time period, it is preempted and
other process executes for a given time period.
• Context switching is used to save states of preempted processes.

Wait time of each process is as follows −

Process Wait Time : Service Time - Arrival Time

P0 (0 - 0) + (12 - 3) = 9

P1 (3 - 1) = 2

P2 (6 - 2) + (14 - 9) + (20 - 17) = 12

P3 (9 - 3) + (17 - 12) = 11

Average Wait Time: (9+2+12+11) / 4 = 8.5

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.

Producer Consumer Problem

About Producer-Consumer problem: The Producer-Consumer problem is a


classic problem that is used for multi-process synchronisation i.e.
synchronisation between more than one processes.

The job of the Producer is to generate the data, put it into the buffer, and
again start generating data. While the job of the Consumer is to consume the
data from the buffer.

What’s the problem here?


The following are the problems that might occur in the Producer-Consumer:

The producer should produce data only when the buffer is not full. If the buffer
is full, then the producer shouldn’t be allowed to put any data into the buffer.
The consumer should consume data only when the buffer is not empty. If the
buffer is empty, then the consumer shouldn’t be allowed to take any data from
the buffer.
The producer and consumer should not access the buffer at the same time.

We can solve this problem by using semaphores.


Banker’s Algorithm
It is a banker algorithm used to avoid deadlock and allocate resources safely to
each process in the computer system. The ‘S-State’ examines all possible tests
or activities before deciding whether the allocation should be allowed to each
process. It also helps the operating system to successfully share the resources
between all the processes. The banker’s algorithm is named because it checks
whether a person should be sanctioned a loan amount or not to help the bank
system safely simulate allocation resources.

Explain Cache
Cache memory is an extremely fast memory type that acts as a buffer between
RAM and the CPU. It holds frequently requested data and instructions so that
they are immediately available to the CPU when needed.
Cache: The small section of SRAM memory, added between main memory and
processor(CPU) to speed up the process of execution, is known as cache
memory. It includes a small amount of SRAM & more amount of DRAM. It is a
high speed & expensive memory.
Cache hit ratio: It measures how effectively the cache fulfills the request for
getting content.
Cache hit ratio = No of cache hits/ (No of cache hits + No. of cache Miss)
If data has been found in the cache, it is a cache hit else a cache miss.

Cache Mapping:
The process /technique of bringing data of main memory blocks into the cache
block is known as cache mapping.
The mapping techniques can be classified as:
• Direct Mapping
• Associative
• Set-Associative
Difference between Direct-mapping, Associative Mapping & Set-
Associative Mapping:

Set-Associative
Direct-mapping Associative Mapping
Mapping

Needs comparison with


all tag bits, i.e., the
Needs comparisons
Needs only one comparison cache control logic must
equal to number of
because of using direct examine every block’s
blocks per set as the set
formula to get the effective tag for a match at the
can contain more than 1
cache address. same time in order to
blocks.
determine that a block is
in the cache/not.

Main Memory Address is


divided into 3 fields :
TAG, BLOCK & WORD.
The BLOCK & WORD
together make an index. The Main Memory Address Main Memory Address
least significant WORD bits is divided into 1 fields : is divided into 3 fields :
identify a unique word within TAG & WORD. TAG, SET & WORD.
a block of main memory, the
BLOCK bits specify one of
the blocks and the Tag bits are
the most significant bits.

There is one possible location The mapping of the main


The mapping of the
in the cache organization for memory block can be
main memory block can
each block from main memory done with a particular
be done with any of the
because we have a fixed cache block of any
cache block.
formula. direct-mapped cache.

If the processor need to


If the processor need to access In case of frequently
access same memory
same memory location from 2 accessing two different
location from 2 different
different main memory pages pages of the main
main memory pages
frequently, cache hit ratio memory if reduced, the
frequently, cache hit
decreases. cache hit ratio reduces.
ratio has no effect.

Search time is more as


Search time is less here Search time increases
the cache control logic
because there is one possible with number of blocks
examines every block’s
location in the cache per set.
tag for a match.
Set-Associative
Direct-mapping Associative Mapping
Mapping

organization for each block


from main memory.

The index is given by


The index is given by the The index is zero for
the number of sets in
number of blocks in cache. associative mapping.
cache.

It has less tags bits than


It has the greatest associative mapping and
It has least number of tag bits.
number of tag bits. more tag bits than direct
mapping.

Advantages-
• Simplest type of
mapping Advantages-
• Fast as only tag • It gives better
field matching is Advantages- performance
required while • It is fast. than the direct
searching for a • Easy to and
word. implement associative
• It is comparatively mapping
less expensive than techniques.
associative
mapping.

Disadvantages-
Disadvantages- Disadvantages-
• It is most
• It gives low • Expensive
expensive as
performance because it
with the
because of the needs to store
increase in set
replacement for address along
size cost also
data-tag value. with the data.
increases.

Multitasking v/s Multiprocessing


1. Multi-tasking :
Multi-tasking is the logical extension of
multiprogramming. In this system, the CPU
executes multiple jobs by switching among
them typically using a small time quantum,
and these switches occur so frequently that
the users can interact with each program while it is running. Multitasking is
further classified into two categories: Single User & Multiuser.

2. Multiprocessing :
Multiprocessing is a system that
has two or more than two
processors. In this, CPUs are added
for increasing computing speed of
the system. Because of
Multiprocessing, there are many
processes that are executed
simultaneously. Multiprocessing is
further classified into two
categories: Symmetric Multiprocessing and Asymmetric Multiprocessing.

Difference between Multitasking and Multiprocessing :

S
No. Multi-tasking Multiprocessing
The availability of more than one processor
The execution of more than per system, that can execute several set of
one task simultaneously is instructions in parallel is known as
1. known as multitasking. multiprocessing.
2. The number of CPU is one. The number of CPUs is more than one.
It takes moderate amount of
3. time. It takes less time for job processing.
In this, one by one job is In this, more than one process can be
4. being executed at a time. executed at a time.
5. It is economical. It is less economical.
The number of users is more The number of users is can be one or more
6. than one. than one.
7. Throughput is moderate. Throughput is maximum.
8. Its efficiency is moderate. Its efficiency is maximum.
It is of two types: Single user
multitasking and Multiple It is of two types: Symmetric Multiprocessing
9. user multitasking. and Asymmetric Multiprocessing.

You might also like