io
io
Ethan Blanton
Department of Computer Science and Engineering
University at Buffalo
Introduction UNIX I/O Standard I/O Buffering Summary References
Everything is a File
These services are particularly important on UNIX systems.
On UNIX, “everything is a file”.
Many devices and services are accessed by opening device
nodes that behave like files.
Examples:
/dev/null: Always readable, contains no data. Always
writable, discards anything written to it.
/dev/urandom: Always readable, reads a cryptographically
secure stream of random data.
File Descriptors
All access to files is through file descriptors.
A file descriptor is a small integer representing an open file in a
particular process.
There are three “standard” file descriptors:
0: standard input
1: standard output
2: standard error
…sound familiar? (stdin, stdout, stderr)
File Modes
Every file on a POSIX system has an owner and group.
File permissions are handled by mode bits.1
Mode bits are of the form: rwxrwxrwx
The rwx triplets are user, group, and other permissions.
The user bits apply to the file’s owner.
The group bits apply to members of the file’s group.
The other bits apply to all other users.
r means read, w means write, x execute.
1
Modern POSIX systems also have access control lists.
© 2018 Ethan Blanton / CSE 410: Systems Programming
Introduction UNIX I/O Standard I/O Buffering Summary References
Mode Examples
File modes are normally represented as octal numbers.
Octal numbers range from 0-7 and are three bits long.
rwxrwxrwx
Examples:
750 (111101000b): rwxr-x---
User can read, write, execute; group can read and execute;
others have no access.
664 (110110100b): rw-rw-r--
User and group can read and write, others can read.
Opening Files
There are two2 calls to open a file on a POSIX system:
# include < fcntl .h >
2
…OK, three.
© 2018 Ethan Blanton / CSE 410: Systems Programming
Introduction UNIX I/O Standard I/O Buffering Summary References
Open Flags
int open(const char *path, int flags, mode_t mode);
O_CREAT|O_EXCL
Reading
# include < unistd .h >
Writing
What Standard?
Opening Streams
Stream Modes
FILE * fopen ( const char * path , const char * mode );
FILE * fdopen ( int fd , const char * mode );
Binary I/O
Unlike UNIX I/O, errors and EOF return the same value.
There are two functions provided to detect errors and EOF:
int feof(FILE *fp);
int ferror(FILE *fp);
fread():
% time seconds usecs / call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
0.00 0.000000 0 258 read
0.00 0.000000 0 3 open
0.00 0.000000 0 2 close
------ ----------- ----------- --------- --------- ----------------
100.00 0.000000 263 total
Buffering Mechanism
Buffer Example
fread ( & len , sizeof ( len ) , 1 , fp ) ;
data = malloc ( len ) ;
fread ( & data , 1 , len , fp ) ;
Buffer Example
fread ( & len , sizeof ( len ) , 1 , fp ) ;
data = malloc ( len ) ;
fread ( & data , 1 , len , fp ) ;
Buffer Example
fread ( & len , sizeof ( len ) , 1 , fp ) ;
data = malloc ( len ) ;
fread ( & data , 1 , len , fp ) ;
Buffer Example
fread ( & len , sizeof ( len ) , 1 , fp ) ;
data = malloc ( len ) ;
fread ( & data , 1 , len , fp ) ;
Summary
Next Time …
References I
Required Readings
[1] Randal E. Bryant and David R. O’Hallaron. Computer Science: A Programmer’s
Perspective. Third Edition. Chapter 10: 10.1-10.4, 10.10-10.12. Pearson, 2016.
License