CSCI 4061 Introduction to Operating Systems
Instructor:
Outline
System I/O and Files
File definition and types File I/O operations
2
Data Input/Output
Hardware devices:
Keyboard, monitor, printer, touchscreen Hard disk, flash drive, network card
Q1: How does the OS make it easy to access multiple devices?
Q2: How do applications read and write data?
3
Operating System Structure
Applications
Operating System
Hardware
4
1
OS Interface to Devices
All I/O is basically done through device drivers Device drivers:
Special kernel programs that control devices Hide complexity of device
Protect device from unauthorized access
5
Application Interface to OS
OS provides system call interface to applications to perform I/O
In some Operating Systems, the I/O interface could be heavily device-dependent
Is this a problem?
6
Unix I/O: Files
All devices are mapped to files Everything is a file
Uniform interface for I/O:
A single set of system calls (open, close, read, write) with some additional ones
Actual implementation of the system calls depends on device type
Benefits?
7
File Types
Files may be of different types, depending on: Type of device (e.g., disk vs. network)
Type of I/O (sequential vs. random-access)
Some additional operations may be exposed to the user based on file type
E.g.: creating a regular file on disk vs. creating a network connection
8
2
Unix File Types
Regular files: Collection of data stored on disk
Device files: Devices mapped onto files:
Block or character
Pipes and FIFOs: Special files used for data-
sharing across processes
Directories: Collections of files
Symbolic Links: Pointers to other files
Sockets: Files used for network communication
9
File I/O Operations
Open a file
Read/write to/from the file
Move around in the file if required
Close the file when done
Additional operations for different file types
E.g.: make, change directories
E.g.: set up, accept network connections
10
File I/O Operations: System Calls
Open a file: open
Read/write to/from the file:
read
write
Move around in the file if required
lseek (for regular/block device files)
Close the file when done: close
Additional system calls for different file types
E.g.: mkdir, chdir for directories E.g.: connect, accept for sockets
11
12
Opening a File: open
Opens a file or creates a new file Returns a file descriptor
Process-specific handle to the file
Used by process to identify the file Parameters
path: name of file to be opened oflag: Mode of opening the file
O_RDONLY, O_WRONLY, O_RDWR
Additional flags: Combine using OR operation (|)
int open(char *path, int oflag, …);
3
Reading from a File: read
Reads data from the current offset in the file Parameters
fd: file descriptor of file
buf: buffer into which data is to be read
Should have been allocated
DO NOT pass NULL or unallocated buffer nbytes: number of bytes to read
read is typically a blocking system call 13
ssize_t read(int fd, void *buf, size_t nbytes);
Return value of read
Number of actual bytes read
Could be less than number of bytes requested
End-of-file reached
Character devices: line-by-line reading Sockets: Network buffering
0: End-of-file, could be device-dependent -1: Error
14
ssize_t read(int fd, void *buf, size_t nbytes);
Reading from a File: read
char *buf= malloc(NBYTES*sizeof(char)); size_t nbytes, bytes_read;
bytes_read=read(fd, buf, nbytes);
if (bytes_read <= 0)
/* Error or EOF */ handle_error_or_eof();
else if (bytes_read < nbytes)
/* Try to read remaining bytes */ bytes_read=read(fd, buf+bytes_read,
nbytes-bytes_read);
15
Writing to a File: write
Writes data to the current offset in the file Parameters
fd: file descriptor of file
buf: buffer from which data is to be written nbytes: number of bytes to write
Returns number of bytes written
Typically equal to the number of bytes passed
Otherwise, possible error (disk full, kernel buffer
full, etc.)
16
ssize_t write(int fd, void *buf, size_t nbytes);
4
17
Closing a File: close Closes an open file
Releases resources associated with the file: file descriptors, other kernel data if last close of file Deletes file if marked for deletion and last close
of file
Parameter: file descriptor to close Returns:
0 if successful -1 if error
int close(int fd);
Setting Offset in a File: lseek Current file offset:
Position in file where next byte would be read from or written to
0 when file is opened, or end-of-file if opened in append mode
Returns:
New file offset -1 if error
18
off_t lseek(int fd, off_t offset, int whence);
19
Setting Offset in a File: lseek
Parameters:
fd: file descriptor
offset: number of bytes to skip whence: where to skip from
SEEK_SET: from beginning of file SEEK_CUR: from current position SEEK_END: from end of file
off_t lseek(int fd, off_t offset, int whence);
20
Changing Offset in a File
open read(..., 10)
lseek(..., 40)
read(..., 10)
EOF
0 10
50 60
100
5
Buffering
All reads/writes eventually go to the disk or I/O device
What if we write 1 byte at a time?
Solution: Buffer a certain number of bytes before reading/writing them
22
Can be done in OS or in user space (e.g., through a library)
Buffering Policies
Depends on device and file type Fully Buffered
Data read/written only when whole buffer is filled
E.g.: Disk files (buffered in chunks of blocks) Line buffered
Data written on newline
E.g.: Terminal I/O, stdin, stdout Unbuffered
Data read/written immediately
E.g.: stderr
Device files: Depends on internal device/driver buffer
23
Buffering example
In what order do the prints happen? 24
printf(“A”); printf(“B”); fprintf(stderr, “Z”); printf(“C\n”); fprintf(stderr, “Y”); printf(“D\n”); fprintf(stderr, “X”);
Forced Buffer Cleanup: fflush
Forces writing of any unwritten data in a buffer
printf(“A”); fflush(stdout); printf(“B”); fprintf(stderr, “Z”); printf(“C\n”); fprintf(stderr, “Y”); printf(“D\n”); fprintf(stderr, “X”);
25
6
Buffering: Benefits and Limitations
Discussion: What are the benefits and limitations of buffering?
26
7