CS312 Lec 4
CS312 Lec 4
CS312 Lec 4
PROGRAMMING
LECTURE # 4
SYSTEM CALL & FILE I/O
1
AGENDA FOR TODAY
2
FILE I/O MODEL
One of the distinguishing features of the I/O model on UNIX systems is the
concept of universality of I/O. This means that the same system calls (open(),
read(), write(), close(), and so on) are used to perform I/O on all types of files,
including devices.
The kernel translates the application’s I/O requests into appropriate filesystem
or device-driver operations that perform I/O on the target file or device. Thus,
a program employing these system calls will work on any type of file.
The kernel essentially provides one file type: a sequential stream of bytes,
which, in the case of disk files, disks, and tape devices, can be randomly
accessed using the lseek() system call.
Many applications and libraries interpret the newline character as terminating
one line of text and commencing another.
UNIX systems have no end-of-file character; the end of a file is detected by a
read that returns no data. 3
FILE DESCRIPTORS
The I/O system calls refer to open files using a file descriptor, a (usually small)
nonnegative integer.
A file descriptor is typically obtained by a call to open(), which takes a
pathname argument specifying a file upon which I/O is to be performed.
Normally, a process inherits three open file descriptors when it is started by the
shell: descriptor 0 is standard input, the file from which the process takes its
input; descriptor 1 is standard output, the file to which the process writes its
output; and descriptor 2 is standard error, the file to which the process writes
error messages and notification of exceptional or abnormal conditions.
In an interactive shell or program, these three descriptors are normally
connected to the terminal.
In the stdio library, these descriptors correspond to the file streams stdin,
stdout, and stderr.
4
THE STDIO LIBRARY
5
YOUR FIRST LINUX C PROGRAM
6
LINUX FILE STRUCTURE
Files that represent physical devices and provide the interface to those
devices are conventionally found in a directory called /dev.
9
FILES AND DEVICES
14
LOW-LEVEL FILE ACCESS
0: Standard input
1: Standard output
2: Standard error
15
WRITE SYSTEM CALL
16
READ SYSTEM CALL
17
OPEN SYSTEM CALL
18
OPEN SYSTEM CALL
19
CLOSE SYSTEM CALL
20
Reading material:
21