Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm.

Similar presentations


Presentation on theme: "CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm."— Presentation transcript:

1 CSE333 SECTION 3

2 Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm

3 String vs Byte APIs Strings are character(byte) arrays that terminate in '\0' str___() API operates on strings mem____() API operates on blocks of memory strcpy(dst,src) is equivalent to memcpy(dst,src,strlen(src)+1) strcmp() vs memcmp() strchr() vs memchr()

4 File I/O C’s stdio provides the notion of a stream A stream is a way of reading or writing a sequence of characters from/to a device A stream can be either text or binary; Linux does not distinguish A stream is buffered by default: libc reads ahead of you Three streams are provided by default: stdin, stdout, stderr You can open additional streams to read/write to files

5 #include #define READBUFSIZE 128 int main(int argc, char **argv) { FILE *f; char readbuf[READBUFSIZE]; size_t readlen; if (argc != 2) { fprintf(stderr, "usage:./fread_example filename\n"); return EXIT_FAILURE; // defined in stdlib.h } // Open, read, and print the file f = fopen(argv[1], "rb"); // "rb" --> read, binary mode if (f == NULL) { fprintf(stderr, "%s -- ", argv[1]); perror("fopen failed -- "); return EXIT_FAILURE; } // Read from the file, write to stdout. while ((readlen = fread(readbuf, 1, READBUFSIZE, f)) > 0) fwrite(readbuf, 1, readlen, stdout); fclose(f); return EXIT_SUCCESS; // defined in stdlib.h } #include #define READBUFSIZE 128 int main(int argc, char **argv) { FILE *f; char readbuf[READBUFSIZE]; size_t readlen; if (argc != 2) { fprintf(stderr, "usage:./fread_example filename\n"); return EXIT_FAILURE; // defined in stdlib.h } // Open, read, and print the file f = fopen(argv[1], "rb"); // "rb" --> read, binary mode if (f == NULL) { fprintf(stderr, "%s -- ", argv[1]); perror("fopen failed -- "); return EXIT_FAILURE; } // Read from the file, write to stdout. while ((readlen = fread(readbuf, 1, READBUFSIZE, f)) > 0) fwrite(readbuf, 1, readlen, stdout); fclose(f); return EXIT_SUCCESS; // defined in stdlib.h } stderr is a stream for printing error output to a console fopen opens a stream to read or write a file perror writes a string describing the last error to stderr stdout is for printing non-error output to the console printf(...) is equivalent to fprintf(stdout,...) fread_example.c

6 File Stats Returns the information about a specific file int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); int lstat(const char *path, struct stat *buf);

7 File Stats struct stat { dev_t st_dev; /* ID of device containing file */ ino_t st_ino; /* inode number */ mode_t st_mode; /* protection */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ dev_t st_rdev; /* device ID (if special file) */ off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };

8 File I/O C standard library to access files Specifically, fopen, fread, fwrite, fclose, fseek These provide a (FILE*) stream abstraction These are convenient and portable… …but they are buffered …and they are implemented by using lower-level OS calls

9 A Gotcha By default, stdio turns on buffering for streams Data “written” by fwrite() is copied into a buffer The buffer will be drained to the destination… …when you call fflush() on the stream …when the buffer size is exceeded (often 1024 or 4096 bytes) …when writing to a console and a newline is written (“line buffered”) …when you call fclose() on the stream …when your process exits gracefully (call to exit() or return from main() )

10 Why is This Important? What happens if... …your computer loses power before the buffer is flushed? …your program assumes data is written to a file, and it signals another program to read it? What are the performance implications? Data is copied into the stdio buffer Consumes CPU cycles and memory bandwidth Can potentially slow down high performance applications, like a web server or database (see: “zero copy”)

11 What to Do About It Accept it Buffering exists for a reason Turn off buffering with setbuf() This, too, may cause performance problems E.g., if your program does many small fwrite() s, each will now trigger a separate system call into the kernel Use a different API POSIX provides open(), read(), write(), close(), and others No buffering is done at the user level Maps to system calls But...what about the layers below? The OS caches disk reads and writes in the FS buffer cache Disk controllers have caches too!

12 Lower-Level File Access Most UNIX-en support a common set of lower- level file access APIs open, read, write, close, lseek Similar in spirit to their fopen (etc.) counterparts Lower-level and unbuffered Also less convenient You will have to use these for network I/O, so we might as well learn them now

13 open / close To open a file... Pass in the filename and access mode, similar to fopen Get back a “file descriptor” Similar to a (FILE *) from fopen, but just an int #include... int fd = open("foo.txt", O_RDONLY); if (fd == -1) { perror("open failed"); exit(EXIT_FAILURE); }... close(fd); #include... int fd = open("foo.txt", O_RDONLY); if (fd == -1) { perror("open failed"); exit(EXIT_FAILURE); }... close(fd);

14 Reading from a File ssize_t read(int fd, void *buf, size_t count); Returns the # of bytes read Might be fewer bytes than you requested (!!!) Returns 0 if you’re at end-of-file Returns -1 on error Warning: read() has some very surprising error modes!

15 read() Error Modes On error, the errno global variable is set You need to check it to see what kind of error happened What errors might read() encounter? EBADF -- bad file descriptor EFAULT -- output buffer is not a valid address EINTR -- read was interrupted, please try again …many others

16 How to Use read() #include... char *buf =...; int bytes_left = n; int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno != EINTR)) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; } #include... char *buf =...; int bytes_left = n; int result = 0; while (bytes_left > 0) { result = read(fd, buf + (n-bytes_left), bytes_left); if (result == -1) { if (errno != EINTR)) { // a real error happened, return an error result } // EINTR happened, do nothing and loop back around continue; } bytes_left -= result; }

17 Other Low-Level Functions Read the man pages to learn about write() -- write data fsync() -- flush data to the underlying device opendir(), readdir_r(), closedir() -- get a directory listing


Download ppt "CSE333 SECTION 3. Important Dates Jan 27 th – Homework 1 Due Feb 6 th – Midterm."

Similar presentations


Ads by Google