File-API_0
File-API_0
Files
The file system
End-user wants see a
nice tree view. Let me
USER
enable it through a
Storage devices
simple system call
/ APIs.
Hard disk
drive
code file.txt
Others
The file system
USER Storage devices
OS
/ Hard disk
drive
File system
etc bin sbin home lib SSD
layer
code file.txt
Others
- fd → file handle
- buf → user buffer as read destination
- count → #of bytes to read
- read ( ) returns #of bytes actually read, can be smaller than count
Read and Write
ssize_t read (int fd, void *buf, size_t count);
- fd → file handle
- buf → user buffer as read destination
- count → #of bytes to read
- read ( ) returns #of bytes actually read, can be smaller than count
- Similar to read
Process view of file
PCB (P1)
P1
fd1 =open(“file1”) 0 1 2 3 file 1
- What do file descriptors 0, 1 and 2 represent? Inode 1
-- Can multiplefile
Per-process FDsdescriptor
point to the same
table withfilepointer
object?to a “file” object
lseek
off_t lseek(int fd, off_t offset, int whence);
- fd → file handle
- offset → target offset
- whence → SEEK_SET, SEEK_CUR, SEEK_END
- On success, returns offset from the starting of the file
lseek
off_t lseek(int fd, off_t offset, int whence);
- fd → file handle
- offset → target offset
- whence → SEEK_SET, SEEK_CUR, SEEK_END
- On success, returns offset from the starting of the file
- Examples
- lseek(fd, SEEK_CUR, 100) → forwards the file position by 100 bytes
- lseek(fd, SEEK_END, 0) → file pos at EOF, returns the file size
- lseek(fd, SEEK_SET, 0) → file pos at beginning of file
File information (stat, fstat)
int stat(const char *path, struct stat *sbuf);
- The dup() system call creates a “copy” of the file descriptor oldfd
- Returns the lowest-numbered unused descriptor as the new descriptor
- The old and new file descriptors represent the same file
Duplicate file handles (dup and dup2)
int fd, dupfd;
fd = open(“tmp.txt”);
close(1);
dupfd = dup(fd); //What will be the value of dupfd?
printf(“Hello world\n”); // Where will be the output?
Duplicate file handles (dup and dup2)
int fd, dupfd;
fd = open(“tmp.txt”);
close(1);
dupfd = dup(fd); //What will be the value of dupfd?
printf(“Hello world\n”); // Where will be the output?
closed before)
After dup( )
- Duplicate descriptors
+ dup(fd1) PCB (P1) share the same file state
0 1 2 3 file 1 - Closing one file
descriptor does not
close the file
Use of dup: shell redirection
exec(ls )
Process view of file
PCB (P1)
P1
- What do file descriptors00, 11 and
fd1 =open(“file1”) 2 32 represent?
file 1
Inode 1
- 0 → STDIN, 1 → STDOUT and 2 → STDERR
- What P2 happens to the FD PCB table
(P2)and the filefile
objects
1 across fork( )?
fd1 = open(“file1”)
What happens in exec(
fd2 =- open(“file2”) 0 1 )?2 3 4 file 2 Inode 2
PCB (P1)
- pipe( ) takes array of two
P1
pipe(fd[2]) 0 1 2 fd[0] fd[1]
FDs as input
- fd[0] is the read end of
IN (Read)
the pipe
OUT (Write)
- fd[1] is the write end of
Pipe (FIFO Queue) the pipe
- Implemented as a FIFO
queue in OS
UNIX pipe( ) with fork( )
PCB (Parent)
- fork( ) duplicates the file
Parent
pipe(fd[2]) 0 1 2 fd[0] fd[1]
descriptors
- At this point, both the
IN (Read)
parent and the child
OUT (Write)
processes can read/write to
Pipe (FIFO Queue) the pipe
0 1 2 fd[0] fd[1]
PCB (Child)
UNIX pipe( ) with fork( )
PCB (Parent)
- fork( ) duplicates the file
Parent
pipe(fd[2]) 0 1 2 fd[0] fd[1]
descriptors
- close( ) one end of the pipe,
IN (Read)
both in child and parent
OUT (Write)
- Result
Pipe (FIFO Queue) - A queue between
parent and child
0 1 2 fd[0] fd[1]
PCB (Child)
Shell piping : ls | wc -l
PCB (Parent)
- pipe( ) followed by fork( )
Parent
pipe(fd[2]) 0 1 2 fd[0] fd[1]
- Parent: exec( “ls”) after
making STDOUT → out fd
of the pipe (using dup)
OUT (Write)
IN (Read) Pipe (FIFO Queue)
0 1 2 fd[0] fd[1]
PCB (Child)
Shell piping : ls | wc -l
PCB (Parent)
- pipe( ) followed by fork( )
Parent
pipe(fd[2]) 0 1 2 fd[0] fd[1]
- Parent: exec( “ls”) after
making STDOUT → out fd
of the pipe (using dup)
OUT (Write)
- Child: exec(“wc” ) after
IN (Read) Pipe (FIFO Queue) closing STDIN and duping
in fd of pipe
- Result: input of “wc” is
0 1 2 fd[0] fd[1]
connected to output of “ls”
PCB (Child)