11 Unixio
11 Unixio
(video #1)
Alan L. Cox
[email protected]
#include "csapp.h"
int
main(void)
{
char c;
while(Read(STDIN_FILENO, &c, 1) != 0)
Write(STDOUT_FILENO, &c, 1);
return (0);
}
Available at:
/clear/www/htdocs/comp321/src/csapp.c
/clear/www/htdocs/comp321/include/csapp.h
rio_t structure
ssize_t
rio_readnb(...)
{
char rio_buf[RIO_BUFSIZE]
while (nleft > 0) { refill rio_buf
when empty
suck data
} out of rio_buf
}
Hard
Harddisk
disk
#include "csapp.h"
int
main(int argc, char *argv[])
{
int n;
rio_t rio;
char buf[MAXLINE];
Rio_readinitb(&rio, STDIN_FILENO);
while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0)
Rio_writen(STDOUT_FILENO, buf, n);
return (0);
}
Alan L. Cox
[email protected]
...
fd 4
...
File B (disk)
File access
File size
File pos
refcnt=1
File type
...
...
...
...
File B
File pos
refcnt=1
...
...
fd 4 ...
...
...
fd 4
fd 0 fd 0
fd 1 a fd 1 b
fd 2 fd 2
fd 3 fd 3
fd 4 b fd 4 b
Cox System-level I/O 26
I/O Redirection Example
...
fd 4 ...
File B
File access
File size
File pos
refcnt=1
File type
...
...
...
fd 4
...
File B
File access
File size
File pos
refcnt=2
File type
...
...
Alan L. Cox
[email protected]
fopen fdopen
fread fwrite
fscanf fprintf
sscanf C application program
sprintf fgets rio_readn
fputs fflush rio_writen
fseek Standard I/O RIO
rio_readinitb
fclose functions functions
rio_readlineb
open read rio_readnb
Unix I/O functions
write lseek
(accessed via system calls)
stat close
mmap
Pros:
Buffering increases efficiency of small operations,
e.g., fgetc(), by decreasing the number of read and
write system calls
Short counts are handled automatically
Cons:
Provides no functions for accessing file metadata
Standard I/O is not appropriate for input and
output on network sockets
There are poorly documented restrictions on stdio
streams that interact badly with restrictions on
network sockets
Restrictions on streams:
Restriction 1: input function cannot follow output
function without an intervening call to fflush,
fseek, fsetpos, or rewind
• Latter three functions all use lseek to change file
position
Restriction 2: output function cannot follow an
input function without an intervening call to fseek,
fsetpos, or rewind
Restriction on sockets:
You are not allowed to change the file position of a
socket
fclose(fpin);
fclose(fpout);
Networking
ALU
system bus memory bus
I/O main
bus interface
bridge memory
mousekeyboard monitor
disk
Cox System-level I/O 41
Reading a Disk Sector: Step 1
CPU chip
CPU initiates a disk read by writing
register file
a command, logical block number,
and destination memory address to
ALU
a port (address) associated with the
disk controller
main
bus interface
memory
I/O bus
mousekeyboard monitor
disk
Cox System-level I/O 42
Reading a Disk Sector: Step 2
CPU chip
register file Disk controller reads the sector and
performs a direct memory access
ALU (DMA) transfer into main memory
main
bus interface
memory
I/O bus
mousekeyboard monitor
disk
Cox System-level I/O 43
Reading a Disk Sector: Step 3
CPU chip
When the DMA transfer completes,
register file the disk controller notifies the CPU
with an interrupt (i.e., asserts a
ALU
special “interrupt” pin on the CPU)
main
bus interface
memory
I/O bus
mousekeyboard monitor
disk
Cox System-level I/O 44
Asynchronous I/O
POSIX P1003.4 Asynchronous I/O interface functions:
(available in Solaris, AIX, Tru64 Unix, Linux 2.6,…)
aio_cancel
• cancel asynchronous read and/or write requests
aio_error
• retrieve Asynchronous I/O error status
aio_fsync
• asynchronously force I/O completion, and sets errno to ENOSYS
aio_read
• begin asynchronous read
aio_return
• retrieve return status of Asynchronous I/O operation
aio_suspend
• suspend until Asynchronous I/O Completes
aio_write
• begin asynchronous write
lio_listio
• issue list of I/O requests