Unit 3 and 4 notes OS
Unit 3 and 4 notes OS
3.1 Deadlocks:
3.1.1 Concept:
Deadlock is a situation where a set of processes are blocked because each process is
holding a resource and waiting for another resource acquired by some other process.
Consider an example when two trains are coming toward each other on the same track and
there is only one track, none of the trains can move once they are in front of each other.
A similar situation occurs in operating systems when there are two or more processes that
hold some resources and wait for resources held by other(s).
EXAMPLE OF DEADLOCK
A real-world example would be traffic, which is going only in one direction. Here, a bridge
is considered a resource. So, when Deadlock happens, it can be easily resolved if one car
backs up (Preempt resources and rollback). Several cars may have to be backed up if a
deadlock situation occurs. So starvation is possible.
P1
R1 R2
P2
For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2
which is acquired by process 2, and process 2 is waiting for resource 1.
There should be a resource that can only be held by one process at a time. In the diagram below,
there is a single instance of Resource 1 and it is held by Process 1 only.
2
2. Hold and Wait: A process is holding at least one resource and waiting for resources
A process can hold multiple resources and still request more resources from other processes
which are holding them. In the diagram given below, Process 2 holds Resource 2 and Resource 3
and is requesting the Resource 1
3. No Preemption: A resource cannot be taken from a process unless the process releases the
resource.
A resource cannot be preempted from a process by force. A process can only release a resource
voluntarily. In the diagram below, Process 2 cannot preempt Resource 1 from Process 1. It will
only be released when Process 1 relinquishes it voluntarily after its execution is complete.
4. Circular Wait: A set of processes are waiting for each other in circular form.
A process is waiting for the resource held by the second process, which is waiting for the resource
held by the third process and so on, till the last process is waiting for a resource held by the first
process. This forms a circular chain.
For example: Process 1 is allocated Resource2 and it is requesting Resource 1. Similarly, Process
2 is allocated Resource 1 and it is requesting Resource 2. This forms a circular wait loop.
3
3.1.4 CONCEPTS OF DEADLOCK STRATEGIES
DEADLOCK IGNORANCE
• Ignore the problem altogether: Most widely used approach among all the mechanism.
1. All the processes that are involved in the deadlock are terminated. This is not a good
approach as all the progress made by the processes is destroyed.
2. Resources can be preempted from some processes and given to others till the
deadlock is resolved.
DEADLOCK RECOVERY
• This approach let the processes fall in deadlock and then periodically check whether
deadlock occur in the system or not. If it occurs then it applies some of the recovery
methods to the system to get rid of deadlock.
1. Suspend/Resume a process:
– A process is selected based on a variety of criteria (low priority) and it is
suspended for a long time. The resources are reclaimed from that process and
4
then allocated to other processes that are waiting for them. When one of the
waiting processes gets over, the original suspended process is resumed.
2. Kill a process:
– The Operating system decides to kill a process and reclaim all its resources after
ensuring that such action will solve the deadlock. This solution is simple but
involves loss of at least one process.
DEADLOCK PREVENTION
• The idea is to not let the system into a deadlock state.
DEADLOCK AVOIDANCE
• Avoidance is kind of futuristic in nature.
• By using strategy of “Avoidance”, an assumption is made.
• Need to ensure that all information about resources which process will require is known
to prior to execution of the process.
• Banker’s algorithm is used to avoid deadlock.
• In deadlock avoidance, the operating system checks whether the system is in safe state
or in unsafe state at every step which the operating system performs.
• The process continues until the system is in safe state. Once the system moves to unsafe
state, the OS has to backtrack one step.
• In simple words, The OS reviews each allocation so that the allocation doesn't cause the
deadlock in the system.
5
UNIT III- 3.2 INFORMATION MANAGEMENT
File Structure
A file has various kinds of structure. Some of them can be:
1. Simple Record Structure with lines of fixed or variable lengths.
2. Complex Structures like formatted document or reloadable load files.
3. No Definite Structure like sequence of words and bytes etc.
File Attributes
Following are some of the attributes of a file:
Name: It is the only information which is in human-readable form.
Identifier: The file is identified by a unique tag(number) within file system.
Type: It is needed for systems that support different types of files.
Location: Pointer to file location on device.
Size: The current size of the file.
Protection: This controls and assigns the power of reading, writing, executing.
Time, date, and user identification. This is the data for protection, security, and usage
monitoring.
The way that files are accessed and read into memory is determined by Access methods.
Usually a single access method is supported by systems while there are OS's that support
multiple access methods.
1. Sequential Access
Most of the operating systems access the file sequentially. In other words, we can say
that most of the files need to be accessed sequentially by the operating system.
6
In sequential access, the OS read the file word by word. A pointer is maintained which
initially points to the base address of the file. If the user wants to read first word of the file
then the pointer provides that word to the user and increases its value by 1 word. This
process continues till the end of the file.
Modern word systems do provide the concept of direct access and indexed access but
the most used method is sequential access due to the fact that most of the files such as
text files, audio files, video files, etc. need to be sequentially accessed.
The Direct Access is mostly required in the case of database systems. In most of the
cases, we need filtered information from the database. The sequential access can be
very slow and inefficient in such cases.
Suppose every block of the storage stores 4 records and we know that the record we
needed is stored in 10th block. In that case, the sequential access will not be implemented
because it will traverse all the blocks in order to access the needed record.
Direct access will give the required result despite of the fact that the operating system
has to perform some complex tasks such as determining the desired block number.
However, that is generally implemented in database applications.
7
3. Indexed Sequential Access
What is a Directory?
Information about files is maintained by Directories. A directory can contain multiple files.
It can even have directories inside of them. In Windows we also call these directories as
folders.
Following is the information maintained in a directory:
8
a) SINGLE-LEVEL DIRECTORY
• Single level directory is simplest directory structure. In it all files are contained in same
directory which make it easy to support and understand.
• A single level directory has a significant limitation, however, when the number of files
increases or when the system has more than one user. Since all the files are in the same
directory, they must have the unique name. if two users call their dataset test, then the
unique name rule violated.
• Advantages:
– Since it is a single directory, so its implementation is very easy.
– If the files are smaller in size, searching will become faster.
– The operations like file creation, searching, deletion, updating are very easy in such
a directory structure.
• Disadvantages:
– There may chance of name collision because two files cannot have the same name.
– Searching will become time taking if the directory is large.
b) TWO-LEVEL DIRECTORY
• A single level directory often leads to confusion of files names among different users. the
solution to this problem is to create a separate directory for each user.
• In the two-level directory structure, each user has their own user files directory (UFD). The
UFDs has similar structures, but each list only the files of a single user. system’s master
file directory (MFD) is searches whenever a new user id=s logged in. The MFD is indexed
by username or account number, and each entry points to the UFD for that user.
• Advantages:
– We can give full path like /User-name/directory-name/.
– Different users can have same directory as well as file name.
– Searching of files become easier due to path name and user-grouping.
9
• Disadvantages:
– A user is not allowed to share files with other users.
– Still it not very scalable, two files of the same type cannot be grouped together in
the same us
c) TREE-STRUCTURED DIRECTORY
• Once we have seen a two-level directory as a tree of height 2, the natural generalization is
to extend the directory structure to a tree of arbitrary height. This generalization allows the
user to create their own subdirectories and to organize on their files accordingly.
• The tree has a root directory, and every file in the system have a unique path.
• Advantages:
– Very generalize, since full path name can be given.
– Very scalable, the probability of name collision is less.
– Searching becomes very easy, we can use both absolute path as well as relative.
• Disadvantages:
– Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
– We cannot share files.
– It is inefficient, because accessing a file may go under multiple directories.
----------------------------------------------------****---------------------------------------------------------
ACCESS PATHS
• An Access Path is the directory path required to access a file. Access Paths are used by the
IDE in search operations to find files. In particular, Access Paths are used to find the
library, source, and header files in a Build Target. If the compiler can’t find a file used in
the Build Target, your program will not compile. Essentially these are the Paths to where
your sources/libraries are located on the computer. The access path is the
location Microsoft DOS or Windows looks when a command is not an internal command
or in the current directory.
• For example, when typing in "fdisk" as a command, if the access path is not set to the
location of fdisk.exe, you'll receive a "bad command or file name" error message. Even if
this file exists elsewhere on the computer.
10
DIRECTORY OPERATIONS
• Create
– A directory is created. It is empty except for dot and dotdot, which are put there
automatically by the system.
• Delete
– A directory is delete. Here, only those directory can be deleted which are empty.
• Opendir
– Directories can be read. But before reading any directory, it must be opened first.
Therefore, to list all the files present in a directory, a listing program opens that
required directory to read out the name of all files that this directory contains.
• Closedir
– Directory should be closed just to free up the internal table space when it has been
read.
• Readdir
– This call returns the next entry in an open directory.
• Rename
– Directory can also be renamed just like the files.
• Link
– Linking is a technique that allows a file to appear in more than one directory.
• Unlink
– A directory entry is removed.
------------------------------------------------------*****----------------------------------------------------
Operations like renaming, editing the existing file, copying; these can also be controlled.
There are many protection mechanisms. each of them mechanism have different
advantages and disadvantages and must be appropriate for the intended application.
Access Control:
There are different methods used by different users to access any file. The general
way of protection is to associate identity-dependent access with all the files and
directories an list called access-control list (ACL) which specify the names of the
users and the types of access associate with each of the user.
The main problem with the access list is their length. If we want to allow everyone
to read a file, we must list all the users with the read access.
To condense the length of the access-control list, many systems recognize three
classification of users in connection with each file:
• Owner –Owner is the user who has created the file.
• Group –A group is a set of members who has similar needs and they are
sharing the same file.
• Universe –In the system, all other users are under the category called universe.
The most common recent approach is to combine access-control lists with the
normal general owner, group, and universe access control scheme. For example:
Solaris uses the three categories of access by default but allows access-control
lists to be added to specific files and directories when more fine-grained access
control is desired.
-----------------------------------------------------*****----------------------------------------------------
12
UNIT IV- MEMORY MANAGEMENT
4.1 Functions of Memory Management
• To keep track of all memory locations-free or allocated and if allocated, to which
process and how much.
• To decide the memory allocation policy i.e. which process should get how much
memory, when and where.
• To use various techniques and algorithms to allocate and de-allocate memory
locations.
4.1.1 Issues in memory management scheme
Premature frees and dangling pointers
Many programs give up memory, but attempt to access it later crash or behave randomly.
This condition is known as a PREMATURE FREE, and the surviving reference to the
memory is known as a DANGLING POINTER. This is usually confined to manual
memory management.
Memory leak
Some programs continually allocate memory without ever giving it up and eventually run
out of memory. This condition is known as a MEMORY LEAK.
External fragmentation
A poor allocator can do its job of giving out and receiving blocks of memory so badly that
it can no longer give out big enough blocks despite having enough spare memory. This
is because the free memory can become split into many small blocks, separated by
blocks still in use. This condition is known as EXTERNAL FRAGMENTATION.
Poor locality of reference
Another problem with the layout of allocated blocks comes from the way that modern
hardware and operating system memory managers handle memory: successive memory
accesses are faster if they are to nearby memory locations. If the memory manager
places far apart the blocks a program will use together, then this will cause performance
problems. This condition is known as poor LOCALITY OF REFERENCE.
Relocation and address translation
Relocation and Address Translation refers to the problem that arises because at the time
of compilation, the exact physical memory locations that a program is going to occupy at
the runtime are not known. Therefore the compiler generates the executable machine
code assuming that each program is going to be loaded from memory word 0. At the
execution time, the program may need to be relocated to different locations, and all the
addresses will need to be changed before execution.
14
• At any time one of them runs in the main memory.
• When this process is blocked, it is 'swapped out' from the main memory to the
disk.
• The next highest priority process is 'swapped in' the main memory from the disk
and it starts.
• Thus, there is only one process in the main memory even if conceptually it is a
multi-programming system.
• For example, MS-DOS operating system allocates memory in this way.
• The Operating System is loaded in the memory. All the rest of the memory is free.
• A program P I is loaded in the memory and it starts executing (after which it
becomes a process).
• A program P2 is loaded in the memory and it starts executing (after which it
becomes a process).
• A program P3 is loaded in the memory and it starts executing (after which it
becomes a process).
• The process PI is blocked at certain time. After a while, a new high priority program
P4 wants to occupy the memory.
16
• Let us assume that P4 is smaller than PI but bigger than the free area available at
the bottom.
• P4 is now loaded in the memory and it starts executing (after which it becomes a
process).
• Note that P4 is loaded in the same space where PI was loaded.
• After sometime P2 is completed whereas P3 and P4 continue.
• The area at the top and one released by P2 can be joined together to form a larger
partition.
• P1 is swapped in when the input output is completed.
• Another process P5 is also loaded in the memory.
Advantages:
1. No Internal Fragmentation: In variable Partitioning, space in main memory is allocated
strictly according to the need of process, hence there is no case of internal
fragmentation. There will be no unused space left in the partition.
Disadvantages:
• Difficult Implementation: Implementing variable Partitioning is difficult as compared to
Fixed Partitioning as it involves allocation of memory during run-time rather than during
system configure.
• External Fragmentation: There will be external fragmentation in spite of absence of
internal fragmentation. For example, process P1(2MB) and process P3(1MB) completed
their execution. Hence two spaces are left i.e. 2MB and 1MB. Let’s suppose process P5 of
size 3MB comes. The empty space in memory cannot be allocated as no spanning is
allowed in contiguous allocation. The rule says that process must be contiguously present
in main memory to get executed. Hence it results in External Fragmentation.
4.2.4 Fragmentation
1. Internal Fragmentation
• Internal Fragmentation is found in fixed partition scheme.
• To overcome the problem of internal fragmentation, instead of fixed partition
scheme, variable partition scheme is used.
17
2. External Fragmentation
• External Fragmentation is found in variable partition scheme.
• To overcome the problem of external fragmentation, compaction technique is used
or non-contiguous memory management techniques are used.
19
Address translation in paging
20
• To run a program of size n pages, need to find n free frames and load program.
• This is achieved by a register called Page Map Table Limit Register (PMTLR)
• It contains the no. of pages contained in a process.
• There is 1 PMTLR for each PMT
• PMTLR is present inside the PCB of each process
MERITS:
1. Simple method to implement
2. Inexpensive
22
DEMERITS:
1. Slow process
2. Additional memory reference is required to find PMT
Advantages of Paging:
1. Easy to allocate memory
2. Easy to swap — pages, frames, and often disk blocks as well, all are same size.
3. This table lookup adds an extra memory reference for every address translation.
4.3.2 Segmentation
• A segment can be defined as a logical grouping of instructions, such as a subroutine,
array, or a data area. A program is a collection of segments. Segmentation is a
technique for managing these segments. Divide a process into unequal size blocks called
segments. Each segment has a name and a length.
Advantages:
• Protect each entity independently
• Allow each segment to grow independently
• Share each segment independently
• Each of these segments are of variable
length and length will be defined in the
program.
23
Segmentation Architecture
• Segment Table
• If the offset is not within the limit, trap to the operating system. (logical address beyond
end of segment).
24
Segmentation Example:
Address Translation
25
Advantages and Disadvantages of Segmentation
• Advantages:
– Each segment can be
1. located independently
2. separately protected
3. grow independently
4. Segments can be shared between processes.
5. Eliminate the fragmentation.
• Disadvantages:
– Allocation algorithms as for memory partitions
– External fragmentation, Solution: combine segmentation and paging.
26
4.4 Concept of Virtual Memory
• A computer can address more memory than the amount physically installed on the
system. This extra memory is actually called virtual memory and it is a section of a hard
disk that's set up to emulate the computer's RAM.
• The main visible advantage of this scheme is that programs can be larger than physical
memory.
• Virtual memory serves two purposes.
– First, it allows us to extend the use of physical memory by using disk.
– Second, it allows us to have memory protection, because each virtual address is
translated to a physical address.
4.4.2 Definitions
Locality of Reference
• Locality of Reference refers to a phenomenon in which a computer program tends to
access same set of memory locations for a particular time period.
• Locality of Reference refers to the tendency of the computer program to access
instructions whose addresses are near one another.
• The property of locality of reference is mainly shown by loops and subroutine calls in a
program.
Working Set
• Working set is a concept in computer science which defines the amount of memory that
a process requires in a given time interval.
• The set of pages that a process is currently using is called its working set
27
Dirty Page/Dirty Bit
• Dirty page is the page in memory (page cache) that have been changed from what is
currently stored on disk. This usually happens when an existing file on the disk is altered
or appended.
• Dirty bit: one bit for each page frame, set by hardware whenever the page is modified.
If a dirty page is replaced, it must be written to disk before its page frame is reused.
Demand Paging
• Demand paging is a method of virtual memory management.
• In a system that uses demand paging, the operating system copies a disk page into
physical memory only if an attempt is made to access it and that page is not already in
memory (i.e., if a page fault occurs).
• It follows that a process begins execution with none of its pages in physical memory and
many page faults will occur until most of a process's working set of pages are located in
physical memory.
• Demand paging follows that pages should only be brought into memory if the executing
process demands them.
DEADLOCKS:
28
3. What are the activities involved in Information management? 3 marks
4. What do you mean by file structure? 3 marks
5. Explain the various attributes of a file? 3/ 6 marks
6. Explain the various File access methods in detail. 3/6 marks
7. Explain the following File access methods:
a) Sequential file access
b) Direct access
c) Indexed Sequential access 3/6 marks
8. What do you mean by Directory and Directory structure. 3/ 6marks
9. Explain the information maintained in a directory. 3/6 marks
10. Explain hierarchical Directory system with the help of a suitable diagram. 3/ 6 marks
11. What do you mean by Access Paths? 3 marks
12. Explain the various directory operations in detail. 3/6 marks
13. Why file protection is important in file system? 3 marks
14. What are the various access types of file? 3/6 marks
15. What do you mean by access control? Explain in detail. 3/ 6marks
30