Unit 1 - File IO Flashcards

1
Q

What are File Descriptors

A

To the kernel, all open files are referred to by file descriptors. A file descriptor is a
non-negative integer. When we open an existing file or create a new file, the kernel
returns a file descriptor to the process. When we want to read or write a file, we
identify the file with the file descriptor that was returned by open or creat as an
argument to either read or write.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

File Descriptors
0 -> ?
1 -> ?
2 -> ?

A

UNIX System shells associate
file descriptor 0 with the standard input of a process, file descriptor 1 with the standard output
file descriptor 2 with the standard error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Purpose of open and openat Functions

A

A file is opened or created by calling either the open function or the openat function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Syntax of open and openat functions

A
#include 
int open(const char *path, int oflag, ... /* mode_t mode */ );
int openat(int fd, const char *path, int oflag, ... /* mode_t mode */ );
Both return: file descriptor if OK, −1 on error
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain “path” parameter in open function

A

The path parameter is the name of the file to open or create.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain “oflag” parameter in open function

A
This argument is
formed by ORing together one or more of the following constants from the  header:
O_RDONLY Open for reading only.
O_WRONLY Open for writing only.
O_RDWR Open for reading and writing.

Optional contraints
O_APPEND Append to the end of file on each write.
O_CREAT Create the file if it doesn’t exist. This option requires a third argument to the open function, the mode, which specifies the access permission bits of the new file.
O_EXCL Generate an error if O_CREAT is also specified and the file already exists.
O_TRUNC If the file exists and if it is successfully opened for either write-only or read–write, truncate its
length to 0.
O_NOCTTY If the pathname refers to a terminal device, do not allocate the device as the controlling terminal for this process.
O_NONBLOCK : If the pathname refers to a FIFO, a block special file, or a character special file, this option sets the non blocking mode for both the opening of the file and subsequent I/O.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Syntax and purpose of creat Function

A

new file can be created by calling the creat function.

#include 
int creat(const char *path, mode_t mode);

Returns: file descriptor opened for write-only if OK, −1 on error

that this function is equivalent to
open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Syntax and purpose of close Function

A
An open file is closed by calling the close function.
#include 
int close(int fd);

Returns: 0 if OK, −1 on error

Closing a file also releases any record locks that the process may have on the file. When a process terminates, all of its open files are closed automatically by thekernel.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Syntax and purpose of lseek function

A

Every open file has an associated ‘‘current file offset,’’ normally a non-negative integer that measures the number of bytes from the beginning of the file. Read and write operations normally start at the current file offset and cause the offset to be incremented by the number of bytes read or written. By default, this offset is initialized to 0 when a file is opened, unless the O_APPEND option is specified.

An open file’s offset can be set explicitly by calling lseek.
#include 
off_t lseek(int fd, off_t offset, int whence);

Returns: new file offset if OK, −1 on error

lseek only records the current file offset within the kernel—it does not cause any I/O to take place.

This offset is then used by the next read or write operation.

lseek is defined in unistd.h

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain “whence” parameter in lseek function

A

If whence is SEEK_SET, the file’s offset is set to offset bytes from the beginning of the file.

If whence is SEEK_CUR, the file’s offset is set to its current value plus the offset.

If whence is SEEK_END, the file’s offset is set to the size of the file plus the offset.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Syntax and purpose of read function

A
Data is read from an open file with the read function.
#include 
ssize_t read(int fd, void *buf, size_t nbytes);

Returns: number of bytes read, 0 if end of file, −1 on error
If the read is successful, the number of bytes read is returned. If the end of file is encountered, 0 is returned.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Syntax and purpose of write function

A
#include 
ssize_t write(int fd, const void *buf, size_t nbytes);

Returns: number of bytes written if OK, −1 on error

For a regular file, the write operation starts at the file’s current offset. If the
O_APPEND option was specified when the file was opened, the file’s offset is set to the
current end of file before each write operation. After a successful write, the file’s offset
is incremented by the number of bytes actually written.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Syntax and purpose of pread function

A

Atomic functions

#include 
ssize_t pread(int fd, void *buf, size_t nbytes, off_t offset);
Returns: number of bytes read, 0 if end of file, −1 on error
ssize_t pwrite(int fd, const void *buf, size_t nbytes, off_t offset);
Returns: number of bytes written if OK, −1 on error

Calling pread, pwrite is equivalent to calling lseek followed by a call to read/write, with the
following exceptions.
• There is no way to interrupt the two operations that occur when we call pread.
• The current file offset is not updated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Syntax and purpose of dup and dup2 functions

A
An existing file descriptor is duplicated by either of the following functions:
#include 
int dup(int fd);
int dup2(int fd, int fd2);

With dup2, we specify the value of the new descriptor with the fd2 argument. If fd2 is already open, it is first closed. If fd equals fd2, then dup2 returns fd2 without closing it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Equivalent function calls of dup and dup2 using fctnl

A

The call
dup(filedes);
is equivalent to
fcntl(filedes, F_DUPFD, 0);

Similarly, the call
	dup2(filedes, filedes2);
is equivalent to
close(filedes2);
fcntl(filedes, F_DUPFD, filedes2);

Note: dup2 is an atomic operation, whereas the alternate form involves two function calls (close and the fcntl)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantages of dup/dup2

A
  1. Used in file sharing, especially in implementing inter process communication.
  2. Helps in redirection of standard i/o descriptors
  3. Used in opening a std i/o stream functions i.e fdopen( )
  4. Helps the child process to inherit the property of it’s parent
  5. Used to implement atomic inheritance.
  6. Used in record locking
  7. Used to implement pipes(popen()) in IPC
17
Q

Synatx and purpose of sync, fsync, and fdatasync Functions

A

When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called delayed write.
The kernel eventually writes all the delayed-write blocks to disk, normally when it needs to reuse the buffer for some other disk block. To ensure consistency of the file system on disk with the contents of the buffer cache, the sync, fsync, and fdatasync functions are provided.

#include 
int fsync(int fd);
int fdatasync(int fd);
Returns: 0 if OK, −1 on error
void sync(void);

The sync function simply queues all the modified block buffers for writing and returns;
it does not wait for the disk writes to take place.

The function fsync refers only to a single file, specified by the file descriptor fd,and waits for the disk writes to complete before returning. This function is used when
an application, such as a database, needs to be sure that the modified blocks have been written to the disk.

The fdatasync function is similar to fsync, but it affects only the data portions of a file. With fsync, the file’s attributes are also updated synchronously.

18
Q

Syntax and purpose of fcntl function

A

The fcntl function can change the properties of a file that is already open.

#include 
int fcntl(int fd, int cmd, ... /* int arg */ );
The fcntl function is used for five different purposes.
1. Duplicate an existing descriptor (cmd = F_DUPFD or F_DUPFD_CLOEXEC)
2. Get/set file descriptor flags (cmd = F_GETFD or F_SETFD)
3. Get/set file status flags (cmd = F_GETFL or F_SETFL) (returns O_RDONLY,O_WRONLY,O_RDWR)
4. Get/set asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN)
5. Get/set record locks (cmd = F_GETLK, F_SETLK, or F_SETLKW)
19
Q

Program to test the file status flags for a file

A

int val=fcntl(fd, F_GETFL, 0);

        switch(val & O_ACCMODE)
        {
                case O_RDONLY:
                        printf("read only");
                        break;
                case O_WRONLY:
                        printf("write only");
                        break;
                case O_RDWR:
                        printf("read write");
                        break;
                default:
                        printf("unknown access mode");
        }
         if(val & O_APPEND)
                printf(", append");
         if(val & O_NONBLOCK)
                printf(", nonblocking");
         if(val & O_SYNC)
                printf(", synchronous writes");
20
Q

Blocking read and nonblocking read

A

Blocking Read
By default, read() waits until at least one byte is available to return to the application; this default is called “blocking” mode.

Non Blocking read()
In this read() on a slow file will return immediately, even if no bytes are available.

21
Q

Blocking write and nonblocking write

A

Blocking write:
Normally, write() will block until it has written all of the data to the file.
Non blocking write:
If that particular file descriptor (or file structure) is in non-blocking mode, however, write() will write as much data into the file as it can, and then return. This means that it will store as few as 0 bytes, or as much as the full number that was requested.
Ex: write(fd, buf,25)