Professional Documents
Culture Documents
Files
Contents
2
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File System in UNIX
3
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Boot Block
4
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Super Block
5
The contents here are for Aricent internal training purposes only and do not carry any commercial value
INODE
6
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Data Blocks
7
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Addresses in an Inode
The inode of a file contains 15 pointer to the disk blocks containing the
file's data contents.
First 12 point to direct blocks.
Next three point to indirect blocks.
First indirect block pointer is the address of a single indirect blocks,
an index block containing the addresses of blocks that do contain data.
Second is a double-indirect-block pointer, the address of a block that contains the
addresses of blocks that contain pointer to the actual data blocks.
The third is a triple indirect pointer.
8
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Direct and Indirect Blocks in an Inode
Data
Inode Blocks
direct0
direct1
direct2
direct3
direct4
direct5
direct6
direct7
direct8
direct9
single
indirect
double
indirect
triple
indirect
9
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Addresses in an INODE
File Size: blocksize = 1K, block address is of three bytes in the disk resident inode,
converted to four bytes in memory (1024 / 4 = 256 block addresses in one block):
direct block address: 10K
indirect block addresses: 1024/4*1K=256K
double indirect block addresses: (1024/4)^2*1K = 64M
tripe indirect block addresses: (1024/4)^3 * 1K= 16G
10
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Data structures for open files
11
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Process Table
Each process has an entry in the process table.Within each process table entry
is a table of open file descriptors .The information associated with each file
descriptor are as follows:
The file descriptor flags
A pointer to a file table entry
For every process ,by default file descriptor 0,1, and 2 are opened which
represents as follows:
Entry 0: standard input
Entry 1: standard output
Entry 2: error output
12
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Table
The kernel maintains a file table for all open files.Each file table entry
contains:
The file status flags for the file
The current file offset
A pointer to the i-node table entry for the file
13
The contents here are for Aricent internal training purposes only and do not carry any commercial value
I-node Table
Each open file has an inode that contains information about the type of file and
the other information related to file.
Mapping of a File Descriptor to an Inode.
System calls that refer to open files indicate the file is passing a file descriptor as an
argument.
The file descriptor is used by the kernel to index a table of open files for the current
process.
Each entry of the table contains a pointer to a file structure.
This file structure in turn points to the inode.
14
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Kernel data structures for open files
A single process having two different files (open on standard input(0) and on
standard output(1))
15
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Links
Hard Links
An entry in a directory consisting of a name and an i-number is called a hard link.
Hard Link is simply an alias to an existing file.
All hard links for a given inode refer to a same file ,so any changes for the file is
reflected both in the hard link as well as in the file.
To create a hard link.
ln file hardlink
For ex: ln abcfile abclink
Abclink is a hardlink to abcfile.
Contents of abclink is same as abcfile.
Removing abcfile will not free inode.
16
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Links
17
The contents here are for Aricent internal training purposes only and do not carry any commercial value
stat/fstat
stat/fstat: A process may query the status of a file (locked) file type, file owner,
access permission. file size, number of links, inode number, access time.
syntax:
stat (pathname, statbuffer);
fstat (fd, statbuffer);
18
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Information
19
The contents here are for Aricent internal training purposes only and do not carry any commercial value
dup Functions
The new file descriptor returned by dup is guaranteed to be the lowest numbered available file
descriptor. With dup2 we specify the value of the new descriptor with the filedes2 argument. If
filedes2 is already open, it is first closed. If filedes equals filedes2, then dup2 returns filedes2
without closing it
The new file descriptor that is returned as the value of the functions shares the same file table
entry as the filedes argument
20
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Kernel data structures after dup
21
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Handling System calls
Open()
Creat()
Read()
Write()
Lseek()
Close()
Dup()
Stat()/fstat()
22
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Handling System Calls
23
The contents here are for Aricent internal training purposes only and do not carry any commercial value
File Handling System calls
dup (filedes);
It duplicates the existing file descriptor. The new file descriptor is guaranteed to be
the lowest numbered
24
The contents here are for Aricent internal training purposes only and do not carry any commercial value
An Example of creat function
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
main ()
{
int fd;
fd = creat ("datafile.dat , S_IRWXU);
if (fd == -1)
printf ("\nerror in opening file");
else
printf ("\ndatafile created for read and write and is currently empty\n");
close (fd);
exit (0);
}
25
The contents here are for Aricent internal training purposes only and do not carry any commercial value
An Example of lseek and open function
28
The contents here are for Aricent internal training purposes only and do not carry any commercial value
An Example of open,read,write ,lseek system call
if (fd != -1)
{
printf ("\ndatafile opened for read/write access\n");
write (fd, message, sizeof (message));~
lseek (fd, 0L, 0); /* go to beginning of file */
else
printf ("\nFile already exists");
exit (0);
}
29
The contents here are for Aricent internal training purposes only and do not carry any commercial value
stat
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
#include<sys/types.h>
main()
{ int fd1,fd2,fd3;
struct stat bufStat1,bufStat2;
fd1=open("/etc/passwd",O_RDONLY);
fd2=open("filefstat.c",O_RDONLY);
fstat(fd1,&bufStat1);
fstat(fd2,&bufStat2);
Stevens
Maurice J Bach
Unix Internals
Uresh Vahalia
31
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Disclaimer
32
The contents here are for Aricent internal training purposes only and do not carry any commercial value
Thank you
33
The contents here are for Aricent internal training purposes only and do not carry any commercial value