0% found this document useful (0 votes)
21 views20 pages

Os Unit 4

Uploaded by

Yash Patel
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)
21 views20 pages

Os Unit 4

Uploaded by

Yash Patel
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/ 20

UNIT-IV

File Management: Concept of File, 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 and
performance.
I/O Hardware: I/O devices, Device controllers, Direct memory access Principles of I/O
Software: Goals of Interrupt handlers, Device drivers, Device independent I/O software.

File System

File Concept:

Computers can store information on various storage media such as, magnetic disks,
magnetic tapes, optical disks. The physical storage is converted into a logical storage
unit by operating system. The logical storage unit is called FILE. A file is a collection of
similar records. A record is a collection of related fields that can be treated as a unit by
some application program. A field is some basic element of data. Any individual field
contains a single value. A data base is collection of related data.

Student Marks Marks Fail/Pas


KUMA 85 86 P
LAKSH 93 92 P
DATA FILE

Student name, Marks in sub1, sub2, Fail/Pass is fields. The collection of fields is
called a RECORD. RECORD:
LAKSH 93 92 P
Collection of these records is called a data file.

FILE ATTRIBUTES :

1. Name : A file is named for the convenience of the user and is referred by its
name. A name is usually a string of characters.
2. Identifier : This unique tag, usually a number ,identifies the file within the file system.
3. Type : Files are of so many types. The type depends on the extension of the file.

Example:
.exe Executable file
1
.obj Object file
.src Source file
4. Location : This information is a pointer to a device and to the location of
the file on thatevice.

5. Size : The current size of the file (in bytes, words,blocks).


6. Protection : Access control information determines who can do reading,
writing, executing and so on.
7. Time, Date, User identification : This information may be kept for
creation, last modification,last use.

FILE OPERATIONS

1. Creating a file : Two steps are needed to create a file. They are:
 Check whether the space is available ornot.
 If the space is available then made an entry for the new file in the
directory. The entry includes name of the file, path of the file,etc…
2. Writing a file : To write a file, we have to know 2 things. One is name of the
file and second is the information or data to be written on the file, the system searches
the entired given location for the file. If the file is found, the system must keep a write
pointer to the location in the file where the next write is to take place.
3. Reading a file : To read a file, first of all we search the directories for the file, if
the file is found, the system needs to keep a read pointer to the location in the file where
the next read is to take place. Once the read has taken place, the read pointer is updated.
4. Repositioning within a file : The directory is searched for the appropriate
entry and the current file position pointer is repositioned to a given value. This
operation is also called file seek.
5. Deleting a file : To delete a file, first of all search the directory for named
file, then released the file space and erase the directoryentry.
6. Truncating a file : To truncate a file, remove the file contents only but, the
attributes are as itis.

FILE TYPES:The name of the file split into 2 parts. One is name and second is
Extension. The file type is depending on extension of the file.

File Type Extension Purpose


Executable .exe Ready to run
.com (or) ready
.bin to run
machine

2
Source code .c Source code in
.cpp various
.asm languages.
Object .obj Compiled,
.o machine
Batch .bat Commands to
.sh the command

3
Text .txt Textual
.doc data,
docume
nts
Word processor .doc Various word
.wp proc
.rtf essor
form
ats
Library .lib Libraries of
.dll routines for
Print or View .pdf Binary file in a
.jpg format for
Archive .arc Related files
.zip grouped into a
Multimedia .mpeg Binary file
.mp3 containing
.avi audio
or audio/video

FILE STRUCTURE

File types also can be used to indicate the internal structure of the file. The operating
system requires that an executable file have a specific structure so that it can determine
where in memory to load the file and what the location of the first instruction is. If OS
supports multiple file structures, the resulting size of OS is large. If the OS defines 5
different file structures, it needs to contain the code to support these file structures. All
OS must support at least one structure that of an executable file so that the system is able
to load and run programs.

INTERNAL FILE STRUCTURE

In UNIX OS, defines all files to be simply stream of bytes. Each byte is individually
addressable by its offset from the beginning or end of the file. In this case, the logical
record size is 1 byte. The file system automatically packs and unpacks bytes into
physical disk blocks, say 512 bytes per block.

The logical record size, physical block size, packing determines how many logical
records are in each physical block. The packing can be done by the user’s application
program or OS. A file may be considered a sequence of blocks. If each block were 512
bytes, a file of 1949 bytes would be allocated 4 blocks (2048 bytes). The last 99 bytes
4
would be wasted. It is called internal fragmentation all file systems suffer from internal
fragmentation, the larger the block size, the greater the internal fragmentation.
FILE ACCESS METHODS

Files stores information, this information must be accessed and read into computer
memory. There are so many ways that the information in the file can be accessed.

1. Sequential file access:

Information in the file is processed in order i.e. one record after the other.
Magnetic tapes are supporting this type of file accessing.
Eg : A file consisting of 100 records, the current position of read/write head is 45th
record, suppose we want to read the 75th record then, it access sequentially from 45,
46, 47
…….. 74, 75. So the read/write head traverse all the records between 45 to 75.

2. Direct access:

Direct access is also called relative access. Here records can read/write randomly
without any order. The direct access method is based on a disk model of a file, because
disks allow random access to any file block.
Eg : A disk containing of 256 blocks, the position of read/write head is at 95th block. The
block is to be read or write is 250th block. Then we can access the 250th block directly
without any restrictions.

Eg : CD consists of 10 songs, at present we are listening song 3, If we want to listen


song 10, we can shift to 10.

3. Indexed Sequential File access

The main disadvantage in the sequential file is, it takes more time to access a Record
.Records are organized in sequence based on a key field.
Eg :
A file consisting of 60000 records,the master index divide the total records into 6 blocks,
each block consisiting of a pointer to secondary index.The secondary index divide the
10,000 records into 10 indexes.Each index consisting of a pointer to its orginal
5
location.Each record in the index file consisting of 2 field, A key field and a pointer field.

DIRECTORY STRUCTURE
Sometimes the file system consisting of millions of files,at that situation it is very hard
to manage the files. To manage these files grouped these files and load one group into
one partition.

Each partition is called a directory .a directory structure provides a mechanism for


organizing many files in the file system.

OPERATION ON THE DIRECTORIES :


1. Search for a file : Search a directory structure for requiredfile.

2. createafile : New files need to be created, added to thedirectory.

3. Deleteafile : When a file is no longer needed,we want to remove it fromthe

directory.

4. List adirectory : We can know the list of files in thedirectory.

5. Renameafile : When ever we need to change the name of the file,wecanchange


thename.
6. Traverse the file system : We need to access every directory and every file
with in a directory structure we can traverse the file system

6
The various directory structures

1. Single level directory:

The directory system having only one directory,it consisting of


all files some times it is said to be root directory.

E.g :- Here directory containing 4 files (A,B.C,D).the advantage of the scheme


is its simplicity and the ability to locate files quickly.The problem is different
users may accidentally use the same names for their files.

E.g :- If user 1 creates a files caled sample and then later user 2 to creates a file
called sample,then user2’s file will overwrite user 1 file.Thats why it is not used
in the multi user system.

2. Two level directory:

The problem in single level directory is different user may be accidentally use
7
the same name for their files. To avoid this problem each user need a private
directory,

Names chosen by one user don't interfere with names chosen by a different
user.

Root directory is the first level directory.user 1,user2,user3 are user level of
directory A,B,C are files.

3. Tree structured directory:

Two level directory eliminates name conflicts among users but it is not
satisfactory for users with a large number of files.To avoid this create the sub-
directory and load the same type of files into the sub-directory.so, here each can
have as many directories are needed.

8
There are 2 types of path

1. Absoulte path
2. Relative path
Absoulte path : Begging with root and follows a path down to specified
files giving directory, directory name on the path.
Relative path : A path from current directory.

4. Acyclic graphdirectory

Multiple users are working on a project, the project files can be stored in a
comman sub-directory of the multiple users. This type of directory is called
acyclic graph directory .The common directory will be declared a shared
directory. The graph contain no cycles with shared files, changes made by one
user are made visible to other users.A file may now have multiple absolute paths.
when shared directory/file is deleted, all pointers to the directory/ files also to be
removed.

5. General graph directory:


When we add links to an existing tree structured directory, the tree
structure is destroyed, resulting is a simple graph structure.

Advantages :- Traversing is easy. Easy sharing is possible.

9
File system structure:
Disk provides the bulk of secondary storage on which a file system is maintained.
They have 2 characteristics that make them a convenient medium for storing
multiple files.
1. A disk can be rewritten in place. It is possible to read a block from
the disk, modify the block, and write it back into same place.
2. A disk can access directly any block of information it contains.

Application Programs

Logical File System

File Organisation Module

Basic File System

I/O Control

Devices

I/O Control: consists of device drivers and interrupt handlers to transfer


information between the main memory and the disk system. The device driver
writes specific bit patterns to special locations in the I/O controller’s memory to
tell the controller which device location to act on and what actions to take.
The Basic File System needs only to issue commands to the appropriate device
driver to read and write physical blocks on the disk. Each physical block is
identified by its numeric disk address (Eg. Drive 1, cylinder 73, track2, sector
10).

The File Organization Module knows about files and their logical blocks and
physical blocks. By knowing the type of file allocation used and the location of
the file, file organization module can translate logical block address to physical
addresses for the basic file system to transfer. Each file’s logical blocks are
numbered from 0 to n. so, physical blocks containing the data usually do not
match the logical numbers. A translation is needed to locate each block.
10
The Logical File System manages all file system structure except the actual data
(contents of file). It maintains file structure via file control blocks. A file control
block (inode in Unix file systems) contains information about the file, ownership,
permissions, location of the file contents.

Allocation Methods – Contiguous


An allocation method refers to how disk blocks are allocated for files:
Contiguous allocation – each file occupies set of contiguous blocks o Best
performance in most cases
o Simple – only starting location (block #) and length (number ofblocks) are required
o Problems include finding space for file, knowing file size, external
fragmentation, need for compaction off-line (downtime) or on-line

Linked
Linked allocation – each file a linked list
of blocks o File ends at nil pointer
o No external fragmentation
o Each block contains pointer to next block
o No compaction, external fragmentation
o Free space management system called when new block needed
o Improve efficiency by clustering blocks into groups but
increases internal fragmentation
o Reliability can be a problem
o Locating a block can take many I/Os
and disk seeks FAT (File Allocation
Table) variation
11
o Beginning of volume has table, indexed by block number
o Much like a linked list, but faster on disk and cacheable

12
File-Allocation Table

Indexed allocation
o Each file has its own index block(s) of pointers to its data blocks

13
Secondary storage structure:
Overview of mass storage structure

Magnetic disks: Magnetic disks provide the bulk of secondary storage for modern
computer system. Each disk platter has a flat circular shape, like a CD. Common platter
diameters range from 1.8 to 5.25 inches. The two surfaces of a platter are covered with
a magnetic material. We store information by it magnetically on the platters.

Moving head disk mechanism

A read /write head files just above each surface of every platter. The heads are attached
to a disk arm that moves all the heads as a unit. The surface of a platter is logically
divided into circular tracks, which are sub divided into sectors. The set of tracks that
are at one arm position makes up a cylinder. There may be thousands of concentric
cylinders in a disk drive, and each track may contain hundreds of sectors.

When the disk in use, a driver motor spins it at high speed. Most drivers rotate 60 to
200 times per second. Disk speed has 2 parts. The transfer rate is the at which data
flow between the drive and the computer. To read/write, the head must be positioned
at the desired track and at the beginning of the desired sector on the track, the time it
takes to position the head at the desired track is called seek time. Once the track is
selected the disk controller waits until desired sector reaches the read/write head. The
time it takes to reach the desired sector is called latency time or rotational dealy-
access time. When the desired sector reached the read/write head, then the real data
transferring starts.
14
A disk can be removable. Removable magnetic disks consist of one platter, held in a
plastic case to prevent damage while not in the disk drive. Floppy disks are in
expensive removable magnetic disks that have a soft plastic case containing a flexible
platter. The storage capacity of a floppy disk is 1.44MB.

A disk drive is attached to a computer by a set of wires called an I/O bus. The data
transfer on a bus are carried out by special processors called controllers. The host
controller is the controller at the computer end of the bus. A disk controller is built
into each disk drive . to perform i/o operation, the host controller operates the disk
drive hardware to carry out the command. Disk controllers have built in cache, data
transfer at the disk drive happens b/w cache and disk surface. Data transfer at the host,
occurs b/w cache and host controller.

Magnetic Tapes: magnetic tapes was used as an early secondary storage medium. It is
permanent and can hold large amount of data. It access time is slow compared to main
memory and magnetic disks. Tapes are mainly used for back up, for storage of
infrequently used information. Typically they store 20GB to 200GB.

Disk Structure: most disks drives are addressed as large one dimensional arrays of
logical blocks. The one dimensional array of logical blocks is mapped onto the
sectors of the disk sequentially. sector 0 is the fist sector of the first track on the
outermost cylinder. The mapping proceeds in order through that track, then through
the rest of the tracks in that cylinder, and then through the rest of the cylinder from
outermost to inner most. As we move from outer zones to inner zones, the number of
sectors per track decreases. Tracks in outermost zone hold 40% more sectors then
innermost zone. The number of sectors per track has been increasing as disks
technology improves, and the outer zone of a disk usually has several hundred sectors
per track. Similarly, the number of cylinders per disk has been increasing; large disks
have tens of thousands of cylinders.

Disk Scheduling Algorithms

Disk scheduling algorithms are used to allocate the services to the I/O requests on the
disk . Since seeking disk requests is time consuming, disk scheduling algorithms try to
minimize this latency. If desired disk drive or controller is available, request is served
immediately. If busy, new request for service will be placed in the queue of pending
requests. When one request is completed, the Operating System has to choose which
pending request to service next. The OS relies on the type of algorithm it needs when
dealing and choosing what particular disk request is to be processed next. The
objective of using these algorithms is keeping Head movements to the amount as
15
possible. The less the head to move, the faster the seek time will be. To see how it
works, the different disk scheduling algorithms will be discussed and examples are also
provided for better understanding on these different algorithms.

1. First Come First Serve(FCFS)

It is the simplest form of disk scheduling algorithms. The I/O requests are served or
processes according to their arrival. The request arrives first will be accessed and
served first. Since it follows the order of arrival, it causes the wild swings from the
innermost to the outermost tracks of the disk and vice versa . The farther the location
of the request being serviced by the read/write head from its current location, the
higher the seek time will be.

Example: Given the following track requests in the disk queue, compute for the
Total Head Movement (THM) of the read/write head :

95, 180, 34, 119, 11, 123, 62, 64

Consider that the read/write head is positioned at location 50. Prior to this track location
199 was serviced. Show the total head movement for a 200 track disk (0-199).
Solution:

16
Total Head Movement Computation: (THM) =

(180 - 50) + (180-34) + (119-34) + (119-11) + (123-11) + (123-62) + (64-62) =

130 + 146 + 85 + 108 + 112 + 61 + 2 (THM) = 644 tracks

Assuming a seek rate of 5 milliseconds is given, we compute for the seek time
using the formula: Seek Time = THM * Seek rate
=644 * 5 ms
Seek Time = 3,220 ms.

2. Shortest Seek Time First(SSTF):

This algorithm is based on the idea that that he R/W head should proceed to the track
that is closest to its current position . The process would continue until all the track
requests are taken care of. Using the same sets of example in FCFS the solution are as
follows:
Solution:

(THM) = (64-50) + (64-11) + (180-11) =

14 + 53 + 169 (THM) = 236 tracks

Seek Time = THM * Seek rate

= 236 * 5ms
Seek Time = 1,180 ms
In this algorithm, request is serviced according to the next shortest distance. Starting at
50, the next shortest distance would be 62 instead of 34 since it is only 12 tracks away
from 62 and 16 tracks away from 34 . The process would continue up to the last track
request. There are a total of 236 tracks and a seek time of 1,180 ms, which seems to be
17
a better service compared with FCFS which there is a chance that starvation3 would
take place. The reason for this is if there were lots of requests closed to each other, the
other requests will never be handled since the distance will always be greater.

3. SCAN Scheduling Algorithm

This algorithm is performed by moving the R/W head back-and-forth to the innermost
and outermost track. As it scans the tracks from end to end, it process all the requests
found in the direction it is headed. This will ensure that all track requests, whether in
the outermost, middle or innermost location, will be traversed by the access arm
thereby finding all the requests. This is also known as the Elevator algorithm. Using the
same sets of example in FCFS the solution are as follows:

Solution:

This algorithm works like an elevator does. In the algorithm example, it scans down
towards the nearest end and when it reached the bottom it scans up servicing the
requests that it did not get going down. If a request comes in after it has been
scanned, it will not be serviced until the process comes back down or moves back up.
This process moved a total of 230 tracks and a seek time of 1,150. This is optimal
than the previous algorithm.

4 .Circular SCAN (C-SCAN)Algorithm

This algorithm is a modified version of the SCAN algorithm. C-SCAN sweeps the
disk from end-to-end, but as soon it reaches one of the end tracks it then moves to the
18
other end track without servicing any requesting location. As soon as it reaches the
other end track it then starts servicing and grants requests headed to its direction. This
algorithm improves the unfair situation of the end tracks against the middle tracks.
Using the same sets of example in FCFS the solution are as

follows:

Notice that in this example an alpha3 symbol (α) was used to represent the dash line.
This return sweeps is sometimes given a numerical value which is included in the
computation of the THM . As analogy, this can be compared with the carriage return
lever of a typewriter. Once it is pulled to the right most direction, it resets the typing
point to the leftmost margin of the paper . A typist is not supposed to type during the
movement of the carriage return lever because the line spacing is being adjusted . The
frequent use of this lever consumes time, same with the time consumed when the R/W
head is reset to its starting position.

Assume that in this example, α has a value of 20ms, the computation


would be as follows: (THM) = (50-0) + (199-62) + α
= 50 + 137 + 20 (THM)

= 207 tracks

Seek Time = THM * Seek rate

= 187 * 5ms Seek Time = 935 ms .

The computation of the seek time excluded the alpha value because it is not an actual
seek or search of a disk request but a reset of the access arm to the starting position

19
20

You might also like