0% found this document useful (0 votes)
40 views6 pages

File 1. File Concept

A file is a named collection of related information stored on secondary storage. It has attributes like a name, size, and location. There are several ways to access and organize files on a disk including sequentially, directly by record number, or through indexes. Files are allocated storage on disks either contiguously in a consecutive block range, through linked lists of scattered blocks, or via an index block containing pointers to all blocks for a file.

Uploaded by

Karthi
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)
40 views6 pages

File 1. File Concept

A file is a named collection of related information stored on secondary storage. It has attributes like a name, size, and location. There are several ways to access and organize files on a disk including sequentially, directly by record number, or through indexes. Files are allocated storage on disks either contiguously in a consecutive block range, through linked lists of scattered blocks, or via an index block containing pointers to all blocks for a file.

Uploaded by

Karthi
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/ 6

FILE

1. File Concept
A file is a named collection of related information that is recorded on secondary storage.
Commonly, files represent programs (both source and object forms) and data. Many different
types of information may be stored in a file — source or executable programs, numeric or text
data, photos, music, video, and so on. A file has a certain defined structure, which depends on its
type.
 A text file is a sequence of characters organized into lines (and possibly pages).
 A source file is a sequence of functions, each of which is further organized as
declarations followed by executable statements.
 An executable file is a series of code sections that the loader can bring into memory
and execute.
File Attributes
A file is named and is referred to by its name. A name is usually a string of characters, such
as example.c. A file’s attributes vary from one operating system to another but typically
consist of these:
 Name : The symbolic file name is the only information kept in human-readable form.
 Identifier : This unique tag, usually a number, identifies the file within the file system.
 Type : This information is needed for systems that support different types of files.
 Location : This information is a pointer to a device and to the location of the file on that
device.
 Size. The current size of the file (in bytes, words, or blocks).
 Protection : Access-control information determines who can do reading, writing,
executing,
 Time, date, and user identification : This information may be kept for creation, last
modification, and last use. These data can be useful for protection, security, and usage
monitoring.
File Operations
A file is an abstract data type. To define a file properly, we need to consider the operations
that can be performed on files. The operating system can provide system calls to create, write,
read, reposition, delete, and truncate files.
Creating a file. Two steps are necessary to create a file. First, space in the file system must be
found for the file. Second, an entry for the new file must be made in the directory.
Writing a file. To write a file, we make a system call specifying both the name of the file and
the information to be written to the file.
 Given the name of the file, the system searches the directory to find the file’s location.
 The system must keep a write pointer to the location in the file where the next write is to
take place.
 The write pointer must be updated whenever a write occurs.
Reading a file. To read from a file, we use a system call that specifies the name of the file and
where (in memory) the next block of the file should be put.
 Again, the directory is searched for the associated entry, and 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.
Repositioning within a file. The directory is searched for the appropriate entry, and the current-
file-position pointer is repositioned to a given value.
Deleting a file. To delete a file, we search the directory for the named file. Having found the
associated directory entry, we release all file space, so that it can be reused by other files, and
erase the directory entry.
Truncating a file. The user may want to erase the contents of a file but keep its attributes.
Rather than forcing the user to delete the file and then recreate it, this function allows all
attributes to remain unchanged

 The operating system keeps a table, called the open-file table, containing information
about all open files.
 When a file operation is requested, the file is specified via an index into this table, so no
searching is required.
 When the file is no longer being actively used, it is closed by the process,
 and the operating system removes its entry from the open-file table.
 create() and delete() are system calls that work with closed rather than open files.
 The open-file table also has an open count associated with each file to indicate how many
processes have the file open.
 Each close() decreases this open count, and when the open count reaches zero.
 The file is no longer in use, and the file’s entry is removed from the open-file table.

File Types
The file type is to include the type as part of the file name. The name is split into two parts
— a name and an extension, usually separated by a period. Examples include resume.docx,
server.c, and ReaderThread.cpp.
2 Access Methods
Files store information. When it is used, this information must be accessed and read into
computer memory. The information in the file can be accessed in several ways.
Sequential Access
 The simplest access method is sequential access.
 Information in the file is processed in order, one record after the other.
 For example, editors and compilers usually access files in this fashion.
 Reads and writes make up the bulk of the operations on a file.
 A read operation — read next()— reads the next portion of the file and automatically
advances a file pointer
 Similarly, the write operation — write next()— appends to the end of the file and advances
to the end of the newly written material (the new end of file).
 Sequential access, which is depicted in Figure.
Direct Access
 Another method is direct access (or relative access).
 A file is made up of fixed-length logical records that allow programs to read and write
records in no particular order.
 The direct-access method is based on a disk model of a file, since disks allow random
access to any file block.
 The file is viewed as a numbered sequence of blocks or records.
Example : we may read block 14, then read block 53, and then write block 7. There are
norestrictions on the order of reading or writing for a direct-access file.
 Direct-access files are of great use for immediate access to large amounts of information.
 The file operations must be modified to include the block number as a parameter.
 read(n) write(n), where n is the block number.
 The block number provided by the user to the operating system is normally a relative
block number.
 A relative block number is an index relative to the beginning of the file.
 Thus, the first relative block of the file is 0, the next is 1, and so on.
An alternative approach is to retain read next() andwrite next(), as with sequential access,
and to add an operation posi-tion file(n) where n is the block number. Then, to effect a
read(n),we would position file(n) and then read next().
Index Access Methods
 Other access methods can be built on top of a direct-access method.
 These methods generally involve the construction of an index for the file.
 The index, like an index in the back of a book, contains pointers to the various blocks.
 To find a record in the file, we first search the index and then use the pointer to access
the file directly and to find the desired record.
 With large files, the index file itself may become too large to be kept in memory.
 One solution is to create an index for the index file.
 The primary index file contains pointers to secondary index files, which point to the
actual data items.

File Allocation Methods


The allocation methods define how the files are stored in the disk blocks. There are three main
disk space or file allocation methods.
 Contiguous Allocation
 Linked Allocation
 Indexed Allocation
The main idea behind these methods is to provide:
 Efficient disk space utilization.
 Fast access to the file block

Contiguous Allocation
Contiguous allocation requires that each file occupy a set of contiguous blocks on the disk. Disk
addresses define a linear ordering on the disk.
The directory entry for a file with contiguous allocation contains
 Address of starting block
 Length of the allocated portion.

The file ‘mail’ in the following figure starts from the block 19 with length = 6 blocks. Therefore,
it occupies 19, 20, 21, 22, 23, 24 blocks.

Linked List Allocation

With linked allocation, each file is a linked list of disk blocks; the disk blocks may be scattered
anywhere on the disk. The directory contains a pointer to the first and last blocks of the file. Each
block contains a pointer to the next block
For example,

 a file of five blocks might start at block 9 and continue at block 16,
 then block 1, then block 10, and finally block 25

The file ‘jeep’ in following image shows how the blocks are randomly distributed. The
last block (25) contains -1 indicating a null pointer and does not point to any other block.

Indexed Allocation
In this scheme, a special block known as the Index block contains the pointers to all the blocks
occupied by a file. Each file has its own index block. The ith entry in the index block contains
the disk address of the ith file block. The directory entry contains the address of the index block
as shown in the image:

You might also like