You are on page 1of 33

File Management

Files

Contents

 Overview of Unix File System
– Boot Block
– Super Block
– Inode Table
– Data Block
 Data structures used by Kernel to represent an open file
 Links
– Hard Link
– Soft Link/Symbolic Link
 System Calls related to file : open,creat,read,write,lseek,dup,stat,fstat

2
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”

File System in UNIX

 Everything in UNIX is a File.
 The file system is a collection of files and directories on a disk in standard UNIX file
system format.
 Each UNIX file system contains four major parts:
– Boot Block
– Super block
– Inode Table
– Data Blocks

3
“The contents here are for Aricent internal training purposes only and do not carry any commercial value”

 A boot block contains a short loader program for booting (Bootstrap Loader) and other initialization programs. 4 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .Boot Block  A boot block may contain several physical blocks(512 bytes).

Super Block  Super block contains the key information of the file system.  Super block information: – Size of a file system and status • Label • Size • Date – Information of i-nodes • Number of i-nodes • Number of free i-nodes – Information of data blocks 5 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .

.block address = 4 bytes) – Double indirect: (128*128=16384 data blocks) – Triple indirect: (128*128*128 data blocks) 6 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .pipe.(block size =512 bytes. modification.INODE  Inode contains the following information of the file – Mode • Type:file. access time – size: the number of bytes – block count: the number of data blocks – direct blocks: pointers to the data – single indirect: pointer to a data block which pointers to the data blocks (128 data blocks).) – timestamp: creation..directory.execute – owner: who own the i-node (file. . directory.link • Access:read.write.

• File is a stream of bytes. • A data block may contains data of files or data of a directory.Data Blocks • A data block has 512 bytes. 7 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . (dependent on UNIX distribution).

– First 12 point to direct blocks. the address of a block that contains the addresses of blocks that contain pointer to the actual data blocks.File Addresses in an Inode  The inode of a file contains 15 pointer to the disk blocks containing the file's data contents. – Next three point to indirect 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” . • Second is a double-indirect-block pointer. • First indirect block pointer is the address of a single indirect blocks. an index block containing the addresses of blocks that do contain data.

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

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” . block address is of three bytes in the disk resident inode.File Addresses in an INODE  File Size: blocksize = 1K.

Data structures for open files  How do the kernel represents the open files?  Three tables are maintained by the kernel: – Process Table – Open File Table – Inode Table 11 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .

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” .The information associated with each file descriptor are as follows: – The file descriptor flags – A pointer to a file table entry  For every process .Within each process table entry is a table of open file descriptors .by default file descriptor 0.1.Process Table • Each process has an entry in the process table.

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

– This file structure in turn points to the inode. – System calls that refer to open files indicate the file is passing a file descriptor as an argument. 14 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . – The file descriptor is used by the kernel to index a table of open files for the current process.  Mapping of a File Descriptor to an Inode. – Each entry of the table contains a pointer to a file structure.I-node Table  Each open file has an inode that contains information about the type of file and the other information related to file.

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

– ln file hardlink – For ex: ln abcfile abclink – Abclink is a hardlink to abcfile.so any changes for the file is reflected both in the hard link as well as in the file. – Hard Link is simply an alias to an existing file. – All hard links for a given inode refer to a same file . – Contents of abclink is same as abcfile. 16 “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. – Removing abcfile will not free inode.  To create a hard link.

– A symbolic link is a small file containing the text of a path to the object you want to link to.Links  Soft Link/symbolic Link – A symbolic link is an inode which points to the original file. – A symbolic link can reference another symbolic link means that the kernel keeps dereferencing symbolic links until it finds something that isn’t a symbolic link.  To create a symbolic link – ln –s filename softlink – ln –s abcfile abclink  To remove a symbolic link – unlink (softlink) – unlink (abclink) 17 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .

inode number.  syntax: – stat (pathname. statbuffer). statbuffer). – fstat (fd. file owner. access time. file size. number of links.stat/fstat  stat/fstat: A process may query the status of a file (locked) file type. 18 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . access permission.

File Information  stat( “/usr/stud/gb/d. /* device type (if inode device) */ – off_t st_size. /* time of last change */ 19 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . / *Id of device containing a directory entry for this file. (struct stat *) buf)  Stat system call gives the following information: – dev_t st_dev. /* inode number */ – umode_t st_mode. /* protection /file mode*/ – nlink_t st_nlink. in bytes */ – unsigned long st_blksize. /* number of hard links */ – uid_t st_uid.h”. /* blocksize for filesystem I/O */ – unsigned long st_blocks. /* number of blocks allocated */ – time_t st_atime. /* group ID of owner */ – dev_t st_rdev. /* total size. */ – ino_t st_ino. /* user ID of owner */ – gid_t st_gid. / * time of last modification */ – time_t st_ctime. /* time of last access */ – time_t st_mtime.

– int dup2(int filedes. If filedes2 is already open. 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” . With dup2 we specify the value of the new descriptor with the filedes2 argument. int filedes2). Both return: new file descriptor if OK.dup Functions  An existing file descriptor is duplicated by either of the following functions: – int dup(int filedes). it is first closed. -1 on error The new file descriptor returned by dup is guaranteed to be the lowest numbered available file descriptor.

Kernel data structures after dup Assuming the process executes newfd=dup(1) 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” .

buf. count) –Write writes count bytes of data from user address space buf to the file whose descriptor is fd.  read (fd. mode) –Opens the specified file according to the value of flag & returns a file descriptor for use in other system calls. buf . dup. creat. –Reads up to count bytes from file descriptor fd in to the user array buf and returns the number of bytes read  write (fd .File Handling System Calls  A file is opened or created by calling the open function  open (pathname .  close ( int fd) –Close closes the file descriptor fd obtained from a prior open . count) . 23 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . pipe or fcntl system call or a file descriptor inherited from a fork call. flag .

offset.  lseek (filedes. 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” . – Where whence denotes – SEEK_SET = 0 beginning of file – SEEK_CUR = 1 current offset of the file – SEEK_END = 2 end of the file  dup (filedes). whence). – where filename is a pointer to null terminated character string that names the file and mode specifies the file access permissions. – It duplicates the existing file descriptor.File Handling System calls  A file is created by calling the creat system call. mode). – filedes identifies the I/O channel and offset and whence works together to describe how to change the file pointer.  creat (filename.

h> main () { int fd. close (fd). exit (0). } 25 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .h> #include<sys/stat. else printf ("\ndatafile created for read and write and is currently empty\n"). if (fd == -1) printf ("\nerror in opening file").An Example of creat function /*Program to demonstrate how to create File */ #include<stdio. fd = creat ("datafile.dat“ . S_IRWXU).h> #include<sys/types.

0L. position).h> #include <fcntl. } else printf("can't open datafile.dat\n"). long position. O_RDONLY). fd = open("open1. 2).c". else perror("lseek error").An Example of lseek and open function /* An example to demonstrate lseek() */ #include <stdio.h> int main() { int fd. } 26 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . ~ close(fd). if ( fd != -1) { position = lseek(fd. /* seek 0 bytes from end-of-file */ if (position != -1) printf("The length of datafile.dat is %ld bytes.\n".

/* exit() will close the files */ } 27 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . exit (1). if (fd == -1) { perror(“dup.h> #include <fcntl.O_WRONLY | O_CREAT. } close(1).h> #include <sys/stat.txt*/ exit (0).*/ #include <stdio. /* close the extra slot */ printf("Hello. world!\n"). fd = open("dup. /* fd will be duplicated into standard out's slot */ close(fd).txt"). /* should go to file dup. S_IREAD | S_IWRITE ).txt".h> int main() { int fd. /* close standard output */ dup(fd).An Example of dup function /*Program to demonstrate(dup) redirection of standard output to a file.h> #include <sys/types.

h> #include<fcntl.dat if it does not exist (O_CREAT) return error if datafile already exists (O_EXCL) permit read/write access to file (S_IWRITE | S_IREAD) */ fd = open ("foo. main () { int fd.read. O_RDWR | O_CREAT|O_TRUNC .h> /*define types used by sys/stat.S_IREAD | S_IWRITE).write function /* Example to demonstrate for open system call */ #include<stdio.bar".An Example of open . char buffer[80].h */ #include<sys/stat.txt for read/write access (O_RDWR) create datafile. /* open dup.h> #include<sys/types. 28 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .h> static char message[] = "Good morning".

sizeof (message)). buffer).read.~ lseek (fd. 0L. 0).lseek system call if (fd != -1) { printf ("\ndatafile opened for read/write access\n"). sizeof (message)) == sizeof (message)) printf ("\n%s was written to data file". write (fd. close (fd). exit (0). message. } else printf ("\nFile already exists"). /* go to beginning of file */ if (read (fd. } 29 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . message.write .An Example of open. else printf ("\nerror reading datafile").

h> main() { int fd1.st_ino.fd2.st_blksize.bufStat2. } 30 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .bufStat2.st_bloc ks.st_rdev).inode no=%d block size =%d blocks=%d: total size =%d De vice Type = %d".O_RDONLY).bufStat1.O_RDONLY).bufStat2. bufStat1.stat #include<sys/stat.fd2. printf("\nfd2=%d.h> #include<stdio.st_ino. fstat(fd2. struct stat bufStat1.&bufStat1).h> #include<fcntl. printf("\nfd1=%d.bufStat2.h> #include<sys/types.c".st_blksize.bufStat2. fd2=open("filefstat.bufStat1.&bufStat2).fd3.fd1. fstat(fd1.st_rdev).st_size.inode no=%d block size =%d blocks=%d: total size=%d Dev ice type=%d".bufStat1.bufStat1. fd1=open("/etc/passwd".st_size.bufStat2.st_blocks.

References •Advanced Programming UNIX Environment »Stevens •The design of the Unix Operating System »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” .

any materials not specifically acknowledged is purely unintentional” 32 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” . The content/materials in the slides are of a general nature and are not intended to address the specific circumstances of any particular individual or an institution.Disclaimer  “Aricent makes no representations or warranties with respect to contents of these slides and the same are being provided “as is”.

Thank you 33 “The contents here are for Aricent internal training purposes only and do not carry any commercial value” .