Advanced UNIX progamming Fall 2002 Instructor: Ashok Srinivasan Lecture 6 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 4 • Pages 77-81, 92-95 – APUE Chapter 5 Review • Portability – Standards: ANSI, POSIX, etc – 32 bit vs 64 bit – Byte order: Little endian vs big endian • Introduction to the UNIX API – Environment variables – Exit status – Process ID – User ID • 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 system abstraction • File: a sequence of bytes of data • Filesystem: a space in which files can be stored • Link: a named logical connection from a directory to a file • Directory: a special kind of file, that can contain links to other files • Filename: the name of a link • Pathname: a chain of one or more filenames, separated by /'s File system abstraction ... continued • inode: a segment of data in a filesystem that describes a file, including how to find the rest of the file in the system • File descriptor: a non-negative integer, with a per-process mapping to an open file description • Open file description: an OS internal datastructure, shareable between processes Directories Directories ... continued • • • • • Names belong to links, not to files There may be multiple hard links to one file Renaming only renames one link to that file Unix allows both hard and soft links A file will exist even after the last hard link to it has been removed, as long as there are references to it from open file descriptions – Soft links do not prevent deletion of the file • A directory may have multiple (hard) links to it – But this capability is usually restricted, to prevent creation of directory cycles File Descriptors • Each open file is associated with an open file description – Each process has a (logical) array of references to open file descriptions – Logical indices into this array are file descriptors • These integer values are used to identify the files for I/O operations – The file descriptor 0 is reserved for standard input, the file descriptor 1 for standard output, and the file descriptor 2 for the standard error File Descriptors ... continued 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-infirst-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: – Use man pages to get information on system calls – Look into the system header files • /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, where we want greater efficiency, or where the extra (stream) layer of buffering causes us problems with synchronization, it is better to use the direct I/O operations Using man • Look at the man page for open() • 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 open or man -S 2 open to see the page on open from Section 2 of the Unix manual • On Solaris, you can type man -s 2 open 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