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

System Call For File System

The document discusses various system calls related to file systems such as open, read, write, close, creat, and mknod. It provides details on the algorithms and steps involved in each system call, including obtaining file descriptors, parsing path names, allocating and freeing inodes, performing I/O operations, and adjusting file positions. Special considerations are given for file creation and making special file types like pipes and devices.

Uploaded by

Vijal Chokshi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

System Call For File System

The document discusses various system calls related to file systems such as open, read, write, close, creat, and mknod. It provides details on the algorithms and steps involved in each system call, including obtaining file descriptors, parsing path names, allocating and freeing inodes, performing I/O operations, and adjusting file positions. Special considerations are given for file creation and making special file types like pipes and devices.

Uploaded by

Vijal Chokshi
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Chapter 5 System Calls for File System

Click to edit Master subtitle style

4/27/12

System calls that return file descriptors for use in other system call; System calls that use the namei parse a path name; algorithm to

System calls that assign and free inodes , using algorithm ialloc and ifree; System calls that set or change the attribute of a file; System calls that do I/O to and from a process , using algorithms alloc ,free, and the buffer allocation algorithms; System calls that change the structure of the file system;
4/27/12

System calls that allow a process to change its

Open

The open system call is the first step a process must take to access the data in a file. Syntax: fp=open(pathname , flags, modes); Pathname: file name flags: type of open (reading or writing) modes: file permission File descriptor: open system call returns an integer called user file descriptor

4/27/12

The kernel searches the file system for the file name parameters using namei algorithm; It checks permissions for opening the file after it finds the in-core inode and allocates an entry in the file table for open file; The file table entry contains a pointer to the inode of the open file and a field that indicates the byte offset in the file where the kernel expects the next read or write begin; The kernel initializes the offset 0 during the open system call; The kernel allocate an entry in a private table in the process u area called the user file descriptor table; 4/27/12

Algorithm open Inputs : file name type of open file permissions (for creation type of

open)

Output : file descriptor 1) convert file name to inode(algorithm namei); 2) if file does not exist or not permitted access return error;

allocate the table entry for inode , initialize count, offset; allocate user file descriptor entry , set pointer to file table entry; 4/27/12

Example:

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

4/27/12

User file descript or table 0 1 2 3 4 5 6 7 wr

File table : : count 1 read : : count 1 rd-

Inode table

Count : 2 /etc/passwd

: : count 1 write

Count:1 Local

4/27/12

Example process 1 fd1=open(/etc/passwd, O_RDONLY); fd2=open(local, O_RDWR); fd3=open(/etc/passwd, O_WRONLY); Process 2 fd1=open(/etc/passwd, O_RDONLY); fd2=open(private, O_RDONLY);

4/27/12

User file descriptor table 0 1 2 3 4 5 : : 0 1 2 3 4 : : : 4/27/12

File table : : count:1 read : : count:1 rd-write : : count:1 read

Inode table

Count : 3 /etc/passwd

Count:1 Local

count:1 write

Count:1 private

Read

Syntax : Number=read( fd , buffer, count) fd : the file descriptor return by open Buffer : address of a data structure in the user process that will contain the read data on successful completion of the call Count: number of bytes the user wants to read Number : number of bytes actually read
4/27/12

The kernel gets the file table entry that corresponds to the user file descriptor It now sets the I/O parameters in the u area. Mode indicates read or write Count count of bytes to read or write Offset byte offset in file Address target address to copy data, in user or kernel memory Flag indicates if address is in user or kernel memory
figure 5.6 I/O parameters saved in U area

4/27/12

After the kernel sets the I/O parameters in the u area; It follows the pointer from the file table entry to the inode ,looking the inode before it reads the file; the algorithm now goes into loop until the read is satisfied; The kernel converts the file byte offset into a block number, using algorithm bmap And it notes the byte offset in the block where the I/O should begin and how many bytes in the block it should read. 4/27/12

Algorithm read Input : user file descriptor address of buffer in user process number of bytes to read Output : count of bytes into user space 1) get file table entry from user file descriptor; 2) check file accessibility; 3) set parameters in u area for user address ,byte count , I/O to user; 4) get inode from file table; 5) lock inode; 6) set byte offset in u area from file table offset;
4/27/12

7) while could not satisfied

convert file offset to disk block (algorithm bmap); calculate offset into block , number of bytes to read;

8) if number bytes to read is 0 /*trying to read end of file */ break; 9) read block (algorithm breada if with read ahead , algorithm bread otherwise); 10) copy data from system buffer to user address; 11) update u area fields for file offset ,read count , address to write into user space;
4/27/12

Release buffer;

Unlock inode Update file table offset for next read; return (total number of bytes read);

4/27/12

Write

Syntax : Number=write( fd , buffer , count); fd : the file descriptor return by open Buffer : address of a data structure in the user process that will contain the read data on successful completion of the call Count: number of bytes the user wants to read Number : number of bytes actually read
4/27/12

The algorithm for writing a regular file is similar to that for reading a regular file. However , if the file dose not contain a block that corresponds to the byte offset to be written, the kernel allocates a new block using algorithm alloc And assigns the block number to the correct position in the inode s table of contents. if the byte offset is that of an indirect block , the kernel may have to allocate several blocks for use as indirect blocks and data blocks. The inode is locked for the duration of the write; Because the kernel may change the inode when allocating new blocks;
4/27/12

Allowing other processes to access to the file could corrupt the inode ,if several processes allocate blocks simultaneously for the same byte offsets; When the write is complete, the kernel updates the file size entry in the inode if the file has grown larger;

4/27/12

#include<fcntl.h> #include<sys/stat.h> #define BUFSIZE 1024 main(void) { int fd1,fd2;

int n; Char buf[BUFSIZE]; Fd1=open(/etc/passwd, O_RDONLY); Fd2=open(passwd.bak, O_WRONLY | O_CREAT); While((n=read(fd1,buf,BUFSIZE))>0) Write(fd2,buf,n);
4/27/12

Close(fd1); close(fd2);

Process 1
fd=open(/home/stu/p.c , O_WRONLY); Write(fd,hello,5);

Process 2
fd=open(/home/stu/p.c, O_RDONLY); read(fd,buf,5);

4/27/12

Adjusting the position of file I/O LSEEK The ordinary use of read and write system calls provide sequential access to a file ,but processes can use the lseek system call to position the I/O and allow random access to a file . Syntax :

position = lseek (fd , offset, reference);


fd : file descriptor identifying the file offset : a byte offset reference : indicate whether offset should be considered from the file , from the current position of the read/write offset , or from end of file. 4/27/12

The lseek system call has nothing to do with the seek operation that positions a disk arm over a particular disk sector; To implement lseek , the kernel simply adjusts the offset value in the file table; Subsequent read or write system calls use the file table as their starting byte offset;

4/27/12

Close

A process closes an open file when it no longer wants to access it; syntax: Close(fd); fd : file descriptor for open file The kernel does the close operation by manipulating the file descriptor and the corresponding file table and inode table entries.
4/27/12

If the reference count of the file table entry is greater than 1 because of dup or fork system calls , then other user file descriptors reference the file table entries; The kernel decrement count and close completes;

4/27/12

User file descriptor table 0 1 2 3 4 5 : : 0 1 2 NULL NULL : : : 4/27/12

File table : : count:1 read : : count:1 rd-write : : count:0

Inode table

Count : 2 /etc/passwd

Count:1 Local

count:1 write

Count:0 private

FILE CREATION

The open system call gives a process access to an existing file, but the creat system call creates a new file in the system. Syntax :

fd=creat(pathname , modes);
Pathname: file name modes: file permission File descriptor: open system call returns an integer called user file descriptor

4/27/12

If no such file previously existed , the kernel creates a new file with the specified name and permission mode; If the file already existed , the kernel truncates the file (releases all existing data blocks and sets the file size to 0);

4/27/12

Algorithm Creat

Input : file name permission settings Output : file descriptor 1) get inode for file name ( algorithm namei); 2) if file already exists if not permitted access release inode(algorithm iput); return(error); 3) else assign free inode from file system(algorithm

ialloc) 4/27/12

4) allocate file table entry for inode , initialized count; 5) if file did exist at time of create free all file blocks (algorithm free) unlock(inode); return(user file descriptor);

4/27/12

CREATION OF SPECIAL FILES

The system call mknod creates special file in the system , including : named pipes; Device files; Directories; It similar to creat in that the kernel allocates an inode for the file.

4/27/12

Algorithm make new node Inputs : node(file name) file type permissions

4/27/12

Assign free inode from file system for new node ( ialloc ); Create new directory entry in parent directory ;include new inode name and newly assigned inode number; Release parent directory inode number(iput); 3)If (new node is block or char special file) write major , minor numbers into inode structure; release new node inode(iput);

4/27/12

4/27/12

4/27/12

4/27/12

You might also like