PowerPoint Presentation
Lecture 10
System-Level I/O
Copyright By PowCoder代写 加微信 powcoder
COMP1411: Introduction to Computer Systems
Dr. Xianjin XIA
Department of Computing,
The Hong Kong Polytechnic University
Spring 2022
These slides are only intended to use internally. Do not publish it anywhere without permission.
Acknowledgement: These slides are based on the textbook (Computer Systems: A Programmer’s Perspective) and its slides.
Main message
Understanding the basic concepts of I/O
Understanding file operations in programming
An overview of I/O devices
Computer hardware
The narrow view: CPU + memory
The general view: CPU + memory + I/O
A computer use I/O devices to
Interact with the user, other computers
To do many other work besides computation on the CPU
Bus interface
Register file
System bus
Memory bus
controller
controller
Expansion slots for
other devices such
as network adapters.
How I/O devices interact with CPU
The main issue: CPU is too fast, I/O devices are too slow
Design principle: should not let CPU wait for I/O devices
Interrupt-based
How I/O devices interact with CPU
The main issue: CPU is too fast, I/O devices are too slow
Design principle: should not let CPU wait for I/O devices
DMA – Direct Memory Access, dedicated hardware helping CPU to move data between I/O devices and main memory
Reading a File (1)
Register file
controller
controller
Bus interface
CPU initiates a disk read by writing a command, logical block number, and destination memory address to a port (address) associated with disk controller.
Reading a File (2)
Register file
controller
controller
Bus interface
Disk controller reads the sector and performs a direct memory access (DMA) transfer into main memory.
Reading a File (3)
Register file
controller
controller
Bus interface
When the DMA transfer completes, the disk controller notifies the CPU with an interrupt (i.e., asserts a special “interrupt” pin on the CPU)
Unix I/O Overview
A file (in Unix/Linux) is a sequence of m bytes:
B0 , B1 , …. , Bk , …. , Bm-1
Interesting fact: All I/O devices are represented as files:
/dev/sda2 (/usr disk partition)
/dev/tty2 (terminal)
The kernel is also represented as a file:
/boot/vmlinuz-3.13.0-55-generic (kernel image)
/proc (kernel data structures)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Unix I/O Overview
Mapping of files to devices allows kernel to export simple interface called Unix I/O:
Opening and closing files
open()and close()
Reading and writing a file
read() and write()
Changing the current file position (seek)
indicates next offset into file to read or write
Current file position = k
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
File Types
Each file has a type indicating its role in the system
Regular file: Contains arbitrary data
Directory: Index for a related group of files
Socket: For communicating with a process on another machine
We ignore the other file types (beyond our scope)
Named pipes (FIFOs)
Symbolic links
Character and block devices
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Regular Files
A regular file is used to store data
Applications often distinguish between text files and binary files
Text files are regular files with only ASCII or Unicode characters
Binary files are everything else
e.g., object files, JPEG images
Text file is sequence of text lines
Text line is sequence of chars terminated by newline char (‘\n’)
Newline is 0xa, same as ASCII line feed character (LF)
End of line (EOL) indicators
Linux and Mac OS: ‘\n’ (0xa)
line feed (LF)
Windows & Internet protocols: ‘\r\n’ (0xd 0xa)
Carriage return (CR) followed by line feed (LF)
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Directories
Directory consists of an array of links
Each link maps a filename to a file
Each directory contains at least two entries
. (dot) is a link to itself
.. (dot dot) is a link to the parent directory in the directory hierarchy (next slide)
Commands for manipulating directories
mkdir: create empty directory
ls: view directory contents
rmdir: delete empty directory
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Directory Hierarchy
All files are organized as a hierarchy anchored by root directory named / (slash)
Kernel maintains current working directory (cwd) for each process
Modified using the cd command
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
Locations of files in the hierarchy denoted by pathnames
Absolute pathname starts with ‘/’ and denotes path from root
/home/droh/hello.c
Relative pathname denotes path from current working directory
../droh/hello.c
cwd: /home/bryant
Bryant and O’Hallaron, Computer Systems: A Programmer’s Perspective, Third Edition
I/O programming
Application program
Standard Library
OS System API
Device Drivers
fopen() fread() fwrite() fgets() fputs() fflush() fseek() fclose()
open() read()
Write() lseek()
Stat() close()
Opening files
When you open a file informs the kernel that you are ready to access that file
Returns an identifying integer file descriptor
fd == -1 indicates that an error occurred
int fd; /* file descriptor */
if ((fd = open(“/etc/hosts”, O_RDONLY)) < 0) { perror("open"); Closing files When you closing a file informs the kernel that you have finished accessing that file It is important to check the error code int fd; /* file descriptor */ int retval; /* return value */ if ((retval = close(fd)) < 0) { perror("close"); Reading files Reading a file copies bytes from the current file position to memory, and then updates file position Returns number of bytes read from file fd into buf Return type ssize_t is signed integer nbytes < 0 indicates that an error occurred char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open file fd ... */ /* Then read up to 512 bytes from file fd */ if ((nbytes = read(fd, buf, sizeof(buf))) < 0) { perror("read"); Writing files Writing a file copies bytes from memory to the current file position, and then updates current file position Returns number of bytes written from buf to file fd nbytes < 0 indicates that an error occurred char buf[512]; int fd; /* file descriptor */ int nbytes; /* number of bytes read */ /* Open the file fd ... */ /* Then write up to 512 bytes from buf to file fd */ if ((nbytes = write(fd, buf, sizeof(buf)) < 0) { perror("write"); Going further Exercise file operations in the tutorial Let’s program /docProps/thumbnail.jpeg 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com