1 / 16

File I/O

File I/O. open close lseek read and write – unbuffered I/O dup and dup2. File Descriptors. Kernel maintains file descriptors to reference open files Non-negative integer The shell defines 3 by convention stdin 0 stdout 1 stderr 2. Open. Functions defined in <unistd.h>

dessa
Download Presentation

File I/O

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. File I/O • open • close • lseek • read and write – unbuffered I/O • dup and dup2

  2. File Descriptors • Kernel maintains file descriptors to reference open files • Non-negative integer • The shell defines 3 by convention • stdin 0 • stdout 1 • stderr 2

  3. Open • Functions defined in <unistd.h> • open functionint open(const char *pathname, int flags);int open(const char *pathname, int flags, mode_t mode);

  4. Open (cont) • Required Flags • O_RDONLY • O_WRONLY • O_RDWR • Useful Optional Flags • O_APPEND • O_CREAT • O_EXCL (used with O_CREAT to cause the open function to fail if file already exists) • O_TRUNC

  5. Open Example int fd; fd = open(“data.txt”, O_RDONLY | O_CREAT | O_TRUNC, mode); if(fd < 0) { perror(“Unable to open file”); exit(1); }

  6. creat • creat function int creat(const char *pathname, mode_t mode); • Same as using open with O_WRONLY | O_CREAT | O_TRUNC, mode)

  7. close • close functionint close(int fd); • Returns 0 on success or -1 on error • Kernel will close any file descriptors left open by a process when it exits

  8. lseek • lseek functionoff_t lseek(int fildes, off_t offset, int whence); • Returns current file offset if sucessful, otherwise -1 • whence • SEEK_SET – Sets offset from beginning of file • SEEK_CUR – Relative offset from current position • SEEK_END – Sets offset past end of file

  9. Unbuffered I/O - read • read functionssize_t read(int fd, void *buf, size_t count); • Reads count bytes from the file indicated by the file descriptor fd into the buffer pointed to by buf • Returns -1 on error, 0 on end of file and number of bytes read otherwise (may be less that the number requested)

  10. Unbuffered I/O - write • write functionssize_t write(int fd, const void *buf, size_t count); • Attempts to write out count bytes from buffer buf to file indicated by file descriptor fd. • Returns -1 on error, number of bytes written otherwise

  11. Buffer Size and Efficiency • Program in Fig 3.4 page 69 • Table in Fig 3.5 page 70

  12. File Sharing • Multiple processes can access a file simultaneously • 3 Kernel level data structures are used to keep track of file information • Process table • File table • v-node • See Fig 3.6 and 3.7 on pages 72, 73

  13. Atomic Operations • Appending to a file • Separate lseek and write operations can result in race conditions • Open file with O_APPEND to force an atomic seek to end of file before each write • Creating a file • Separate check for file existence and creation can result in race condition • Use both the O_CREAT and O_EXCL flags

  14. dup and dup2 • Both are functions for duplicating a file descriptor within a process and return -1 on error • dup • int dup(int oldfd); • Returns a new fd that points to the same entry in the file table • dup2 • int dup2(int oldfd, int newfd); • Returns newfd which now points to the same entry in the file table as oldfd

  15. fcntl • Allows us to change properties of an already opened file • Several uses. See page 78 for more info • All return -1 on error, but meaning of other return types depends on specific use

  16. ioctl • int ioctl(int d, int request, ...); • Catchall for I/O functions that don’t fit well in other headers.

More Related