0% found this document useful (0 votes)
5 views

Os Notes

Uploaded by

tanyatomar766
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)
5 views

Os Notes

Uploaded by

tanyatomar766
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/ 41

lOMoARcPSD|43339171

OS Notes

Operating Systems (Guru Gobind Singh Indraprastha University)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by tanya tomar ([email protected])
lOMoARcPSD|43339171

UNIT-1

# INTRODUCTION

What is Operating system:


An Operating System (OS) is a software that acts as an interface between computer hardware components and the
user. Every computer system must have at least one operating system to run other programs. Applications like Browsers,
MS Office, Notepad Games, etc., need some environment to run and perform its tasks.

The OS helps you to communicate with the computer without knowing how to speak the computer’s language. It is not
possible for the user to use any computer or mobile device without having an operating system.

Types of OS:

• Batch
Some computer processes are very lengthy and time-consuming. To speed the same process, a job with a similar type of
needs are batched together and run as a group.

The user of a batch operating system never directly interacts with the computer. 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.

Advantages:
• Execution time taken for similar jobs is higher.
• Multiple users can share batch systems.
• Managing large works becomes easy in batch systems.
• The idle time for a single batch is very less.

Disadvantages:
• It is hard to debug batch systems.
• If a job fails, then the other jobs have to wait for an unknown time till the issue is resolved.
• Batch systems are sometimes costly.

Examples: payroll system, bank statements, data entry, etc.

• Multiprogram
A multiprogramming operating system may run many programs on a single processor computer. In a multiprogramming
OS, if one program must wait for an input/output transfer, the other programmes are ready to use the CPU. As a result,
different jobs may have to split CPU time. However, their jobs are not scheduled to be completed at the same time.

Advantages:
• CPU utilization is high because the CPU is never goes to idle state.
• Memory utilization is efficient.
• CPU throughput is high and also supports multiple interactive user terminals.

Disadvantages:
• CPU scheduling is compulsory because lots of jobs are ready to run on CPU simultaneously.
• User is not able to interact with jobs when it is executing.
• Programmers also cannot modify a program that is being executed.

Examples: User can use MS-Excel, download apps, transfer data from one point to another point, Firefox or Google
Chrome browser, and more at a same time

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• Time Sharing

Time-sharing operating system enables people located at a different terminal(shell) to use a single computer system at
the same time. The processor time (CPU) which is shared among multiple users is termed as time sharing.

This system provides access to a large number of users, and each user gets the time of CPU as they get in a single
system. The tasks performed are given by a single user or by different users. The time allotted to execute one task is
called a quantum, and as soon as the time to execute one task is completed, the system switches over to another task.

Advantages:

• Each task gets equal time for execution.


• The idle time for the CPU will be the lowest.
• There are very few chances for the duplication of the software.

Disadvantages:

• Processes with higher priority cannot be executed first as equal priority is given to each process or task.
• Various user data is needed to be taken care of from unauthorized access.
• Sometimes there is a data communication problem.

Examples: UNIX, etc.

• Personal

it is a single user operating that is in which one user works on one interface. in these systems no other operating system
is interrupting with the processing. single user operating systems work on processing that requires less resources.

Advantages:

• support one user at a time.


• easy to maintain
• less chance to damage
• concentrate on one task
Disadvantages:

• tasks take longer time to complete


• idle time ID is higher

Examples of Personal OS: desktop computers, laptops, tablets and smartphones, etc.

• Parallel

Parallel operating systems are designed to speed up the execution of programs by dividing the programs into multiple
fragments and processing these fragments at the same time.

Advantages:
• Cost
• Scalability
• Diversity

Disadvantages:
• power consumption
• Configuration

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• Administration

Examples: VMware, Microsoft Hyper-V, Red hat enterprise, Oracle VM, KVM/QEMU, and Sun xVM Server

• Distributed

In a distributed OS, various computers are connected through a single communication channel. These independent
computers have their memory unit and CPU and are known as loosely coupled systems. The system processes can be of
different sizes and can perform different functions. The major benefit of such a type of operating system is that a user can
access files that are not present on his system but in another connected system. In addition, remote access is available to
the systems connected to this network.

Advantages:

• Failure of one system will not affect the other systems because all the computers are independent of each other.
• The load on the host system is reduced.
• The size of the network is easily scalable as many computers can be added to the network.
• As the workload and resources are shared therefore the calculations are performed at a higher speed.
• Data exchange speed is increased with the help of electronic mail.

Disadvantages:

• The setup cost is high.


• Software used for such systems is highly complex.
• Failure of the main network will lead to the failure of the whole system.

Examples of Distributed OS: LOCUS, etc.

• Real Time

A real time operating system time interval to process and respond to inputs is very small. The time interval is called
response time. real-time systems are used when there are time requirements that are very strict like missile system.
Examples: Military Software Systems, Space Software Systems are the Real time OS example.

Types of RTOS:
• Hard real-time OS

The hard real-time OS is the operating system for mainly the applications in which the slightest delay is also
unacceptable. The time constraints of such applications are very strict. Such systems are built for life-saving equipment
like parachutes and airbags, which immediately need to be in action if an accident happens.

• Soft real-time OS

The soft real-time OS is the operating system for applications where time constraint is not very strict.
In a soft real-time system, an important task is prioritized over less important tasks, and this priority remains active until
the completion of the task. Furthermore, a time limit is always set for a specific job, enabling short time delays for future
tasks, which is acceptable. For Example, virtual reality, reservation systems, etc.

Advantages of Real-Time OS:

• It provides more output from all the resources as there is maximum utilization of systems.
• It provides the best management of memory allocation.
• These systems are always error-free.
• These operating systems focus more on running applications than those in the queue.
• Shifting from one task to another takes very little time.

Disadvantages of Real-Time OS:

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• System resources are extremely expensive and are not so good.


• The algorithms used are very complex.
• Only limited tasks can run at a single time.
• In such systems, we cannot set thread priority as these systems cannot switch tasks easily.

Examples: Medical imaging systems, robots, etc.

# MEMORY MANAGEMENT

What is Memory Management:


Memory management is the process of controlling and coordinating a computer's main memory. It ensures that
blocks of memory space are properly managed and allocated so the operating system (OS), applications and other
running processes have the memory they need to carry out their operations.

It is the most important function of an operating system that manages primary memory. It helps processes to move back
and forward between the main memory and execution disk. It helps OS to keep track of every memory location,
irrespective of whether it is allocated to some process or it remains free.

Logical Address vs physical Address:


Parameter LOGICAL ADDRESS PHYSICAL ADDRESS
Basic generated by CPU location in a memory unit
Address Logical Address Space is set of all logical Physical Address is set of all physical addresses
Space addresses generated by CPU in reference to mapped to the corresponding logical addresses.
a program.
Visibility User can view the logical address of a User can never view physical address of program.
program.
Generation generated by the CPU Computed by MMU
Access The user can use the logical address to The user can indirectly access physical address but
access the physical address. not directly.
Editable Logical address can be change. Physical address will not change.
Also called virtual address. real address.

Techniques of Memory Management:

• Contiguous Allocation
Contiguous memory allocation refers to a memory management technique in which whenever there occurs a request by a
user process for the memory, one of the sections of the contiguous memory block would be given to that process, in
accordance with its requirement.

Pros of Contiguous Memory Allocation


• It supports a user’s random access to files.
• The user gets excellent read performance.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• It is fairly simple to implement.

Cons of Contiguous Memory Allocation


• Having a file grow might be somewhat difficult.
• The disk may become fragmented.

• Swapping
Swapping is a method in which the process should be swapped temporarily from the main memory to the backing store. It
will be later brought back into the memory for continue execution.

Backing store is a hard disk or some other secondary storage device that should be big enough in order to accommodate
copies of all memory images for all users. It is also capable of offering direct access to these memory images. Though
performance is usually affected by swapping process but it helps in running multiple and big processes in parallel and that's
the reason Swapping is also known as a technique for memory compaction.

Benefits of Swapping
Here, are major benefits/pros of swapping:

• It offers a higher degree of multiprogramming.


• Allows dynamic relocation. For example, if address binding at execution time is being used, then processes can be
swap in different locations. Else in case of compile and load time bindings, processes should be moved to the same
location.
• It helps to get better utilization of memory.
• Minimum wastage of CPU time on completion so it can easily be applied to a priority-based scheduling method to
improve its performance.

What is fragmentation

• Paging
Paging is a storage mechanism that allows OS to retrieve processes from the secondary storage into the main memory
in the form of pages. In the Paging method, the main memory is divided into small fixed-size blocks of physical memory,
which is called frames. The size of a frame should be kept the same as that of a page to have maximum utilization of the
main memory and to avoid external fragmentation. Paging is used for faster access to data, and it is a logical concept.

Pages = secondary memory / logical memory


Frames = main memory / physical memory

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Advantages and Disadvantages of Paging


• Paging reduces external fragmentation, but still suffer from internal fragmentation.
• Paging is simple to implement and assumed as an efficient memory management technique.
• Due to equal size of the pages and frames, swapping becomes very easy.
• Page table requires extra memory space, so may not be good for a system having small RAM.

• Segmentation
Segmentation method works almost similarly to paging. The only difference between the two is that segments are of
variable-length, whereas, in the paging method, pages are always of fixed size.
A program segment includes the program’s main function, data structures, utility functions, etc. The OS maintains a
segment map table for all the processes. It also includes a list of free memory blocks along with its size, segment
numbers, and its memory locations in the main memory or virtual memory.

Difference between Paging and Segmentation

# VIRTUAL MEMORY

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

What is Virtual Memory:


Virtual Memory is a storage mechanism which offers user an illusion of having a very big main memory. It is done by
treating a part of secondary memory as the main memory. In Virtual memory, the user can store processes with a bigger
size than the available main memory.

Therefore, instead of loading one long process in the main memory, the OS loads the various parts of more than one
process in the main memory. Virtual memory is mostly implemented with demand paging and demand segmentation.

Why Need Virtual Memory?


• Whenever your computer doesn’t have space in the physical memory it writes what it needs to remember to the
hard disk in a swap file as virtual memory.
• If a computer running Windows needs more memory/RAM, then installed in the system, it uses a small portion of
the hard drive for this purpose.

How Virtual Memory Works?


In the modern world, virtual memory has become quite common these days. It is used whenever some pages require to
be loaded in the main memory for the execution, and the memory is not available for those many pages.

So, in that case, instead of preventing pages from entering in the main memory, the OS searches for the RAM space that
are minimum used in the recent times or that are not referenced into the secondary memory to make the space for the
new pages in the main memory.

For example:
Let’s assume that an OS requires 300 MB of memory to store all the running programs. However, there’s
currently only 50 MB of available physical memory stored on the RAM.

o The OS will then set up 250 MB of virtual memory and use a program called the Virtual Memory
Manager(VMM) to manage that 250 MB.
o So, in this case, the VMM will create a file on the hard disk that is 250 MB in size to store extra memory
that is required.
o The OS will now proceed to address memory as it considers 300 MB of real memory stored in the RAM,
even if only 50 MB space is available.
o It is the job of the VMM to manage 300 MB memory even if just 50 MB of real memory space is available.

Virtual Memory is Implemented using following:

1. Demand Paging
A demand paging mechanism is very much similar to a paging system with swapping where processes stored in the
secondary memory and pages are loaded only on demand, not in advance.

So, when a context switch occurs, the OS never copy any of the old program’s pages from the disk or any of the new
program’s pages into the main memory. Instead, it will start executing the new program after loading the first page and
fetches the program’s pages, which are referenced.

Performance of Demand Paging


Evaluating of paging performance is one of the important tasks.
For most computer systems, the mеmοry-accеss time, dеnοtеd ‘ma’ ranges from 10 to 200 nanoseconds. As long as we
have no page faults, the effective access time is equal to the memory access time. If hοwеvеr a pagе fault occurs, we must
first read the relevant pagе from disk and then access the desired word.
Let ‘p’ be the probability of a pagе fault. Wе would еxpеct p tο be close tο zero-that is, wе would еxpеct tο have only a few
pagе faults. The effective access time is then

Effective Access Time = (1 – p) * ma + p * pagе fault time.

To compute the effective access time, we must know how much time is nееdеd to service a page fault.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

2. Page Replacement
During the program execution, if the program references a page that may not be available in the main memory because it
was swapped, then the processor considers it as an invalid memory reference. That’s because the page fault and
transfers send control back from the program to the OS, which demands to store page back into the memory.

**Page Fault= It is a condition in which a running process refers to a page that is not loaded in the main memory.

Page replacement Algorithms:

A. FIFO
FIFO (First-in-first-out) is a simple implementation method. In this method, memory selects the page for a replacement
that has been in the virtual address of the memory for the longest time.

Features:
• Whenever a new page loaded, the page recently comes in the memory is removed. So, it is easy to decide which
page requires to be removed as its identification number is always at the FIFO stack.
• The oldest page in the main memory is one that should be selected for replacement first.

B. LIFO
• As the name suggests, this algorithm works on the principle of “Last in First out“.
• It replaces the newest page that arrived at last in the main memory.
• It is implemented by keeping track of all the pages in a stack.

C. LRU (Least Recently Used)

This method helps OS to find page usage over a short period of time. This algorithm should be implemented by
associating a counter with an even- page. This algorithm works on the basis of the principle of locality of a reference
which states that a program has a tendency to access the same set of memory locations repetitively over a short period of
time. So, pages that have been used heavily in the past are most likely to be used heavily in the future also.
In this algorithm, when a page fault occurs, then the page that has not been used for the longest duration of time is
replaced by the newly requested page.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

How does it work?


• Page, which has not been used for the longest time in the main memory, is the one that will be selected for
replacement.
• Easy to implement, keep a list, replace pages by looking back into time.

Features:
• The LRU replacement method has the highest count. This counter is also called aging registers, which specify
their age and how much their associated pages should also be referenced.
• The page which hasn’t been used for the longest time in the main memory is the one that should be selected for
replacement.
• It also keeps a list and replaces pages by looking back into time.

**Fault rate is a frequency with which a designed system or component fails. It is expressed in failures per unit of time. It
is denoted by the Greek letter (lambda).\

D. OPR (Optimal Page Replacement)

Optimal page replacement is the best page replacement algorithm as this algorithm results in the least number of page
faults. In this algorithm, the pages are replaced with the ones that will not be used for the longest duration of time in the
future. In simple terms, the pages that will be referred farthest in the future are replaced in this algorithm.

3. Allocation of Frames
The main memory of the system is divided into frames.
The OS has to allocate a sufficient number of frames for each process and to do so, the OS uses various algorithms.

The five major ways to allocate frames are as follows:

• Proportional frame allocation


The proportional frame allocation algorithm allocates frames based on the size that is necessary for the execution and the
number of total frames the memory has.
The only disadvantage of this algorithm is it does not allocate frames based on priority. This situation is solved by Priority
frame allocation.

• Priority frame allocation


Priority frame allocation allocates frames based on the priority of the processes and the number of frame allocations.
If a process is of high priority and needs more frames then the process will be allocated that many frames. The allocation
of lower priority processes occurs after it.

• Global replacement allocation


When there is a page fault in the operating system, then the global replacement allocation takes care of it.
The process with lower priority can give frames to the process with higher priority to avoid page faults.

• Local replacement allocation


In local replacement allocation, the frames of pages can be stored on the same page.
It doesn’t influence the behavior of the process as it did in global replacement allocation.

• Equal frame allocation


In equal frame allocation, the processes are allocated equally among the processes in the operating system.
The only disadvantage in equal frame allocation is that a process requires more frames for allocation for execution and
there are only a set number of frames.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

4. Thrashing

**Thrash is a term used to describe the poor performance of a virtual memory system when the same pages are loaded
repeatedly owing to a shortage of main memory to store them in secondary memory. Thrashing happens in when a
computer's virtual memory resources are overutilized, resulting in a persistent state of paging and page faults, which
inhibits most application-level activity. It causes the computer's performance to decline or collapse. The scenario can last
indefinitely unless the user stops certain running apps or active processes to free up extra virtual memory resources.

If page faults and swapping often occur at a high rate, the operating system must spend more time switching these pages.
This is referred to as thrashing in the operating system. The CPU utilization will be lowered as a result of Thrashing in
operating system.

• Thrashing in operating system is a phenomenon in computing that occurs when virtual memory is employed. It
occurs when a computer's virtual memory rapidly exchanges data for data on the hard drive, to the exclusion of
most application-level operations. As main memory is depleted, more pages must be swapped into and out of
virtual memory.
• Swapping results in a high rate of hard drive access. Thrashing can last for a long time if the underlying problem
is not addressed. Thrashing in operating system has the ability to cause the computer's hard disk to fail
completely.

Thus, thrashing in operating system is sometimes referred to as disk thrashing.

Causes of Thrashing:
• High level of multiprogramming especially when the CPU’s usage is low.
• lack of frames to carry out processes.
Size of locality > total memory size
Preventions:
• Working set model
• Page fault frequency

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

UNIT-2

# PROCESSES

Process Concept:
• Process is a program in execution.
• A process defines the fundamental unit of computation for the computer.
• A process can run to completion only when all requested resources have been allocated to the process.
• Two or more processes could be executing the same program each using their own data and resources.

Multiple parts of a process:


• The program code, also called text section.
• Current activity including program counter.
• Stack containing temporary data (function parameters, return addresses, local variables).
• Data section containing global variables.
• Heap containing memory dynamically allocated during runtime.

What is a program:
• Program is a static entity made-up of program statement.
• Program contains the instructions.
• Program exists at a single place in space and continues to exist.

Process State:
• New: when a new process is being created.
• Running: A process is said to be in running state when instructions are being executed.
• Waiting: The process is waiting for some event to occur (such as an I/O operation).
• Ready: The process is waiting for processor.
• Terminated: The process has finished execution.

Operation on Process:
1. Process creation- system initialization, execution of a process, initiation of a batch job.
2. Process termination- process is going to be terminated by a call to kill in Unix or terminate process in windows.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Process Schedulers:
When there are two or more runnable processes then it is decided by the Operating system which one to run first then it is
referred to as Process Scheduling.

A scheduler is used to make decisions by using some scheduling algorithm.

• Long- Long term scheduler is also known as job scheduler. It chooses the processes from the secondary memory
and keeps them in the ready queue maintained in the primary memory.
• Short- Short term scheduler is also known as CPU scheduler. It selects one of the Jobs from the ready queue and
dispatch to the CPU for the execution.
• Medium- Medium term scheduler takes care of the swapped-out processes. If the running state processes needs
some IO time for the completion, then there is a need to change its state from running to waiting.

# CPU SCHEDULING

Basic Concepts:
CPU scheduling is a process that allows one process to use the CPU while the execution of another process is on hold(in
waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. The aim of CPU
scheduling is to make the system efficient, fast, and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be
executed. The selection process is carried out by the short-term scheduler (or CPU scheduler). The scheduler selects
from among the processes in memory that are ready to execute and allocates the CPU to one of them.

Types of scheduling:

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Scheduling Criteria:
• CPU utilization - Ideally the CPU would be busy 100% of the time, so as to waste 0 CPU cycles. On a real system
CPU usage should range from 40% (lightly loaded) to 90% (heavily loaded.)
• Throughput - Number of processes completed per unit time. May range from 10 / second to 1 / hour depending
on the specific processes.
• Turnaround time - Time required for a particular process to complete, from submission time to completion.
• Waiting time - How much time processes spend in the ready queue waiting their turn to get on the CPU.
• Response time - The time taken in an interactive program from the issuance of a command to the
commencement of a response to that command.
In general, one wants to optimize the average value of a criteria (Maximize CPU utilization and throughput, and
minimize all the others. However sometimes one wants to do something different, such as to minimize the maximum
response time.

Scheduling Algorithms:
explanation

1. First Come First Serve:


explanation

FCFS considered to be the simplest of all operating system scheduling algorithms. First come first serve scheduling
algorithm states that the process that requests the CPU first is allocated the CPU first and is implemented by using FIFO
queue.

Characteristics:
• FCFS supports non-preemptive and preemptive CPU scheduling algorithms.
• Tasks are always executed on a First-come, First-serve concept.
• FCFS is easy to implement and use.
• This algorithm is not much efficient in performance, and the wait time is quite high.

Advantages:
• Easy to implement
• First come, first serve method

Disadvantages:
• FCFS suffers from Convoy effect.
• The average waiting time is much higher than the other algorithms.
• FCFS is very simple and easy to implement and hence not much efficient.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

2. Shortest Job First (SJF):


expalantion
Shortest job first (SJF) is a scheduling process that selects the waiting process with the smallest execution time to
execute next. This scheduling method may or may not be preemptive. Significantly reduces the average waiting time for
other processes waiting to be executed. The full form of SJF is Shortest Job First.

Characteristics:
• Shortest Job first has the advantage of having a minimum average waiting time among all operating system
scheduling algorithms.
• It is associated with each task as a unit of time to complete.
• It may cause starvation if shorter processes keep coming. This problem can be solved using the concept of
ageing.

Advantages:
• As SJF reduces the average waiting time thus, it is better than the first come first serve scheduling algorithm.
• SJF is generally used for long term scheduling

Disadvantages:
• One of the demerit SJF has is starvation.
• Many times it becomes complicated to predict the length of the upcoming CPU request

3. Round robin:
explanation

Round Robin is a CPU scheduling algorithm where each process is cyclically assigned a fixed time slot. It is the
preemptive version of First come First Serve CPU Scheduling algorithm. Round Robin CPU Algorithm generally focuses
on Time Sharing technique.

Characteristics:
• It’s simple, easy to use, and starvation-free as all processes get the balanced CPU allocation.
• One of the most widely used methods in CPU scheduling as a core.
• It is considered preemptive as the processes are given to the CPU for a very limited time.

Advantages:
• Round robin seems to be fair as every process gets an equal share of CPU.
• The newly created process is added to the end of the ready queue.

4. Shortest Remaining Time First:


Shortest remaining time first is the preemptive version of the shortest job first which we have discussed earlier where the
processor is allocated to the job closest to completion. In SRTF the process with the smallest amount of time remaining
until completion is selected to execute.

Characteristics:
• SRTF algorithm makes the processing of the jobs faster than SJF algorithm, given it’s overhead charges are not
counted.
• The context switch is done a lot more times in SRTF than in SJF and consumes the CPU’s valuable time for
processing. This adds up to its processing time and diminishes its advantage of fast processing.

Advantages:
• In SRTF the short processes are handled very fast.
• The system also requires very little overhead since it only makes a decision when a process completes or a new
process is added.

Disadvantages:
• Like the shortest job first, it also has the potential for process starvation.
• Long processes may be held off indefinitely if short processes are continually added.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

5. Priority Scheduling:
explanation

Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling algorithm that works based
on the priority of a process. In this algorithm, the editor sets the functions to be as important, meaning that the most
important process must be done first. In the case of any conflict, that is, where there are more than one processor with
equal value, then the most important CPU planning algorithm works on the basis of the FCFS (First Come First Serve)
algorithm.

Characteristics:
• Schedules tasks based on priority.
• When the higher priority work arrives while a task with less priority is executed, the higher priority work takes the
place of the less priority one and
• The latter is suspended until the execution is complete.
• Lower is the number assigned, higher is the priority level of a process.

Advantages:
• The average waiting time is less than FCFS
• Less complex

Disadvantage:
• One of the most common demerits of the Preemptive priority CPU scheduling algorithm is the Starvation Problem.
This is the problem in which a process has to wait for a longer amount of time to get scheduled into the CPU. This
condition is called the starvation problem.

6. Highest Response Ratio Next:


Highest Response Ratio Next is a non-preemptive CPU Scheduling algorithm and it is considered as one of the most
optimal scheduling algorithms. The name itself states that we need to find the response ratio of all available processes
and select the one with the highest Response Ratio. A process once selected will run till completion.

Characteristics:
• The criteria for HRRN is Response Ratio, and the mode is Non-Preemptive.
• HRRN is considered as the modification of Shortest Job First to reduce the problem of starvation.
• In comparison with SJF, during the HRRN scheduling algorithm, the CPU is allotted to the next process which has
the highest response ratio and not to the process having less burst time.

Response Ratio = (W + S)/S


Here, W is the waiting time of the process so far and S is the Burst time of the process.

Advantages:
• HRRN Scheduling algorithm generally gives better performance than the shortest job first Scheduling.
• There is a reduction in waiting time for longer jobs and also it encourages shorter jobs.

Disadvantages:
• The implementation of HRRN scheduling is not possible as it is not possible to know the burst time of every job in
advance.
• In this scheduling, there may occur an overload on the CPU.

Multiple Process Scheduling:


Multiple processor scheduling or multiprocessor scheduling focuses on designing the system's scheduling function,
which consists of more than one processor. Multiple CPUs share the load (load sharing) in multiprocessor scheduling so
that various processes run simultaneously. In general, multiprocessor scheduling is complex as compared to single
processor scheduling.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Approaches:
• Symmetric Multiprocessing: It is used where each processor is self-scheduling. All processes may be in a
common ready queue, or each processor may have its private queue for ready processes. The scheduling
proceeds further by having the scheduler for each processor examine the ready queue and select a process to
execute.
• Asymmetric Multiprocessing: It is used when all the scheduling decisions and I/O processing are handled by a
single processor called the Master Server. The other processors execute only the user code. This is simple and
reduces the need for data sharing, and this entire scenario is called Asymmetric Multiprocessing.

Process Affinity:
A process has an affinity for a processor on which it runs. This is called processor affinity.
• Soft Affinity: When an operating system has a policy of keeping a process running on the same processor but
not guaranteeing it will do so, this situation is called soft affinity.
• Hard Affinity: Hard Affinity allows a process to specify a subset of processors on which it may run. Some Linux
systems implement soft affinity and provide system calls like sched_setaffinity() that also support hard affinity.

Load Balancing:
In a multi-processor system, all processors may not have the same workload. Some may have a long ready queue, while
others may be sitting idle. To solve this problem, load balancing comes into the picture. Load Balancing is the
phenomenon of distributing workload so that the processors have an even workload in a symmetric multi-processor
system.
• Push Migration: In push migration, a task routinely checks the load on each processor. Some processors may
have long queues while some are idle. If the workload is unevenly distributed, it will extract the load from the
overloaded processor and assign the load to an idle or a less busy processor.
• Pull Migration: In pull migration, an idle processor will extract the load from an overloaded processor itself.

Symmetric Multiprocessor:
The memory has only one operating system, which can be run by any central processing unit. When a system call is
made, the CPU on which the system call was made traps the kernel and processed that system call.
• Locking system: The resources in a multi-processor are shared among the processors. To make the access
safe of these resources to the processors, a locking system is required. This is done to serialize the access of the
resources by the processors.
• Shared data: Since multiple processors are accessing the same data at any given time, the data may not be
consistent across all of these processors. To avoid this, we must use some kind of strategy or locking scheme.
• Cache Coherence: When the resource data is stored in multiple local caches and shared by many clients, it may
be rendered invalid if one of the clients changes the memory block. This can be resolved by maintaining a
consistent view of the data.

# PROCESS SCHEDULING

Process Synchronization:
In-detail

Processes Synchronization or Synchronization is the way by which processes that share the same memory space are
managed in an operating system. It helps maintain the consistency of data by using variables or hardware so that only
one process can make changes to the shared memory at a time. There are various solutions for the same such as
semaphores, mutex locks, synchronization hardware, etc.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

In the above image, if Process1 and Process2 happen at the same time, user 2 will get the wrong account balance as Y
because of Process1 being transacted when the balance is X.

Inconsistency of data can occur when various processes share a common resource in a system which is why there is a
need for process synchronization in the operating system.

How Process Synchronization in OS Works?


For example, if a process1 is trying to read the data present in a memory location while another process2 is trying to
change the data present at the same location, there is a high chance that the data read by the process1 will be incorrect.

Critical Section Problem:


Why do we need to have a critical section? What problems occur if we remove it?

A part of code that can only be accessed by a single process at any moment is known as a critical section. This means
that when a lot of programs want to access and change a single shared data, only one process will be allowed to change
at any given moment. The other processes have to wait until the data is free to be used.
The wait() function mainly handles the entry to the critical section, while the signal() function handles the exit from the
critical section. If we remove the critical section, we cannot guarantee the consistency of the end outcome after all
the processes finish executing simultaneously.

We'll look at some solutions to Critical Section Problem but before we move on to that, let us take a look at what
conditions are necessary for a solution to Critical Section Problem.

Requirements of synchronization:
• Mutual exclusion: If a process is running in the critical section, no other process should be allowed to run in that
section at that time.
• Progress: If no process is still in the critical section and other processes are waiting outside the critical section to
execute, then any one of the threads must be permitted to enter the critical section. The decision of which process
will enter the critical section will be taken by only those processes that are not executing in the remaining section.
• No starvation: Starvation means a process keeps waiting forever to access the critical section but never gets a
chance. No starvation is also known as Bounded Waiting.
.1. A process should not wait forever to enter inside the critical section.
.2. When a process submits a request to access its critical section, there should be a limit or bound.
.3. After this bound is reached, this process should be allowed to access the critical section.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Solutions to CSP:
1. Peterson’s solution
It is a classical software-based solution.
In Peterson's solution, we have two shared variables that are used by the processes.
• A boolean Flag[]: A boolean array Flag which is initialized to FALSE. This Flag array represents which process is
which process wants to enter into the critical solution.
• int Turn: A integer variable Turn indicates the process number which is ready to enter into the critical section.

2. Synchronization Hardware
Hardware can occasionally assist in the solving of critical section issues. Some operating systems provide a lock feature.
When a process enters a critical section, it is given a lock, which the process must release before the process can exit the
critical section. As a result, additional processes are unable to access a critical section if anyone process is already using
the section. The lock can have either of the two values, 0 or 1.

3. Mutex Locks
Implementation of Synchronization hardware is not an easy method, which is why Mutex Locks were introduced.
Mutex is a locking mechanism used to synchronize access to a resource in the critical section. In this method, we use a
LOCK over the critical section. The LOCK is set when a process enters from the entry section, and it gets unset when the
process exits from the exit section.

4. Semaphores
A semaphore is a signaling mechanism, and a process can signal a process that is waiting on a semaphore. This differs
from a mutex in that the mutex can only be notified by the process that sets the shared lock. Semaphores make use of the
wait() and signal() functions for synchronization among the processes.

There are two kinds of semaphores:


A. Binary Semaphores
Binary Semaphores can only have one of two values: 0 or 1. Because of their capacity to ensure mutual exclusion, they
are also known as mutex locks.
• A single binary semaphore is shared between multiple processes.
• When the semaphore is set to 1, it means some process is working on its critical section, and other processes
need to wait, and if the semaphore is set to 0, that means any process can enter the critical section.
Hence, whenever the binary semaphore is set to 0, any process can then enter its critical section by setting the binary
semaphore to 1. When it has completed its critical section, it can reset the binary semaphore to 0, enabling another
process to enter it.

B. Counting Semaphores
Counting Semaphores can have any value and are not limited to a certain area. They can be used to restrict access to a
resource that has a concurrent access limit.

Classical Problems of Synchronization:

Bound-Buffer problem
Also known as the Producer-Consumer problem. In this problem, there is a buffer of n slots, and each buffer is capable
of storing one unit of data. There are two processes that are operating on the buffer – Producer and Consumer. The
producer tries to insert data and the consumer tries to remove data.
If the processes are run simultaneously they will not yield the expected output.
The solution to this problem is creating two semaphores, one full and the other empty to keep a track of the concurrent
processes.

Sleeping Barber Problem

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

This problem is based on a hypothetical barbershop with one barber.


When there are no customers the barber sleeps in his chair. If any customer enters he will wake up the barber and sit in
the customer chair. If there are no chairs empty they wait in the waiting queue.

Dining Philosopher’s problem


This problem states that there are K number of philosophers sitting around a circular table with one chopstick placed
between each pair of philosophers. The philosopher will be able to eat if he can pick up two chopsticks that are adjacent
to the philosopher.
This problem deals with the allocation of limited resources.

Readers and Writers Problem


This problem occurs when many threads of execution try to access the same shared resources at a time. Some threads
may read, and some may write. In this scenario, we may get faulty outputs.

UNIT-3

# DEADLOCKS

What are deadlocks:


All the processes in a system require some resources such as central processing unit(CPU), file storage, input/output
devices, etc to execute it. Once the execution is finished, the process releases the resource it was holding. However,
when many processes run on a system they also compete for these resources they require for execution. This may arise
a deadlock situation.
A deadlock is a situation in which more than one process is blocked because it is holding a resource and also requires
some resource that is acquired by some other process. Therefore, none of the processes gets executed.

System Model:
A system model or structure consists of a fixed number of resources to be circulated among some opposing processes.
The resources are then partitioned into numerous types, each consisting of some specific quantity of identical instances.
Memory space, CPU cycles, directories and files, I/O devices like keyboards, printers and CD-DVD drives are prime
examples of resource types. When a system has 2 CPUs, then the resource type CPU got two instances.

Under the standard mode of operation, any process may use a resource in only the below-mentioned sequence:

• Request: When the request can't be approved immediately (where the case may be when another process is
utilizing the resource), then the requesting job must remain waited until it can obtain the resource.
• Use: The process can run on the resource (like when the resource is a printer, its job/process is to print on the
printer).
• Release: The process releases the resource (like, terminating or exiting any specific process).

Deadlock characterization:
The four necessary conditions for a deadlock to arise are as follows.

• Mutual Exclusion: Only one process can use a resource at any given time i.e. the resources are non-sharable.
• Hold and wait: A process is holding at least one resource at a time and is waiting to acquire other resources held
by some other process.
• No preemption: The resource can be released by a process voluntarily i.e. after execution of the process.
• Circular Wait: A set of processes are waiting for each other in a circular fashion. For example, lets say there are
a set of processes {P0 ,P1 ,P2 ,P3 } such that P0 depends on P1 , P1 depends on P2 , P2 depends on P3 and

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

P3 depends on P0 . This creates a circular relation between all these processes and they have to wait forever to
be executed.

Methods for Handling deadlocks:

1. Prevention:
This is done by restraining the ways a request can be made. Since deadlock occurs when all the above four conditions
are met, we try to prevent any one of them, thus preventing a deadlock.

2. Avoidance:
When a process requests a resource, the deadlock avoidance algorithm examines the resource-allocation state. If
allocating that resource sends the system into an unsafe state, the request is not granted.
Therefore, it requires additional information such as how many resources of each type is required by a process. If the
system enters into an unsafe state, it has to take a step back to avoid deadlock.

• Denial of process initiation


• Denial of resource allocation

3. Detection:
If resources have a single instance –
In this case for Deadlock detection, we can run an algorithm to check for the cycle in the Resource Allocation Graph. The
presence of a cycle in the graph is a sufficient condition for deadlock.

If there are multiple instances of resources –


Detection of the cycle is necessary but not sufficient condition for deadlock detection, in this case, the system may or may
not be in deadlock varies according to different situations.

Recovery of Deadlocks:
A traditional operating system such as Windows doesn’t deal with deadlock recovery as it is a time and space-consuming
process. Real-time operating systems use Deadlock recovery.
Killing the process –
Killing all the processes involved in the deadlock. Killing process one by one. After killing each process check for deadlock
again keep repeating the process till the system recovers from deadlock. Killing all the processes one by one helps a
system to break circular wait condition.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Resource Preemption –
Resources are preempted from the processes involved in the deadlock; preempted resources are allocated to other
processes so that there is a possibility of recovering the system from deadlock. In this case, the system goes into
starvation.

# DEVICE MANAGEMENT

Device management in operating system known as the management of the I/O devices such as a keyboard, magnetic
tape, disk, printer, microphone, USB ports, scanner, etc.as well as the supporting units like control channels. An operating
system or the OS manages communication with the devices through their respective drivers. The operating system
component provides a uniform interface to access devices of varied physical attributes.

Techniques:
• Keep tracks of all devices and the program which is responsible to perform this is called I/O controller.
• Monitoring the status of each device such as storage drivers, printers and other peripheral devices.
• Enforcing preset policies and taking a decision which process gets the device when and for how long.
• Allocates and deallocates the device in an efficient way. Deallocating them at two levels: at the process level
when I/O command has been executed and the device is temporarily released, and at the job level, when the job
is finished and the device is permanently released.
• Optimizes the performance of individual devices.

Types of Devices:
1. Dedicated Devices: There are many devices when they are allocated to a process, they are not released until
the job is completed. The reason for this is it is not efficient to switch them between two or more devices and
sometimes not allocating devices until the job is done becomes a disadvantage because, if the current process is
not taking 100% use of it then also, we cannot deallocate it. Printers, plotters, tape drivers, etc. are examples of
such devices.
2. Shared Devices: Devices that can be shared or allocated between two or more processes at the same time are
known as shared devices. For sharing the same device between two or more processes device manager must
carefully control the interleaving, and all the pre-determined policies are resolved. HDDs or SDDs, tape silos,
optical jukeboxes, etc. are some examples of shared devices.
3. Virtual Devices: The third type of device or virtual device is the combination of the above two discussed devices
means when the dedicated devices are converted into shared devices then they are known as virtual devices.
The best example of a virtual device is the printer. A printer can be transformed into a shared device from the
dedicated device by using the spooling program, in which all the requests to the printer are redirected to a disk.
By converting a dedicated device to a shared device, we can create many virtual instances of the current device
and can increase its efficiency.

Input or Output Devices:

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Storage Devices:

There are two types of storage devices:

• Volatile Storage Device –


It loses its contents when the power of the device is removed. Eg Cache memory and RAM.

• Non-Volatile Storage device –


It does not lose its contents when the power is removed. It holds all the data when the power is removed. Eg
Secondary storage, HDD, ROM, Disks, etc.

Secondary Storage is used as an extension of main memory.


Secondary storage devices can hold the data permanently.

Storage devices consists of Registers, Cache, Main-Memory, Electronic-Disk, Magnetic-Disk, Optical-Disk, Magnetic-
Tapes. Each storage system provides the basic system of storing a datum and of holding the datum until it is retrieved at
a later time. All the storage devices differ in speed, cost, size and volatility. The most common Secondary-storage
device is a Magnetic-disk, which provides storage for both programs and data.

In this fig Hierarchy of storage is shown –

• In this hierarchy all the storage devices are arranged according to speed
and cost.
• The higher levels are expensive, but they are fast.
• As we move down the hierarchy, the cost per bit generally decreases,
whereas the access time generally increases.
• The storage systems above the ‘Electronic Disk’ are Volatile, where as
those below are Non-Volatile.

1. An Electronic disk can be either designed to be either Volatile or Non-Volatile. During normal operation, the
electronic disk stores data in a large DRAM array, which is Volatile. But many electronic disk devices contain a
hidden magnetic hard disk and a battery for backup power. If external power is interrupted, the electronic disk
controller copies the data from RAM to the magnetic disk. When external power is restored, the controller copies
the data back into the RAM.
2. The design of a complete memory system must balance all the factors. It must use only as much expensive
memory as necessary while providing as much inexpensive, Non-Volatile memory as possible. Caches can be
installed to improve performance where a large access-time or transfer-rate disparity exists between two
components.

Buffering:

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

A buffer is a memory area that stores data being transferred between two devices or between a device and an application.

Uses of I/O Buffering:

• Buffering is done to deal effectively with a speed mismatch between the producer and consumer of the data
stream.
• A buffer is produced in main memory to heap up the bytes received from modem.
• After receiving the data in the buffer, the data get transferred to disk from buffer in a single operation.
• This process of data transfer is not instantaneous; therefore, the modem needs another buffer in order to store
additional incoming data.
• When the first buffer got filled, then it is requested to transfer the data to disk.
• The modem then starts filling the additional incoming data in the second buffer while the data in the first buffer
getting transferred to disk.
• When both the buffers completed their tasks, then the modem switches back to the first buffer while the data from
the second buffer get transferred to the disk.
• The use of two buffers disintegrates the producer and the consumer of the data, thus minimizes the time
requirements between them.
• Buffering also provides variations for devices that have different data transfer sizes.

Types of various I/O buffering techniques:

1. Single Buffer:
A buffer is provided by the operating system to the system portion of the main memory.

Block oriented device –


• System buffer takes the input.
• After taking the input, the block gets transferred to the user space by the process and then the process requests
for another block.
• Two blocks works simultaneously, when one block of data is processed by the user process, the next block is
being read in.
• OS can swap the processes.
• OS can record the data of system buffer to user processes.

Stream oriented device –


• Line- at a time operation is used for scroll made terminals. User inputs one line at a time, with a carriage return
signaling at the end of a line.
• Byte-at a time operation is used on forms mode, terminals when each keystroke is significant.

2. Double Buffer:

Block oriented –
• There are two buffers in the system.
• One buffer is used by the driver or controller to store data while waiting for it to be taken by higher level of the
hierarchy.
• Other buffer is used to store data from the lower-level module.
• Double buffering is also known as buffer swapping.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• A major disadvantage of double buffering is that the complexity of the process gets increased.
• If the process performs rapid bursts of I/O, then using double buffering may be deficient.

Stream oriented –
• Line- at a time I/O, the user process need not be suspended for input or output, unless process runs ahead of the
double buffer.
• Byte- at a time operations, double buffer offers no advantage over a single buffer of twice the length.

3. Circular Buffer:

• When more than two buffers are used, the collection of buffers is itself referred to as a circular buffer.
• In this, the data do not directly pass from the producer to the consumer because the data would change due to
overwriting of buffers before they had been consumed.
• The producer can only fill up to buffer i-1 while data in buffer i is waiting to be consumed.

Secondary Storage Structure:

Secondary storage devices are those devices whose memory is non-volatile, meaning, the stored data will be intact even
if the system is turned off. Here are a few things worth noting about secondary storage.

• Secondary storage is also called auxiliary storage.


• Secondary storage is less expensive when compared to primary memory like RAMs.
• The speed of the secondary storage is also lesser than that of primary storage.
• Hence, the data which is less frequently accessed is kept in the secondary storage.
• A few examples are magnetic disks, magnetic tapes, removable thumb drives etc.

Disk Structure:

In modern computers, most of the secondary storage is in the form of magnetic disks. Hence, knowing the structure of a
magnetic disk is necessary to understand how the data in the disk is accessed by the computer.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Structure of a Magnetic Disk

A magnetic disk contains several platters. Each platter is divided into circular shaped tracks. The length of the tracks
near the center is less than the length of the tracks farther from the center. Each track is further divided into sectors, as
shown in the figure.

Tracks of the same distance from center form a cylinder. A read-write head is used to read data from a sector of the
magnetic disk.

The speed of the disk is measured as two parts:


• Transfer rate: This is the rate at which the data moves from disk to the computer.
• Random access time: It is the sum of the seek time and rotational latency.

Seek time is the time taken by the arm to move to the required track. Rotational latency is defined as the time taken by
the arm to reach the required sector in the track.

Even though the disk is arranged as sectors and tracks physically, the data is logically arranged and addressed as an
array of blocks of fixed size. The size of a block can be 512 or 1024 bytes. Each logical block is mapped with a sector on
the disk, sequentially. In this way, each sector in the disk will have a logical address.

Disk Scheduling:
On a typical multiprogramming system, there will usually be multiple disk access requests at any point of time. So those
requests must be scheduled to achieve good efficiency. Disk scheduling is similar to process scheduling. Some of the disk
scheduling algorithms are described below.

1. First Come First Serve


• This algorithm performs requests in the same order asked by the system.
• Let’s take an example where the queue has the following requests with cylinder numbers as follows:
98, 183, 37, 122, 14, 124, 65, 67. Assume the head is initially at cylinder 56. The head moves in the given order
In in the queue i.e., 56→98→183→…→67.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

2. Shortest Seek Time First (SSTF)


• Here the position which is closest to the current head position is chosen first.
• Consider the previous example where disk queue looks like, 98, 183, 37, 122, 14, 124, 65, 67. Assume the head
is initially at cylinder 56. The next closest cylinder to 56 is 65, and then the next nearest one is 67, then 37, 14, so
on.

3. SCAN algorithm
• This algorithm is also called the elevator algorithm because of its behavior. Here, first the head moves in a
direction (say backward) and covers all the requests in the path. Then it moves in the opposite direction and
covers the remaining requests in the path. This behavior is similar to that of an elevator.
• Let’s take the previous example, 98, 183, 37, 122, 14, 124, 65, 67. Assume the head is initially at cylinder 56. The
head moves in backward direction and accesses 37 and 14. Then it goes in the opposite direction and accesses
the cylinders as they come in the path.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Disk Management:

Disk Formatting

• Disk formatting is a process to configure the data-storage devices such as hard-drive, floppy disk and flash drive
when we are going to use them for the very first time or we can say initial usage.
• Disk formatting is usually required when new operating system is going to be used by the user.
• It is also done when there is space issue and we require additional space for the storage of more data in the
drives.

• When we format the disk then the existing files within the disk is also erased.
• We can perform disk formatting on both magnetic platters hard-drives and solid-state drives.
→ When we are going to use hard-drive for initial use it is going to search for virus. It can scan for virus and
repair the bad sectors within the drive. Disk formatting has also the capability to erase the bad
applications and various sophisticated viruses.
→ As we know that disk formatting deletes data and removes all the programs installed with in the drive. So
it can be done with caution. We must have the backup of all the data and applications which we require.
No-doubt disk formatting requires time. But the frequent formatting of the disk decreases the life of the
hard-drive.

1. Low-level Formatting:
• Low level formatting is a type of physical formatting. It is the process of marking of cylinders and tracks of the
blank hard-disk.
• We have data in our hard-disk and when we perform low-level formatting in the presence of data in the hard-disk
all the data have been erased and it is impossible to recover that data. Some users make such a format that they
can avoid their privacy leakage. Otherwise, low-level will cause damage to hard-disk shortens the service-life.
• Therefore, this formatting is not suggested to users.

2. Partitioning:
• As suggesting from the name, partitioning means divisions. Partitioning is the process of dividing the hard-disk
into one or more regions. The regions are called as partitions.
• It can be performed by the users and it will affect the disk performance.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

3. High-level Formatting:
• High-level formatting is the process of writing. Writing on a file system, cluster size, partition label, and so on for a
newly created partition or volume. It is done to erase the hard-disk and again installing the operating system on
the disk-drive.

Steps for high-level formatting

• Firstly, High-level formatting clears the data on hard-disk, then it will generate boot information, then it will
initialise FAT after this it will go for label logical bad sectors when partition has existed.
• Formatting done by the user is the high-level formatting.
• Generally, it does not harm the hard-disk. It can be done easily with the Administrator, Windows snap-in Disk
Management tool, disk part, etc.
• We can use such a format to fix some problems like errors in the file system, corrupted hard-drive and develop
bad sectors.

Swap-Space Management:

Swapping is a memory management technique used in multi-programming to increase the number of processes sharing
the CPU. It is a technique of removing a process from the main memory and storing it into secondary memory, and then
bringing it back into the main memory for continued execution. This action of moving a process out from main memory to
secondary memory is called Swap Out and the action of moving a process out from secondary memory to main memory
is called Swap In.

Swap-Space Management:
Swap-Space:
The area on the disk where the swapped-out processes are stored is called swap space.

Swap-Space management is another low-level task of the operating system. Disk space is used as an extension of main
memory by the virtual memory. As we know the fact that disk access is much slower than memory access, In the swap-
space management we are using disk space, so it will significantly decrease system performance. Basically, in all our
systems we require the best throughput, so the goal of this swap-space implementation is to provide the virtual memory
the best throughput.
In this article, we are going to discuss how swap space is used, where swap space is located on disk, and how swap
space is managed.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Swap-Space Use:
Swap-space is used by the different operating-system in various ways. The systems which are implementing swapping
may use swap space to hold the entire process which may include image, code and data segments. Paging systems may
simply store pages that have been pushed out of the main memory. The need of swap space on a system can vary
from a megabyte to gigabytes but it also depends on the amount of physical memory, the virtual memory it is
backing and the way in which it is using the virtual memory.
It is safer to overestimate than to underestimate the amount of swap space required, because if a system runs out of
swap space it may be forced to abort the processes or may crash entirely. Overestimation wastes disk space that could
otherwise be used for files, but it does not harm other.

Swap-Space Location:

A swap space can reside in one of the two places –

1. Normal file system


2. Separate disk partition

Let, if the swap-space is simply a large file within the file system. To create it, name it and allocate its space normal file-
system routines can be used. This approach, through easy to implement, is inefficient. Navigating the directory structures
and the disk-allocation data structures takes time and extra disk access. During reading or writing of a process image,
external fragmentation can greatly increase swapping times by forcing multiple seeks.

There is also an alternate to create the swap space which is in a separate raw partition. There is no presence of any file
system in this place. Rather, a swap space storage manager is used to allocate and de-allocate the blocks. from the raw
partition. It uses the algorithms for speed rather than storage efficiency, because we know the access time of swap space
is shorter than the file system. By this Internal fragmentation increases, but it is acceptable, because the life span of the
swap space is shorter than the files in the file system. Raw partition approach creates fixed amount of swap space in case
of the disk partitioning.

Some operating systems are flexible and can swap both in raw partitions and in the file system space, example: Linux.

Disk Reliability:
Reliability is the ability of the disk system to accommodate a single- or multi-disk failure and still remain available to the
users. Performance is the ability of the disks to efficiently provide information to the users.

Adding redundancy almost always increases the reliability of the disk system. The most common way to add redundancy
is to implement a Redundant Array of Inexpensive Disks (RAID).

There are two types of RAID:


• Hardware — The most commonly used hardware RAID levels are: RAID 0, RAID 1, RAID 5, and RAID 10. The
main differences between these RAID levels focus on reliability and performance as previously defined.
• Software — Software RAID can be less expensive. However, it is almost always much slower than hardware
RAID, because it places a burden on the main system CPU to manage the extra disk I/O.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

UNIT-4

# INFORMATION MANAGEMENT

Introduction:
Information management (IM) is primarily concerned with the capture, digitization, representation, organization,
transformation, and presentation of information. Because a computer’s main memory provides only temporary storage,
computers are equipped with auxiliary disk storage devices that permanently store data. These devices are characterized
by having much higher capacity than main memory but slower read/write (access) speed. Data stored on a disk must be
read into main memory before it can be processed. A major goal of IM systems, therefore, is to develop efficient
algorithms to store and retrieve specific data for processing.

A Simple File System:


A file is a collection of correlated information which is recorded on secondary or non-volatile storage like magnetic disks,
optical disks, and tapes. It is a method of data collection that is used as a medium for giving input and receiving output
from that program. In general, a file is a sequence of bits, bytes, or records whose meaning is defined by the file creator
and user. Every File has a logical location where they are located for storage and retrieval.

Objective of File management System


• It provides I/O support for a variety of storage device types.
• Minimizes the chances of lost or destroyed data
• Helps OS to standardized I/O interface routines for user processes.
• It provides I/O support for multiple users in a multiuser systems environment.

Properties of a File System


• Files are stored on disk or other storage and do not disappear when a user logs off.
• Files have names and are associated with access permission that permits controlled sharing.
• Files could be arranged or more complex structures to reflect the relationship between them

General Model:
- File Structure

A File Structure needs to be predefined format in such a way that an operating system understands. It has an exclusively
defined structure, which is based on its type.
Three types of files structure in OS:
• A text file: It is a series of characters that is organized in lines.
• An object file: It is a series of bytes that is organized into blocks.
• A source file: It is a series of functions and processes.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Types of Files:
File type refers to the ability of the operating system to distinguish different types of files such as text files source files and
binary files etc. Many operating systems support many types of files. Operating system like MS-DOS and UNIX have the
following types of files −

Ordinary files
• These are the files that contain user information.
• These may have text, databases or executable program.
• The user can apply various operations on such files like add, modify, delete or even remove the entire file.

Directory files
• These files contain list of file names and other information related to these files.

Special files
• These files are also known as device files.
• These files represent physical device like disks, terminals, printers, networks, tape drive etc.
These files are of two types −
• Character special files − data is handled character by character as in case of terminals or printers.
• Block special files − data is handled in blocks as in the case of disks and tapes.

File System Interface:


For the majority of users, the file system is the most obvious aspect of any operating system. This provides users the
method for storage and access to data as well as programs of the operating system where all the users of the computer
system can use it.

The file system consists of 2 distinct parts:


• a collection of files, that store related data, and
• a directory structure, which organizes and provides information about all the files in the system.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

- File Attributes

A file is named, for the ease of its users and is referred by its name. A name is usually a string of characters like
filename.cpp, along with an extension which designates the file format. Some systems (like Linux) distinguish between
uppercase and lowercase characters in names, whereas other systems don't. When a file is given a name, it becomes
independent of the process, the user and also the system which created it. Let's suppose, one user might make the file
filename.cpp, and another user might be editing that file by deducing its name. The file's owner may write the file to a
compact disk (CD) or send it via an e-mail or copy it across a network, and it could still be called filename.cpp on the
destination system.

- File Components

A file has a name and data. Moreover, it also stores meta information like file creation date and time, current size, last
modified date, etc. All this information is called the attributes of a file system.
Here, are some important File attributes used in OS:
• Name: It is the only information stored in a human-readable form.
• Identifier: Every file is identified by a unique tag number within a file system known as an identifier.
• Location: Points to file location on device.
• Type: This attribute is required for systems that support various types of files.
• Size: Attribute used to display the current file size.
• Protection: This attribute assigns and controls the access rights of reading, writing, and executing the file.
• Time, date and security: It is used for protection, security, and also used for monitoring

- File Operations

A file is an abstract data type. For defining a file properly, we need to consider the operations that can be performed on
files. The operating system can provide system calls to create, write, read, reposition, delete, and truncate files. There are
six basic file operations within an Operating system. These are:

• Creating a file: There are two steps necessary for creating a file. First, space in the file system must be found for
the file. We discuss how to allocate space for the file. Second, an entry for the new file must be made in the
directory.
• Writing a file: To write to a file, you make a system call specify about both the name of the file along with the
information to be written to the file.
• Reading a file: To read from a file, you use a system call which specifies the name of the file and where within
memory the next block of the file should be placed.
• Repositioning inside a file: The directory is then searched for the suitable entry, and the 'current-file-position'
pointer is relocating to a given value. Relocating within a file need not require any actual I/O. This file operation is
also termed as 'file seek.'
• Deleting a file: For deleting a file, you have to search the directory for the specific file. Deleting that file or
directory release all file space so that other files can re-use that space.
• Truncating a file: The user may wish for erasing the contents of a file but keep the attributes same. Rather than
deleting the file and then recreate it, this utility allows all attributes to remain unchanged — except the file length
— and let the user add or edit the file content.

Access Methods:
File access is a process that determines the way that files are accessed and read into memory. Generally, a single access
method is always supported by operating systems. Though there are some operating system which also supports multiple
access methods.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Three file access methods are:


• Sequential access
• Direct random access
• Index sequential access

Sequential Access
In this type of file access method, records are accessed in a certain pre-defined sequence. In the sequential access
method, information stored in the file is also processed one by one. Most compilers access files using this access method.

Random Access
The random-access method is also called direct random access. This method allows accessing the record directly. Each
record has its own address on which can be directly accessed for reading and writing.

Sequential Access
This type of accessing method is based on simple sequential access. In this access method, an index is built for every file,
with a direct pointer to different memory blocks. In this method, the Index is searched sequentially, and its pointer can
access the file directly. Multiple levels of indexing can be used to offer greater efficiency in access. It also reduces the
time needed to access a single record.

Directory Structure:
On a computer, a directory is used to store, arrange, and segregate files and folders. It is similar to a telephone directory
in that it just contains lists of names, phone numbers, and addresses rather than the real papers. It uses a hierarchical
structure to organize files and directories. On many computers, directories are referred to as drawers or folders, much like
a workbench or a standard filing cabinet in an office. You may, for instance, create a directory for images and another for
all of your documents. You could easily access the type of file you wanted to see by saving particular file types in a folder.

There are several logical structures of a directory, these are given below.

• Single level directory


• Two-level directory
• Tree structure or hierarchical directory
• Acyclic graph directory
• General graph directory structure

1. Single level directory


It is the simplest directory structure. In a single-level directory, there is only one directory in the system, meaning there is
only one folder, and all the files are stored in that single directory. There is no way to segregate important files from non-
important files.
Implementation of a single-level directory is the simplest. However, there are various disadvantages of it.
The pictorial representation of a single-level directory is given below. There is only one directory ( root directory), and all
the files are stored in the same directory. Here f1, f2, f3, f4, f5 represent the five different files. Practically it can be thought
of as a structure where all the files are stored in the same folder.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Advantages:
• The main advantage of a single-level directory is that it is very simple to implement.
• Since all the files are present in the same directory, in case the number of files is less, then searching for a
particular file is faster and easier.
• Simple operations like file creation, search, deletion, and updating are possible with a single-level directory
structure.
• The single-level directory is easier to understand in practical life.

Disadvantages:
• In case we want to organize the files in some groups, it is not possible to do so since we cannot create
subdirectories.
• Two file names cannot be the same. In case two files are given the same name, the previous one is overridden.
• If the number of files is very large, searching a particular file is very inefficient.
• Segregation of important and unimportant files is not possible.
• The single-level directory is not useful for multi-user systems.

2. Two-level directory
We saw how the single-level directory proves to be inefficient if multiple users are accessing the system. If two different
users wanted to create a file with the same name (say report.doc), it was not allowed in a single level directory.
In a two-level directory structure, there is a master node that has a separate directory for each user. Each user can store
the files in that directory. It can be practically thought of as a folder that contains many folders, each for a particular user,
and now each user can store files in the allocated directory just like a single level directory.

The pictorial representation of a two-level directory is shown below. For every user, there is a separate directory. At the
next level, every directory stores the files just like a single-level directory. Although not very efficient, the two-level
directory is better than a single-level directory structure.

Advantages:
• Searching is very easy.
• There can be two files with the same name in two different user directories. Since they are not in the same
directory, the same name can be used.
• Grouping is easier.
• A user cannot enter another user’s directory without permission.
• Implementation is easy.

Disadvantages:
• One user cannot share a file with another user.
• Even though it allows multiple users, still a user cannot keep two same type files in a user directory.
• It does not allow users to create subdirectories.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

3. Tree-structured directory or Hierarchical directory


This type of directory is used in our PCs. The biggest disadvantage of a two-level directory was that one could not create
sub-directories in a directory. The tree-structured directory solved this problem. In a tree-structured directory, there is a
root directory at the peak. The root directory contains directories for each user. The users can, however, create
subdirectories inside their directory and also store the files.
This is how things work on our PCs. We can store some files inside a folder and also create multiple folders inside a
folder.
The pictorial representation of a tree-structured directory is shown below. The root directory is highly secured, and only
the system administrator can access it. We can see how there can be subdirectories inside the user directories. A user
cannot modify the root directory data. Also, a user cannot access another user's directory.

Advantages:
• Highly scalable compared to the previous two types of directories.
• Allows subdirectories inside a directory.
• Searching is easy.
• Allows grouping.
• Segregation of important and unimportant files is easy.

Disadvantages:
• As one user cannot enter another user’s directory, this restricts sharing of files.
• Too many subdirectories may make the search complicated.
• Users cannot modify the root directory’s data.
• Files might have to be saved in multiple directories in case all of them do not fit into one.

4. Acyclic graph directory


Suppose there is a file abcd.txt. Out of the three types of directories we studied above, none of them provide the flexibility
to access the file abcd.txt from multiple directories, i.e., we cannot access a particular file or subdirectory from two or
more directories. The file or the subdirectory can be accessed only by the directory it is present inside.

The solution to this problem is presented by the acyclic-graph directory. In this type of directory, we can access a file or a
subdirectory from multiple directories. Hence files can be shared between directories. It is designed in such a way that
multiple directories point to a particular directory or file with the help of links.

A practical example of this is a doc file shared between two users. If any of the users makes a change in the file, the
change is reflected for both the users.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Advantages:
• Allows sharing of files or subdirectories from more than one directory.
• Searching is very easy.
• Provides more flexibility to the users.

Disadvantages:
• Harder to implement in comparison to the previous three.
• Since the files are accessed from multiple directories, deleting a file may cause some errors if the user is not
cautious.
• If the files are linked by a hard link, then it is necessary to delete all the references to that file to permanently
delete the file.

5. General-graph directory
This is an extension to the acyclic-graph directory. In the general-graph directory, there can be a cycle inside a directory.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

In the above image, we can see that a cycle is formed in the user 2 directory. Although it provides greater flexibility, it is
complex to implement this structure.

Advantages:
• Compared to the others, the General-Graph directory structure is more flexible.
• Cycles are allowed in the directory for general-graphs.

Disadvantages:
• It costs more than alternative solutions.
• Garbage collection is an essential step here.

# PROTECTION
Goals of Protection:
- System protection
- Defining policies for system protection
- Mechanism and Policies

System Protection:
• Protection is not required for single user system. It is important in a multi user computer system, when multiple
users use computer resources such as Physical resources like CPU, memory, I/O devices, etc. and Logical
resources like files, processes (system and application).
• Hence the system resources should be protected from unauthorized users and processes by providing
authorization to users to access the resources like what to access, where to access, how to access and when to
access.
• It is the operating system's responsibility to offer a mechanism that:
→ protects each process from other processes.
→ safely share a common logical namespace, such as a directory of files, or share a common
physical namespace, such as main memory.
→ grant different access rights(role based access control) to different users (authorization), to
access the resources.
• The application programmer needs to use protection mechanisms, good resources and supported by an
application subsystem against misuse.

Defining policies for system protection:


• The policy ensures the ways to access system resources by each program component or the users.
• The policy defines how processes access the computer system’s resources such as CPU, memory, software, and
even the operating system. The policies are modified at any time.
• It is the responsibility of both the operating system designer and the app programmer to implement these policies.
• The protection policies either established by OS, or by management or by programmers to ensure that their
programs are protected to the greatest extent possible.
• It also provides a multi programming OS with the security to its users when sharing common space such as files
or directories.

Mechanisms and policies:


• Mechanisms are distinct from policies.
• Mechanism determines how something will be done whereas policies decide what will be done.
• The separation of policy and mechanism is important for flexibility because policies are likely to change from
place to place or time to time.
• Protection system must have the flexibility to enforce a variety of policies.
• In the worst case, every change in policy would require a change in the underlying mechanism.
• Using general mechanism enables us to avoid such a situation.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Domain of Protection:
A computer system is a collection of processes and objects; hardware objects (CPU, memory segments, printers, disk,
and tape drives) and software objects (files, programs, and semaphores). Each object has a unique name and well-
defined and meaningful operations. Objects are essentially abstract data types.
The operations that are possible, may depend on the object. For example;
• CPU: can only execute
• Memory Segments: read and write
• CR-ROM or DVD-ROM: can only be read
• Tape drives: read, write and rebound
• Data files: can be created, opened, read, written, closed and deleted
• Program files: can be read, written, executed, and deleted

A process should be allowed to access only those resources for which it has authorization. Furthermore, at any time, a
process should be able to access only those resources that it currently requires to complete its task.
This second requirement, commonly referred to as the need-to-know principle, is useful in limiting the amount of damage
a faulty process can cause in the system.
For example, when process p invokes procedure A (), the procedure should be allowed to access only its own variables
and the formal parameters passed to it; it should not be able to access all the variables of process p.

Domain Structure
• A process operates within a protection domain, which specifies the resources that the process may access.
• Each domain defines a set of objects and the types of operations.
• A domain is a collection of access rights (the ability to execute an operation on an object), each of which is an
ordered pair <object-name, rights-set>.
• For example, if domain ‘D’ as the access right <file F, {read, write}>, then a process executing in domain D can
both read and write file F. It cannot perform any other operation on that object.
• A domain can be realized in a variety of ways:
→ Each user may be a domain. In this case, the set of objects that can be accessed depends on the
identity of the user. Domain switching occurs when the user is changed—generally when one user
logs out and another user logs in.
→ Each process may be a domain. In this case, the set of objects that can be accessed depends on the
identity of the process. Domain switching occurs when one process sends a message to another
process and then waits for a response.
→ Each procedure may be a domain. In this case, the set of objects that can be accessed corresponds
to the local variables defined within the procedure. Domain switching occurs when a procedure call is
made.

Access Rights:
• The permissions that are granted to a user, or to an application, to read, write and erase files in the computer.
• An access right is the right to perform a particular operation on the object. For example, the FILE_READ_DATA
access right specifies the right to read from a file.
• When you open a handle to an object, you specify a set of access rights corresponding to the operations that may
be performed on the object. The system checks the specified access rights against the object's security descriptor
to see if each operation is permitted for the current user.

Access rights come in two types:


1. Specific access right is a right to perform a single operation. These rights can depend on the type of object.
2. Generic access right is a right to perform one of a set of similar operations. These rights are independent of the
type of object.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

Consistency:
Consistency Semantics is concept which is used by users to check file systems which are supporting file sharing in their
systems. Basically, it is specification to check that how in a single system multiple users are getting access to same file
and at same time. They are used to check various things in files, like when will modification by some user in some file is
noticeable to others.
Consistency Semantics is concept which is in a direct relation with concept named process synchronization algorithms.
But process synchronization algorithms are not used in case of file I/O because of several issues like great latency, slower
rate of transfer of disk and network.

1. UNIX Semantics:
The file systems in UNIX uses following consistency semantics –

• The file which user is going to write will be visible to all users who are sharing that file at that time.
• There is one mode in UNIX semantics to share files via sharing pointer of current location. But it will affect all
other sharing users.

In this, a file which is shared is associated with a single physical image that is accessed as an exclusive resource. This
single image causes delays in user processes.

2. Session Semantics:
The file system in Andrew uses following consistency semantics.

• The file which user is going to be write will not visible to all users who are sharing that file at that time.
• After closing file, changes done to that file by user are only visible only in sessions starting later. If changes file is
already open by other user, then changes will not be visible to that user.

In this, a file which is shared is associated with a several images and there is no delay in this because it allows multiple
users to perform both read and write accesses concurrently on their images.

3. Immutable-Shared-Files Semantics:
There seems a unique approach immutable shared files. In this, user is not allowed to modify file, which is declared as
shared by its creator. An Immutable file has two properties which are as follows –

• Its name may not be reused.


• Its content may not be altered.

In this file system, content of file is fixed. The implementation of semantics in a distributed system is simple, because
sharing is disciplined.

# SEMANTICS SECURITY
Security refers to providing a protection system to computer system resources such as CPU, memory, disk, software
programs and most importantly data/information stored in the computer system. If a computer program is run by an
unauthorized user, then he/she may cause severe damage to computer or data stored in it. So, a computer system must
be protected against unauthorized access, malicious access to system memory, viruses, worms etc.

Authentication:
Authentication refers to identifying each user of the system and associating the executing programs with those users. It is
the responsibility of the Operating System to create a protection system which ensures that a user who is running a
particular program is authentic. Operating Systems generally identifies/authenticates users using following three ways −

• Username / Password − User need to enter a registered username and password with Operating system to login
into the system.

Downloaded by tanya tomar ([email protected])


lOMoARcPSD|43339171

• User card/key − User need to punch card in card slot, or enter key generated by key generator in option provided
by operating system to login into the system.
• User attribute - fingerprint/ eye retina pattern/ signature − User need to pass his/her attribute via designated
input device used by operating system to login into the system.

Program Threats:
Operating system's processes and kernel do the designated task as instructed. If a user program made these process do
malicious tasks, then it is known as Program Threats. One of the common example of program threat is a program installed
in a computer which can store and send user credentials via network to some hacker. Following is the list of some well-
known program threats.

• Trojan Horse − Such program traps user login credentials and stores them to send to malicious user who can
later on login to computer and can access system resources.
• Trap Door − If a program which is designed to work as required, have a security hole in its code and perform
illegal action without knowledge of user then it is called to have a trap door.
• Logic Bomb − Logic bomb is a situation when a program misbehaves only when certain conditions met otherwise
it works as a genuine program. It is harder to detect.
• Virus − Virus as name suggest can replicate themselves on computer system. They are highly dangerous and
can modify/delete user files, crash systems. A virus is generatlly a small code embedded in a program. As user
accesses the program, the virus starts getting embedded in other files/ programs and can make system unusable
for user.

System Threats:
System threats refers to misuse of system services and network connections to put user in trouble. System threats can be
used to launch program threats on a complete network called as program attack. System threats creates such an
environment that operating system resources/ user files are misused.
Following is the list of some well-known system threats.
• Worm − Worm is a process which can choked down a system performance by using system resources to
extreme levels. A Worm process generates its multiple copies where each copy uses system resources, prevents
all other processes to get required resources. Worms processes can even shut down an entire network.
• Port Scanning − Port scanning is a mechanism or means by which a hacker can detects system vulnerabilities to
make an attack on the system.
• Denial of Service (DoS) − Denial of service attacks normally prevent user to make legitimate use of the system.
For example, a user may not be able to use internet if denial of service attacks browser's content settings.

Encryption:
Data encryption is a way of translating data from plaintext (unencrypted) to ciphertext (encrypted). Users can access
encrypted data with an encryption key and decrypted data with a decryption key.
Types of data encryption:
• Asymmetric: Asymmetric encryption, also known as Public-Key Cryptography, encrypts and decrypts the data
using two separate cryptographic asymmetric keys. These two keys are known as a “public key” and a “private
key”.
• Symmetric: Symmetric encryption is a type of encryption where only one secret symmetric key is used to encrypt
the plaintext and decrypt the ciphertext.

Downloaded by tanya tomar ([email protected])

You might also like