5CS4-03 Operating System Priyanka Sharma
5CS4-03 Operating System Priyanka Sharma
5CS4-03 Operating System Priyanka Sharma
UNIT - II
UNIT - III
Resource Allocation Graph
n Deadlocks can be described in terms of a
directed graph called a system resource-
allocation graph.
n A set of vertices V and a set of edges E
n Process
n Pi requests instance of Rj
n Pi is holding an instance of Rj
Graph with no cycles
Graph with cycles
Graph with cycles and deadlock
Basic facts
n No Preemption
q If a process that is holding some resources requests
another resource that cannot be immediately allocated to it,
the process releases the resources currently being held.
q Preempted resources are added to the list of resources for
which the process is waiting.
q Process will be restarted only when it can regain its old
resources as well as the new ones that it is requesting.
n Circular Wait
q Impose a total ordering of all resource types.
q Require that processes request resources in increasing
order of enumeration; if a resource of type N is held,
process can only request resources of types > N.
Deadlock Avoidance
P1 10 5 5
P2 4 2 2
P3 9 2 7
P1 3 0 2 0 2 0
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
Deadlock Detection
n Allow system to enter deadlock state
q An algorithm that examines the state of system to
determine whether a deadlock has occurred (
Deadlock Detection algorithms)
q An algorithm to recover from deadlock (Recovery
Scheme)
n Detection Algorithms
q Single Instance of each resource type
n Wait-for graph
q Several instances of a resource type
n Banker’s Algorithm
Single Instance of each resource
type
n Maintain wait-for graph: if graph contain cycle
then deadlock else not.
n Nodes are processes
n Pi Pj if Pi is waiting for Pj.
n Periodically invoke an algorithm that
searches for a cycle in the graph.
n An algorithm to detect a cycle in a graph
requires an order of n^2 operations, where n
is the number of vertices in the graph.
Wait-for-Graph
Several instances of a resource
type
n Banker’s algorithms
n Data Structures used
q Available: Vector of length m. If Available[j] = k, there are k
instances of resource type Rj available.
q Allocation: n m matrix. If Allocation[i,j] = k, then process Pi
is currently allocated k instances of resource type Rj.
q Request : An n m matrix indicates the current request of
each process. If Request [i,j] = k, then process Pi is
requesting k more instances of resource type Rj .
Deadlock Detection Algorithm
n Step 1: Let Work and Finish be vectors of length m and n, respectively.
Initialize
q Work := Available
q go to step 2
n Resource Preemption
q Selecting a victim - minimize cost.
q Rollback
n return to some safe state, restart process from that
state.
q Starvation
n same process may always be picked as victim; include
number of rollback in cost factor.
Combined approach to deadlock
handling
n Combine the three basic approaches
n Prevention
n Avoidance
n Detection
Operating System
Unit - IV
File Management
Priyanka Sharma
Assistant Professor
Department of Computer Science
What is File?
File Management
Examples of files :
hText File
hSource File
hObject File
File Attributes
Parameters used to keep track of files in OS.
hName
hIdentifier
hType
hLocation
hSize
File Attributes (Cont…)
hProtection
hUser Id
File Operations
h Creating a file
− ABC.txt
Reading a file
− read(a)
File Operations (Cont…)
hDeleting a file
hTruncating a file
File Operations (Cont…)
h Append
hRename
hSome other operations can be done on a file by combining the primitive
operations. Such as –
− Copying a file
− Searches the directory, copying the directory entry into open-file table.
− When turns 0 ,then file is no longer in use. (Entry gets removed from table)
− Access rights
File Locking
hEnables access for one process and prevents others.
Source Code c, app, java , perl Set of programs in various programming languages
Multimedia mpeg, mov, mp3, mp4, Binary file containing audio or A/V information
avi
File Structure
− Packing Technique
File Structure (Cont…)
hSome amount of Internal Fragmentation exists.
Example –
− Block Size : 1024 bytes (1KB)
● Sequential Access
● Direct Access
● Other Access Methods
Sequential Access
● 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
Beginning End
Rewind Read/Write
Sequential Access (Cont…)
● File operations :
− Read
− Write
Sequential Access (Cont…)
− Advance the file pointer to the end of newly written material (i.e. new
end of file)
Sequential Access (Cont…)
● Reset
● Skip
● Applications
− Payroll of Employees
● Eg- first read block 50, then read block 24, read then block 87 and so on.
Direct Access (Cont…)
Direct Access
--------------------
--------------------
--------------------
--------------------
Direct Access (Cont…)
● Stores all information about any specific flight in block number identified
by flight number
● Number of seats booked for flight number 100 is stored in block 100 of
reservation file.
Direct Access (Cont…)
● File operations :
− Read
− Write
Direct Access (Cont…)
− n : Block Number
− n : Block Number
− Sequential Access
N = L * (N - 1)
reset cp = 0;
● Applications
− For larger files – index file also required to be too large in memory
● Basic Idea
● Many files stored on same disk.
● How to allocate space to these file
● Aim
● Effective utilization of disk space
● Quick access to files
File Allocation Methods
● Contiguous Allocation
● Linked Allocation
● Indexed Allocation
Contiguous Allocation
● First access block b, then accessing block b+1 does not require any head
movement.
Contiguous Allocation (Cont…)
Contiguous Allocation (Cont…)
● Advantages
− Easy to implement
− Fast Access
● Disdvantages
− Internal Fragmentation
− External Fragmentation
Linked Allocation
● Directory contains :
● Advantages
− No External Fragmentation
● Disdvantages
− Slow Access
Indexed Allocation
● Index of Addresses
Indexed Allocation (Cont…)
● Directory contains :
− Address of index block
● Advantages
− No External Fragmentation
Directory
F1 F2 F4
Files
F3 F 45
Directory (Cont…)
− Type
− Address
− Current length
− Maximum length
− Owner ID
− Protection information
Directory (Cont…)
● Operation Performed on a Directory
− Create a file
− Delete a file
− List a directory
− Rename a file
h Grouping – logical grouping of files by properties, (e.g., all Java programs, all games, …)
Types of Directory
● Single-Level Directory
● Two-Level Directory
● Advantages
− Easy Implementation
● Limitations
− Naming Problem
Two Level Directory
● When user refers to a specific file, only his own UFD is searched.
Two Level Directory (Cont…)
Two Level Directory (Cont…)
● Advantages
− Searching of files become more easy due to path name and user-grouping.
● Disadvantages
− Still it not very scalable, two files of the same type cannot be grouped together in the
same user.
Tree - Structured Directory
● A directory contains :
− Contains most of the files that are of current interest to the process.
● Disadvantages
− Every file does not fit into the hierarchical model, files may be saved into multiple
directories.
mkdir <dir-name>
Example: if in current directory /mail
mkdir count
Note - Deleting “mail” will
mail delete he entire sub-tree rooted
by “mail”.
● Advantages
● Disadvantages
− We share the files via linking, in case of deleting it may create the problem,
General Graph Directory
● where multiple directories can be derived from more than one parent
directory.
− Allows cycles
● Disadvantages
− Costly
possible.
● Free Space List : Keeps track of all free disk spaces. (not those allocated
● To create a file :
● When a file is deleted, its disk space is added to free space list.
Implementing Free Space List
● Advantages :
− Simplicity
− Efficiency
● Another approach :
used.
● Mount Point :
− Location within the file structure where the file system is to be attached.
− An Empty Directory
Mounting (Cont…)
● Example : In Unix File System, a file system containing a user’s home
directories might be mounted as /home.
● Remote file systems add new failure modes, due to network failure, server
failure
● Recovery from failure can involve state information about status of each
remote request
● Types of access
− Read
− Write
− Execute
− Append
− Delete
− List
Access Control List
● Specify user names and types of access allowed for each user.
● When a user requests a specific file, OS checks ACL associated with that
file.
● If the user is listed for the requested access, the access is allowed,
Otherwise denied.
1.2
STRUCTURAL OVERVIEW
Clear separation between user and kernel
portions was the big difference between
Unix and contemporary systems —only
the essential features inside OS, not the
editors, command interpreters, compilers,
etc.
3.
FILE ABSTRACTION
File as an unstructured sequence of bytes which was relatively unusual at the time:
most systems lent towards files being composed of records
Cons: don't get nice type information; programmer must worry about format of
things inside file
Pros: less stuff to worry about in the kernel; and programmer has flexibility to
choose format within file!
4.2
FILE
OPERATIONS
Operations on files are:
fd = open(pathname, mode)
fd = creat(pathname, mode)
bytes = read(fd, buffer, nbytes)
count = write(fd, buffer, nbytes)
reply = seek(fd, offset, whence)
reply = close(fd)
The kernel keeps track of the current position within the file
4.3
DIRECTORY
HIERARCHY
Directories map names to files (and
directories) starting from distinguished root
directory called /
4.4
FILE SYSTEM
IMPLEMENTATION
4.6
DIRECTORIES AND
LINKS
Directory is (just) a file which
maps filenames to i-nodes —
that is, it has its own i-node
pointing to its contents
An instance of a file in a
directory is a (hard) link hence
the reference count in the i-
node. Directories can have at
most 1 (real) link. Why?
4.8
ON-DISK
STRUCTURES
A disk consists of a boot block followed by one or more partitions. Very old disks
would have just a single partition. Nowadays have a boot block containing a
partition table allowing OS to determine where the filesystems are
Figure shows two completely independent filesystems; this is not replication for
redundancy. Also note |inode table| ≫|superblock|; |data blocks| ≫|inode table|
4.9
ON-DISK
STRUCTURES
A partition is just a contiguous range of N fixed-size blocks of size k for some N and
k, and a Unix filesystem resides within a partition
Common block sizes: 512B, 1kB, 2kB, 4kB, 8kB
4 . 10
MOUNTING FILE
SYSTEMS
Entire filesystems can be
mounted on an existing
directory in an already mounted
filesystem
Cannot have hard links across mount points: why? What about soft links?
4 . 11
IN-MEMORY
TABLES
Recall process sees files as file
descriptors
4 . 12
ACCESS
CONTROL
Three bits for each of owner, group and world: read, write and execute
What do these mean for directories? Read entry, write entry, traverse directory
4 . 13
CONSISTENCY
ISSUES
To delete a file, use the unlink system call —from the shell, this is rm
<filename>
Procedure is:
Check if user has su cient permissions on the file (must have write access)
Check if user has su cient permissions on the directory (must have write access)
If ok, remove entry from directory
Decrement reference count on inode
If now zero: free data blocks and free inode
If crash: must check entire filesystem for any block unreferenced and any block
double referenced
4 . 14
I/O
IMPLEMENTATION
Everything accessed via the file system
Two broad categories: block and character; ignoring low-level gore:
Character IO low rate but complex —most functionality is in the "cooked"
interface
Block IO simpler but performance matters —emphasis on the buffer cache
5.2
THE BUFFER
CACHE
Basic idea: keep copy of some parts of disk in memory for speed
On read do:
Locate relevant blocks (from inode)
Check if in buffer cache
If not, read from disk into memory
Return data from buffer cache
On write do same first three, and then update version in cache, not on disk
5.3
UNIX
PROCESSES
Recall: a process is a program in execution
Processes have three segments: text, data
and stack. Unix processes are heavyweight
Text: holds the machine instructions for the
program
6.2
UNIX PROCESS
DYNAMICS
Process is represented by an opaque process id (pid), organised hierarchically with
parents creating children. Four basic operations:
pid = fork ()
reply = execve(pathname, argv, envp)
exit(status)
pid = wait(status)
6.3
UNIX PROCESS STATES
ru = running rk = running
(user- (kernel-
mode) mode)
z = zombie p = pre-
empte
d
sl = sleeping rb = runnable
c = created
NB. This is simplified —see Concurrent
Systems section 23.14 for detailed
descriptions of all states/transitions
6.7
THE SHELL
Introduction
Design Principles
Filesystem
IO
Processes
The Shell
Examples, Standard
IO
Summary
7.1
THE
SHELL
Shell just a process like everything else.
Needn't understand commands, just files
7.2
SHELL
EXAMPLES
$ pwd
/Users/mort/src
$ ls -F
awk-scripts/ karaka/ ocamllint/ sh-scripts/
backup-scripts/ mrt.0/ opensharingtoolkit/ sockman/
bib2x-0.9.1/ ocal/ pandoc-templates/ tex/
c-utils/ ocaml/ pttcp/ tmp/
dtrace/ ocaml-libs/ pyrt/ uon/
exapraxia-gae/ ocaml-mrt/ python-scripts/ vbox-bridge/
external/ ocaml-pst/ r/
junk/ ocaml.org/ scrapers/
$ cd python-scripts/
/Users/mort/src/python-scripts
$ ls -lF
total 224
-rw-r--r-- 1 mort staff 17987 2 Jan 2010 LICENSE
-rw-rw-r-- 1 mort staff 1692 5 Jan 09:18 README.md
-rwxr-xr-x 1 mort staff 6206 2 Dec 2013 bberry.py*
-rwxr-xr-x 1 mort staff 7286 14 Jul 2015 bib2json.py*
-rwxr-xr-x 1 mort staff 7205 2 Dec 2013 cal.py*
-rw-r--r-- 1 mort staff 1860 2 Dec 2013 cc4unifdef.py
-rwxr-xr-x 1 mort staff 1153 2 Dec 2013 filebomb.py*
7.3
MAIN UNIX
FEATURES
File abstraction
A file is an unstructured sequence of bytes
(Not really true for device and directory files)
Hierarchical namespace
Directed acyclic graph (if exclude soft links)
Thus can recursively mount filesystems
Heavy-weight processes
IO: block and character
Dynamic priority scheduling
Base priority level for all processes
Priority is lowered if process gets to run
Over time, the past is forgotten
But V7 had inflexible IPC, ine cient memory management, and poor kernel
concurrency
Later versions address these issues.
8.2
Case Study : LINUX
UNIX/Linux Goals
• Simple
• Elegant
• Consistent
• Powerful
• Flexible
Interfaces to Linux
• Real-time FIFO.
• Real-time round robin.
• Timesharing.
Scheduling in Linux (2)
Fig: The Linux I/O system showing one file system in detail.
The Linux File System (1)
The Linux File System (2)