Download presentation
Presentation is loading. Please wait.
Published byMark Bradford Modified over 8 years ago
1
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 1 File I/O System Calls Reference COMP 229, 444, 5201 Revision 1.2 Date: July 21, 2004
2
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 2 Contents Calls Overview –open() –read() –write() –close() –lseek() References
3
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 3 open() Opens/creates files (whatever is abstracted by a file). SYNOPSIS #include 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);
4
January 7, 2003Serguei 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
5
January 7, 2003Serguei 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
6
January 7, 2003Serguei 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.
7
January 7, 2003Serguei 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
8
January 7, 2003Serguei 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 ssize_t read(int fd, void *buf, size_t count);
9
January 7, 2003Serguei 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().
10
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 10 write() Writes out data to a file descriptor. SYNOPSIS #include ssize_t write(int fd, const void *buf, size_t count);
11
January 7, 2003Serguei 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.
12
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 12 close() “Release” file descriptor. SYNOPSIS #include int close(int fd);
13
January 7, 2003Serguei 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!
14
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 14 lseek() Navigate within the file using file pointer. SYNOPSIS #include off_t lseek(int fildes, off_t offset, int whence);
15
January 7, 2003Serguei 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.
16
January 7, 2003Serguei Mokhov, mokhov@cs.concordia.ca 16 lseek() Returns –Resulting offset on success –((off_t)-1) on error
17
January 7, 2003Serguei 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.