Unit 2
Unit 2
Unit 2
By
Kala.C.L
Lecturer, Dept of ISE
SJBIT, Bengaluru
UNIT-II
UNIX FILES
Files are the building blocks of any OS.
When you execute a command in UNIX, the kernel fetches the
executable file from the file system and also any files required for reading
and writing.
Files in UNIX and POSIX cover a wide range of file types.
They also provide a common set of interfaces to files.
File Types
• Regular Files
• Directory File
• Character Device File
• Block Device File
• FIFO file
• link file
Regular File
It is a special pipe device file which provides temporary buffer for two
processes to communication by writing/reading data from the buffer.
Size of the buffer is fixed to PIPE_BUF.
create a FIFO file – mkfifo command
mkfifo /usr/prog/fifo_pipe
mknod /usr/prog/fifo_pipe p
Remove a fifo file – rm command.
Symbolic link file
ln –s /usr/jose/original /usr/mary/slink
cat –n /usr/mary/slink
ls –l /usr/mary/slink
sr—r—r-- 1 terry 20 Aug 20, 1994 slink->usr/jose/original
UNIX and POSIX File Systems
Kernel Space
r
rc = 1
rw rc = 1
xyz
rc = 1
w rc = 2 abc
rc = 1
Process Space
Kernel data structures for open files
Two independent processes with the same
file open
Kernel data structures after dup(1)
Read/write function
The file descriptor is the first argument to read/write system call.
Kernel uses the file descriptor to index to the file descriptor table to
find the file table entry.
It checks the file table entry to see if the appropriate mode and
permissions are there.
Use the file table entry to access the file’s inode record.
Use the file pointer in the file table entry to determine where
read/write should occur.
Checks the file type in inode record and invokes the appropriate
driver function.
lseek system call
Invoked to change the file pointer to a different offset for next
read/write operation.
The kernel gets access to the file inode record and checks that the
file is not a character device file, a FIFO file or a symbolic link file.
If file type is compatible, change the file pointer to the value in
lseek.
Close function
1. The kernel sets the FD entry to be unused
2. Decrement the reference count in file table by 1. If still non-zero, go to
6.
3. The file table entry is marked as unused.
4. Decrement the reference count in file inode table by 1, if still non-zero,
go to 6.
5. If hard-link count of inode is non zero, return to caller. Otherwise it
marks the inode table entry as unused and deallocates all the physical
disk storage of the file, as all the file path names have been removed by
some process.
6. It returns to the process with a 0 (success) status.
C Stream Pointers and File Descriptors
Cannot link files across file systems Can link files across file systems
Increase the hard link count of the linked Does not change the hard link count of
inode the linked inode
ln Vs cp
ln creates a new directory entry to a referenced file whereas cp creates a duplicated copy of
the file to another file with a different name.
ln /usr/mary/abc /usr/mary/xyz
Inode Filename Inode Filename
number number
115 515
89 989
89 989