1 / 21

System Programming

System Programming. Nittida Nuansri. Streams and File Object. Buffering. Functions Opening a stream Reading & Writing a stream. Standard I/O Efficiency. Streams & File Objects. When we open or create a file with the standard I/O library

pkriner
Download Presentation

System Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. System Programming Nittida Nuansri

  2. Streams and File Object • Buffering • Functions Opening a stream Reading & Writing a stream • Standard I/O Efficiency

  3. Streams & File Objects • When we open or create a file with the standard I/O library • We have associated a stream with the file, such as • fopen() returns a pointer to a FILE object

  4. Streams & File Objects FILE- A structure that contains all information required by the standard I/O library to manage the stream : - The file descriptor used for actual I/O - a pointer to a buffer for the stream - the size of the buffer - a count of the number of characters currently in the buffer - an error flag etc.

  5. Streams & File Objects Application software should never need to examine a FILE object to reference the stream => use a file pointer (type FILE*)

  6. Buffering • Fully buffered • Line buffered • Unbuffered

  7. Buffering Fully buffered - the actual I/O takes place when the standard I/O buffer is filled - flush describes the writing of standard I/O buffer - a buffer can be automatically flushed by the standard I/O routines (such as when a buffer is filled), or a function fflush() is called

  8. Buffering Line buffered - The standard I/O library performs I/O when a new line character is encountered an input or output => It is possible to perform a single character at a time - Typically used a terminal stream (e.g. standard I/O)

  9. Buffering Unbuffered - The standard I/O library does not buffer the characters The standard error stream is normally unbuffered --> so that any error messages are displayed as quickly as possible

  10. Buffering ANSI C By default - standard input and standard output are fully buffered, if and only if they do not refer to an interactive device - standard error is never fully buffered

  11. Buffering • To buffer I/O setbuf (FILE *fp, char *buf); setvbuf (FILE *fp, cahr *buf, int mode, size_t size); where mode is one of the following _IOFBF _IOLBF -IONBF

  12. Buffering • Opening a Stream FILE *fopen(char *pathname, char *type); FILE *freopen(char *pathname, char *type, FILE *fp); FILE *fdopen(int filedes, char *type); return : file pointer if OK, NULL on error type: r, w, a, r+, w+, a+, etc. int fclose (FILE *fp);

  13. Buffering • Standard I/O Library • handles details such as buffer allocation • performing I/O in optimal-sized chunks (users do not need to worry about the correct clock size)

  14. Standard I/O Library • ANSI C -- by default • 1. Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device • 2. Standard error is never fully buffered • Both SVR4 and 4.3+BSD -- defualt • 1. Standard error is always unbuffered • 2. All other streams are line buffered if they refer to a terminal device, otherwise they are fully buffered

  15. Standard I/O Library • The defaults can be altered using void setbuf(FILE *fp, char *buf); int setvbuf(FILE *fp, char *buf, int mode, size_t size); mode : _IOFBF. _IOLBF, _IONBF

  16. Standard I/O Library • Opening a Stream FILE *fopen(char *pathname, char *type); FILE *freopen(char *pathname, char *type, FILE *fp); FILE *fdopen(int filedes, char *type); All three return: file pointer if ok, NULL on error type: r, w, a, r+, w+, a+ int fclose(FILE *fp);

  17. Standard I/O Library • Reading and Writing a Stream • Character-at-a-time • Line-at-a-time • Direct I/O

  18. Standard I/O Library • Functions: • Character-at-a-time: int getc(FILE *fp); int fgetc(FILE *fp); int getchar(void); int putc(FILE *fp); int fputc(FILE *fp); int putchar(void);

  19. Standard I/O Library • Functions: • Character-at-a-time: char *fgets(char *buf, int n, FILE *fp); char *gets(char *buf); char *fputs(char *buf, int n, FILE *fp); char *puts(char *buf);

  20. Standard I/O Library • Assignment : Modify your previous program (about buffer variation test) - use buffer size which yields best performance only - add the following functions fgets, fputs, getc, putc, fgetc, fputc - compare each performance (result from running your program)

  21. Standard I/O Library • Assigment (Cont.) Time Bytes of Function |User System Real prog text -------------------------------------------- best time from | previous program | fgets, fputs | getc, putc | fgets, fputc | single byte time from | previous program |

More Related