Understanding Linux I/O Concepts and Practices

Slide Note
Embed
Share

Delve into the world of Linux I/O by exploring topics such as unbuffered and buffered I/O operations, file representations, performance comparisons, and practical exercises. Learn to utilize functions like fwrite, fread, fopen, fclose, fseek, ftell, and fgets in C programming for efficient input/output handling. Understand the pros and cons of buffered I/O over unbuffered I/O, and discover the application of fgets for reading user input. Practice your skills with interactive C programs and explore the nuances of file stream management in Linux environments.


Uploaded on Oct 04, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. CSSE 132 Linux I/O (cont.) Tuesday, May 5, 2020

  2. Agenda Linux IO review What is IO? How does unbuffered IO work? How to use buffered IO? fwrite, fread, fopen, fclose, fseek, ftell Performance comparison between buffered and unbuffered IO? Interactive C program: read from keyboard fgets Practice

  3. File Representations: Buffered vs. Unbuffered Unbuffered Buffered Unbuffered Buffered Diagram from https://www.usna.edu/Users/cs/wcbrown/courses/IC221/classes/L09/Class.html

  4. Using Buffered I/O Write and Read: size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); Write nmemb number of objects with size size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); Open/Close: FILE *fopen(const char *pathname, const char *mode); r: read w: write w+: write, create if not exist. int fclose(FILE *stream); Move the cursor: int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream);

  5. Buffered I/O Open a file, get a file stream (FILE *, a pointer to a struct) FILE includes file descriptor, a pre-fetch buffer, cursor position, etc. Read/write to the buffer first, and commit to OS in a batched manner. Pros and Cons of buffered I/O v.s. unbuffered I/O

  6. fgets A special-case fread designed for strings! char * fgets(char* s, int size, FILE* stream); Read (size 1) bytes from stream to s, and append a null terminator in the end; Reading stops after EOF or a new line( \n ) Especially useful for reading in user input from stdin. Example: fgets(buffer, 5, stdin); read 4 bytes from keyboard input to buffer and append \0 at the end

  7. In-class practice https://repl.it/@song3/IOPractice https://repl.it/@song3/fgetspractice

Related


More Related Content