0% found this document useful (0 votes)
136 views13 pages

System Calls For The File System

The document summarizes the system calls for opening, reading, and writing files in a Unix file system. It describes the syntax and algorithms for the open(), read(), and write() system calls. The open() call converts the file name to an inode, allocates entries in the file and inode tables, and returns a file descriptor. The read() call gets the file table entry, locks the inode, copies data from disk blocks to a user buffer, and updates offsets. The write() call allocates new blocks if needed, locks the inode, updates the file size, and writes data to disk.

Uploaded by

Proxten Dsilva
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views13 pages

System Calls For The File System

The document summarizes the system calls for opening, reading, and writing files in a Unix file system. It describes the syntax and algorithms for the open(), read(), and write() system calls. The open() call converts the file name to an inode, allocates entries in the file and inode tables, and returns a file descriptor. The read() call gets the file table entry, locks the inode, copies data from disk blocks to a user buffer, and updates offsets. The write() call allocates new blocks if needed, locks the inode, updates the file size, and writes data to disk.

Uploaded by

Proxten Dsilva
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 13

SYSTEM CALLS FOR THE FILE SYSTEM

OPEN

Syntax: fd = open(pathname,flags,modes) filename type of open (R/W) permissions returntype

Algorithm for Opening a File Inputs : file name type of open file permissions (for creation type of open) Output : file descriptor { convert file name to inode (algorithm namei) ; if (file does not exist or not permitted access) return (error);

29-07-2012 4

CS2028 UNIX INTERNALS

allocate file table entry for inode, initialize count, offset; allocate user file descriptor entry, set pointer to file table entry; if (type of open specifies truncate file) free all file blocks (algorithm free); unlock(inode); /* locked above in namei */ return (user file descriptor);
}

Open Example
User file descriptor table File table inode table

0 1 2 3 4 5 6 7

count Read 1 ... count Rd-Wrt 1 ... count Write 1 ...

count (/etc/passwd) 2 ... count (local) 1 ...

... ...

fd1 = open(/etc/passwd, O_RDONLY); fd2 = open(local, O_RDWR); fd3 = open(/etc/passwd, O_WRONLY);


29-07-2012 6 CS2028 UNIX INTERNALS

Open Example(Cntd..)
User file descriptor table (proc A) File table inode table

0 1 2 3 4 5

count Read 1 ...

(proc B)

0 1 2 3 4 5

... count Rd-Wrt 1 ... count Read 1


... count Write 1 ... count Read 1

count (/etc/passwd) 3 ... count (local) 1 ... count (private) 1 ...

...

fd1= open(/etc/passwd, O_RDONLY); fd2 = open(private, O_RDONLY);


29-07-2012 7 CS2028 UNIX INTERNALS

READ

Syntax: number = read( fd, descriptor returned by open

buffer,

count)

No of bytes read

address of data structure that will contain read data on successful completion of call no of bytes the user wants to read

Algorithm read Input : user file descriptor address of buffer in user process number of bytes to read Output : count of bytes copied into user space { get file table entry from user file descriptor; check file accessibility; set parameters in u area for user address, byte count, I/O to user; get inode from file table; lock inode; set byte offset in u area from file table offset; }

while (count not satisfied) { convert file offset to disk block (algorithm bmap); calculate offset into block, number of bytes to read ; if (number of bytes to read is 0) /* trying to read end of file */ break; /* out of loop */ read block (algorithm breada if with read ahead, algorithm bread otherwise); copy data from system buffer to user address; update u area field for file byte offset, read count, address to write into user space; release buffer; /* locked in bread */ } unlock inode; update file table offset for next read; return(total number of bytes read);

Sample Program for Reading a File


#include <fcntl.h> main() { int fd; char lilbuf[20], bigbuf[1024]; fd = open(/etc/passwd, O_RDONLY);

read(fd, lilbuf, 20); read(fd, bigbuf, 1024); read(fd, lilbuf, 20);


}

the example shows how advantageous it is for I/O requests to start

on file system block boundaries and to be multiples of the block size.

WRITE
Syntax

o number = write(fd, buffer, count);


Algorithm o If the file does not contain a block that corresponds to the byte offset to be written, the kernel allocate a new block

o The inode is locked


o Update the file size entry in the inode
29-07-2012 13 CS2028 UNIX INTERNALS

You might also like