Operating system notes
Operating system notes
a.)static partition
b.)dynamic partition
DIS-ADVANTAGE-
This schemes divides the memory into number of separates fixed area,
each of which can hopld one process.
For example-
The memory is consisting of three fixed area, of size 200k, 300k and
400k respectively, each of which hold a process of 150k, 200k, 300k
respectively. all three processes could be active at any time. This
scheme is also known as partitioned memory allocation-static.
ADVANTAGES-
Dis-advatage-
1. The fixed partition sizes can prevent a process being run due to
the unavailability of a partition of sufficient size.
2. Internal fragmentation wastes space which, collectively could
accommodate another process.
These problems can be solved by next scheme.
This obvious cure for the fixed partition problems is to allow the
partitions to be variable in size at load time. In other words, to allocate
the exact amount of memory to the process it requires. Processes are
loaded into consecutive areas until the memory is filled, or, more likely,
the remaining space is too small to accommodate another process.
ii)First fir policy- an incoming process is placed in the first available hole
which can accommodate it. Using the same example to fulfill 12kb
request, first fit will allocate 12kb of the 12kb block of the process.
ADVANTAGES-
Dis-advantage-
for example the memory map of (a) can be compacted as shown in fig.
the two holes of sizes 200k and 250k can be compacted into one hole
of the size 450k. now it is possible to allocate the memory to a new
process D of size 300k.
ADVANTAGES-
DIS-ADVANTAGES-
1. Complex
2. High cost
3. Slow down the speed
4. Compaction time may be substantial
5. Some memory will still be unused because even though it is
compacted, the amount of free area may be less than the needed
partition size.
Fragmentation
As processes are loaded and removed from memory, the free memory space
is broken into little pieces. It happens after sometimes that processes cannot
be allocated to memory blocks considering their small size and memory
blocks remains unused. This problem is known as Fragmentation.
Fragmentation is of two types −
1
External fragmentation
Total memory space is enough to satisfy a request or to reside a process in it,
but it is not contiguous, so it cannot be used.
2
Internal fragmentation
Memory block assigned to process is bigger. Some portion of memory is left
unused, as it cannot be used by another process.
External fragmentation-
Internal fragmentation-
As shown in figure three processes which have been loaded into frame.
The pages remain logically contiguous but the corresponding frames
are not necessarily contiguous.
Another definition-Paging is a memory management technique in which
process address space is broken into blocks of the same size called pages .
The size of the process is measured in the number of pages.
Similarly, main memory is divided into small fixed-sized blocks of (physical)
memory called frames and the size of a frame is kept the same as that of a
page to have optimum utilization of the main memory and to avoid external
fragmentation.
Advantages and Disadvantages of Paging
Here is a list of advantages and disadvantages of paging −
Paging reduces external 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.
Increases memory and processor utilization.
Dis-advantages-
but still suffer from internal fragmentation.
Page table requires extra memory space, so may not be good for a
system having small RAM.
The page address mapping hardware usually increases the cost of the
computer and at the same time slows down the processor.
Some memory will still be unused if the number of avaliable frames is
not sufficient for the address spaces of the process to be run.
Segmentation memory management technique-
Segmentation is a memory management technique in which each job is
divided into several segments of different sizes, one for each module that
contains pieces that perform related functions. Each segment is actually a
different logical address space of the program.
ADVANTAGES –
1. Allows dynamic segment growth.
2. Assists dynamic linking.
3. Facilities shared segments.
Dis-advantages-
1. Considerable compaction overhead.
2. This is difficult in managing variable size segments on secondary
storage.
3. the maximum size of a segment is limited by size of main memory.
Demand Paging-
According to the concept of virtual memory, the entire process doesn't need
to be loaded into the main memory to execute any process at the given time.
The process can be executed efficiently if only some of the pages are present
in the main memory at a particular time. But, the problem here is how we
decide the basis of the selection of pages to be loaded into the main memory
for execution of a process beforehand. It means which page should be present
in the main memory at a particular time and which one should not be there.
In demand paging, the pager brings only those necessary pages into the
memory instead of swapping in a whole process. Thus, demand paging avoids
reading into memory pages that will not be used anyways, decreasing the
swap time and the amount of physical memory needed.
o The amount of processor overhead and the number of tables used for
handling the page faults is greater than in simple page management
techniques.
o It has more probability of internal fragmentation.
o Its memory access time is longer.
o Page Table Length Register (PTLR) has a limit for virtual memory.
o Page map table is needed additional memory and registers.
DEMAND SEGMENTATION-
According to the concept of virtual memory, the entire process doesn't need
to be loaded into the main memory to execute any process at the given time.
The process can be executed efficiently if only some of the segments are
present in the main memory at a particular time. But, the problem here is how
we decide the basis of the selection of pages to be loaded into the main
memory for execution of a process beforehand. It means which segments
should be present in the main memory at a particular time and which one
should not be there.
In other language- process is divided into variable segments and loaded into
main memory into parts not fully at one time. and another segments are
loaded when demanded.
Page Fault-
A page fault occurs when a program attempts to access data or code that is
in its address space, but is not currently located in the system RAM.
In other words-
When we load any page into main memory and the data inside that page doesn’t
belong to main memory then page fault occurs and then we use page
replacement to get the required page.
Steps to handle a page fault-
In FIFO policy, BELADY ANOMALY may exist. Belady’s anomaly reflects the
fact that for some page-replacement algorithms, the page-fault rate may
increase as the number of allocated frames increases. But this is not true
always.
2. Least recently used (LRU) page replacement algorithm-
In this method, we replace the page that has not been used for the longest
period of time, that’s why we call it ‘least recently used’. The LRU is
implemented by using stack.
Whenever a page is referenced, it is removed from the stack and put on the
top. In this way , the top of the stack is always the most recently used page
and the bottom is the LRU page.
As you see in the above example, enteries may be removed from the
middle of the stack. It is implemented by a doubly linked list with a head
and tail pointer.
3. Optimal page replacement algorithm-
An optimal rage-replacement algorithm has the lowest page-fault rate of all
algorithms. An optimal will never suffer from Belady’s anomaly. An optimal
page-replacement algorithm says that
Replace the page that will not be used for the longest period of time.
Unfortunately, the optimal page-replacement algorithm is difficult to
implement, because it requires future knowledge of the reference string.
Principle of localization of references-
THRASHING-
There may be situation when the memory is full with frequently used
pages. A process needs a page that is not in memory , then a page fault
occurs. In order to make space for swapping in the required page, one
of the frequently accessed page is swapped out from the main memory.
Soon, the page that is swapped out is required for execution and this
again results in page fault. page fault occurs again and again and
swapping becomes a lalrge overhead. This high paging activity is called
thrashing.
Dis-advantage-
When a process is born, its life in the system begins. During its experience, a
process goes through a series of discrete states or we can say that a process
executes, it changes state.
Each process may be in one of the following states-
The PCB is data structure containing certain important about the process. Each
process has its own PCB to represent it in the operating system.
The PCB simply serves as the repository for any information that may vary
from process to process.
Mode switch (change from system mode to user mode and vice-
versa)-
The mode switch is used when the CPU changes privilege levels. for example when
a system call is made or a fault occurs. The kernel works in more a privileged
mode than a standard user task. If a user process wants to access things that
are only accessible to the kernel, a mode switch must occur. The currently
executing process need not be changed during a mode switch.
mode switch changes the process privilege between the modes like user mode,
kernel mode etc.
example- p1 to p2 then p1 will handle control to o/s and o/s will handle control to
p2.one process switch involve two mode switch.
STATE SPACE-set of states of all the process constitute state space. Combination
of all states is state space. Space acquired by collection of all process is called
state space.
STATE TRANSITION DIAGRAM-
Waiting time=t2-t1
N= no. of processes.
2. Turn around time-it may be defined as interval from the time of submission
of a process to the time of its completion. it is the sum of periods spent
waiting to get into memory. Waiting in the ready queue, CPU time and I/O
operations. It should be as less as possible.
t2=completion of process
t2=start running
t3=completes
t4=input completes
t5=output appears/produces.
|t1-t2|=waiting time
|t4-t5|=response time
TYPES OF SCHEDULING-
1. Non-preemptive scheduling
2. Preemptive scheduling
1. Non-preemptive scheduling: In non preemptive scheduling, a scheduling
decision is made every time some job in the system finishes its execution
and at system initialization time. It means once a process has been given
the C.P.U, the CPU cannot be taken away from that process. In non-
preemptive scheduling, short jobs are made to wait by longer jobs, but the
treatment of all process is fairer. Response times are more predictable
because incoming high priority jobs cannot displace waiting jobs.
Which process will get C.P.U/resource when and for how much time is
scheduling.
Scheduler is an operating system module that m akes decision for admitting
next job into system for execution.
Long term scheduler is also known as a job scheduler. This scheduler regulates
the program and select process from the queue and loads them into memory
for execution. It also regulates the degree of multi-programing. However, the
main goal of this type of scheduler is to offer a balanced mix of jobs, like
Processor, I/O jobs, that allows managing multiprogramming.
3. Short-term scheduling-
Short term scheduling is also known as CPU scheduler. The main goal of
this scheduler is to boost the system performance according to set criteria.
This helps you to select from a group of processes that are ready to execute
and allocates CPU to one of them. The dispatcher gives control of the CPU
to the process selected by the short term scheduler.
in simple words, pick up/select a process out of several ready state to running
state/ allocate the C.P.U to process
STARVATION-
Key concept of this algorithm is "allocate the processor (CPU) in the order
in which the processor arrive".
Example 1
Consider the following set of processes that arrive at time 0, with the
length of CPU Burst time (or run time) given in milliseconds. CPU - burst
time indicates that for how much time, the process needs the CPU.
If the processes arrive in the order P1, P2, P3 and are served in FCFS order,
we get the result in "GRANTT CHART".
The waiting time is 0 millisecond for process P1, 24 milliseconds for process
p2, and 27 milliseconds for process p3.
Thus the average waiting time under FFS policy is generally very long. FIFO
is rarely used on its own but it is often embedded within other schemes.
Advantages-
Disadvantages-
1.) Waiting time can be large if short requests wait behind the long runes.
2.) It is not suitable for time sharing systems where it is important that each
user should get the CPU for an equal amount of time interval.
3.) A proper mix of jobs (I/O based and CPU based jobs) is needed to
achieve good results from FFS scheduling.
4.) Jobs are executed on first come, first serve basis.
5.) It is a non-preemptive, pre-emptive scheduling algorithm.
6.) Easy to understand and implement.
7.) Its implementation is based on FIFO queue.
8.) Poor in performance as average wait time is high.
Amongst the processes in the ready queue, CPU is always assigned to the process
with least CPU burst requirement. SJF reduces average waiting time over FCFS.
When CPU is available it is assigned to the process that has the smallest run time.
If two processes have the same run time, FCFS is used to break the tie. The
shorter the job, the better service it will receive. This tends to reduce the number
of waiting jobs, and also reduces the number of waiting jobs, and also reduces the
number of jobs waiting behind large. As a result, SJF can minimize the average
waiting time of Jobs. The obvious problem with SJF is that it require precise
knowledge of how long a job or process will run. This is also known as shortest
job first, or SJF
Example 2
Consider the set of processes, with the length of CPU burst time given in
milliseconds:
"GRANTT CHART"
Waiting Time is 3 milliseconds for process P1, 16 milliseconds for process P2, 9
3+16+9+0/4=28/4= 7ms
If we were using FCFS policy, Average waiting Time would be 10.25 ms.
Thus SJF algorithm is optimal because it gives the minimum average waiting time.
Disadvantages- (i) The problem is to know the length of time for which CPU is
needed by a
process. A prediction formula can be used to predict the amount of time for
A priority is associated with each process and the CPU is allocated to the process
with highest priority. Equal priorities again are scheduled in FCFS order.
External priorities are set by criteria that are external to Operating system, such
as importance of process, type of process etc. Priority scheduling can be either
pre-emptive or non-preemptive. When a process arrives at ready queue, its
priority is compared with the priority of currently running process. The CPU will
be allocated to the new process if the priority of the newly arrived process is
higher than the priority of the currently running process. On the other hand non-
preemptive priority scheduling will simply put the new process at the head of
ready Queue.
EXAMPLE-
consider the processes:
“grant chart”
6+0+16 + 18 + 1/5=
41/5=8.2 ms
Suppose there are two processes p1 and p2 both have 50 and 5 C.P.U brust
time simultaneously. P1 arrives at 09:00 and p2 arrives at 09:10 so first the
processer is taken by process p1 but in case of pre-emptive processes C.P.U
is taken by process p2(only if the C.P.U brust time of process p2 is less than
p1 (executing process) ) at time 09:05 stops the execution of process p1 in
between.this is shortes remaining time
Example 4
Advantages-
1. it is simple to understand
2. Suitable for Interactive Systems or time sharing systems
Disadvantages-
Renaging - after joining queue and after watching sylagerous behavior of queue
and then leave.
Balking – leave after watching queue without joining the queue is called balking.
A single queue of ready processes is maintained easily. But processes can never
be homogenous in nature. So processes with different priority different c.p.u
brust not easy to maintain into single queue so we divide the types of process by
which we can create queue-
Example 5
Each queue has absolute priority over lower-priority queue. No process in batch
queue can run before system process & so on.
Another possibility is to time slice between the queues. Each queue runs for a
particular time slice.
Advantages-
Disadvantages-
It is inflexible as the processes can never change their queues and thus may have
to starve for the CPU if one or the other higher priority queues are never
becoming empty.
7. BIASED ROUND ROBIN-
In simple words, the process which have low C.P.U brust time that will be
added in between of the high priority processes.
Inter-process communication-
Interprocess communication is the mechanism provided by the operating
system that allows processes to communicate with each other. This
communication could involve a process letting another process know that
some event has occurred or the transferring of data from one process to
another.
COMPUTING RESOURCES-
1. Non-shared resources
2. Shared resources
i. Serially sharable
ii. Parallel sharable
Non-shared resources-these are the computing resources which cannot be
shared in processes these are used only in the defined process.
Example-a variable used in a function(local variable)
Shared resources- these are the computing resources which can be shared in a
processes these can be used by many process depending on their requirement
and the type of the resource.
There are two types of shared resources-
1. Serially sharable-these are sequencially share resources means these
resources are provided to the processes serially.these resources are
provided to processes one by one.
Example-p1-require printer and p2-require printer so the resource is printer
and this one is given to the process in serial firstly to process p1 then to
process p2.
2. Parallely sharable- these are the resources which can be parallel sharable
to more than one processes.
Example- global variables are the resources which can be shared parallely to
processes.
SEMAPHORE-
Semaphores are integer variables that are used to solve the critical section
problem by using two atomic operations, wait and signal that are used for
process synchronization.
The definitions of wait and signal are as follows −
Wait
The wait operation decrements the value of its argument S, if it is positive. If
S is negative or zero, then no operation is performed.
Signal
The signal operation increments the value of its argument S.
Types of Semaphores
There are two main types of semaphores i.e. counting semaphores and binary
semaphores. Details about these are given as follows −
Counting Semaphores
These are integer value semaphores and have an unrestricted value domain.
These semaphores are used to coordinate the resource access, where the
semaphore count is the number of available resources. If the resources are
added, semaphore count automatically incremented and if the resources are
removed, the count is decremented.
Binary Semaphores
The binary semaphores are like counting semaphores but their value is
restricted to 0 and 1. The wait operation only works when the semaphore is
1 and the signal operation succeeds when semaphore is 0. It is sometimes
easier to implement binary semaphores than counting semaphores
Advantages of Semaphores
Some of the advantages of semaphores are as follows −
Semaphores allow only one process into the critical section. They follow the
mutual exclusion principle strictly and are much more efficient than some
other methods of synchronization.
There is no resource wastage because of busy waiting in semaphores as
processor time is not wasted unnecessarily to check if a condition is fulfilled
to allow a process to access the critical section.
Semaphores are implemented in the machine independent code of the
microkernel. So they are machine independent.
Disadvantages of Semaphores
Some of the disadvantages of semaphores are as follows −
Semaphores are complicated so the wait and signal operations must be
implemented in the correct order to prevent deadlocks.
Semaphores are impractical for last scale use as their use leads to loss of
modularity. This happens because the wait and signal operations prevent
the creation of a structured layout for the system.
Semaphores may lead to a priority inversion where low priority processes
may access the critical section first and high priority processes later.
UNIT-4(OPERATING SYSTEM)
DEADLOCK-
Every process needs some resources to complete its execution. However, the resource is granted
in a sequential order.
A Deadlock is a situation where each of the computer process waits for a resource which is
being assigned to some another process. In this situation, none of the process gets executed
since the resource it needs, is held by some other process which is also waiting for some other
resource to be released.
A deadlock is a situation in which two or more processes are unable to proceed because each is
waiting .deadlock can occur in variety of situations, including when two or more processes
request the same resource simultaneously, and when a process holds aresource and requests
another resource that is held by another process.
Let us assume that there are three processes P1, P2 and P3. There are three different resources
R1, R2 and R3. R1 is assigned to P1, R2 is assigned to P2 and R3 is assigned to P3.
After some time, P1 demands for R1 which is being used by P2. P1 halts its execution since it
can't complete without R2. P2 also demands for R3 which is being used by P3. P2 also stops its
execution because it can't continue without R3. P3 also demands for R1 which is being used by
P1 therefore P3 also stops its execution.
In this scenario, a cycle is being formed among the three processes. None of the process is
progressing and they are all waiting. The computer becomes unresponsive since all the
processes got blocked.
Necessary conditions for Deadlocks
Four conditions that must be met for a deadlock to occur in a system.
1. Mutual exclusion
2. Hold and wait
3. No pre-emption
4. Circular wait
1. Mutual Exclusion
A process waits for some resources while holding another resource at the
same time.
3. No pre-emption
The process which once scheduled will be executed till the completion. No
other process can be scheduled by the scheduler meanwhile.no another
can take the resource from the process which is holding the resource
another process have to wait till its execution.
4. Circular Wait
All the processes must be waiting for the resources in a cyclic manner so
that the last process is waiting for the resource which is being held by the
first process.
A set of processes must exist such that each process is waiting for a
resource that is held by another process in the set this creates a cycle of
dependency where no process can proceed, and the system is in deadlock.
It’s worth nothing that these conditions are sufficient but not necessary. The
above four conditions must be met simultaneously, otherwise the system will not
be in a deadlock.
ADDITIONAL TOPIC- single resource entry and multiple resource entry are two
different approaches to managing shared resources in operating system.
SITUATIONS 2-
SITUATION 2-
SITUATION 3-
There are two main approaches for dealing with deadlock in operating system.
1. Deadlock prevention.
2. Deadlock avoidance.
3. Deadlock detection and recovery.
4. Ostrich approach.
The idea behind the approach is very simple that we have to fail one of the four
conditions but there can be a big argument on its physical implementation in the
system.
We can false the hold and wait condtion but if we do that it should affect the 2 nd
objective of operating system(proper utilization of resources) means we don’t
have to hold the resource we have to acquire the resources only when after
holding we don’t have to wait for resources we directly can execute the process.
We can false the circular wait condition by adding some condition that
Allocate all resources types a number-RT1, RT2, RT3….. and then impose a
condition that a process can request a resource in increasing resource type
number.
Means if a process acquire a resource type RT3 then it cann’t demand the RT1
and RT2 so there will no chance to create a cycle so there is no circular wait
condition.
In simple words, The OS reviews each allocation so that the allocation doesn't
cause the deadlock in the system.
There are two states safe state and unsafe state. To avoid deadlock the system
should be in safe state and in unsafe state deadlock may occur or not.
If any process request for any resource then we firstly check that state is safe or
not. If state is safe then we will give the resource that we pretent to give to
process but if state is unsafe then we do not give the resource that we pretend to
give to process.
In safe state decreasing the availability of resource and increasing the allocation
of resource.
1. n=no. of processors
2. m=no. of resource types.
3. Available- A 1-D array keeping information of current availability of all
resource types. Size=m
4. Max-demand (n*m)- a 2-D array indicating the max. demand each
process can raise in respect of each resource type.
5. Allocation- a 2-D array indicating current allocation of each resource
type to all the processes.
2. If request(i)<=availability go to step
Else “wait”.
3. Pretent the resource requested by process p(i) has been allocated by
modifying the data structure.
Undo of step 3=
R1 R2
P1 2 3
P2 5 1
P3 0 2
All values of request need available are given we just have to check
these steps of algorithm and find the state is safe or unsafe.
SAFETY PART-
1. Define two local vectors work(w) and finish(n) of integer and Boolean type
respectively.
2. Initialize work (w in starting)=available AND finish(n in starting)=false for all
process.
For i=1 to n. finish(i) = false.
For i=1 to m. work(i) = available(i).
3. Find an “i”:finish(i)= false AND need(i)<=work.
If no such “i” exist go to step 5.
4. Work=work+allocation(i)
Finish(i)=true.
Go to step 3.
5. If finish(i)=true for all i
Then declare the state is SAFE
Else UNSAFE.
How to solve question on safe part-
Allocation, Max. demand, Need, Avaliable is given we just have to put
available in w and take finish is false for all i and check the condition till the
finish will become true for all I if it will become true then declare the state
safe otherwise it is unsafe state.
Example:
Considering a system with five processes P0 through P4 and three
resources of type A, B, C. Resource type A has 10 instances, B has 5
instances and type C has 7 instances. Suppose at time t0 following
snapshot of the system has been taken:
Question. What will be the content of the Need matrix?
Need [i, j] = Max [i, j] – Allocation [i, j]
So, the content of Need Matrix is:
Is the system in a safe state? If Yes, then what is the safe sequence?
Applying the Safety algorithm on the given system,
.
What will happen if process P1 requests one additional instance of resource
type A and two instances of resource type C?
We use resource request part algorithm-
We must determine whether this new system state is safe. To do so, we again
execute Safety algorithm on the above data structures.
3.Deadlock detection and recovery: If Deadlock prevention or avoidance is not
applied to the software then we can handle this by deadlock detection and
recovery. which consist of two phases:
1. In the first phase, we examine the state of the process and check whether
there is a deadlock or not in the system.
2. If found deadlock in the first phase then we apply the algorithm for recovery
of the deadlock.
In Deadlock detection and recovery, we get the correctness of data but
performance decreases.
4.Ostrich approach-
Deadlock Ignorance is the most widely used approach among all the mechanism.
This is being used by many operating systems mainly for end user uses. In this
approach, the Operating system assumes that deadlock never occurs. It simply
ignores deadlock. This approach is best suitable for a single end user system
where User uses the system only for browsing and all other normal stuff.
In these types of systems, the user has to simply restart the computer in the case
of deadlock. Windows and Linux are mainly using this approach.
There are three ways to access a file into a computer system: Sequential-Access,
Direct Access, Index sequential Method.
SequentialAccess-
It is the simplest access method. Information in the file is processed in order,
one record after the other. This mode of access is by far the most common.
For example if we want to access the 5th file we nust have to access the record
of all previously files.
Another method is direct access method also known as relative access method. A
fixed-length logical record that allows the program to read and write record
rapidly. in no particular order. The direct access is based on the disk model of a
file since disk allows random access to any file block. For direct access, the file is
viewed as a numbered sequence of block or record. Thus, we may read block 14
then block 59, and then we can write block 17. There is no restriction on the
order of reading and writing for a direct access file.
A block number provided by the user to the operating system is normally
a relative block number, the first relative block of the file is 0 and then 1 and so
on.
In simple words, we can directly access the file. Direct allow sequential as well as
direct access.
Example- magnetic disc, magnetic drum, optical disc, disk drum media, hard
disk, floppy disc.
It is the other method of accessing a file that is built on the top of the sequential
access method. These methods construct an index for the file. The index, like an
index in the back of a book, contains the pointer to the various blocks. To find a
record in the file, we first search the index, and then by the help of pointer we
access the file directly.
in simple words, all the electric devices in nature are random access storage
devices. Means we can access all the files present in system in equal time.
Random access allows sequential as well as direct access.
Random access storage devices are fastest and sequential access storage devices
are slowest. In a condition To access all files use sequential access storage
devices or to access selected files use direct or random access storage devices.
To get the benefit of different file systems on the different operating systems, A
hard disk can be divided into the number of partitions of different sizes. The
partitions are also called volumes or mini disks.
Each partition must have at least one directory in which, all the files of the
partition can be listed. A directory entry is maintained for each file in the
directory which stores all the information related to that file.
A directory can be viewed as a file which contains the Meta data of the bunch of
files.
1. File Creation
2. Search for the file
3. File deletion
4. Renaming the file
5. Traversing Files
6. Listing of files
Advantages-
Disadvantages-
Absolute path is the path of the file with respect to the root directory of the
system while relative path is the path with respect to the current working
directory of the system. In tree structured directory systems, the user is given the
privilege to create the files as well as directories.
These kinds of directory graphs can be made using links or aliases. We can have
multiple paths for a same file. Links can either be symbolic (logical) or hard link
(physical).
1. In the case of soft link, the file just gets deleted and we are left with a dangling
pointer.
2. In the case of hard link, the actual file will be deleted only if all the references
to it gets deleted.
All the three methods have their own advantages and disadvantages as
discussed below:
1. Contiguous Allocation
In this scheme, each file occupies a contiguous set of blocks on the disk. For
example, if a file requires n blocks and is given a block b as the starting location,
then the blocks assigned to the file will be: b, b+1, b+2,……b+n-1. This means
that given the starting block address and the length of the file (in terms of blocks
required), we can determine the blocks occupied by the file.
Advantages:
Both the Sequential and Direct Accesses are supported by this. For direct
access, the address of the kth block of the file which starts at block b can
easily be obtained as (b+k).
This is extremely fast since the number of seeks are minimal because of
contiguous allocation of file blocks.
Disadvantages:
This method suffers from both internal and external fragmentation. This
makes it inefficient in terms of memory utilization.
Increasing file size is difficult because it depends on the availability of
contiguous memory at a particular instance.
The file ‘jeep’ in following image shows how the blocks are randomly distributed. The last
block (25) contains -1 indicating a null pointer and does not point to any other block.
Advantages:
This is very flexible in terms of file size. File size can be increased easily since
the system does not have to look for a contiguous chunk of memory.
This method does not suffer from external fragmentation. This makes it
relatively better in terms of memory utilization.
Disadvantages:
Because the file blocks are distributed randomly on the disk, a large number
of seeks are needed to access every block individually. This makes linked
allocation slower.
It does not support random or direct access. We can not directly access the
blocks of a file. A block k of a file can be accessed by traversing k blocks
sequentially (sequential access ) from the starting block of the file via block
pointers.
Pointers required in the linked allocation incur some extra overhead.
3. Indexed Allocation-
In this scheme, a special block known as the Index block contains the
pointers to all the blocks occupied by a file. Each file has its own index block.
The ith entry in the index block contains the disk address of the ith file block.
The directory entry contains the address of the index block as shown in the
image:
Advantages:
This supports direct access to the blocks occupied by the file and therefore
provides fast access to the file blocks.
It overcomes the problem of external fragmentation.
Disadvantages:
The pointer overhead for indexed allocation is greater than linked allocation.
For very small files, say files that expand only 2-3 blocks, the indexed
allocation would keep one entire block (index block) for the pointers which is
inefficient in terms of memory utilization. However, in linked allocation we
lose the space of only 1 pointer per block.
For files that are very large, single index block may not be able to hold all the
pointers.
Following mechanisms can be used to resolve this:
1. Linked scheme: This scheme links two or more index blocks together for
holding the pointers. Every index block would then contain a pointer or the
address to the next index block.
2. Multilevel index: In this policy, a first level index block is used to point to the
second level index blocks which inturn points to the disk blocks occupied by
the file. This can be extended to 3 or more levels depending on the maximum
file size.
3. Combined Scheme: In this scheme, a special block called the Inode
(information Node) contains all the information about the file such as the
name, size, authority, etc and the remaining space of Inode is used to store
the Disk Block addresses which contain the actual file as shown in the image
below. The first few of these pointers in Inode point to the direct blocks i.e
the pointers contain the addresses of the disk blocks that contain data of the
file. The next few pointers point to indirect blocks. Indirect blocks may be
single indirect, double indirect or triple indirect. Single Indirect block is the
disk block that does not contain the file data but the disk address of the
blocks that contain the file data. Similarly, double indirect blocks do not
contain the file data but the disk address of the blocks that contain the
address of the blocks containing the file data.