200 likes | 314 Views
Advanced UNIX progamming. Fall 2002 Instructor: Ashok Srinivasan Lecture 7. Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan. Announcements. Reading assignment APUE Chapter 3 Pages 47-56, 56-62 APUE Chapter 5
E N D
Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 7 Acknowledgements: The syllabus and power point presentations are modified versions of those by T. Baker and X. Yuan
Announcements • Reading assignment • APUE Chapter 3 • Pages 47-56, 56-62 • APUE Chapter 5 • Section 5.4 is particularly important • APUE Chapter 7 • Section 7.3 is important • You should know the material from 7.1, 7.2, 7.4 – 7.9 from previous courses and classes • APUE Chapter 8 • Sections 8.1-8.3, 8.5-8.6, 8.9-8.10 • You should also understand the idea behind race conditions
Review • UNIX file system • File system abstraction • Directories • File descriptors
Week 3 Topics • UNIX file system • File system abstraction • Directories • File descriptors • Unix API Programming Examples and Techniques • Example with direct IO • open, close, fdopen, lseek, unlink • Variable argument list • HW1 hints
Week 3 Topics ... continued • File I/O • File descriptors • open, creat, close, dup, dup2 • I/O redirection • Process management • fork, exit, wait, waitpid, execv • Pipes • Named and unnamed pipes • Implementing pipe in a shell
UNIX file system • File system abstraction • Directories • File descriptors
File Descriptors ... continued • The POSIX standard defines the following • File descriptor: A per-process, unique, nonnegative integer used to identify an open file for the purposes of file access • Open file description: A record of how a process or group of processes are currently accessing a file • Each file descriptor refers to exactly one open file description, but an open file description may be referred to by more than one file descriptor • A file offset, file status, and file access modes are attributes of an open file description • File access modes: Specification of whether the file can be read and written
File Descriptors ... continued • File offset: The byte position in the file where the next I/O operation through that open file description begins • Each open file description associated with a regular file, block special file, or directory has a file offset • There is no file offset specified for a pipe or FIFO (described later) • File status: Includes the following information • append mode or not • blocking/nonblocking • Etc
File Descriptors ... continued • FIFO special file: A type of file with the property that data written to such a file is read on a first-in-first-out basis • Pipe: An object accessed by one of the pair of file descriptors created by the pipe() function • Once created, the file descriptors can be used to manipulate the pipe, and it behaves identically to a FIFO special file when accessed this way • It has no name in the file hierarchy
File Descriptors ... continued • Important points • A file descriptor does not describe a file • It is just a number that is ephemerally associated with a particular open file description • An open file description describes a past "open" operation on a file; its does not describe the file • The description of the file is in the inode • There may be several different open file descriptors (or none) referring at it any given time
Unix API Programming Examples and Techniques • Examples with direct IO • open, close, fdopen, lseek, unlink • Variable argument list • Note: • Read the POSIX standard and sse man pages to get information on system calls • Look into the system header files • Example: /usr/include/sys/types.h
Direct I/O • Using open() • The usual C-language stream-oriented I/O operations, like printf(), use buffers and process data character-by-character • They are implemented using the lower-level direct I/O operations read() and write() • In situations where we do not want to view the data as characters, or where we want greater efficiency, it is better to use the direct I/O operations
Using man • Look at the man page for write() • If there is more than one page on a given name, man will give you the one that is first in the chapter order of the Unix manual. • Shell commands are in Section 1, I/O and OS interface calls are in Section 2 and Section 3 respectively • Specification of section number varies • On Red Hat Linux, type man 2 write or man -S 2 write to see the page on write from Section 2 of the Unix manual • On Solaris, you can type man -s 2 write
man page for open #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> • Solaris 2.6 includes the following synopsis • int open(const char *path, int oflag, /* mode_t mode */ ...); • Red Hat Linux 6.2 • int open(const char *pathname, int flags, mode_t mode); • 1996 POSIX standard synopsis is as follows • int open(const char *path, int oflag, ...); • The latest official POSIX/Unix synopsis • No <sys/types.h> What does the ... mean here? Will a compiler allow this in an actual program?
Variable Argument Lists • The ... indicates a variable number of arguments • Similar to that in printf() • For more on variable argument lists, look at the file /usr/include/stdarg.h • Functions with variable argument lists can be dangerous • It is difficult to check types, and the use of a correct number of arguments
Example Programs • example1.c illustrates a common programming error • Failure to provide the correct number of arguments to a vararg function • example2.c illustrates opening a file
File I/O • File descriptors • open, creat, close, dup, dup2 • I/O redirection
File descriptors • Implication of the file descriptor/open file descriptions/inode table organization in UNIX • open and creat • search for the first empty slot in the process file descriptor table • allocate an open file description in the file table, which has a pointer to the inode table • See example3.c • dup and dup2 • Duplicate the file descriptor in the process file descriptor table • See example3b.c • Where is the current file position stored?
I/O redirection • All UNIX processes have three predefined files open • stdin -- STDIN_FILENO (0) • stdout -- STDOUT_FILENO (1) • stderr -- STDERR_FILENO (2) • We can redirect the I/O by manipulating these file descriptors • See example4.c and example5.c