Session-08 File System Calls
Session-08 File System Calls
19CS2106S
Session-8
File System Calls
Session Plan
Learning Outcomes
• Opening Files
• Reading and Writing Files
• Closing Files
• File Descriptor
• Concept of inode
• inode table
• File Table
Lets start with File Descriptor
• At least one file descriptor exists for every open file on the system
• The first three user file descriptors (0, 1, and
2) are called the standard input, standard
output, and standard error file descriptors.
• Syntax:
close (fd);
where fd is the file descriptor for the open file.
• The kernel does the close operation by
manipulating the file descriptor and the
corresponding file table and mode table
entries.
• When the close system call completes, the user
file descriptor table entry is empty.
• Attempts by the process to use that file descriptor
result in an error until the file descriptor is
reassigned as a result of another system call.
• When a process exits, the kernel examines its
active user file descriptors and internally closes
each one.
• Hence, no process can keep a file open after it
terminates.
• The entries for file descriptors 3 and 4 in the user
file descriptor table are empty.
• The count fields of the file table entries are now
0, and the entries are empty.
• The mode reference count for the files
"ietc/passwd" and "private" are also decremented.
• The mode entry for "private“ is on the free list
because its reference count is 0, but its entry is
not empty.
xv6 Case Study
• sys_open
• filealloc
• sys_read, fileread
• sys_write, filewrite
• sys_close, fileclose
Design and Implementation of sysfile.c
• Its an xv6 file that consists of functions of
various file system calls.
• Its available from Sheet No. 60-65 of xv6 code
manual.
• It contains the file related functions like
sys_open, filealloc, sys_read, fileread,
sys_write, filewrite, sys_close, fileclose
sys_open
• sys_open() system call opens the specified
filename, using the program associated with
the corresponding file type of filename.
• The behaviour of this command is the same as
that of double clicking on filename in the
Windows Explorer.
• For example, if filename is "
c:\mydata\sales.xls " and the .xls extension is
associated with Microsoft Excel,
sys_open("c:\mydata\sales.xls") will open
Excel and load the file.
Xv6 Function of sys_open
filealloc
• All the open files in the system are kept in a
global file table, the ftable.