Upload
vartan
View
52
Download
0
Embed Size (px)
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
January 7, 2003 Serguei Mokhov, [email protected]
1
File I/O System Calls Reference
COMP 229, 444, 5201
Revision 1.2
Date: July 21, 2004
January 7, 2003 Serguei Mokhov, [email protected]
2
Contents
• Calls Overview– open()– read()– write()– close()– lseek()
• References
January 7, 2003 Serguei Mokhov, [email protected]
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);
January 7, 2003 Serguei Mokhov, [email protected]
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
January 7, 2003 Serguei Mokhov, [email protected]
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
January 7, 2003 Serguei Mokhov, [email protected]
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.
January 7, 2003 Serguei Mokhov, [email protected]
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
January 7, 2003 Serguei Mokhov, [email protected]
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);
January 7, 2003 Serguei Mokhov, [email protected]
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().
January 7, 2003 Serguei Mokhov, [email protected]
10
write()
• Writes out data to a file descriptor.
SYNOPSIS #include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
January 7, 2003 Serguei Mokhov, [email protected]
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.
January 7, 2003 Serguei Mokhov, [email protected]
12
close()
• “Release” file descriptor.
SYNOPSIS #include <unistd.h>
int close(int fd);
January 7, 2003 Serguei Mokhov, [email protected]
13
close()
• Descriptor ready for re-use.
• All locks removed.
• Returns– 0 on success– (-1) on error– ALWAYS CHECK THE RETURN VALUES!
January 7, 2003 Serguei Mokhov, [email protected]
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);
January 7, 2003 Serguei Mokhov, [email protected]
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.
January 7, 2003 Serguei Mokhov, [email protected]
16
lseek()
• Returns– Resulting offset on success– ((off_t)-1) on error
January 7, 2003 Serguei Mokhov, [email protected]
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