0% found this document useful (0 votes)
94 views

Session-08 File System Calls

The document discusses various file system calls like open(), read(), write(), and close() in Unix-like operating systems. It explains that open() returns a file descriptor which is used to access an open file. read() and write() use the file descriptor to read from or write to an open file. close() closes the file and releases resources associated with the file descriptor. The document also explores the implementation of these calls in the xv6 operating system through functions like sys_open(), filealloc(), sys_read(), sys_write(), and sys_close() defined in the sysfile.c source file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views

Session-08 File System Calls

The document discusses various file system calls like open(), read(), write(), and close() in Unix-like operating systems. It explains that open() returns a file descriptor which is used to access an open file. read() and write() use the file descriptor to read from or write to an open file. close() closes the file and releases resources associated with the file descriptor. The document also explores the implementation of these calls in the xv6 operating system through functions like sys_open(), filealloc(), sys_read(), sys_write(), and sys_close() defined in the sysfile.c source file.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Operating Systems Design

19CS2106S

Session-8
File System Calls
Session Plan
Learning Outcomes

• To understand open ( ), read ( ), write ( ) and


close ( ) systems calls.
• To explore the above system calls in xv6 as
case study.
• To understand the design and
implementation of sysfile.c
Basic Operations on Files

• Opening Files
• Reading and Writing Files
• Closing Files

• All the above operations can be implemented


using Open ( ), Read ( ), Write( ), Close ( )
File System Calls
Review of basic terminology with Files

• File Descriptor
• Concept of inode
• inode table
• File Table
Lets start with File Descriptor

• A file descriptor is just an integer, private per process, and is used


in UNIX systems to access files.

• It is dynamically generated by the kernel when you call open( )


system call (or certain other system calls).

• When a file is opened, the file descriptor is used to read or write


the file assuming you have permission to do so.

• The descriptor is identified by a unique non-negative integer, such


as 0, 12, or 567.

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

• Processes on UNIX systems conventionally


use the standard input descriptor to read input
data, the standard output descriptor to write
output data, and the standard error descriptor
to write error data (messages).
• When a process makes a successful request to open a
file, the kernel returns a number(file descriptor) which
points to an entry in the kernel's global file table.
• The file table entry contains information such as the
inode of the file, byte offset, and the access
restrictions for that data stream (read-only, write-only
• Byte offset is the number of bytes from the beginning
of the file.
• Read and write operations normally start at the
current offset and cause the offset to be incremented
the number of bytes read or written, etc.).
Syntax

fd =open(pathname, flags, modes);


Or
int fd = open(pathname , flags , modes);
Here

pathname is a file name, flags indicate the type


of open (such as for reading or writing), and
modes give the file permissions if the file is
being created.
open ( ) file system Call
• The open system call is the first step a process
must take to access the data in a file.

• The syntax for the open system call is


fd =open(pathname, flags, modes);

• Here pathname is a file name, flags indicate the


type of open (such as for reading or writing),
and modes give the file permissions if the file is
being created.
• Algorithmic Design-open ( ) file system call
Algorithmic Design for open ( ) system call
• The kernel searches the file system for the file name
parameter using algorithm namei .
• It checks permissions for opening the file after it finds
the in-core mode and allocates an entry in the file table
for the open file.
• The file table entry contains a pointer to the mode of
the open file and a field that indicates the byte offset in
the file where the kernel expects the next read or write
to begin.
• The kernel initializes the offset to 0 during the open
call, meaning that the initial read or write starts at the
beginning of a file by default
Data Structures after Open
• Suppose a process executes the following
code, opening the file letc/passwd“ twice, once
read-only and once write-only, and the file
"local" once, for reading and writing.

• fdl = open(letc/passwd", O_RDONLY);


• fd2 = open("local", 0 RDWR);
• fd3 = open(letc/passwd", O_WRONLY);
• Here, each open returns a file descriptor to the
process, and the corresponding entry in the
user file descriptor table points to a unique
entry in the kernel file table even though one
file ("/etc/passwd") is opened twice.

• The file table entries of all instances of an


open file point to one entry in the in-core
mode table.
Data Structures after TWO Processes
opens a File
read ( ) File System Call
• The syntax of the read system call is
number = read(fd, buffer, count)
where
fd is the file descriptor returned by open
buffer is the address of a data structure in the
user process that will contain the read data on
successful completion of the call
count is the number of bytes the user wants to
read, and number is the number of bytes
actually read.
• Algorithmic Design for read ( ) system call
Algorithm for reading a file ( )
Contd…..
Sample Program for reading a File
write ( ) system call
• Syntax :

number = write(fd, buffer, count);


where the meaning of the variables fd, buffer,
count, and number are the same as they are for
the read system call
close ( ) system call

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

• filealloc is a function present in file table


ftable to allocate a file
Xv6 Function of filealloc
sys_read
sys_write
sys_close

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy