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

File-API_0

The document discusses the file system as a crucial operating system subsystem that provides abstractions like files and directories while hiding the complexity of storage devices. It covers various system calls and APIs for file handling, including opening files, reading and writing data, and managing file descriptors. Additionally, it explains the use of pipes for inter-process communication and shell piping in UNIX systems.

Uploaded by

RishavGoel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

File-API_0

The document discusses the file system as a crucial operating system subsystem that provides abstractions like files and directories while hiding the complexity of storage devices. It covers various system calls and APIs for file handling, including opening files, reading and writing data, and managing file descriptors. Additionally, it explains the use of pipes for inter-process communication and shell piping in UNIX systems.

Uploaded by

RishavGoel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CS330: Operating Systems

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

etc bin sbin home lib OS SSD

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

- File system is an important OS subsystem


- Provides abstractions like files and directories
- Hides the complexity of underlying storage devices
File system interfacing

Input/Output Library - Processes identify files through a


(fopen, fclose, fread, fprintf …)
file handle a.k.a. file descriptors
- In UNIX, the POSIX file API is
used to access files, devices,
System call API
(open, close, read, write …) sockets etc.
- What is the mapping between
library functions and system
Files Devices Sockets
calls?
open: getting a handle

int open (char *path, int flags, mode_t mode)


open: getting a handle

int open (char *path, int flags, mode_t mode)

- Access mode specified in flags : O_RDONLY, O_RDWR, O_WRONLY


- Access permissions check performed by the OS
- On success, a file descriptor (integer) is returned
- If flags contain O_CREAT, mode specifies the file creation mode
- Refer man page (“man 2 open")
Process view of file
PCB (P1)
P1
fd1 =open(“file1”) 0 1 2 3 file 1
Inode 1

P2 PCB (P2) file 1


fd1 = open(“file1”)
fd2 = open(“file2”) 0 1 2 3 4 file 2 Inode 2

- Per-process file descriptor table with pointer to a “file” object


- file object → inode is many-to-one
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

- What P2 happens to the FD PCB


table
(P2)and the filefile
objects across fork( )?
1
fd1 = open(“file1”)
What happens in exec(
fd2 =- open(“file2”) )?
0 1 2 3 4 file 2 Inode 2
- Can multiple FDs point to the same file object?
- Per-process file descriptor table with pointer to a “file” object
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
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

ssize_t write (int fd, void *buf, size_t 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

- 0→ P2 STDIN, 1 → STDOUT PCBand


(P2) 2 → STDERR file 1
fd1 = open(“file1”)
- fd2What happens to the FD0 table
= open(“file2”) and the file objects across fork( )?
1 2 3 4 file 2 Inode 2
- What happens in exec( )?

-- 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);

- Returns the information about file/dir in the argument path


- The information is filled up in structure called stat
File information (stat, fstat)
int stat(const char *path, struct stat *sbuf);

- Returns the information about file/dir in the argument path


- The information is filled up in structure called stat

struct stat sbuf;


stat(“/home/user/tmp.txt”, &sbuf);
printf(“inode = %d size = %ld\n”, sbuf.st_ino, sbuf.st_size);
- Other useful fields in struct stat : st_uid, st_mode (Refer stat man page)
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

- The FD table is copied across fork( ) ⇒ File objects are shared


-- On exec, openfilefiles
Per-process remain shared
descriptor by default
table with pointer to a “file” object
- Can multiple FDs point to the same file object?
Duplicate file handles (dup and dup2)
int dup(int oldfd);

- 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?

- Value of dupfd = 1 (assuming STDIN is open)


- “Hello world” will be written to tmp.txt file
Duplicate file handles (dup and dup2)

int dup2(int oldfd, int newfd);

- Close newfd before duping the file descriptor oldfd


- dup2 (fd, 1) equivalent to
- close(1);
- dup(fd);
Duplicate file handles (dup and dup2)
Before dup( )
- Lowest numbered
PCB (P1)
P1 unused fd (i.e., 1) is used
fd1 =open(“file1”) file 1
(Assume STDOUT is
0 1 2 3

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

- Example: ls > tmp.txt


- How implemented?
Use of dup: shell redirection

- Example: ls > tmp.txt


- How implemented?
fd = open (“tmp.txt”)

close( 1); close(2); // close STDOUT and STDERR

dup(fd); dup(fd) // 1→ fd, 2 → fd

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

- The FD table is copied across fork( ) ⇒ File objects are shared


-- On exec, openfilefiles
Per-process remain shared
descriptor by default
table with pointer to a “file” object
- Can multiple FDs point to the same file object?
- Yes, duped FDs share the same file object (within a process)
UNIX pipe( ) system call

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)

You might also like