5CS4-03 Operating System Priyanka Sharma

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

COURSE NOTES

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 V is partitioned into 2 types


n P = {P1, P2,…,Pn} - the set of processes in the system
n R = {R1, R2,…,Rn} - the set of resource types in the
system
n Two kinds of edges
n Request edge - Directed edge Pi ---> Rj
n Assignment edge - Directed edge Rj ----> Pi
Resource Allocation Graph

n Process

n Resource type with 4 instances

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 If graph contains no cycles


q NO DEADLOCK
n If graph contains a cycle
q if only one instance per resource type, then
deadlock
q if several instances per resource type, possibility
of deadlock.
Methods for handling deadlocks

n There are three ways to handle the deadlock


1. Use protocols to prevent or avoid deadlocks,
ensuring that the system will never enter a
deadlock state.
2. Allow the system to potentially enter a deadlock
state, detect it and then recover.
3. Ignore the problem and imagine that deadlocks
never occur in the system;
q Used by many operating systems, e.g. UNIX and Windows
Deadlock Management
q Prevention
q Design the system in such a way that deadlocks can never
occur. i.e ensure that at least one of necessary conditions can’t
hold.
q Avoidance
q Impose less stringent conditions than for prevention, OS be
given in advance additional information concerning which
resource a process will request and use during its lifetime.
allowing the possibility of deadlock but bypassing it as it occurs.
q Detection
q Allow possibility of deadlock, determine if deadlock has
occurred and which processes and resources are involved.
q Recovery
q After detection, clear the problem, allow processes to complete
and resources to be reused. May involve destroying and
restarting processes.
Deadlock Prevention

q If any one of the conditions for deadlock (with


reusable resources) is denied, deadlock is
impossible.
q Restrain ways in which requests can be made
n Mutual Exclusion
q non-issue for sharable resources. e.g reading a file
q It must hold for non-sharable resources. e.g Printer
n Hold and Wait - guarantee that when a process requests
a resource, it does not hold other resources.
q Force each process to acquire all the required resources at
once. Process cannot proceed until all resources have
been acquired.
q Low resource utilization, starvation possible
Deadlock Prevention (cont.)

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

n Requires that the system has some additional


apriori information available.
q Simplest and most useful model requires that each process
declare the maximum number of resources of each type
that it may need.
q The deadlock-avoidance algorithm dynamically examines
the resource-allocation state to ensure that there can never
be a circular-wait condition.
q Resource allocation state is defined by the number of
available and allocated resources, and the maximum needs
of the processes.
Safe state
n When a process requests an available resource, system must
decide if immediate allocation leaves the system in a safe
state.
n System is in safe state if there exists a safe sequence of all
processes.
n A Sequence of processes <P1, P2, …Pn> is safe, if for each
Pi, the resources request that P i can be satisfied by currently
available resources + resources held by P j with j<i.
q If Pi resource needs are not available, Pi can wait until all Pj have
finished.
q When Pj is finished, Pi can obtain needed resources, execute,
return allocated resources, and terminate.
q When Pi terminates, Pi+1 can obtain its needed resources...
Safe State-Example
n Total Resources=12

Process Maximum Demand Allocated Need

P1 10 5 5
P2 4 2 2
P3 9 2 7

n At time t0 system in safe state and safe sequence is


<P2,P1,P3>
n At time t1 process P3 need one more resource and it is
allocated to P3, then system in unsafe state--deadlocked
Basic Facts
n If a system is in a safe state no deadlocks.
n If a system is in unsafe state possibility of
deadlock.
n Avoidance ensure that a system will never
reach an unsafe state.
n Deadlock Avoidance algorithms
q Resource Allocation Algorithms:
n Use for single instance of resource
q Banker’s Algorithms
n Use for Multiple instance of resource
Resource Allocation Graph
Algorithm
n Used for deadlock avoidance when there is
only one instance of each resource type.
q Claim edge: Pi Rj indicates that process Pi may request
resource Rj; represented by a dashed line.
q Claim edge converts to request edge when a process
requests a resource.
q Resources must be claimed a priori in the system.
n If request assignment does not result in the formation of
a cycle in the resource allocation graph - safe state, else
unsafe state.
Claim Graph
Claim Graph
Banker’s Algorithm
n Used for multiple instances of each resource type.
n Name was chosen because this algorithm could be used in
banking system:
q To ensure that the bank never allocated its available cash in such a
way that it could no longer satisfy the needs of all its customers.
n Each process must a priori claim maximum use of each
resource type.
n When a process requests a resource then the system must
determine whether the allocation of these resources will
leave the system in safe state:
q If yes then then resources are allocated to requested process.
q Else it must wait until some other process release enough resources.
n When a process gets all its resources it must return them in
a finite amount of time.
Data Structures for the Banker’s
Algorithm
n Let n = number of processes and m = number of
resource types.
q Available: AVector of length m indicates the no. of available
resources of each type. If Available[j] = k, there are k instances of
resource type Rj available.
q Max: An n m matrix define maximum demand of each process.
If Max[i,j] = k, then process Pi may request at most k instances of
resource type Rj.
q Allocation: An n m matrix define the no. of resources of each
type currently allocated to each process. If Allocation[i,j] = k, then
process Pi is currently allocated k instances of resource type Rj.
q Need: An n m matrix indicate the remaining resource need of
each process. If Need[i,j] = k, then process Pi may need k more
instances of resource type Rj to complete its task.
Need[i,j] = Max[i,j] - Allocation[i,j]
Safety Algorithm
n Let Work and Finish be vectors of length m and n,
respectively. Initialize
q Work := Available
q Finish[i] := false for i = 1,2,…,n.
n Find an i (i.e. process Pi) such that both:
q Finish[i] == false
q Need_i <= Work
q If no such i exists, go to step 4.
n Work := Work + Allocation_i
q Finish[i] := true
q go to step 2
n If Finish[i] = true for all i, then the system is in a safe
state.
Resource-Request Algorithm for
Process Pi
n Determines whether the request is safely granted or not
n Request_i = request vector for process Pi. If Request_i[j] = k, then process
Pi wants k instances of resource type Rj.
q STEP 1: If Request(i) £ Need(i), go to step 2. Otherwise, raise error
condition, since process has exceeded its maximum claim.
q STEP 2: If Request(i) £ Available, go to step 3. Otherwise, Pi must wait
since resources are not available.
q STEP 3: Pretend to allocate requested resources to Pi by modifying the
state as follows:
Available := Available - Request (i);
Allocation (i) := Allocation (i) + Request (i);
Need (i) := Need (i) - Request (i);
q If safe resources are allocated to Pi.
q If unsafe Pi must wait and the old resource-allocation state is
restored.
Example of Banker’s Algorithm
n 5 processes Allocation Max Available
q P0 - P4; A B C A B C A B C
n 3 resource types P0 0 1 0 7 5 3 3 3 2
q A(10 instances), B (5 instances),
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
and C (7 instances) P3 2 1 1 2 2 2
n Snapshot at time T0 P4 0 0 2 4 3 3
n The content of the matrix Need is defined to be Max -
Allocation.
Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

n The system is in a safe state since the sequence


<P1,P3,P4,P2,P0> satisfies safety criteria
Example: P1 requests (1,0,2)
n Check to see that Request £ Need
q ((1,0,2) £ (1,2,2)) true.

n Check to see that Request £ Available


n ((1,0,2) £ (3,3,2)) true.
n Now change Need, Allocation and Available
n Executing the safety algorithm shows that sequence <P1, P3, P4,
P0, P2> satisfies safety requirement.
n Can request for (3,3,0) by P4 be granted? Allocation Need Available
A B C A B C A B C
n Can request for (0,2,0) by P0 be granted? P0 0 1 0 7 4 3 2 3 0

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 For i = 1,2,…,n, if Allocation(i) 0, then Finish[i] := false, otherwise


Finish[i] := true.
n Step 2: Find an index i such that both:
q Finish[i] = false

q Request (i) Work

q If no such i exists, go to step 4.

n Step 3: Work := Work + Allocation(i)


q Finish[i] := true

q go to step 2

n Step 4: If Finish[i] = false for some i, 1 i n, then the system is in a deadlock


state. Moreover, if Finish[i] = false, then Pi is deadlocked.

Algorithm requires an order of m (n^2) operations to detect whether the system is in


a deadlocked state.
Example of Detection Algorithm
n 5 processes - P0 - P4; 3 resource types - A(7 instances), B(2 instances),
C(6 instances)
n Snapshot at time T 0: <P0,P2,P3,P1,P4> will result in Finish[i] = true for all i.
n P2 requests an additional instance of type C.
n State of system
q Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes’ requests.
q Deadlock exists, consisting of P 1,P 2,P 3 and P 4.

Allocation Request Available


Request
A B C A B C A B C
A B C
P0 0 1 0 0 0 0 0 0 0
P0 0 0 0
P1 2 0 0 2 0 2
P1 2 0 2
P2 3 0 3 0 0 0 P2 0 0 1
P3 2 1 1 1 0 0 P3 1 0 0
P4 0 0 2 0 0 2 P4 0 0 2
Detection-Algorithm Use

q When, and how often to invoke depends on:


n How often a deadlock is likely to occur?
n How many processes will need to be rolled back?

q One for each disjoint cycle


q How often --
n Every time a request for allocation cannot be granted
immediately
q Allows us to detect set of deadlocked processes and process
that “caused” deadlock. Extra overhead.
q Every hour or whenever CPU utilization drops.
n With arbitrary invocation there may be many cycles in the
resource graph and we would not be able to tell which of the
many deadlocked processes “caused” the deadlock.
Recovery from Deadlock:
q Process Termination
n Abort all deadlocked processes.
n Abort one process at a time until the deadlock cycle is
eliminated.
n In which order should we choose to abort?
q Priority of the process
q How long the process has computed, and how much longer
to completion.
q Resources the process has used.
q Resources process needs to complete.
q How many processes will need to be terminated.
q Is process interactive or batch?
Recovery from Deadlock:

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

allowing the use of the optimal approach for each class of


resources in the system.
n Partition resources into hierarchically ordered
classes.
q Use most appropriate technique for handling
deadlocks within each class.
UNIT - IV

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

hTime & Date

hUser Id
File Operations

h Creating a file
− ABC.txt

hWriting into a file


− write(a, “Text which is being written”)

Reading a file

− read(a)
File Operations (Cont…)

hRepositioning within a file

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

− Get & set various attributes of a file

− Determine the status of a file (file length, file attributes etc).


File Operations (Cont…)
hMost of above mentioned operations requires :
− Searching the directory for the named file

hSearching makes processing slow.

hTo avoid searcing every time ,most system requires :


− open() System call

hOS maintains a table : open-file table


Open() System Call
hOpen() operation takes :
− File name

− Searches the directory, copying the directory entry into open-file table.

hAvailable modes : create, read only, read write, append etc.

hReturns a pointer to the entry in open-file table.


Open-File Table
hWhen a file operation is required to be executed, the file specified via an
index into open-file table.
− no searching is required

hWhen file is no longer used : respective entry is removed from open-file


table by OS.

hUses the information open count.


Open-File Table (Cont…)
hopen count :
− Number of opened files

− Decreses with each close() opeartion

− When turns 0 ,then file is no longer in use. (Entry gets removed from table)

hSeveral other infromations for a open file


− File Pointer

− Disk location of file

− Access rights
File Locking
hEnables access for one process and prevents others.

− Shared Lock : Reader lock (several processes can acquire)

− Exclusive Lock : Writer lock (only one processes can acquire)


File Types
Type of File File Extension Description

Executable exe, com, bin Machine Language Program (Ready to run)

Object obj , o Machine language (compiled), no linking done

Source Code c, app, java , perl Set of programs in various programming languages

Batch bat, sh Command interpreter commands

Markup xml, html, tex Documents, textual data


File Types (Cont…)
Type of File File Extension Description

Library lib, a , so, dll Routine Libraries

Word Processor xml Word processor formats

Archive rtf, docx Group of related files (compressed/storage)

Print/view gif, pdf, jpeg Binary/ASCII files in printable or viewable


format.

Multimedia mpeg, mov, mp3, mp4, Binary file containing audio or A/V information
avi
File Structure

h Disk space always allocated in blocks.

h Well defined block size (size of sector)

h Disk I/O – performed in units of one block (Physical record)

h All blocks are of same size.

h Structure – various logical records are packed in physical blocks.


File Structure (Cont…)

hNo. of logical records (in each block) determined by :

− Logical Record Size

− Physical Block Ssize

− Packing Technique
File Structure (Cont…)
hSome amount of Internal Fragmentation exists.

Example –
− Block Size : 1024 bytes (1KB)

− A file of 3000 bytes would be allocated to 3 blocks (3,072 bytes)

− Resulting in wastage of 72 bytes (Internal Fragmentation)

− Larger the block size, higher the internal fragmentation


File Access Methods

● Sequential Access
● Direct Access
● Other Access Methods
Sequential Access

● Information Processing – Sequential


● Mostly used by editors and compilers
● Performs well on sequential access devices and random access
devices
● Based on the tape model of file
Sequential Access (Cont…)

● 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.


Sequential Access (Cont…)

File Pointer (Current Position)

Beginning End

Rewind Read/Write
Sequential Access (Cont…)
● File operations :

− Read

− Write
Sequential Access (Cont…)

● Write Opeartion : write_next ()

− Appends to the end of the file

− Advance the file pointer to the end of newly written material (i.e. new
end of file)
Sequential Access (Cont…)

● Reset

− Resets to the beginning

● Skip

− Skip n records backward/forward


Sequential Access (Cont…)

● Applications

− Payroll of Employees

− Student Data Processing


Direct Access
● Relative Access Method

● File - Fixed Length Logical Records

● Based on the disk model of file

● File – A numbered sequence of blocks and records

● Allow read/ write operations to be performed rapidly (i.e. no specific order)

● Eg- first read block 50, then read block 24, read then block 87 and so on.
Direct Access (Cont…)

● Used when we need filtered information from the database (Sequential

access can be very slow and inefficient).

● No need of determining the desired block number.

● Generally implemented in database applications.

● Lagre amount of information – immediate access


Direct Access (Cont…)
Sequential Access

Direct Access

--------------------
--------------------
--------------------
--------------------
Direct Access (Cont…)

● Example – Airline Reservation System

● 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…)

● Read Opeartion : read (n)

− n : Block Number

− Reads block number n of file


Direct Access (Cont…)

● Write Opeartion : write (n)

− n : Block Number

− Writes to block number n of file


Direct Access (Cont…)

● Retain read_next () and write_next ()

− Sequential Access

− Add position_file (n) ; n - Block Number

− Read (n) : position_file (n) + read_next()


Direct Access (Cont…)

● Address of an arbitrary record N

N = L * (N - 1)

Where L : length of Logical Record


Direct Access (Cont…)

● Simulation of Sequential Access

Sequential Access Implementation for Direct Access

reset cp = 0;

read_next () read cp;


cp = cp + 1;
write_next () write cp;
cp = cp + 1;
Direct Access (Cont…)

● Applications

− Customer account processing in bank

− Airline Reservation System


Other Access Methods

● Built on top of Direct Access Method

● Requires an “index” for file

● Index – contains pointer to various blocks


Other Access Methods (Cont…)

● Searching a record/entry in the file

− Search the index

− Use pointer to access the file directly for desired record


Other Access Methods (Cont…)

● Primary/ Secondary Index

− For larger files – index file also required to be too large in memory

− Solution – Create “index” for index file

− Primary Index – Points to Secondary Index

− Secondary Index – Points to data


File Allocation

● 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

● Each file occupy a set of contiguous blocks on disk.

● Disk addresses define a linear ordering on the disk.

● Assume only single job is accessing the disk.

● 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

● Resolves all issues of contiguous allocation.

● File – Linked list of disk blocks.

● Disk blocks may be separated anywhere on the disk.

● Directory contains :

− Pointer to first block of file

− Pointer to last block of file


Linked Allocation (Cont…)
Linked Allocation (Cont…)

● Advantages

− No External Fragmentation

● Disdvantages

− Slow Access
Indexed Allocation

● Resolves all issues of contiguous allocation & Linked allocation.

● Brings all pointers together into one location – “Index Block”.

● Each file has its own index block.

● Block – An array of disk block addresses (DBA)

● Index of Addresses
Indexed Allocation (Cont…)

● ith entry in index block points to ith block of file.

● Directory contains :
− Address of index block

● To find ith block –

− Use pointer in the ith block entry


Indexed Allocation (Cont…)
Indexed Allocation (Cont…)

Index Node (i node)


Indexed Allocation (Cont…)

● Advantages

− Problem of limited file size is resolved

− No space wastage (direct pointer)

− No External Fragmentation

− Time is relatively very low


Directory

● Physical Disk can be broken up into multiple partitions (multi-disks)

● A collection of nodes containing information about all files.

● Both the directory structure and the files reside on disk.

● Backups of these two structures are kept on tapes.


Directory (Cont…)

Directory

F1 F2 F4
Files
F3 F 45
Directory (Cont…)

A Typical File System Organization


Directory (Cont…)

● Information in a Device Directory


− Name

− Type

− Address

− Current length

− Maximum length

− Date last accessed


Directory (Cont…)

● Information in a Device Directory


− Date last updated

− Owner ID

− Protection information
Directory (Cont…)
● Operation Performed on a Directory

− Search for a file

− Create a file

− Delete a file

− List a directory

− Rename a file

− Traverse the file system


Directory (Cont…)
● Recommended ways to organize a Directory

h Efficiency – locating a file quickly.

h Naming – convenient to users.

− Two users can have same name for different files.

− The same file can have several different names.

h Grouping – logical grouping of files by properties, (e.g., all Java programs, all games, …)
Types of Directory

● Single-Level Directory

● Two-Level Directory

● Tree Structured Directory

● Acyclic Graph Directory

● General Graph Directory


Single Level Directory
● A single directory for all users.
● All files are contained in same directory.
Single Level Directory (Cont…)

● Advantages

− Easy Implementation

− With smaller file size, searching will become fast

− Creation, searching, deletion, updation are very easy

● Limitations

− Increased Number of file in system

− When system has more than one user

− Naming Problem
Two Level Directory

● Separate directory for each users.

● Each user has its own User File Directory (UFD).

● All UFD’s have similar structure.

● When user logs in, Master File Directory (MFD) is searched.


Two Level Directory (Cont…)

● MFD : indexed by user name/account number .

● Each entry points to UFD for that user.

● When user refers to a specific file, only his own UFD is searched.
Two Level Directory (Cont…)
Two Level Directory (Cont…)
● Advantages

− Full path like /User-name/directory-name/.

− Different users can have same directory as well as file name.

− Searching of files become more easy due to path name and user-grouping.

● 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 user.
Tree - Structured Directory

● A root directory and every file has a unique path

● A directory contains :

− Set of files or sub-directories.

● All Directories have same internal format.

● Current Directory (CD)

− Contains most of the files that are of current interest to the process.

● When a file reference is made, the current directory is searched.


Tree - Structured Directory (Cont…)
Tree - Structured Directory (Cont…)
● Advantages

− Full path name can be given.

− Probability of name collision is less.

− Searching becomes very easy, (absolute path as well as relative)

● Disadvantages

− Every file does not fit into the hierarchical model, files may be saved into multiple
directories.

− We can not share files.

− It is inefficient, because accessing a file may go under multiple directories.


Tree - Structured Directory (Cont…)

● Creating a new file is done in current directory.


● Delete a file
rm <file-name>
● Creating a new subdirectory is done in current directory.

mkdir <dir-name>
Example: if in current directory /mail
mkdir count
Note - Deleting “mail” will
mail delete he entire sub-tree rooted
by “mail”.

prog copy prt exp count


Acyclic Graph Directory

● A Graph with no cycle.

● Have shared sub-directories and files.

● Allows directories to share sub-directories and files.

● i.e. Same file or sub-directory may be in two different locations.


Acyclic Graph Directory (Cont…)
Acyclic Graph Directory (Cont…)

● Advantages

− We can share files.

− Searching is easy due to different-different paths.

● Disadvantages

− We share the files via linking, in case of deleting it may create the problem,
General Graph Directory

● Cycles are allowed within a directory structure

● where multiple directories can be derived from more than one parent

directory.

● Problem - to calculate total size or space


General Graph Directory
General Graph Directory (Cont…)
● Advantages

− Allows cycles

− More flexible than other directories structure

● Disadvantages

− Costly

− Needs garbage collection


Free Space Management

● Problem : Limited Disk Space

● Solution : for new files, reuse space from deleted files.

● Write once optical disks : Allow only once to write, so no reuse is

possible.

● Free Space List : Keeps track of all free disk spaces. (not those allocated

to some file or directory)


Free Space Management (Cont…)

● To create a file :

− Search free space list for required amount of space

− Allocate that space to the new file

● Space is then removed from free space list.

● When a file is deleted, its disk space is added to free space list.
Implementing Free Space List

● Implementation of Free Space List :

− bit map/bit vector

● Each block is represented by 1 bit.

● If block is free, bit is 1, if block is allocated, bit is 0.


Implementing Free Space List

● Example : Consider a disk block, where : Block Size - 0 to 31

− Blocks 2,3,4,5,8,9,10,11,12,13,17,18,25,26,27 are free

− and rest or the blocks are allocated.

● Free Space bit map : 00111100111111000…………………


Implementing Free Space List

● Advantages :

− Simplicity

− Efficiency

● Another approach :

− Link together all free disk blocks.


Mounting

● File System must be mounted just as a file needs to be opened before it is

used.

● Then it will be available to processes on system.

● 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.

● To access directory structure within this file system , precede directory


name with /home as /home/abc.
Mounting (Cont…)
● Triangle : Subtree of directories.

Fig 1: Existing File System


Mounting (Cont…)

Fig 2: Unmounted File System


Mounting (Cont…)

Fig 3: Mount Point


File Sharing

● Sharing of files on multi-user systems is desirable.

● Sharing may be done through a protection scheme.

● On distributed systems, files may be shared across a network.

● Network File System (NFS) is a common distributed file-sharing method.


File Sharing – Remote File Systems
● Uses networking to allow file system access between systems
− Manually via programs like FTP
− Automatically, seamlessly using distributed file systems
− Semi automatically via the world wide web

● Client-server model allows clients to mount remote file systems from


servers
− Server can serve multiple clients
− Client and user-on-client identification is insecure or complicated
− NFS is standard UNIX client-server file sharing protocol
− CIFS is standard Windows protocol
− Standard operating system file calls are translated into remote calls
File Sharing – Failure Models
● All file systems have failure modes
− For example corruption of directory structures or other non-user

− data, called metadata

● 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

● Stateless protocols such as NFS v3 include all information in each request,


allowing easy recovery but less security
File Protection
● File owner/creator should be able to control:
− what can be done
− by whom

● 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.

● Mode of Access : Read, Write, Execute


Window 7 Access Control List Management
REFERENCES BOOKS
● A. Silberschatz and Peter B Galvin: Operating System Principles, Wiley India Pvt. Ltd.

● Tanenbaum: Modern Operating System, Prentice Hall.


Thank You…
UNIT - V

CASE STUDY: UNIX


OUTLIN
E
Introduction
Design Principles
Structural, Files, Directory Hierarchy
Filesystem
Files, Directories, Links, On-Disk Structures
Mounting Filesystems, In-Memory Tables, Consistency
IO
Implementation, The Buffer Cache
Processes
Unix Process Dynamics, Start of Day, Scheduling and States
The Shell
Examples, Standard IO
Summary

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.

Processes are unit of scheduling and


protection: the command interpreter
("shell") just a process

No concurrency within kernel


All IO looks like operations on files: in
Unix, everything is a file

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!

Represented in user-space by a file descriptor (fd) this is just an opaque identifier


—a good technique for ensuring protection between user and kernel

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

Devices are represented by special files:


Support above operations, although perhaps with bizarre semantics
Also have ioctl for access to device-specific functionality

4.3
DIRECTORY
HIERARCHY
Directories map names to files (and
directories) starting from distinguished root
directory called /

Fully qualified pathnames mean performing


traversal from root

Every directory has . and .. entries: refer to


self and parent respectively. Also have
shortcut of current working directory (cwd)
which allows relative path names; and the
shell provides access to home directory as ~username (e.g. ~mort/). Note that
kernel knows about former but not latter

Structure is a tree in general though this is slightly relaxed

4.4
FILE SYSTEM
IMPLEMENTATION

Inside the kernel, a file is represented by a data structure called an index-node or i-


node which hold file meta-data: owner, permissions, reference count, etc. and
location on disk of actual data (file contents)

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?

Also get soft- or symbolic-


links: a 'normal' file which contains afilename

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

Superblock contains info such as:

Number of blocks and free blocks in filesystem


Start of the free-block and free-inode list
Various bookkeeping information

Free blocks and inodes intermingle with allocated ones


On-disk have a chain of tables (with head in superblock) for each of these.
Unfortunately this leaves superblock and inode-table vulnerable to head crashes so
we must replicate in practice. In fact, now a wide range of Unix filesystems that are
completely different; e.g., log-structure

4 . 10
MOUNTING FILE
SYSTEMS
Entire filesystems can be
mounted on an existing
directory in an already mounted
filesystem

At very start, only / exists so


must mount a root filesystem

Subsequently can mount other


filesystems, e.g.
mount("/dev/hda2",
"/home", options)

Provides a unified name-space: e.g. access /home/mort/ directly (contrast with


Windows9x or NT)

Cannot have hard links across mount points: why? What about soft links?

4 . 11
IN-MEMORY
TABLES
Recall process sees files as file
descriptors

In implementation these are just


indices into process-specific open file
table
Entries point to system-wide open file
table. Why?

These in turn point to (in memory)


inode table

4 . 12
ACCESS
CONTROL

Access control information held in each inode

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

In addition have setuid and setgid bits:

Normally processes inherit permissions of invoking user


Setuid/setgid allow user to "become" someone else when running a given
program
E.g. prof owns both executable test (0711 and setuid), and score file (0600)

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

Crash detected as OS knows if crashed because root fs not unmounted cleanly

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

"Typically" prevents 85% of implied disk transfers


But when does data actually hit disk?
Call sync every 30 seconds to flush dirty buffers to disk

Can cache metadata too —what problems can that cause?

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

Data: contains variables and their values


Stack: used for activation records (i.e.
storing local variables, parameters, etc.)

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)

fork() nearly always


followed by exec()
leading to vfork()
and/or copy-on-write
(COW).Also makes a copy
of entire address space
which is not terribly
efficient

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

Uses path for convenience, to avoid needing


fully qualified pathnames

Conventionally & specifies background


Parsing stage (omitted) can do lots: wildcard
expansion ("globbing"), "tilde" processing

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*

Prompt is $. Use man to find out about commands. User friendly?

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

• Designed by programmers, for programmers


• Designed to be:

• Simple
• Elegant
• Consistent
• Powerful
• Flexible
Interfaces to Linux

Fig: The layers in a Linux system.


Linux Utility Programs (1)

Categories of utility programs:

• File and directory manipulation commands.


• Filters.
• Program development tools, such as editors and
compilers.
• Text processing.
• System administration.
• Miscellaneous.
Linux Utility Programs (2)

Fig: A few of the common Linux utility


programs required by POSIX.
Kernel Structure

Structure of the Linux kernel


Processes in Linux

Fig: Process creation in Linux.


Signals in Linux (1)
Process Management
System Calls in Linux

Fig: Some system calls relating to processes.


A Simple Linux Shell

Fig:10-7. A highly simplified shell.


Implementation of Processes and Threads
Categories of information in the process descriptor:
• Scheduling parameters
• Memory image
• Signals
• Machine registers
System call state
• File descriptor table
• Accounting
• Kernel stack
• Miscellaneous
Implementation of Exec

Fig: The steps in executing the command


ls typed to the shell.
The Clone System Call
Scheduling in Linux (1)

Three classes of threads for scheduling purposes:

• Real-time FIFO.
• Real-time round robin.
• Timesharing.
Scheduling in Linux (2)

Fig:Illustration of Linux runqueue and priority arrays.


Booting Linux

Fig: The sequence of processes used to boot


some Linux systems.
Memory Management in Linux (1)

Fig: Process A’svirtual address space. (b) Physical memory. (c)


Process B’svirtual address space.
Memory Management in Linux (2)

Fig: Two processes can share a mapped file.


Memory Management
System Calls in Linux

Fig: Some system calls relating to memory


management.
Physical Memory Management (2)

Fig: Linux main memory representation.


Physical Memory Management (3)

Fig: Linux uses four-level page tables


Memory Allocation Mechanisms

Fig: Operation of the buddy algorithm.


The Page Replacement Algorithm

Fig: Page states considered in the page frame


replacement algorithm.
Networking

Fig: The uses of sockets for networking


Input/Output System Calls in Linux
The Major Device Table
Implementation of
Input/Output 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)

Fig: (a) Before linking. (b) After linking.


The Linux File System (3)

Fig: (a) Separate file systems. (b) After mounting.


The Linux File System (4)

Fig: (a) A file with one lock.


(b) Addition of a second lock. (c) A third lock.
File System Calls in Linux (1)

Fig: System calls relating to files.


File System Calls in Linux (2)
File System Calls in Linux (3)
The Linux Virtual File System
NFS Protocols

Fig:Examples of remote mounted file systems.


Directories shown as squares, files shown as circles.
NFS Implementation

Fig: The NFS layer structure


Security In Linux
Security System Calls in Linux
Tutorial Sheet
Question Bank

You might also like