套接字,网络及I/O入门

本文讲述了UNIX系统结构,内核如何提供服务,操作系统运行时库,Unix I/O设计概念等内容。
展开查看详情

1. Recall: UNIX System Structure CS162 Operating Systems and Systems Programming User Mode Applications Lecture 4 Standard Libs Introduction to I/O, Sockets, Networking Kernel Mode September 6th, 2017 Prof. Ion Stoica Hardware http://cs162.eecs.Berkeley.edu 9/6/17 CS162 ©UCB Fall 2017 Lec 4.2 How Does the Kernel Provide Services? OS Run-Time Library •  You said that applications request services from the operating system via syscall, but … Proc Proc Proc •  I’ve been writing all sort of useful applications and I never ever 1 2 … n saw a “syscall” !!! OS •  That’s right. Appln login Window Manager •  It was buried in the programming language runtime library (e.g., libc.a) … •  … Layering OS library OS library OS library OS 9/6/17 CS162 ©UCB Fall 2017 Lec 4.3 9/6/17 CS162 ©UCB Fall 2017 Lec 4.4 Page 1

2. A Kind of Narrow Waist Key Unix I/O Design Concepts •  Uniformity Word Processing –  file operations, device I/O, and interprocess communication Compilers Web Browsers through open, read/write, close Email –  Allows simple composition of programs Web Servers »  find | grep | wc … Databases Application / Service •  Open before use Portable OS Library OS –  Provides opportunity for access control and arbitration User System Call –  Sets up the underlying machinery, i.e., data structures System Interface •  Byte-oriented Portable OS Kernel –  Even if blocks are transferred, addressing is in bytes Software Platform support, Device Drivers •  Kernel buffered reads –  Streaming and block devices looks the same Hardware x86 PowerPC ARM PCI –  read blocks process, yielding processor to other task Ethernet (1Gbs/10Gbs) 802.11 a/g/n/ac SCSI Graphics Thunderbolt 9/6/17 CS162 ©UCB Fall 2017 Lec 4.5 9/6/17 CS162 ©UCB Fall 2017 Lec 4.6 Putting it together: web server Key Unix I/O Design Concepts •  Uniformity Kernel buffer –  file operations, device I/O, and interprocess communication through 4. parse request reads 9. format reply open, read/write, close Server request reply –  Allows simple composition of programs buffer buffer »  find | grep | wc … 1. network 3. kernel 10. network socket copy socket 5. file 8. kernel •  Open before use syscall read write syscall read copy –  Provides opportunity for access control and arbitration Kernel wait RTU 11. kernel copy RTU –  Sets up the underlying machinery, i.e., data structures from user buffer to network buffer •  Byte-oriented –  Even if blocks are transferred, addressing is in bytes interrupt 2. copy arriving 12. format outgoing interrupt •  Kernel buffered reads 6. disk 7. disk data packet (DMA) packet and DMA request (DMA) –  Streaming and block devices looks the same –  read blocks process, yielding processor to other task Hardware Network •  Kernel buffered writes interface Disk interface –  Completion of out-going transfer decoupled from the application, allowing it to continue Request Reply 9/6/17 CS162 ©UCB Fall 2017 Lec 4.7 9/6/17 CS162 ©UCB Fall 2017 Lec 4.8 Page 2

3. Putting it together: web server Key Unix I/O Design Concepts •  Uniformity Kernel buffer –  file operations, device I/O, and interprocess communication through writes 4. parse request 9. format reply open, read/write, close Server request reply –  Allows simple composition of programs buffer buffer »  find | grep | wc … 1. network 3. kernel 10. network socket copy socket 5. file 8. kernel •  Open before use syscall read write syscall read copy –  Provides opportunity for access control and arbitration Kernel wait RTU 11. kernel copy RTU –  Sets up the underlying machinery, i.e., data structures from user buffer to network buffer •  Byte-oriented –  Even if blocks are transferred, addressing is in bytes interrupt 2. copy arriving 12. format outgoing interrupt •  Kernel buffered reads 6. disk 7. disk data packet (DMA) packet and DMA request (DMA) –  Streaming and block devices looks the same –  read blocks process, yielding processor to other task Hardware Network •  Kernel buffered writes interface Disk interface –  Completion of out-going transfer decoupled from the application, allowing it to continue •  Explicit close Request Reply 9/6/17 CS162 ©UCB Fall 2017 Lec 4.9 9/6/17 CS162 ©UCB Fall 2017 Lec 4.10 I/O & Storage Layers The File System Abstraction •  High-level idea Application / Service –  Files live in hierarchical namespace of filenames streams •  File High Level I/O –  Named collection of data in a file system Low Level I/O handles –  File data Syscall registers »  Text, binary, linearized objects File System descriptors –  File Metadata: information about the file »  Size, Modification Time, Owner, Security info I/O Driver Commands and Data Transfers »  Basis for access control •  Directory Disks, Flash, Controllers, DMA –  “Folder” containing files & Directories –  Hierachical (graphical) naming »  Path through the directory graph »  Uniquely identifies a file or directory • /home/ff/cs162/public_html/fa17/index.html –  Links and Volumes (later) 9/6/17 CS162 ©UCB Fall 2017 Lec 4.11 9/6/17 CS162 ©UCB Fall 2017 Lec 4.12 Page 3

4. C High-Level File API – Streams (review) Connecting Processes, Filesystem, and Users •  Operate on “streams” - sequence of bytes, whether text or •  Process has a ‘current working directory’ data, with a position •  Absolute Paths –  /home/ff/cs162 #include <stdio.h> FILE *fopen( const char *filename, const char *mode ); int fclose( FILE *fp ); •  Relative paths –  index.html, ./index.html - current WD Mode Text Binary Descriptions –  ../index.html - parent of current WD sh flu r rb Open existing file for reading –  ~, ~cs162 - home directory o tt w wb Open for writing; created if does not exist ge for a ab Open for appending; created if does not exist n’t Do r+ rb+ Open existing file for reading & writing. w+ wb+ Open for reading & writing; truncated to zero if exists, create otherwise a+ ab+ Open for reading & writing. Created if does not exist. Read from beginning, write as append 9/6/17 CS162 ©UCB Fall 2017 Lec 4.13 9/6/17 CS162 ©UCB Fall 2017 Lec 4.14 C API Standard Streams C high level File API – Stream Ops •  Three predefined streams are opened implicitly when a program is executed #include <stdio.h> // character oriented –  FILE *stdin – normal source of input, can be redirected int fputc( int c, FILE *fp ); // rtn c or EOF on err –  FILE *stdout – normal source of output, can be redirected int fputs( const char *s, FILE *fp ); // rtn >0 or EOF –  FILE *stderr – diagnostics and errors, can be redirected int fgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp ); •  STDIN / STDOUT enable composition in Unix –  Recall: Use of pipe symbols connects STDOUT and STDIN »  find | grep | wc … 9/6/17 CS162 ©UCB Fall 2017 Lec 4.15 9/6/17 CS162 ©UCB Fall 2017 Lec 4.16 Page 4

5. C high level File API – Stream Ops C high level File API – Stream Ops #include <stdio.h> #include <stdio.h> // character oriented // character oriented int fputc( int c, FILE *fp ); // rtn c or EOF on err int fputc( int c, FILE *fp ); // rtn c or EOF on err int fputs( const char *s, FILE *fp ); // rtn >0 or EOF int fputs( const char *s, FILE *fp ); // rtn >0 or EOF int fgetc( FILE * fp ); int fgetc( FILE * fp ); char *fgets( char *buf, int n, FILE *fp ); char *fgets( char *buf, int n, FILE *fp ); // block oriented // block oriented size_t fread(void *ptr, size_t size_of_elements, size_t fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); size_t number_of_elements, FILE *a_file); size_t fwrite(const void *ptr, size_t size_of_elements, size_t fwrite(const void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); size_t number_of_elements, FILE *a_file); // formatted int fprintf(FILE *restrict stream, const char *restrict format, ...); int fscanf(FILE *restrict stream, const char *restrict format, ...); 9/6/17 CS162 ©UCB Fall 2017 Lec 4.17 9/6/17 CS162 ©UCB Fall 2017 Lec 4.18 Example Code C Stream API positioning #include <stdio.h> #define BUFLEN 256 FILE *outfile; int fseek(FILE *stream, long int offset, int whence); char mybuf[BUFLEN]; long int ftell (FILE *stream) void rewind (FILE *stream) High%Level%I/O%% int storetofile() { char *instring; offset (SEEK_SET) offset (SEEK_END) Low%Level%I/O%% outfile = fopen("/usr/homes/testing/tokens", "w+"); Syscall% if (!outfile) return (-1); // Error! File%System% while (1) { instring = fgets(mybuf, BUFLEN, stdin); // catches overrun! offset (SEEK_CUR) Upper%I/O%Driver% Lower%I/O%Driver% // Check for error or end of file (^D) if (!instring || strlen(instring)==0) break; // Write string to output file, exit on error if (fputs(instring, outfile)< 0) break; •  Preserves high level abstraction of uniform stream of objects } •  Adds buffering for performance fclose(outfile); // Flushes from userspace } 9/6/17 CS162 ©UCB Fall 2017 Lec 4.19 9/6/17 CS162 ©UCB Fall 2017 Lec 4.20 Page 5

6. What’s below the surface ?? C Low level I/O •  Operations on File Descriptors – as OS object representing the Application / Service state of a file High Level I/O streams –  User has a “handle” on the descriptor Low Level I/O handles Syscall registers #include <fcntl.h> File System descriptors #include <unistd.h> #include <sys/types.h> I/O Driver Commands and Data Transfers int open (const char *filename, int flags [, mode_t mode]) Disks, Flash, Controllers, DMA int creat (const char *filename, mode_t mode) int close (int filedes) Bit vector of: Bit vector of Permission Bits: •  Access modes (Rd, Wr, …) •  User|Group|Other X R|W|X •  Open Flags (Create, …) •  Operating modes (Appends, …) http://www.gnu.org/software/libc/manual/html_node/Opening-and-Closing-Files.html 9/6/17 CS162 ©UCB Fall 2017 Lec 4.21 9/6/17 CS162 ©UCB Fall 2017 Lec 4.22 C Low Level: standard descriptors C Low Level Operations #include <unistd.h> ssize_t read (int filedes, void *buffer, size_t maxsize) - returns bytes read, 0 => EOF, -1 => error STDIN_FILENO - macro has value 0 ssize_t write (int filedes, const void *buffer, size_t size) STDOUT_FILENO - macro has value 1 - returns bytes written STDERR_FILENO - macro has value 2 off_t lseek (int filedes, off_t offset, int whence) int fileno (FILE *stream) int fsync (int fildes) – wait for i/o to finish FILE * fdopen (int filedes, const char *opentype) void sync (void) – wait for ALL to finish •  When write returns, data is on its way to disk and can be read, •  Crossing levels: File descriptors vs. streams but it may not actually be permanent! •  Don’t mix them! 9/6/17 CS162 ©UCB Fall 2017 Lec 4.23 9/6/17 CS162 ©UCB Fall 2017 Lec 4.24 Page 6

7. And lots more ! Another example: lowio-std.c •  TTYs versus files #include <stdlib.h> #include <stdio.h> •  Memory mapped files #include <string.h> #include <unistd.h> •  File Locking #include <sys/types.h> •  Asynchronous I/O #define BUFSIZE 1024 •  Generic I/O Control Operations int main(int argc, char *argv[]) •  Duplicating descriptors { char buf[BUFSIZE]; ssize_t writelen = write(STDOUT_FILENO, "I am a process.\n", 16); int dup2 (int old, int new) int dup (int old) ssize_t readlen = read(STDIN_FILENO, buf, BUFSIZE); ssize_t strlen = snprintf(buf, BUFSIZE,"Got %zd chars\n", readlen); writelen = strlen < BUFSIZE ? strlen : BUFSIZE; write(STDOUT_FILENO, buf, writelen); exit(0); } 9/6/17 CS162 ©UCB Fall 2017 Lec 4.25 9/6/17 CS162 ©UCB Fall 2017 Lec 4.26 Administrivia •  Waitlist was closed Friday –  Unfortunately, no concurrent enrollments will be processed •  Recommendation: Read assigned readings before lecture •  Group sign up with the autograder this week –  Get finding groups ASAP – deadline Friday 9/8 at 11:59PM –  4 people in a group! •  TA preference signup form due Monday 9/11 at 11:59PM BREAK –  Everyone in a group must have the same TA! »  Preference given to same section –  Participation: Get to know your TA! 9/6/17 CS162 ©UCB Fall 2017 Lec 4.27 9/6/17 CS162 ©UCB Fall 2017 Lec 4.28 Page 7

8. What’s below the surface ?? Recall: SYSCALL Application / Service High Level I/O streams Low Level I/O handles Syscall registers File System descriptors I/O Driver Commands and Data Transfers Disks, Flash, Controllers, DMA •  Low level lib parameters are set up in registers and syscall instruction is issued –  A type of synchronous exception that enters well-defined entry points into kernel 9/6/17 CS162 ©UCB Fall 2017 Lec 4.29 9/6/17 CS162 ©UCB Fall 2017 Lec 4.30 What’s below the surface ?? Internal OS File Descriptor •  Internal Data Structure describing everything about the file –  Where it resides Application / Service –  Its status File descriptor number - an int streams High Level I/O –  How to access it Low Level I/O handles Syscall registers •  Pointer: File System descriptors struct file *file File Descriptors - a struct with all the I/O Driver Commands and Data Transfers info about the files Disks, Flash, Controllers, DMA 9/6/17 CS162 ©UCB Fall 2017 Lec 4.31 9/6/17 CS162 ©UCB Fall 2017 Lec 4.32 Page 8

9. File System: from syscall to driver Lower Level Driver In fs/read_write.c •  Associated with particular hardware device ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos) { •  Registers / Unregisters itself with the kernel ssize_t ret; if (!(file->f_mode & FMODE_READ)) return -EBADF; •  Handler functions for each of the file operations if (!file->f_op || (!file->f_op->read && !file->f_op->aio_read)) return -EINVAL; if (unlikely(!access_ok(VERIFY_WRITE, buf, count))) return -EFAULT; ret = rw_verify_area(READ, file, pos, count); if (ret >= 0) { count = ret; if (file->f_op->read) ret = file->f_op->read(file, buf, count, pos); else ret = do_sync_read(file, buf, count, pos); if (ret > 0) { fsnotify_access(file->f_path.dentry); add_rchar(current, ret); } inc_syscr(current); } return ret; } 9/6/17 CS162 ©UCB Fall 2017 Lec 4.33 9/6/17 CS162 ©UCB Fall 2017 Lec 4.34 Recall: Device Drivers Life Cycle of An I/O Request •  Device Driver: Device-specific code in the kernel that interacts User directly with the device hardware Program –  Supports a standard, internal interface –  Same kernel I/O system can interact easily with different device drivers –  Special device-specific configuration supported with the ioctl() system call Kernel I/O Subsystem •  Device Drivers typically divided into two pieces: –  Top half: accessed in call path from system calls Device Driver »  implements a set of standard, cross-device calls like open(), close(), read(), write(), ioctl(), strategy() Top Half »  This is the kernel’s interface to the device driver Device Driver »  Top half will start I/O to device, may put thread to sleep until finished Bottom Half –  Bottom half: run as interrupt routine »  Gets input or transfers next block of output »  May wake sleeping threads if I/O now complete Device Hardware 9/6/17 CS162 ©UCB Fall 2017 Lec 4.35 9/6/17 CS162 ©UCB Fall 2017 Lec 4.36 Page 9

10. So what happens when you fgetc? Communication between processes •  Can we view files as communication channels? Application / Service High Level I/O streams write(wfd, wbuf, wlen); Low Level I/O handles Syscall registers File System descriptors n = read(rfd,rbuf,rmax); I/O Driver Commands and Data Transfers Disks, Flash, Controllers, DMA •  Producer and Consumer of a file may be distinct processes –  May be separated in time (or not) •  However, what if data written once and consumed once? –  Don’t we want something more like a queue? –  Can still look like File I/O! 9/6/17 CS162 ©UCB Fall 2017 Lec 4.37 9/6/17 CS162 ©UCB Fall 2017 Lec 4.38 Communication Across the world looks like file IO Request Response Protocol Client (issues requests) Server (performs operations) write(wfd, wbuf, wlen); write(rqfd, rqbuf, buflen); requests n = read(rfd,rbuf,rmax); n = read(rfd,rbuf,rmax); wait service request write(wfd, respbuf, len); •  Connected queues over the Internet –  But what’s the analog of open? responses –  What is the namespace? –  How are they connected in time? n = read(resfd,resbuf,resmax); 9/6/17 CS162 ©UCB Fall 2017 Lec 4.39 9/6/17 CS162 ©UCB Fall 2017 Lec 4.40 Page 10

11. Request Response Protocol Client-Server Models Client (issues requests) Server (performs operations) Client 1 write(rqfd, rqbuf, buflen); requests Client 2 Server n = read(rfd,rbuf,rmax); *** wait service request write(wfd, respbuf, len); Client n responses n = read(resfd,resbuf,resmax); •  File servers, web, FTP, Databases, … •  Many clients accessing a common server 9/6/17 CS162 ©UCB Fall 2017 Lec 4.41 9/6/17 CS162 ©UCB Fall 2017 Lec 4.42 Conclusion (I) Conclusion (II) •  System Call Interface is “narrow waist” between user programs and kernel •  Device Driver: Device-specific code in the kernel that interacts directly with the device hardware •  Streaming IO: modeled as a stream of bytes –  Supports a standard, internal interface –  Most streaming I/O functions start with “f” (like “fread”) –  Same kernel I/O system can interact easily with different device drivers –  Data buffered automatically by C-library functions •  File abstraction works for inter-processes communication (local or Internet) •  Low-level I/O: –  File descriptors are integers –  Low-level I/O supported directly at system call level •  STDIN / STDOUT enable composition in Unix –  Use of pipe symbols connects STDOUT and STDIN »  find | grep | wc … 9/6/17 CS162 ©UCB Fall 2017 Lec 4.43 9/6/17 CS162 ©UCB Fall 2017 Lec 4.44 Page 11