Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 56

Course Name: Unix and Shell Programming

Course Code: IT541E01


Unit-2
INTERNAL REPRESENTATION OF
FILES
-Babu Kumar S

MISSION VISION CORE VALUES


CHRIST is a nurturing ground for an individual’s Excellence and Service Faith in God | Moral Uprightness
holistic development to make effective contribution to Love of Fellow Beings
the society in a dynamic environment Social Responsibility | Pursuit of Excellence
CHRIST
Deemed to be University

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

Sample Disk Mode

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

Excellence and Service


CHRIST
Deemed to be University

Access an In-Core Inode

Excellence and Service


CHRIST
Deemed to be University

Algorithm
for
Allocation of
In-Core
modes

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

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

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

● To remove the drawback of previous one, the solution is brought.


○ Direct block: directly points to the data block which contains real-data.
○ Single-Indirect
○ Double-Indirect
○ Thriple-Indirect

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Figure 4.10. Directory Layout for /etc

Excellence and Service


CHRIST
Deemed to be University
CONVERSION OF A PATH NAME TO AN INODE
● Because the kernel works internally with inodes rather than with path
names, it converts the path names to inodes to access files.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

ialloc is used to allocate inode to newly opened file.

● 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.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

● 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.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

● 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.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

● When the kernel wants to allocate a block from a file system(alloc), it


allocates the next available block in the super block list.

● Once allocated, the block cannot be reallocated until it becomes free.


If the allocated block is the last available block in the super block
cache, the kernel treats it as a pointer to a block that contains a list of
block numbers, and then proceeds to use the original block number.

Excellence and Service


CHRIST
Deemed to be University

sequence of alloc and free operations


● The algorithm free for freeing a block is the reverse of the one for
allocating a block. If the super block list is not full, the block number
of the newly freed block is placed on the super block list.
● If, however, the super block list is full, the newly freed block becomes
a link block; the kernel writes the super block list into the block and
writes the block to disk. It then places the block number of the newly
freed block in the super block list: That block number is the only
member of the list.
● Starting with one entry on the super block free list. The kernel frees
block 949 and places the block number on the free list. It then allocates
a block and removes block number 949 from the free list.
● Finally, it allocates a block and removes block number 109 from the
free list. Because the super block free list is now empty, the kernel
replenishes the list by copying in the contents of block 109, the next
link on the linked list.
● Figure 4.20(d) shows the full Excellence
super andblock
Service
list and the next link block,
CHRIST
Deemed to be University

sequence of alloc and free operations

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

File System Call


System calls are required in the following situations −
• If a file system requires the creation or deletion of files. Reading and writing from files
also require a system call.
• Creation and management of new processes.
• Network connections also require system calls. This includes sending and receiving
packets.
• Access to a hardware devices such as a printer, scanner etc. requires a system call.
File Management System Call
• These system calls are responsible for file manipulation such as creating a file,
reading a file, writing into a file etc.

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.

Excellence and Service


CHRIST
Deemed to be University

File System Call

Excellence and Service


CHRIST
Deemed to be University

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.

Excellence and Service


CHRIST
Deemed to be University

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.

Excellence and Service


CHRIST
Deemed to be University

File Creation in UNIX


1. touch f1 - Creates a empty file by name f1

2. cat f2 - Creates a file by name f2 with some content, cntl+C / cntrl+D to save
and quit.

3. echo ‘MSG’ > filename


creates a file by filename specified with the content given in single quotes.

4. Vi editor-- text editor


To create a file called purchase.txt, type:
$ vi purchase.txt
Press i to insert new text. To save the file and leave vi, type ESC+:+x

Excellence and Service


CHRIST
Deemed to be University

CHANGE DIRECTORY AND CHANGE ROOT

● The algorithm chdir changes the current directory of a process.


The syntax for the chdir system call is
chdir (pathname);
● where pathname is the directory that becomes the new current
directory of the
Process.
● A process usually uses the global file system root for all path names
starting with "/". The kernel contains a global variable that points to
the mode of the global root, allocated by iget when the system is
booted.
● Processes can change their notion of the file system root via the chroot
system call This is useful if a user wants to simulate the usual file
system hierarchy and run processes there. Its syntax is
chroot (pathname) ;
Excellence and Service
CHRIST
Deemed to be University

CHANGE OWNER AND CHANGE MODE

● Changing the owner or mode (access permissions) of a file. The syntax


of the calls is
○ chown(pathname, owner, group)
○ chmod (path name, mode)
● To change the owner of a file, the kernel converts the file name to an
inode using algorithm namei. The process owner must be superuser or
match that of the file owner (a process cannot give away something
that does not belong to it).
● The kernel then assigns the new owner and group to the file, clears the
set user and set group flags, and releases the mode via algorithm iput.
After the change of ownership, the old owner loses "owner" access
rights to the file.
● To change the mode of a file, the kernel follows a similar procedure,
changing the mode flags in the mode instead of the owner numbers.
Excellence and Service
CHRIST
Deemed to be University

STAT AND FSTAT

● 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.

Excellence and Service


CHRIST
Deemed to be University

MOUNTING AND UNMOUNTING FILE SYSTEMS


● The mount system call connects the file system in a specified
section of a disk to the existing file system hierarchy, and the
umount system call disconnects a file system from the hierarchy.
The mount system call thus allows users to access data in a disk
section as a file system instead of a sequence of disk blocks.
● The syntax for the mount system call is
mount(special pathname, directory pathname, options);
● where special pathname is the name of the device special file of
the disk section containing the file system to be mounted,
directory pathname is the directory in the existing hierarchy
where the file system will be mounted (called the mount point),
and options indicate whether the file system should be mounted
"read-only“
mount (“/dev/dsk1", "/usr", 0);
Excellence and Service
CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

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).

Excellence and Service


CHRIST
Deemed to be University

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"

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

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.

Excellence and Service


CHRIST
Deemed to be University

Excellence and Service


CHRIST
Deemed to be University

O U
K Y
AN
T H

Excellence and Service

You might also like