0% found this document useful (0 votes)
65 views91 pages

Os Unit - 5

The document covers I/O Management and File Management in operating systems, detailing I/O devices, organization of I/O functions, and various disk arm scheduling algorithms. It explains techniques like Programmed I/O, Interrupt-driven I/O, and Direct Memory Access (DMA), emphasizing efficiency and generality in OS design. Additionally, it discusses buffering methods and provides examples of disk scheduling algorithms such as FCFS, SSTF, and LOOK.

Uploaded by

23010101661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views91 pages

Os Unit - 5

The document covers I/O Management and File Management in operating systems, detailing I/O devices, organization of I/O functions, and various disk arm scheduling algorithms. It explains techniques like Programmed I/O, Interrupt-driven I/O, and Direct Memory Access (DMA), emphasizing efficiency and generality in OS design. Additionally, it discusses buffering methods and provides examples of disk scheduling algorithms such as FCFS, SSTF, and LOOK.

Uploaded by

23010101661
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 91

Operating System (OS)

2301CS601

Unit-5
I/O Management and
File Management

Prof. Firoz A Sherasiya


Computer Engineering Department
Darshan Institute of Engineering & Technology, Darshan University, Rajkot
[email protected]
9879879861
✓ Outline
Looping
• I/O Devices
• Organization of I/O functions
• Operating System Design issues
• I/O Buffering
• Disk Arm Scheduling Algorithm
• FCFS/FIFO
• SSTF
• SCAN
• C-SCAN
• LOOK
• C-LOOK
• RAID
• Disk Cache
I/O devices
Section - 1
I/O devices
🞂 External devices that engage in I/O with computer systems can be grouped into 3 categories:
1. Human readable, suitable for communicating with the computer user.
Examples: printers, terminals, video display, keyboard, mouse
2. Machine readable, suitable for communicating with electronic equipment.
Examples: disk drives, USB keys, sensors, controllers
3. Communication, suitable for communicating with remote devices.
Examples: modems, digital line drivers

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 4
Organization of I/O functions
Section - 2
Organization of I/O functions
🞂 Three techniques for performing I/O are:
1. Programmed I/O, the processor issues an I/O command on behalf of a process to an I/O module; that
process then busy waits for the operation to be completed before proceeding.
2. Interrupt-driven I/O, the processor issues an I/O command on behalf of a process
▪ if non-blocking – processor continues to execute instructions from the process that issued the I/O command
▪ if blocking – the next instruction the processor executes is from the OS, which will put the current process in a
blocked state and schedule another process
3. Direct Memory Access (DMA), a DMA module controls the exchange of data between main memory and an
I/O module

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 6
Direct Memory Access
🞂 Feature of computer systems that allows certain hardware subsystems to access main
memory (RAM), independent of the central processing unit (CPU).
🞂 Without DMA, when the CPU is using programmed input/output, it is typically fully occupied for
the entire duration of the read or write operation, and is thus unavailable to perform other work.
🞂 With DMA, the CPU first initiates the transfer, then it does other operations while the transfer
is in progress, and it finally receives an interrupt from the DMA controller when the operation
is done.
🞂 This feature is useful when the CPU needs to perform useful work while waiting for a
relatively slow I/O data transfer.
🞂 Many hardware systems such as disk drive controllers, graphics cards, network cards and
sound cards use DMA.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 7
Disk read-write without a DMA
🞂 The disk controller reads the block from the drive Drive
serially, bit by bit, until the entire block is in the
controller’s buffer. Buffer
🞂 Next, it computes the checksum to verify that no read
errors have occurred.
🞂 Then the controller causes an interrupt, so that OS can
read the block from controller’s buffer (a byte or a word Disk Main
at a time) by executing a loop. CPU Controller Memory

🞂 After reading every single part of the block from


controller device register, the operating system will store
them into the main memory. Bus

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 8
Disk read-write using DMA
1. CPU Drive
programs
DMA
Buffer
controller

DMA Disk Main


CPU Controller Controller Memory

Bus

🞂 Step 1: First the CPU programs the DMA controller by setting its registers so it knows what to
transfer where.
🞂 It also issues a command to the disk controller telling it to read data from the disk into its
internal buffer and verify the checksum.
🞂 When valid data are in the disk controller’s buffer, DMA can begin.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 9
Disk read-write using DMA
1. CPU Drive
programs
DMA
Buffer
controller

2. DMA
requests
DMA transfer to Disk Main
CPU Controller memory Controller Memory

Bus

🞂 Step 2: The DMA controller initiates the transfer by issuing a read request over the bus to the
disk controller.
🞂 This read request looks like any other read request, and the disk controller does not know (or
care) whether it came from the CPU or from a DMA controller.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 10
Disk read-write using DMA
1. CPU Drive
programs
DMA
Buffer
controller

2. DMA
requests
DMA transfer to Disk 3. Data Main
CPU Controller memory Controller transferred Memory

Bus

🞂 Typically, the memory address to write to is on the bus’ address lines, so when the disk
controller fetches the next word from its internal buffer, it knows where to write it.
🞂 Step 3: The write to memory is another standard bus cycle.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 11
Disk read-write using DMA
1. CPU Drive
programs
DMA
Buffer
controller 4. ACK

2. DMA
requests
DMA transfer to Disk 3. Data Main
CPU Controller memory Controller transferred Memory

Bus

🞂 Step 4: When the write is complete, the disk controller sends an acknowledgement signal to the
DMA controller, also over the bus.
🞂 The DMA controller then increments the memory address to use and decrements the byte
count.
🞂 If the byte count is still greater than 0, steps 2 to 4 are repeated until it reaches 0.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 12
Disk read-write using DMA
1. CPU Drive
programs
DMA
Buffer
controller 4. ACK

2. DMA
requests
DMA transfer to Disk 3. Data Main
CPU Controller memory Controller transferred Memory

Bus

🞂 At that time, the DMA controller interrupts the CPU to let it know that the transfer is now
complete.
🞂 When the OS starts up, it does not have to copy the disk block to memory; it is already there.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 13
Operating System Design issues
Section - 3
Operating System Design issues
🞂 Design objectives: Two objectives are paramount in designing the I/O facility.
1. Efficiency
▪ The system should minimize resource usage, such as CPU time and memory, and maximize throughput.
▪ Efficiency is concerned with minimizing resource usage, such as CPU time and memory, and maximizing throughput,
which means the system can perform a larger number of I/O operations in a given amount of time.
▪ Efficiency is important because I/O operations often form a bottleneck in a computing system.
▪ Most I/O devices are extremely slow compared with main memory and the processor.
2. Generality
▪ The system should be able to handle a wide range of devices and types of I/O operations in a consistent manner.
▪ It is desirable to handle all devices in a uniform manner.
▪ Applies to the way processes view I/O devices and the way the operating system manages I/O devices and
operations.
▪ Because of the diversity of device characteristics, it is difficult in practice to achieve true generality.
▪ What can be done is to use a hierarchical, modular approach to the design of the I/O function.
▪ This approach hides most of the details of device I/O in lower-level routines so that user processes and upper levels
of the OS see devices in terms of general functions, such as read, write, open, close, lock, and unlock.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 15
I/O Buffering
Section - 4
Buffering
🞂 Perform input transfers in advance of requests being made and perform output transfers
some time after the request is made is called buffering.
🞂 Types of I/O devices:
⮩ Block oriented: A block-oriented device stores information in blocks that are usually of fixed size, and
transfers are made one block at a time.
Generally, it is possible to reference data by its block number.
Hard disks, floppy disks and optical drives such as DVD-ROM and CD-ROM are examples of block oriented
devices.

⮩ Stream oriented: A stream-oriented device transfers data in and out as a stream of bytes, with no block
structure.
Terminals, printers, communications ports, keyboard, mouse and other pointing devices, and most other
devices that are not secondary storage are stream oriented.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 17
I/O buffering
Operating System User Process
🞂 Without a buffer, Operating system directly accesses the
IN
I/O
Device
device when it needs

Operating System User Process


🞂 Single buffer, Operating system assigns a buffer in the
IN MOVE
I/O
Device
main memory for an I/O request

Operating System User Process 🞂 Double buffer, Operating system use two system buffers
IN
instead of one, also known as buffer swapping. A
I/O MOVE
process can transfer data to or from one buffer while the
Device
operating system empties or fills the other buffer

Operating System User Process 🞂 Circular buffer, Operating system uses two or more
buffers. Each individual buffer is one unit in a circular
I/O IN MOVE
Device
.. buffer. Used when I/O operation must keep up with
process
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 18
Disk Arm Scheduling Algorithm
Section - 5
Definitions (Internal structure of HDD)
🞂 When the disk drive is operating, the disk is
rotating at constant speed.
🞂 To read or write the head must be positioned at
the desired track and at the beginning of the
desired sector on that track
🞂 Track selection involves moving the head in a
movable-head system or electronically selecting
one head on a fixed-head system
🞂 On a movable-head system the time required to
move the disk arm to the required track is
known as seek time.
🞂 The delay waiting for the rotation of the disk to
bring the required disk sector under the read-
write head is called rotational delay.
🞂 The sum of the seek time and the rotational
delay equals the access time.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 20
Disk Arm Scheduling Algorithm
🞂 Following are different types of Disk Arm Scheduling Algorithm
1. FCSC (First Come First Serve) / FIFO (First In First Out)
2. SSTF (Shorted Seek Time First)
3. SCAN
4. C-SCAN
5. LOOK (Elevator)
6. C-LOOK

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 21
Example for Disk Arm Scheduling Algorithm
🞂 Consider an imaginary disk with 51 cylinders. A request comes in to read a block on cylinder
11. While the seek to cylinder 11 is in progress, new requests come in for cylinders 1, 36, 16,
34, 9, and 12, in that order.
🞂 Starting from the current head position, what is the total distance (in cylinders) that the disk
arm moves to satisfy all the pending requests, for each of the following disk scheduling
Algorithms?

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 22
FCSC (First Come First Serve) / FIFO (First In First Out)
🞂 Here requests are served in the order of their arrival.
0 1 9 12 16 34 36 50
11 1,
36,
1 16,
36 34,
9,
16 12
34
9
12

🞂 Disk movement will be 11, 1, 36, 16, 34, 9 and 12.


🞂 Total cylinder movement: (11-1) + (36-1) + (36-16) + (34-16) + (34-9) + (12-9) = 111

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 23
SSTF (Shortest seek time first)
🞂 We can minimize the disk movement by serving the request closest to the current position of
the head.
0 1 9 12 16 34 36 50
11 1,
36,
12 16,
9 34,
9,
16 12
1
34
36

🞂 Disk movement will be 11, 12, 9, 16, 1, 34 and 36.


🞂 Total cylinder movement: (12-11) + (12-9) + (16-9) + (16-1) + (34-1) + (36-34) = 61

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 24
LOOK
🞂 Keep moving in the same direction until there are no more outstanding requests pending in
that direction, then algorithm switches the direction.
🞂 After switching the direction the arm will move to handle any request on the way. Here first it
moves in up direction then goes in down direction.
🞂 In this algorithm, the software maintains 1 bit: the current direction bit, which takes the value
either UP or DOWN.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 25
LOOK
🞂 Here first it moves in up direction then goes in down direction
0 1 9 12 16 34 36 50
11 1,
36,
12
16,
16 34,
34 9,
12
36
9
1

🞂 Disk movement will be 11, 12, 16, 34, 36, 9 and 1.


🞂 Total cylinder movement: (12-11) + (16-12) + (34-16) + (36-34) + (36-9) + (9-1) = 60

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 26
C-LOOK
🞂 Keep moving in the same direction until there are no more outstanding requests pending in that
direction, then algorithm switches direction.
🞂 When switching occurs the arm goes to the lowest numbered cylinder with pending requests
and from there it continues moving in upward direction again.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 27
C-LOOK
🞂 Here first it moves in up direction then goes in down direction
0 1 9 12 16 34 36 50
11 1,
36,
12
16,
16 34,
34 9,
12
36
1
9

🞂 Disk movement will be 11, 12, 16, 34, 36, 1 and 9.


🞂 Total cylinder movement: (12-11) + (16-12) + (34-16) + (36-34) +(36-1)+(9-1) = 68

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 28
SCAN
🞂 From the current position disk arm starts in up direction and moves towards the end, serving
all the pending requests until end.
🞂 At that end arm direction is reversed (down) and moves towards the other end serving the
pending requests on the way.
🞂 This is also called as elevator algorithm.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 29
SCAN
🞂 Here first it moves in up direction then goes in down direction
0 1 9 12 16 34 36 50
11 1,
36,
12
16,
16 34,
34 9,
12
36

9 50
1
🞂 Disk movement will be 11, 12, 16, 34, 36, 50, 9 and 1.
🞂 Total cylinder movement: (12-11) + (16-12) + (34-16) +(36-34) +(50-36) + (50-9) + (9-1) = 88

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 30
C-SCAN
🞂 From the current position disk arm starts in up direction and moves towards the end, serving
request until end.
🞂 At the end the arm direction is reversed (down), and arm directly goes to other end and again
continues moving in upward direction.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 31
C-SCAN
🞂 Here first it moves in up direction then goes in down direction
0 1 9 12 16 34 36 50
11 1,
12 36,
16,
16
34,
34 9,
12
36

0 50
1
9
🞂 Disk movement will be 11, 12, 16, 34, 36, 50, 0, 1 and 9.
🞂 Total cylinder movement: (12-11) + (16-12) + (34-16) +(36-34) +(50-36) + (50-0) + (1-0)+ (9-1) =
98

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 32
Example for Disk Arm Scheduling Algorithm [Exercise]
🞂 Suppose that a disk drive has 200 cylinders, numbered 0 to 199 and order of request is:
(82,170,43,140,24,16,190). And current position of Read/Write head is : 50.
🞂 Starting from the current head position, what is the total distance (in cylinders) that the disk
arm moves to satisfy all the pending requests, for each of the following disk scheduling
Algorithms?
⮩ FCFS/FIFO (642)
⮩ SSTF (208)
⮩ SCAN (332)
⮩ C-SCAN (391)
⮩ LOOK (314)
⮩ C-LOOK (341)

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 33
RAID
Section - 6
RAID
🞂 RAID (Redundant Array of Independent Disks)
🞂 RAID is a data storage virtualization technology that combines multiple physical disk drive
components into a single logical unit for the purposes of data redundancy, performance
improvement, large storage capacity or all.
🞂 Data is distributed across the drives in one of several ways, referred to as RAID levels,
depending on the required level of redundancy and performance.
🞂 All RAID have the property that the data are distributed over drives, to allow parallel operation.
🞂 There are 7 levels of RAID.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 35
RAID 0 (Striping)
🞂 It splits data (file) into blocks of data.
🞂 Stripe the blocks across disks in the system.
🞂 In the diagram to the right, the odd blocks are written to disk 1
and the even blocks to disk 2.
🞂 It is easy to implement.
🞂 No parity calculation overhead is involved.
🞂 It provides good performance by spreading the load across
many channels and drives.
🞂 It provides no redundancy or error detection.
🞂 Not true RAID because there is no fault tolerance. The failure of
just one drive will result in all data in an array being lost.
🞂 After certain amount of drives, performance does not increase
significantly.
🞂 It requires minimum 2 drives to implement.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 36
RAID 1 (Mirroring)
🞂 A complete file is stored on a single disk.
🞂 A second disk contains an exact copy of the file.
🞂 It provides complete redundancy of data.
🞂 Read performance can be improved
⮩ same file data can be read in parallel
🞂 Write performance suffers
⮩ must write the data out twice
🞂 Most expensive RAID implementation.
🞂 It requires twice as much storage space.
🞂 In case a drive fails, data do not have to be rebuild, they just
have to be copied to the replacement drive.
🞂 The main disadvantage is that the effective storage capacity
is only half of the total drive capacity because all data get
written twice.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 37
RAID 2 (Bit-level striping using a Hamming Code parity)
🞂 It stripes data at bit level (rather than block level) with some disks storing error checking and
correcting (ECC) information.
🞂 It uses ECC (Error Correcting Code) to monitor correctness of information on disk.
🞂 A parity disk is then used to reconstruct corrupted or lost data.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 38
RAID 2 (Bit-level striping using a Hamming Code parity)
🞂 Imagine splitting each byte into a pair of 4-bit nibbles, then adding Hamming code to each one to
form a 7-bit word, of which bit 1, 2 and 4 were parity bits.
🞂 In RAID 2 each of seven drives needs synchronized in terms of arm position and rotational
position, and then it would be possible to write the 7-bit Hamming coded word over the seven
drives.
🞂 Here, losing one drive did not cause problem, which can be handled by Hamming code on the fly.
🞂 Big problem is performance
⮩ must have to read data plus ECC code from other disks
⮩ for a write, must have to modify data, ECC, and parity disks

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 39
RAID 3 (Byte-level striping with a dedicated parity)
🞂 It uses striping and dedicates one drive for
storing parity information.
🞂 Here single parity bit is computed for each
data word and written to a parity drive.
🞂 The embedded ECC information is used to
detect errors.
🞂 As in RAID level 2 the drives must be exactly
synchronized.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 40
RAID 4 (Block-level striping with dedicated parity)
🞂 It uses large stripes (block level striping),
which means you can read records from any
single drive.
🞂 They do not require synchronization of
drives.
🞂 RAID 4 is like RAID 0, with strip-for-strip parity
written onto an extra drive.
🞂 If a drive crashes, the lost bytes can be
recomputed from the parity drive by reading
the entire set of drives.
🞂 This design protects against the loss of a
drive but performs poorly for small updates, if
one sector is changed, it is necessary to read
all the drives in order to recalculate the parity.
🞂 It creates heavy load on parity drive.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 41
RAID 5 (Block-level striping with distributed parity)
🞂 This level is based on block-level striping
with parity.
🞂 The parity information is striped across each
drive.
🞂 As with RAID level 4, there is a heavy load in
the parity drive, it may become bottleneck.
🞂 This bottleneck can be eliminated in RAID
level 5 by distributing the parity bits
uniformly over all the drives.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 42
Disk Cache
Section - 7
Disk Cache
🞂 A disk cache is a mechanism for improving the time it takes to read from or write to a hard disk.
🞂 A disk cache can also be a specified portion of random access memory (RAM) for disk sectors.
🞂 The disk cache holds data that has recently been read and, in some cases, adjacent data areas that
are likely to be accessed next.

the request is satisfied


If YES
via the cache
when an I/O request is made
for a particular sector, a
check is made to determine if
the sector is in the disk
cache the requested sector is
If NO read into the disk cache
from the disk

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 44
IMP Questions
1. Write a short note on DMA.
2. Briefly describe all Disk Arm Scheduling Algorithm.
3. Write short note on RAID.
4. Suppose Disk drive has 300 cylinders. The current position of head is 90. The queue of
pending request is 36,79,15,120,199,270,89,170 Calculate head movement for the following
algorithms. 1. FCFS 2. SSTF
5. Suppose that a disk drive has 200 cylinders from 0 to 199. The drive is currently at cylinder 53
and previous request at 43. The queue of pending requests in FIFO order is 98, 183, 37, 122,
14, 124, 65, 67.Starting from the current head position what is the total distance (in cylinder )
that the disk arm moves to satisfy all the pending requests for each of the following disk
scheduling algorithms: FCFS, SSTF, SCAN, LOOK, CLOOK, CSCAN.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 45
IMP Questions
6. Define seek time and rotational latency. Assume that a disk drive has 200 cylinders,
numbered 0 to 199. The drive is currently serving a request at cylinder 100. The queue of
pending requests is 23, 89, 132, 42, 189. Calculate seek time for FCFS and SSTF disk
scheduling algorithm.
7. Suppose that a disk drive has 5000 cylinders, numbered 0 to 4999. The drive is currently
serving a request at cylinder 143,. The queue of pending requests, in FIFO order,86, 1470, 913,
1774, 948, 1509, 1022, 1750, 130Starting from current head position what is total distance (in
cylinders) that disk arm moves to satisfy all the pending request for FCFS and SSTF disk
scheduling algorithm?

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 46
✓ Outline
Looping
• File concept
• File attributes
• Access methods
• File types
• File operation
• Directory structure
• File System structure
• Allocation methods (contiguous, linked, indexed)
• Free-space management (bit vector, linked list, grouping)
• Directory implementation (linear list, hash table)
• Efficiency & performance
File concept
Section - 1
File concept
🞂 A file is a unit of storing data on a secondary storage device such as a hard disk or other
external media.
🞂 Every file has a name and its data.
🞂 Operating system associates various information with files. For example the date and time of
the last modified file and the size of file etc….
🞂 This information is called the file’s attributes or metadata.
🞂 The attributes varies considerably from system to system.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 49
File attributes
Section - 2
File attributes
🞂 Below attributes tell who may access it and who may not:
1. Protection - Who can access the file and in what way.
2. Password - Password needed to access the file.
3. Creator - ID of the person who created the file.
4. Owner - Current owner.
🞂 Flags are bits or short fields that control or enable some specific property.
1. Read only flag - 0 for read/write, 1 for read only.
2. Hidden flag - 0 for normal, 1 for do not display the listings.
3. System flag - 0 for normal, 1 for system file.
4. Archive flag - 0 for has been backed up, 1 for needs to be backed up.
5. Random access flag - 0 for sequential access only, 1 for random access.
6. Temporary flag - 0 for normal, 1 for delete file on process exit.
7. Lock flag - 0 for unlocked, 1 for locked.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 51
File attributes
🞂 Below attributes are only present in files whose record can be looked up using the key. They
provide the information required to find the keys.
1. Record length - Number of bytes in a record.
2. Key position - Offset of the key within each record.
3. Key length - Number of bytes in key field.
🞂 Various attributes keeps times track of when the file was created, most recently accessed and
most recently modified.
1. Creation time - Date and time the file was created.
2. Time of last access - Date and time of file was last accessed.
3. Time of last change - Date and time of file was last changed.
🞂 Various attributes keeps track of file size.
1. Current size - Number of bytes in the file.
2. Maximum size - Number of bytes the file may grow to.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 52
Access methods
Section - 3
Files access methods (Sequential File Access VS Random File Access)
🞂 Sequential File Access
⮩ A sequential file works like a reel of tape.
⮩ Data in a sequential file are processed in order or sequence, starting with the
first item, the processing the second, then the third and so on.
⮩ To reach data in the middle of the file you must go through all the data that
precedes it.
⮩ Example: Magnetic tape

🞂 Random File Access


⮩ A random access file works like a sampler box of chocolate. You can access
a particular item by going directly to it.
⮩ Data in a random access file may be accessed in any order. To read data in
the middle of the file you can go directly to it. Random access files are
sometimes called direct access files.
⮩ Example: Semi-conductor memories

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 54
File types
Section - 4
Types of files

File Type

Regular File Directories

ASCII File Binary File

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 56
Regular file Vs Directories
🞂 Regular File
⮩ Regular files are the ones that contain user information.
⮩ Regular file, as a randomly accessible sequence of bytes, has no other predefined internal structure.
⮩ Application programs are responsible for understanding the structure and content of any specific regular file.
🞂 Directories
⮩ Directories are system files for maintaining the structure of the file system.
⮩ To keep track of files, file systems normally have directories or folder.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 57
Different types of files
🞂 ASCII files
⮩ ASCII file consists of line of text.
⮩ Advantage of ASCII files is that they can be displayed & printed as it is & they can be edited with ordinary text
editor.
⮩ If number of programs use ASCII files for input and output, it is easy to connect the output of one program to
the input of another.
⮩ C/C++/Perl/HTML files are all examples of ASCII files.

🞂 Binary Files
⮩ Binary files contain formatted information that only certain applications or processors can understand.
⮩ Binary files must be run on the appropriate software or processor before humans can read them.
⮩ Executable files, compiled programs, spreadsheets, compressed files, and graphic (image) files are all
examples of binary files.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 58
Different types of files
🞂 Device Files
⮩ Under Linux and UNIX each and every hardware device is treated as a file. A device file allows to accesses
hardware devices so that end users do not need to get technical details about hardware.
⮩ In short, a device file (also called as a special file) is an interface for a device driver that appears in a file
system as if it were an ordinary file.
⮩ This allows software to interact with the device driver using standard input/output system calls, which
simplifies many tasks.

🞂 Character Special Files


⮩ It is a type of device file which talks to devices in a character by character (1 byte at a time).
⮩ Character Special files are related to input/output and use to model serial I/O devices, such as terminals,
printers, and networks.

🞂 Block Special Files


⮩ It is a type of device file which talks to devices 1 block at a time (1 block = 512 bytes to 32KB).
⮩ Block special files are used to model disks, DVD/CD ROM, and memory regions etc.
Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 59
File operations
Section - 5
File operations
🞂 Create
⮩ The purpose of the call is to announce that the file is coming and to set some attributes.
🞂 Delete
⮩ When the file is no longer needed, it has to be deleted to free up disk space.
🞂 Open
⮩ The purpose of the open call is to allow the system to fetch the attributes.
🞂 Close
⮩ When all accesses are finished, the attributes and disk addresses are no longer needed, so the file should be
closed to free up table space.
🞂 Read
⮩ Data are read from file. Usually the bytes come from the current position.
🞂 Write
⮩ Data are written to the file, usually at the current position.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 61
File operations
🞂 Append
⮩ This call is restricted form of write. It can only add data to the end of file.
🞂 Seek
⮩ For a random access files, a method is needed to specify from where to take the data.
⮩ Seek repositions the file pointer to a specific place in the file.
🞂 Get attributes
⮩ Processes often need to read the file attributes to do their work.
🞂 Set attributes
⮩ Some of the attributes are user settable and can be changed after the file has been created. This system call
makes that possible.
🞂 Rename
⮩ It frequently happens that a user needs to change the name of an existing file. This system call makes it
possible.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 62
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 deleted. Only an empty directory can be deleted.
⮩ A directory containing only dot and dotdot is considered as an empty directory.
🞂 Opendir
⮩ Directories can be read.
⮩ For example, to list all the files in a directory, a listing program opens the directory to read out the names of
all the files it contains.
⮩ Before a directory can be read, it must be opened.
🞂 Closedir
⮩ When a directory has been read, it should be closed to free up space.
🞂 Rename
⮩ In many respects, directories are just like files and can be renamed the same way files can be.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 63
Directory operations
🞂 Readdir
⮩ This call returns the next entry in an open directory.
⮩ It was possible to read directories using the usual read system call, but that approach has the disadvantage
of forcing the programmer to know and deal with the internal structure of directories.
⮩ In contrast, readdir always returns one entry in a standard format, no matter which of the possible directory
structure is being used.
🞂 Link
⮩ Linking is a technique that allows a file to appear in more than one directory.
⮩ This system call specifies an existing file and a path name, and creates a link from the existing file to the
name specified by the path.
⮩ In this way, the same file may appear in multiple directories.
🞂 Unlink
⮩ A directory entry is removed.
⮩ If the file being unlinked is only present in one directory (the normal case), it is removed from the file system.
⮩ If it is present in multiple directories, only the path name specified is removed. The others remain.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 64
Directory structure
Section - 6
Directory structure
🞂 Single level directory system
⮩ The simplest form of directory system is having one directory containing
all the files. This is some time called as root directory.
⮩ Here directory contain three files.
⮩ The advantages of this scheme are its simplicity and the ability to locate
files quickly there is only one directory to look, after all.
⮩ The single level is used for simple dedicated application, but for modern
user with thousands of files, it would be impossible to find anything if all
files were in a single directory.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 66
Directory structure
🞂 Hierarchical directory system
⮩ In this system user can create an arbitrary number of subdirectories to
organize their work.
⮩ When the file system is organized as a directory tree, some way is needed
for specifying file names.
⮩ Two methods are used commonly
1. Absolute path name
2. Relative path name

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 67
Absolute path name V/S Relative path name
🞂 Absolute path name
⮩ An absolute path name consisting of the path from the root directory to the file.
⮩ As an example, the path /usr /ast/mailbox means that the root directory contains a subdirectory usr, which in
turn contains a subdirectory ast, which contain the file mailbox.
🞂 Relative path name
⮩ Relative path name is used in conjunction with the concept of the working directory.
⮩ User can designate one directory as the current working directory, in which case, all path names not
beginning at the root directory are taken relative to working directory.
⮩ For example if the current working is /usr/ast, then the file whose absolute path is /usr/ast/mailbox can be
referenced simply as mailbox.
⮩ Most operating systems that support a hierarchical directory system have two special entries in every
directory,
1. “.” or “dot” refers to current directory
2. “..” or “dotdot” refers to parent directory

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 68
File System structure
Section - 7
File system structure
🞂 MBR (Master Boot Record)
⮩ The Master Boot Record (MBR) is the information in the first
sector (sector 0) of hard disk.
⮩ It identifies how and where an operating system is located so
that it can be boot (loaded) into the computer's main storage or
random access memory (RAM).
⮩ It is used to boot the computer.
⮩ The end of the MBR contains the partition table which gives the
starting and ending addresses of each partition

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 70
File system
🞂 It is group of files and information regarding them.
🞂 The disk space allotted to file system is made up of blocks, each of which are 512 bytes.
🞂 All the blocks belonging to file system are logically divided into:
⮩ Boot block
⮩ Super block
⮩ Inode table
⮩ Data block

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 71
File system
🞂 Boot block
⮩ It represent the beginning of file system.
⮩ It contains a program “bootstrap loader”.
⮩ This program is executed when we boot the machine.
⮩ All the file system contain one boot block.
🞂 Super block
⮩ It describe the state of file system.
▪ How large file system is
▪ How many maximum files it can accommodate
▪ How many more files can be created
▪ List of free and allocated blocks
▪ Modification time of the file system

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 72
File system
🞂 Inode table
⮩ The information related to all file (not the content) is stored in an inode table on the disk.
⮩ For each file, there is an inode entry in table.
⮩ Each entry is made up of 64 bytes and contain the detail for that file.
⮩ These details are:
▪ Owner of file
▪ Group to which the owner belongs
▪ Types of file and its size
▪ File access permission
▪ Date and time of last access and last modification
▪ Address of blocks where the file is physically present
🞂 Data block
⮩ It contains actual file content.
⮩ An allocated block can belong to only one file in the file system.
⮩ This block can’t be used for storing any other file’s content unless the file to which it originally belonged is
deleted.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 73
Allocation methods (contiguous,
linked, indexed)
Section - 8
Allocation methods
🞂 Various methods to implement files are listed below,
1. Contiguous allocation
2. Linked list allocation
3. Linked list allocation using a table in memory
4. I-nodes

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 75
Contiguous allocation
🞂 The simplest allocation scheme is to store each file as a contiguous run of disk block.

State of the disk after files D and F have been removed

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 76
Contiguous allocation
🞂 It is simple to implement because we need to keep track of only the first block and the number
of blocks in the file.
🞂 Read performance is excellent because the entire file can be read from the disk in a single
operation.
🞂 Only one seek is needed (to the first block).
🞂 Once the disk is filled, reusing the space requires maintaining a list of hole.
🞂 However, when a new file is to be created, it is necessary to know its final size in order to
choose a hole of the correct size to place it in.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 77
Linked list allocation
🞂 Another method for storing files is to keep each one as a
linked list of the disk blocks.
🞂 The first word of each block is used as a pointer to the
next one. The rest of the block is for data.
🞂 No space is lost to disk fragmentation.
🞂 Reading a file is extremely slower than Contiguous
Allocation.
🞂 Less data occupied in block.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 78
Linked list allocation using a table in memory
🞂 In this method instead of a pointer, a table in memory
called file allocation table is used.
🞂 Take the pointer word from each block and put it in a
table in memory.
🞂 Advantage of this method is that random access of files
is faster.
🞂 The only disadvantage of this method is that the entire
table should always be in the memory.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 79
I-node
🞂 I-node (Index-node) is a data structure used for storing
attributes and disk address of a block in memory.
🞂 A method for keeping track of which blocks belong to
which file is to associate with each file a data structure
called an i-node (index-node), which lists the attributes
and disk addresses of the file’s blocks.
🞂 Here, only the i-node should be in the memory at all
times.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 80
Free-space management (bit
vector, linked list, grouping)
Section - 9
Free-space management
🞂 Techniques to manage free space are:
⮩ bit vector
⮩ linked list
⮩ grouping

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 82
Bit vector
🞂 In this method free space list (block) is implemented as a bit map or bit vector.
🞂 If the block is free the bit is set to 1.
🞂 If the block is allocated the bit is set to 0.
🞂 Example:
⮩ Consider a disk, where blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, 17, 18, 25, 27 are free and rest of blocks are
allocated.
⮩ The free space bit map would be:

🞂 Advantages
⮩ Relatively simple and efficient
⮩ Easy to get contiguous files

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 83
Linked list
🞂 It links all free disk blocks together.
🞂 Each block contain a pointer to next free blocks, and so on
🞂 In figure there is a pointer to block 2, as the first free block.
🞂 Block 2 would contain a pointer to block 3.
🞂 Which would point 4,5,8,9,10,11,12,13,17,18,25,26,27
🞂 Can’t get continuous space easily.
🞂 No waste of space.
🞂 Not efficient for faster access.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 84
Grouping
🞂 It stores addresses of n free blocks in the first free block.

🞂 First n-1 of these block are address of free blocks.


🞂 Last block contains the pointer of another free block.
🞂 Large number of free blocks can be found quickly.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 85
Directory implementation (linear
list, hash table)
Section - 10
Directory implementation
🞂 Linear list
⮩ Linear list of file names with pointer to the data blocks
⮩ It is simple to program
⮩ It is time-consuming to execute
🞂 Hash table
⮩ It uses hash table – linear list with hash data structure
⮩ It decreases directory search time
⮩ It may cause collisions – situations where two file names hash to the same location

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 87
Efficiency & performance
Section - 11
Efficiency & performance
🞂 Efficiency depends on:
⮩ Disk allocation and directory algorithms
⮩ Type of data kept in file
⮩ Example: keep a last write date or last access date
⮩ Thus whenever a file is read, a field in the directory structure must be written to.
🞂 Performance:
⮩ Keep disk cache – a separate section of RAM in the disk controller for frequently used blocks.
⮩ Some system maintain a separate section of main memory for a buffer cache.
⮩ Improve the performance of system by allocating dedicated section of memory as virtual disk or RAM disk.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 89
IMP Questions
1. Differentiate Sequential File Access and Random File Access.
2. List and explain different file operations.
3. List and explain different attributes of file.
4. Explain different types of files.
5. Differentiate Absolute path name and Relative path name.
6. Explain Boot block, Super block, Inode table and Data block.
7. Explain various methods to implement files.
8. Explain techniques to manage free space.

Prof. Firoz A Sherasiya # 2301CS601 (OS) ⬥ Unit 5 – I/O Management and File Management 90
Operating System (OS)
2301CS601

Thank
You

Prof. Firoz A Sherasiya


Computer Engineering Department
Darshan Institute of Engineering & Technology, Darshan University, Rajkot
[email protected]
9879879861

You might also like