Lecture 017

System-Level I/O

strace: print system call

I/O methods

I/O methods

Unix I/O

/dev/: device /dev/tty2: terminal /boot/: kernel /proc/: kernel data structure

Unix File:

File Functions

file descriptor:

open(): return lowest numbered file descriptor not currently open


read(fd, buf, sizeof(buf)):

write(fd, buf, sizeof(buf)):

Short count:

No short count:


Metadata, Sharing, Redirection

File Metadata: maintained by kernel

File Sharing

Example 1

Example 1

Example 2

Example 2

I/O redirection

Standard I/O

printf(): using stream buffer, flush on \n by default fflush(stddout): flush

Robust I/O (RIO) Package


Unbuffered version

ssize_t rio_readn(int fd, void *usrbuf, size_t n)


Calls to rio_readn and rio_writen can be interleaved arbitrarily on the same descriptor

Buffered version

Thread-safe, typically no short count, can be interleaved on the same descriptor with all buffered version.

typedef struct {
  int rio_fd; /* descriptor for this internal buf */
  int rio_cnt; /* unread bytes in internal buf */
  char *rio_bufptr; /* next unread byte in internal buf */
  char rio_buf[RIO_BUFSIZE]; /* internal buffer */
} rio_t;

void rio_readinitb(rio_t *rp, int fd)

ssize_t rio_readlineb(rio_t *rp, void *usrbuf, size_t maxlen)

ssize_t rio_readnb(rio_t *rp, void *usrbuf, size_t n)


mmap() is good for copy file to memory, copy files

Pros and Cons & Working w/ Binary

System I/O Good:

System I/O Bad:

Buffered I/O Good:

Buffered I/O Bad:

Standard I/O: disk, terminal Unix I/O: signal handlers, highest performance RIO: network sockets


Table of Content