Overview
Full lecture notes on storage — Textbook readings
System calls for reading and writing
- Process isolation means processes don’t directly interact
- They can interact via explicit, checked input/output (I/O) system calls
- Some early operating systems offered many I/O system calls for different kinds of device
read_tape
,write_tape
,read_screen
,write_screen
,read_record
,write_record
…
- Modern operating systems unify devices into a minimal set of system calls
File descriptor system calls
ssize_t read(int fd, void* buf, size_t sz)
ssize_t write(int fd, const void* buf, size_t sz)
- Transfer
sz
bytes of data between an abstract devicefd
and a memory bufferbuf
read
: Read bytes from device to memorywrite
: Write bytes from memory to device
- Abstract devices are represented in system calls with file descriptors
fd
of typeint
Creating file descriptors
- Processes start with 3 standard file descriptors
STDIN_FILENO
=0
: standard input (stdin
,std::cin
)STDOUT_FILENO
=1
: standard output (stdout
,std::cout
)STDERR_FILENO
=2
: standard error (stderr
,std::cerr
)
- Processes can create new file descriptors
int open(const char* filename, int oflag, ...)
filename
names the device or sub-device to openoflag
isO_RDONLY
,O_WRONLY
,O_WRONLY | O_CREAT
, …- Returns a previously-unused file descriptor
int close(int fd)
closes an open file descriptor
- Many other system calls create fds for different kinds of device,
including
pipe
,socket
, …
Devices and files
- Some
filename
s correspond to whole devices- Example:
/dev/nvme2n1
,/dev/console
- Example:
- But whole devices are enormous and inconvenient
/dev/nvme2n1
is 75GB!
- The kernel should offer useful abstractions that simplify process programming
- File systems break enormous disks into named sections called files
- Can create, extend, overwrite, rename
- Kernel file system implementation maps files to device storage
Losing power
- Primary memory does not survive power outages or reboots
write(fd, buf, sz)
moves data from primary memorybuf
to some abstract devicefd
- What should happen on power outage or reboot?
Durable storage
- Storage is durable or persistent when modifications survive power
outage or reboot
- Volatile storage is the opposite of durable storage
- Durable storage is inherently slower than volatile storage
Creating a file one byte at a time
w-syncbyte
: Create a file one byte at a time, durably- The OS guarantees that every byte is written durably
w-osbyte
: Create a file one byte at a time, non-durably- The OS does not guarantee that every byte is written durably
- “A successful return from write() does not make any guarantee that data has been committed to disk.”
- The OS will write the bytes durably, eventually (within 30 seconds, but usually much sooner)
w-stdiobyte
: Create a file one byte at a time, non-durably, using a library (stdio)
Write a file one byte at a time using system calls
size_t n = 0;
while (n < size) {
ssize_t r = write(fd, buf, 1);
if (r != 1) {
perror("write");
exit(1);
}
++n;
}
Write a file one byte at a time using the stdio
library
size_t n = 0;
while (n < size) {
int ch = fputc(buf[0], f);
if (ch == EOF) {
perror("write");
exit(1);
}
++n;
}
fputc(buf[0], f)
≈fwrite(buf, 1, 1, f)
, with different error return convention- Check the manual!
Question 1
- How long will it take each program to write data one byte at a time?
- Will the results differ by operating system? How much?
Investigating standard I/O
strace
- Powerful Linux program snoops on another program’s system calls
- Prints a human-readable summary of those system calls to a file
strace -o strace.out PROGRAM ARGUMENTS…
-f
: follow forks (subprocesses)-s N
: Print more bytes of string arguments/return values-e trace=open,openat,close,read,write
: Only print those system callsman strace
for more
What is standard I/O doing?
Cache
- A cache is an area of fast storage used to speed up access to underlying storage
- Computer systems are rife with caches
- One of the most important patterns you learn in this class
- Registers as a cache for primary memory
- Memory as a cache for flash/hard disk
- Life is rife with caches