Unit 2
Unit 2
Unit 2
INODES
● Inodes exist in a static form on disk, and the kernel reads them into an
in-core
inode to manipulate them. Disk inodes consist of the following fields:
File owner identifier. Ownership is divided between an individual owner
and a
"group" owner and defines the set of users who have access rights to a
file. The superuser bas access rights to all files in the system.
File type. Files may be of type regular, directory, character or block
special, or FIFO (pipes).
File access permissions. The system protects files according to three
classes: the owner and the group owner of the file, and other users; each
class bas access rights to read, write and execute the file, which can be
set individually.
File access times, giving the time the file was last modified, when it was
last Excellence and Service
CHRIST
Deemed to be University
Number of links to the file, representing the number of names the file has in
the
directory hierarchy.
Table of contents for the disk addresses of data in a file. Although users treat
the data in a file as a logical stream of bytes, the kernel saves the data in
discontiguous disk blocks. The inode identifies the disk blocks that contain
the file's data.
File size. Data in a file is addressable by the number of bytes from the
beginning of the file, starting from byte offset 0, and the file size is 1 greater
than the highest byte offset of data in the file. For example, if a user creates
a file and writes only 1 byte of Excellence
data atandbyte
Service offset 1000 in the file, the size of
CHRIST
Deemed to be University
The contents of an inode change when changing the contents of a file or when changing its
owner, permission, or link settings etc.
Excellence and Service
CHRIST
Deemed to be University
Algorithm
for
Allocation of
In-Core
modes
For example, suppose a user creates three files, A, B and C, each consisting of 10 disk
blocks of storage, and suppose the system allocated storage for the three files contiguously. If
the user then wishes to add 5 blocks of data to the middle file, B, the kernel would have to copy
file B to a place in the file system that had room for 15 blocks of storage
● Input to this algorithm(ialloc) is a file, and that file is searching for the
inode which is to be assigned for a process.
● This will whether the super block is locked? Means some other process is
using super block for some other purpose.
● If SB is locked, then process requested for an inode goes to sleep and waits
till super block is unlock.
● If SB is not locked, it will check whether super block inode list is empty, if
yes then lock that super block.
● After locking that super block, process has to get the remembered inode for
inode search. And it will search for the free inode in the disk, if the free
inode is not present in the disk then it will return “No inode to the process”.
● But if it has free inode on the disk, then it will get all free inode from the
disk to fill the superblock and superblock become unblock. SB was locked
bcoz there was no inode present in SB.
● If the super block list of free inodes has room for more free inode numbers,
the kernel places the inode umbers on the list, increments the index to the
next free inode and proceeds.
● But if the list of free inodes if full as in figure(1),the kernel compares the
inode number it has freed to the remembered inode number that will start the
next disk search.
● Starting with the free inode list, if kernel frees inode 499,it makes 499
remembered inode and evicts number 535 from the free list.
● If the kernel then frees inode number 601,it does not change the contents of
the free list. Instead it will be stored in the disk.
● When it later uses up the inodes in the super block free list, it will search the
disk for free inodes starting from inode number 499 and find inodes 535 and
601 again.
● When a process writes data to a file, the kernel must allocate disk
blocks from the file system for direct data blocks and, sometimes, for
indirect blocks. The file system super block contains an array that is
used to cache the numbers of free disk blocks in the file system.
● The utility program mkfs(make file system) organizes the data blocks
of a file system in a linked list, such that each link of the list is a disk
block that contains an array of free disk block numbers, and one array
entry is the number of the next block of the linked list.
open()
The open() system call is used to provide access to a file in a file system. This
system call allocates resources to the file and provides a handle that the process uses to
refer to the file. A file can be opened by multiple processes at the same time or be
restricted to one process. It all depends on the file organization and file system.
close()
The close() system call is used to terminate access to a file system. Using this
system call means that the file is no longer required by the program and so the
buffers are flushed, the file metadata is updated and the file resources are de-
allocated.
read()
The read() system call is used to access data from a file that is stored in the file
system. The file to read can be identified by its file descriptor and it should be
opened using open() before it can be read. In general, the read() system calls takes
three arguments i.e. the file descriptor, buffer which stores read data and number of
bytes to be read from the file.
Excellence and Service
CHRIST
Deemed to be University
write()
The write() system calls writes the data from a user buffer into a device such as a
file. This system call is one of the ways to output data from a program. In general,
the write system calls takes three arguments i.e. file descriptor, pointer to the buffer
where data is stored and number of bytes to write from the buffer.
2. cat f2 - Creates a file by name f2 with some content, cntl+C / cntrl+D to save
and quit.
● The system calls stat and fstat allow processes to query the status of
files, returning information such as the file type, file owner, access
permissions, file size, number of links, inode number, and file access
times. The syntax for the system calls is.
○ stat(pathname, statbuffer);
○ fstat (fd, statbuffer);
● where pathname is a file name, fd is a file descriptor returned by a
previous open call, and statbuffer is the address of a data structure in
the user process that will contain the status information of the file on
completion of the call.
● The system calls simply write the fields of the mode into statbuffer.
The kernel has a mount table with entries for every mounted file system. Each
mount table entry contains
● a device number that identifies the mounted file system (this is the logical
file system number mentioned previously);
● a pointer to a buffer containing the file system super block;
● a pointer to the root inode of the mounted file system (“/" of the "idev/dskl“
file system in Figure 5.22);
● a pointer to the mode of the directory that is the mount point ("usr" of the
root file system in Figure 5.22).
LINK
● The link system call links a file to a new name in the file system directory
structure, creating a new directory entry for an existing mode. The syntax for
the link system call is
link(source file name, target file name);
● where source file name is the name of an existing file and target file name is
the new (additional) name the file will have after completion of the link cal'.
● The file system contains a path name for each link the file has, and processes
can access the file by any of the path names. The kernel does not know
which name was the original file name, so no file name is treated specially.
For example, after executing the system calls
link ("iusr/srciuts/sys", "/usr/include/sys");
link (lusr/include/realfile.h", "/usr/src/uts/sysitestfile.h");
● the following three path names refer to the same file:
"/usr/src/uts/sys/testfile.h",
"/usr/include/sys/testfile.h", and lusr/include/realfile"
UNLINK
● The unlink system call removes a directory entry for a file. The syntax for
the`unlink call is
○ unlink(pathname);
● where pathname identifies the name of the file to be unlinked from the
directory hierarchy. If a process unlinks a given file, no file is accessible by
that name until another directory entry with that name is created.
In the following code fragment, for example,
unlink("myfile");
fd=open("myfile", O_RDONLY);
● the open call should fail, because the current directory no longer contains a
file called myfile. If the file being unlinked is the last link of the file, the
kernel eventually frees its data blocks. However, if the file had several
links, it is still accessible by its other names.
O U
K Y
AN
T H