Miscellaneous

I/O without Offset Manipulation

#include <unistd.h>

ssize_t pread(int fd, void *buf, size_t count, off_t offset);
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
  • read() and write() have been made for sequential access

  • Random access only together with lseek()

  • Inefficient

  • Not atomic ⟶ race conditions!

Scatter/Gather I/O

#include <sys/uio.h>

ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
  • Often data is not present in one contiguous block

    • E.g. layered protocols

  • ⟶ Copy pieces together, or issue repeated small system calls

  • ⟶ Scatter/Gather I/O

Scatter/Gather I/O, without Offset Manipulation

#include <sys/uio.h>

ssize_t preadv(int fd, const struct iovec *iov, int iovcnt,
               off_t offset);
ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt,
                off_t offset);

Truncating Files

#include <unistd.h>

int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);
  • Truncating a file …

  • … or create a hole (⟶ lseek())

File Descriptors - Allocation

Value of the next file descriptors is not arbitrarily chosen ⟶ next free slot, starting at 0.

Filedescriptor Allocation
close(STDIN_FILENO);
int fd = open("/dev/null", O_WRONLY);
assert(fd == 0);