1 / 17

File I/O System Calls Reference

File I/O System Calls Reference. COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004. Contents. Calls Overview open() read() write() close() lseek() References. open(). Opens/creates files (whatever is abstracted by a file). SYNOPSIS #include <sys/types.h>

Download Presentation

File I/O System Calls Reference

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.


Presentation Transcript

  1. File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004 Serguei Mokhov, mokhov@cs.concordia.ca

  2. Contents • Calls Overview • open() • read() • write() • close() • lseek() • References Serguei Mokhov, mokhov@cs.concordia.ca

  3. open() • Opens/creates files (whatever is abstracted by a file). SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int open(const char *pathname, int flags); int open(const char *pathname, int flags, mode_t mode); int creat(const char *pathname, mode_t mode); Serguei Mokhov, mokhov@cs.concordia.ca

  4. open(): flags • O_RDONLY or O_WRONLY or O_RDWR + zero or more of the following, concatenated by “|” (some are omitted, see man page for details): • O_CREAT, doesn’t fail if file exists • O_EXCL, in conjunction with O_CREAT will cause open() to fail if file exists • O_TRUNC, if file exists and permissions allow, make it of 0 length • O_APPEND, all stuff written to an open file will be appended regardless lseeks() you do. • O_NONBLOCK, non-blocking I/O • O_SYNC, synchronous I/O, thus blocking • O_NOFOLLOW, don’t follow symlinks, i.e. fail • O_DIRECTORY, fail if a directory Serguei Mokhov, mokhov@cs.concordia.ca

  5. open(): modes • Permissions on new files: • S_IRWXU – User: RWX • S_IRUSR (S_IREAD) – User: R • S_IWUSR (S_IWRITE) – User: W • S_IXUSR (S_IEXEC) – User: X • S_IRWXG – Group: RWX • S_IRGRP – Group: R • S_IWGRP – Group: W • S_IXGRP – Group: X • S_IROTH – Others: R • S_IWOTH – Others: W • S_IXOTH – Others: X Serguei Mokhov, mokhov@cs.concordia.ca

  6. open() • Performs file path to file descriptor translation, to use with read(), write(), etc. • Returns -1 on error. • File descriptor – next available integer starting from 0. • 0 – STDIN_FILENO • 1 – STDOUT_FILENO • 2 – STDERR_FILENO • 3 – probably your file • File descriptors are not shared by opening; however, a forked process among other things inherits parent’s open files. Serguei Mokhov, mokhov@cs.concordia.ca

  7. Raw Device Read • /dev/hda0 – first partition of the first hard disk • To read you need superuser privileges. • open() just like filename, advice: do it read-only :-), i.e. O_RDONLY Serguei Mokhov, mokhov@cs.concordia.ca

  8. read() • Well, read our bytes from somewhere, where our file descriptor points to, to a pre-allocated buffer. SYNOPSIS #include <unistd.h> ssize_t read(int fd, void *buf, size_t count); Serguei Mokhov, mokhov@cs.concordia.ca

  9. read() • Returns • number of bytes read on success • 0 - EOF • (-1) on failure • Make sure you read() as many bytes as you intended to before proceeding to processing of what you read(). Serguei Mokhov, mokhov@cs.concordia.ca

  10. write() • Writes out data to a file descriptor. SYNOPSIS #include <unistd.h> ssize_t write(int fd, const void *buf, size_t count); Serguei Mokhov, mokhov@cs.concordia.ca

  11. write() • Returns • (-1) on error • Number of bytes written on success • 0 if count is 0 • The rest is more or less specific • Double check that the # of bytes written is indeed as requested in count before you proceed. • Note, neither having successful write() nor successful close() after it mean your data physically has reached the disk; use fsync() to have more guarantee that this will happen since kernels usually defer writes for performance reasons. Serguei Mokhov, mokhov@cs.concordia.ca

  12. close() • “Release” file descriptor. SYNOPSIS #include <unistd.h> int close(int fd); Serguei Mokhov, mokhov@cs.concordia.ca

  13. close() • Descriptor ready for re-use. • All locks removed. • Returns • 0 on success • (-1) on error • ALWAYS CHECK THE RETURN VALUES! Serguei Mokhov, mokhov@cs.concordia.ca

  14. lseek() • Navigate within the file using file pointer. SYNOPSIS #include <sys/types.h> #include <unistd.h> off_t lseek(int fildes, off_t offset, int whence); Serguei Mokhov, mokhov@cs.concordia.ca

  15. lseek() • whence: • SEEK_SET  offset is set to offset bytes • SEEK_CUR  current + offset • SEEK_END  file size + offset • If file pointer is beyond file boundaries, zeros will be returned by read() from the gap between current position and actual file end until the gap is filled in with smth. Serguei Mokhov, mokhov@cs.concordia.ca

  16. lseek() • Returns • Resulting offset on success • ((off_t)-1) on error Serguei Mokhov, mokhov@cs.concordia.ca

  17. References • Rusling • Chapter 8, Vahalia • Chapters 3-5, Stevens • Manual pages: • man 2 open • man 2 read • man 2 write • man 2 close • man 2 lseek Serguei Mokhov, mokhov@cs.concordia.ca

More Related