munotes®

What a File Is

Get access to whole semester resourcesSemester Pass

Chapter Ninety-Eight

Syllabus topic Module 2, "File System Interface - File Concept"

Pages 394 to 397 of 452

In one line

A file is a named sequence of bytes on secondary storage, and everything the system knows about it besides its contents is called its attributes.

The definition

A file is a named collection of related information recorded on secondary storage. It is the smallest logical unit of storage: the operating system offers nothing smaller with a name.

From the operating system's point of view a file is a sequence of bytes and nothing more. It does not know whether it holds a program, a picture or a letter. That is the whole reason a file system is usable by programs nobody had thought of when it was written, and it is the answer to whatever a question asks about the structure the operating system imposes on a file.

Who gives a file meaningHow
the program that wrote itby the format it chose
the userby the name and often the extension
the operating systemit does not, beyond a few types it must recognise, such as a program it can execute

The attributes

The list a question asks for, and it is worth learning all nine.

AttributeWhat it is
namethe only part a person uses; kept in the directory
identifiera number that names the file inside the file system, with no name in it: the inode number
typefor systems that distinguish them: regular file, directory, device, and so on
locationa pointer to the file's blocks on the device
sizein bytes, and sometimes also in blocks
protectionwho may read, write, and execute it
timesof creation, last modification and last access
ownerthe user, and usually a group as well
link counthow many names in the directory tree refer to it

The attributes are not in the file. They are kept in the directory structure, on the disk, beside the file rather than inside it. That is why the size of a file and the bytes of a file are two separate things, and it is why reading a file's name costs a directory access and not a read of the file.

What the lab machine keeps

Every attribute above can be asked for by name, and the figures here are worth reading twice.

$ printf 'hello\n' > greeting.txt
$ stat -c 'size %s bytes, allocated %b blocks of 512, links %h, type %F, mode %A, owner %U' greeting.txt
size 6 bytes, allocated 8 blocks of 512, links 1, type regular file, mode -rw-r--r--, owner student
$ echo "$(stat -c %s greeting.txt) bytes of data occupy $(( $(stat -c %b greeting.txt) * 512 )) bytes of disk"
6 bytes of data occupy 4096 bytes of disk
$ od -c greeting.txt
0000000   h   e   l   l   o  \n
0000006
munotes.in394

What a File Is

Three things are proved there.

The machine saysWhat it means
six bytes of data occupy 4096 bytes of diskthe file system allocates a block at a time, so a six byte file takes a whole 4 kilobyte block. 4090 bytes are wasted, and that is Chapter seventy two's internal fragmentation in a file system
h e l l o \n and then 0000006the file really is a sequence of bytes: five letters and the newline printf wrote, and no structure of any kind around them
type regular file, mode -rw-r--r--, owner studentthe attributes are kept by the system and answered without reading the file at all

The 4,090 wasted bytes are the reason a file system chooses its block size carefully, and Chapter one hundred returns to it: a large block wastes more on small files and transfers large files faster.

The operations

The six MU's textbook names, and the two that surround them.

OperationWhat the system call does
createfind space, and make a directory entry
writewrite at the current position, and advance it
readread from the current position, and advance it
reposition, or seekmove the current position without transferring anything
deleterelease the space and remove the directory entry
truncatekeep the attributes, throw the contents away, set the size to zero
openChapter ninety nine: find the file and set up the state the other calls use
closewrite the state back and release it

Every one of read, write and seek works on one current position per open file, and that position is the subject of the next chapter.

Truncate is not delete. The file keeps its name, its owner, its permissions and its identifier: only the contents go. It exists because a program that rewrites a file wants the name and permissions preserved.

Internal structure

Three ways a system can see the inside of a file, and a question about "file structure" wants the distinction.

ModelWhat the system knowsWho uses it
no structure: a stream of bytesnothing at allUNIX, Linux, and everything modern
records: a sequence of fixed or variable length recordswhere each record startsolder systems, and databases inside their own files
a structured file, a defined format the system understandsthe file's whole layoutsystems that support only a few file types

A system that understands file formats must be changed for every new format; a system that sees bytes need never change. The byte stream won, and the cost is that every program has to know its own formats.

munotes.in395

What a File Is

Packing records into blocks

Since the disk deals in blocks and a program may deal in records, something has to put one into the other.

IfThen
a record is smaller than a blockseveral records go in one block, and the leftover space is internal fragmentation
a record is larger than a blockthe record spans blocks, and reading it needs two transfers
records do not divide the block evenlythe remainder is wasted, or a record is split

The 4,090 bytes measured above is this arithmetic at its worst: one record of six bytes in a block of 4096.

Distinctions that carry marks

A file's contentsA file's attributes
Where they livein data blocksin the directory structure and the inode
Read byreadstat
Needed to list a directorynoyes
SizeAllocated space
What it countsthe bytes of datathe blocks given to the file
The six byte file above64096
The difference isinternal fragmentation
DeleteTruncate
The directory entrygoesstays
The attributesgostay
The contentsgogo
The size afterwardsthere is no filezero

What it does not mean

A file is not a stream of characters with a meaning. It is bytes; the meaning is the program's.

An extension is not a type. On most systems it is a convention the programs agree on, and the operating system does not enforce it.

The size is not the space used. A six byte file used 4,096 bytes on the lab machine.

The identifier is not the name. It is a number, and Chapter one hundred one shows one file with two names and one number.

Attributes are not stored in the file. Reading them does not read the file.

Quick revision

  • A file is a named collection of related information on secondary storage, and the

smallest logical unit of storage.

  • To the operating system it is a sequence of bytes with no structure; the meaning belongs to

the program.

  • Attributes: name, identifier, type, location, size, protection, times, owner, link count,

and they live in the directory structure, not in the file.

  • Measured: a 6 byte file occupies 4096 bytes of disk, so 4,090 bytes are wasted:

internal fragmentation in a file system.

  • Operations: create, write, read, reposition, delete, truncate, with open and close

around them.

  • Truncate keeps the name and the attributes and throws away only the contents.
  • File structure models: no structure (the byte stream, and it won), records, or a

structured file the system understands, which needs changing for every new format.

  • Records are packed into blocks, wasting the remainder, or span blocks and cost two
munotes.in396

What a File Is

transfers.

Test yourself

  1. Define a file. A named collection of related information recorded on secondary storage,

and the smallest logical unit of storage the operating system names.

  1. What structure does the operating system impose on a file's contents? None: it sees a

sequence of bytes, and the meaning belongs to the program that wrote it.

  1. List six file attributes. Name, identifier, type, location, size, protection, times, owner

and link count are nine; any six of those.

  1. Where are the attributes kept, and why does it matter? In the directory structure and the

inode, not inside the file, so they can be read without reading the file. 5. A file holds 6 bytes and the machine reports 8 blocks of 512 bytes. How much disk does it use and what is the waste called? 4,096 bytes, so 4,090 are wasted: internal fragmentation.

  1. Name the six file operations. Create, write, read, reposition or seek, delete and

truncate.

  1. Distinguish delete from truncate. Delete removes the directory entry and the attributes

with the contents; truncate keeps the name, the owner, the permissions and the identifier, and sets the size to zero.

  1. Give the three models of internal file structure, and say which is used today. No

structure, a byte stream; records; and a structured file the system understands. The byte stream is what modern systems use, and the cost is that every program must know its own formats.

munotes.in397

The rest of this subject

These notes are cut from the University's printed syllabus. Open the syllabus itself, or the past papers, for the same subject.

Issue
Done!