System Programming.ppt
《System Programming.ppt》由会员分享,可在线阅读,更多相关《System Programming.ppt(41页珍藏版)》请在麦多课文档分享上搜索。
1、System Programming,Chapter 5 Standard I/O Library,Announcement,Programming assignment #1 3/15/2006 4/7/2006 Bring your labtop PC (if you have one) for your next class Active slides,Fun Video I,Paper Windows (Queens, Canada) vs. Graphical Windows,Fun Video II,I/O Brush: The World as the Palette (MIT
2、Media Lab),Document Your Code,The more comments the better? Well documented: Some code explains itself. The comments are written for the readers to understand the code not to misguide the readers.,What does the following code fragment do?,/ pi is a float, start with 0 float pi = 0; / i is an integer
3、, go from 0 to 10000 for (int i=0; i10000; i+) / initialize the floats x an y with random numbersfloat x = (float)rand() / RAND_MAX;float y = (float)rand() / RAND_MAX; / add .0004 to pi if x*x + y*y is smaller than 1if ( x*x + y*y 1 ) pi += .0004; ,Calculation of Pi Using the Monte Carlo Method,The
4、“Monte Carlo Method“ is a method of solving problems using statistics. The probability of randomly selecting a point (x,y) inside the circle to that inside the square is p/4.,(x, y),Take a 2nd look,/ estimate pi by using a Monte Carlo algorithm: / get 10000 random points in the 01 x/y range and use
5、/ Pythagoras to check / if they are inside the circle. Add 4/10000 for each / point within the circle. float pi = 0; for (int i=0; i10000; i+) float x = (float)rand() / RAND_MAX;float y = (float)rand() / RAND_MAX;if ( x*x + y*y 1 ) pi += .0004; ,Contents,Preface/Introduction Standardization and Impl
6、ementation File I/O Standard I/O Library Files and Directories System Data Files and Information Environment of a Unix Process Process Control Signals Inter-process Communication,Standard I/O Library,A major revision by Dennis Ritchie in 1975 based on the Portable I/O library by Mike Lesk An ANSI C
7、standard Easy to use and portable Details handled: Buffer allocation, optimal-sized I/O chunks, better interface, etc.,Standard I/O Library,Differences from File I/O File Pointers vs. File Descriptors fopen vs open When a file is opened/created, a stream is associated with the file. FILE object File
8、 descriptor, buffer size, # of remaining chars, an error flag, and the like. stdin, sdtout, stderr defined in STDIO_FILENO, STDOUT_FILENO,Unbuffered I/O,read( ),read( ),read( ),User Process,Kernel,Storage,Buffered I/O,gets( ),gets( ),gets( ),User Process,Kernel,Storage,Standard I/O Buffer,Buffering,
9、Goal Use the minimum number of read and write calls. Types Fully Buffered Actual I/O occurs when the buffer is filled up. A buffer is automatically allocated when the first-time I/O is performed on a stream. Meaning of “flush”: standard I/O lib vs terminal driver,Buffering,Line Buffered Perform I/O
10、when a newline char is encountered! usually for terminals. Caveats The filling of a fixed buffer could trigger I/O. The flushing of all line-buffered outputs if input is requested. Unbuffered Expect to output ASAP, e.g. using write() E.g., stderr,Buffering,ANSI C Requirements Fully buffered for stdi
11、n and stdout unless interactive devices are referred to. SVR4/4.3+BSD line buffered Standard error is never fully buffered.#include int fflush(FILE *fp); All output streams are flushed if fp = NULL,Buffering,#include void setbuf(FILE *fp, char *buf); int setvbuf(FILE *fp, char *buf, int mode, size_t
12、 size); Full/line buffering if buf is not NULL (BUFSIZ) Terminals mode: _IOFBF, IOLBF, _IONBF () #define BUFSIZ 1024 () They must be called before any op is performed on the streams! What if you want to change it after some op?,Buffering,Possible Memory Access Errors Use automatic allocation NULL fo
13、r *buf in setvbuf() bookkeeping,Standard I/O Library - Open,#include FILE *fopen(const char *pathname, const char *type); FILE *freopen(const char *pathname, const char *type, FILE *fp); fopen/freopen opens a specified file! POSIX.1 Close fp stream first! New files created by a or w have r/w rights
14、for all,Standard I/O Library - Open,Type r w a r+ w+ a+ File exists? Y Y Truncate Y Y R Y Y Y Y W Y Y Y Y Y W only at end Y Y,Standard I/O Library - Open,#include FILE *fdopen(int fildes, const char *type); Associate a standard I/O stream with an existing file descriptor POSIX.1 Pipes, network chann
15、els No truncating for the file for “w” b (in rb, wb, ab, r+b, ) stands for a binary file no effect for Unix kernel O_APPEND supports multiple access. Interleaved R&W restrictions intervening fflush (WR), fseek(WR/RW), fsetpos (WR/RW), rewind (WR/RW), EOF (RW),Standard I/O Library Open/Close,#include
16、 int fclose(FILE *fp); Flush buffered output Discard buffered input All I/O streams are closed after the process exits.setbuf or setvbuf to change the buffering of a file before any operation on the stream.,Multi-byte Files,Standard I/O file streams can be used with single byte and multiple byte cha
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SYSTEMPROGRAMMINGPPT
