System Calls For File Processing
System Calls For File Processing
Topics to cover
open()
read()
write()
lseek()
close()
creat()
mknod()
pipe()
mkfifo()
dup()
link()
unlink()
open()
fd = open ( pathname, flags, modes)
flags : type of open (reading or writing)
modes: file permissions if the file is being created.
return type: non negative integer
-1 in case of error
Steps:
convert file name to inode.
allocate file table entry, initialize count and offset.
allocate user descriptor entry.
free blocks in case of O_TRUNC
open() contd.
Example
Process A
fd1= open(/etc/passwd,O_RDONLY);
fd2= open(local,O_RDWR);
fd3= open(/etc/passwd,O_WRONLY);
Process B
fd1= open(/etc/passwd,O_RDONLY);
fd2= open(private,O_RDONLY);
open() contd.
read()
numbytes = read ( fd, buffer, count)
read() attempts to read upto count bytes from file descriptor
fd into the buffer starting at buffer
Steps:
get file table entry from user file descriptor table.
check file accessibility.
get u-area parameters.
(so that we dont have to pass them explicitly)
get inode from file table.
read data from the file
calculate disk blocks.
calculate offset in the blocks.
read and copy to user space.
repeat until count bytes are read.
read() contd.
u-area parameters
read() contd.
Suppose the content of the file doc is abcd
What will be the output of?
read() contd.
What will be the output of?
read() contd.
What will be the output of?
write()
numbytes = write ( fd, buffer, count)
One or more blocks may be allocated for the use of indirect blocks or
data blocks.
Write to data block is delayed write. This may reduce the number of
disk writes.
close()
close(fd)
file table entry count is reduced.
if the count becomes 0, kernel frees the entry.
incore inode count is reduced.
if the count becomes 0, kernel frees the incore inode.
We didnt call close() in the programs earlier. What happened to
those descriptors?
When the process exists, kernel examines its active user file
descriptors and internally closes each one.
close() contd.
creat()
fd = creat (pathname, modes)
If no such file exists, kernel creates a new one.
If the file exists, kernel truncates the file (subject to permissions)
Steps:
Open the parent directory and see if the file exists.
In case of existing file:
check permissions.
In case if the file doesnt exists
get a free inode.
create entry in the parent directory.
allocate file table entry, initialize count.
In case of existing file, truncate contents, set size to 0.
return user file descriptor.
creat() contd.
What will be the output of?
mknod()
Its used for creating special files.
mknod (pathname, mode, dev)
mode: type and permissions
dev: major and minor device number in case of
block-special/character-special device.
Pipes
Allow data transfer between processes.
Types: named and unnamed.
named pipe: open()
unnamed pipe: pipe()
reading and writing: read()/write()
closing: close()
Unnamed pipes
pipe (pipefd[2])
pipefd[0]: read end
pipefd[1]: write end
Unidirectional data channel for IPC.
Data written from write end is buffered until it is read from the
read end.
Only related processes (descendent of the creator) can access the
pipe.
Named pipes
It has a directory entry, is accessed by a pathname.
Exists permanently in file system hierarchy.
When all processes finish using the pipe, kernel reclaims its inode.
mknode (pathname, S_IFIFO|permissions, dev)
mkfifo (pathname, mode)
Closing pipes
If count of writers goes to 0, kernel awakens processes sleeping to
read data. They return their read calls with no data.
If count of readers goes to 0, kernel awakens process sleeping for
writing data to the pipe. Kernel sends signal indicating error.
If count of readers as well as writers reach 0, kernel frees disk blocks
and adjusts inode to indicate pipe is empty.
dup()
newfd = dup (oldfd)
Creates a copy of the file descriptor oldfd, using the lowest
numbered unused descriptor for the new descriptor.
After a successful return, the old and new file descriptors may be
used interchangeably.
They refer to the same open file description and thus share file
offset
dup() contd.
dup() contd.
What will be the output of?